Coverage for examples/OneRoom_CIA/mpc_and_sim/mixed_integer_mpc_cia.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-10-20 14:09 +0000

1import logging 

2import os 

3from pathlib import Path 

4 

5import casadi as ca 

6 

7from agentlib_mpc.models.casadi_model import ( 

8 CasadiModel, 

9 CasadiInput, 

10 CasadiState, 

11 CasadiParameter, 

12 CasadiOutput, 

13 CasadiModelConfig, 

14) 

15from agentlib.utils.multi_agent_system import LocalMASAgency 

16 

17logger = logging.getLogger(__name__) 

18 

19 

20# script variables 

21ub = 295.15 

22 

23# constants 

24COOLING = 1000 

25 

26 

27class MyCasadiModelConfig(CasadiModelConfig): 

28 inputs: list[CasadiInput] = [ 

29 # controls 

30 CasadiInput( 

31 name="cooling_power", 

32 value=400, 

33 unit="W", 

34 description="Air mass flow " "into zone", 

35 ), 

36 CasadiInput( 

37 name="cooler_on", 

38 value=1, 

39 unit="-", 

40 description="On / off signal of mass flow.", 

41 lb=0, 

42 ub=1, 

43 ), 

44 # disturbances 

45 CasadiInput( 

46 name="load", value=150, unit="W", description="Heat " "load into zone" 

47 ), 

48 CasadiInput( 

49 name="T_in", value=290.15, unit="K", description="Inflow air temperature" 

50 ), 

51 # settings 

52 CasadiInput( 

53 name="T_upper", 

54 value=294.15, 

55 unit="K", 

56 description="Upper boundary (soft) for T.", 

57 ), 

58 ] 

59 

60 states: list[CasadiState] = [ 

61 # differential 

62 CasadiState( 

63 name="T", value=293.15, unit="K", description="Temperature of zone" 

64 ), 

65 # algebraic 

66 # slack variables 

67 CasadiState( 

68 name="T_slack", 

69 value=0, 

70 unit="K", 

71 description="Slack variable of temperature of zone", 

72 ), 

73 ] 

74 

75 parameters: list[CasadiParameter] = [ 

76 CasadiParameter( 

77 name="cp", 

78 value=1000, 

79 unit="J/kg*K", 

80 description="thermal capacity of the air", 

81 ), 

82 CasadiParameter( 

83 name="C", value=100000, unit="J/K", description="thermal capacity of zone" 

84 ), 

85 CasadiParameter( 

86 name="s_T", 

87 value=1, 

88 unit="-", 

89 description="Weight for T in constraint function", 

90 ), 

91 CasadiParameter( 

92 name="r_cooling", 

93 value=1 / 5, 

94 unit="-", 

95 description="Weight for mDot in objective function", 

96 ), 

97 CasadiParameter( 

98 name="cooler_mod_limit", 

99 value=200, 

100 unit="W", 

101 description="Cooling power cannot modulate below this value", 

102 ), 

103 ] 

104 outputs: list[CasadiOutput] = [ 

105 CasadiOutput(name="T_out", unit="K", description="Temperature of zone"), 

106 CasadiOutput(name="P_el", unit="W", description="Electrical power") 

107 ] 

108 

109 

110class MyCasadiModel(CasadiModel): 

111 

112 config: MyCasadiModelConfig 

113 

114 def setup_system(self): 

115 # Define ode 

116 self.T.ode = (self.load - self.cooling_power) / self.C 

117 

118 # Define ae 

119 self.T_out.alg = self.T # math operation to get the symbolic variable 

120 self.P_el.alg = self.cooling_power # math operation to get the symbolic variable 

121 

122 # Constraints: list[(lower bound, function, upper bound)] 

123 self.constraints = [ 

124 # bigM reformulation 

125 (-ca.inf, self.cooling_power - self.cooler_on * COOLING, 0), 

126 (0, self.cooling_power - self.cooler_on * self.cooler_mod_limit, ca.inf), 

127 # soft constraints 

128 (0, self.T + self.T_slack, self.T_upper), 

129 ] 

130 

131 # Objective function 

132 objective = sum( 

133 [ 

134 self.r_cooling * self.cooling_power, 

135 self.s_T * self.T_slack**2, 

136 ] 

137 ) 

138 

139 return objective