Coverage for examples/SimpleBuilding/mpc_and_sim/simple_building.py: 67%

15 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2026-03-26 09:43 +0000

1from agentlib_mpc.models.casadi_model import ( 

2 CasadiModel, 

3 CasadiInput, 

4 CasadiState, 

5 CasadiParameter, 

6 CasadiOutput, 

7 CasadiModelConfig, 

8) 

9from math import inf 

10 

11 

12class BaselineMPCModelConfig(CasadiModelConfig): 

13 

14 inputs: list[CasadiInput] = [ 

15 # controls 

16 # Power var needs to be declared as an output. Here power var is also a model-input  

17 # So two separate var names are used for power var: P_in as input, P_el as output 

18 # P_el set to P_in below 

19 CasadiInput( 

20 name="P_in", 

21 value=100, 

22 unit="W", 

23 description="Electrical power of heating rod (equivalent to P_el)", 

24 ), 

25 # disturbances 

26 CasadiInput( 

27 name="T_amb", 

28 value=290.15, 

29 unit="K", 

30 description="Ambient air temperature", 

31 ), 

32 # settings 

33 CasadiInput( 

34 name="T_upper", 

35 value=294.15, 

36 unit="K", 

37 description="Upper boundary (soft) for T", 

38 ), 

39 CasadiInput( 

40 name="T_lower", 

41 value=290.15, 

42 unit="K", 

43 description="Lower boundary (soft) for T", 

44 ), 

45 ] 

46 

47 states: list[CasadiState] = [ 

48 # differential 

49 CasadiState( 

50 name="T_zone", 

51 value=293.15, 

52 unit="K", 

53 description="Temperature of zone", 

54 ), 

55 # algebraic 

56 # slack variables 

57 CasadiState( 

58 name="T_slack", 

59 value=0, 

60 unit="K", 

61 description="Slack variable for zone temperature", 

62 ), 

63 ] 

64 

65 parameters: list[CasadiParameter] = [ 

66 CasadiParameter( 

67 name="C", 

68 value=10000, 

69 unit="J/K", 

70 description="Thermal capacity of zone", 

71 ), 

72 CasadiParameter( 

73 name="U", 

74 value=5, 

75 unit="W/K", 

76 description="Thermal conductivity of zone", 

77 ), 

78 CasadiParameter( 

79 name="s_T", 

80 value=1, 

81 unit="-", 

82 description="Weight for zone temperature slack var in constraint function", 

83 ), 

84 CasadiParameter( 

85 name="r_pel", 

86 value=1, 

87 unit="-", 

88 description="Weight for P_el in objective function", 

89 ) 

90 ] 

91 

92 outputs: list[CasadiOutput] = [ 

93 CasadiOutput( 

94 name="P_el", 

95 unit="W", 

96 description="Electrical power of heating rod (system input)", 

97 ) 

98 ] 

99 

100 

101class BaselineMPCModel(CasadiModel): 

102 

103 config: BaselineMPCModelConfig 

104 

105 def setup_system(self): 

106 # Define ode 

107 self.T_zone.ode = (self.P_in - self.U * (self.T_zone - self.T_amb)) / self.C 

108 

109 #Define ae for outputs 

110 self.P_el.alg = self.P_in 

111 

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

113 self.constraints = [ 

114 # soft constraints 

115 (-inf, self.T_zone - self.T_slack, self.T_upper), 

116 (self.T_lower, self.T_zone + self.T_slack, inf), 

117 (0, self.T_slack, inf), 

118 # hard constraints 

119 (0, self.P_in, 200), 

120 ] 

121 

122 # Objective function 

123 objective = sum( 

124 [ 

125 self.s_T * self.T_slack**2, 

126 self.r_pel * self.P_in, 

127 ] 

128 ) 

129 

130 return objective