Coverage for examples/OneRoom_SimpleMPC/plot_results.py: 6%

97 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-08-01 15:10 +0000

1import numpy as np 

2import matplotlib.pyplot as plt 

3import agentlib_mpc.utils.plotting.basic as mpcplot 

4from agentlib_mpc.utils.analysis import mpc_at_time_step 

5from agentlib_flexquant.data_structures.flex_results import Results 

6 

7 

8def plot_results(results_data: dict = None): 

9 """ 

10 Example how plotting with matplotlib and mpcplot from agentlib_mpc works 

11 """ 

12 if results_data is None: 

13 res = Results( 

14 flex_config="flex_configs/flexibility_agent_config.json", 

15 simulator_agent_config="mpc_and_sim/simple_sim.json", 

16 results="results" 

17 ) 

18 else: 

19 res = Results( 

20 flex_config="flex_configs/flexibility_agent_config.json", 

21 simulator_agent_config="mpc_and_sim/simple_sim.json", 

22 results=results_data 

23 ) 

24 

25 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=2) 

26 (ax1, ax2) = axs 

27 # load 

28 ax1.set_ylabel(r"$\dot{Q}_{Room}$ in W") 

29 res.df_simulation["load"].plot(ax=ax1) 

30 # T_in 

31 ax2.set_ylabel("$T_{in}$ in K") 

32 res.df_simulation["T_in"].plot(ax=ax2) 

33 x_ticks = np.arange(0, 3600 * 6 + 1, 3600) 

34 x_tick_labels = [int(tick / 3600) for tick in x_ticks] 

35 ax2.set_xticks(x_ticks) 

36 ax2.set_xticklabels(x_tick_labels) 

37 ax2.set_xlabel("Time in hours") 

38 for ax in axs: 

39 mpcplot.make_grid(ax) 

40 ax.set_xlim(0, 3600 * 6) 

41 

42 # room temp 

43 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=1) 

44 ax1 = axs[0] 

45 # T out 

46 ax1.set_ylabel("$T_{room}$ in K") 

47 res.df_simulation["T_upper"].plot(ax=ax1, color="0.5") 

48 res.df_simulation["T_lower"].plot(ax=ax1, color="0.5") 

49 res.df_simulation["T_out"].plot(ax=ax1, color=mpcplot.EBCColors.dark_grey) 

50 mpc_at_time_step( 

51 data=res.df_neg_flex, time_step=9000, variable="T" 

52 ).plot(ax=ax1, label="neg", linestyle="--", color=mpcplot.EBCColors.red) 

53 mpc_at_time_step( 

54 data=res.df_pos_flex, time_step=9000, variable="T" 

55 ).plot(ax=ax1, label="pos", linestyle="--", color=mpcplot.EBCColors.blue) 

56 mpc_at_time_step( 

57 data=res.df_baseline, time_step=9900, variable="T" 

58 ).plot(ax=ax1, label="base", linestyle="--", color=mpcplot.EBCColors.dark_grey) 

59 

60 ax1.legend() 

61 ax1.vlines(9000, ymin=0, ymax=500, colors="black") 

62 ax1.vlines(9900, ymin=0, ymax=500, colors="black") 

63 ax1.vlines(10800, ymin=0, ymax=500, colors="black") 

64 ax1.vlines(18000, ymin=0, ymax=500, colors="black") 

65 

66 ax1.set_ylim(289, 299) 

67 x_ticks = np.arange(0, 3600 * 6 + 1, 3600) 

68 x_tick_labels = [int(tick / 3600) for tick in x_ticks] 

69 ax1.set_xticks(x_ticks) 

70 ax1.set_xticklabels(x_tick_labels) 

71 ax1.set_xlabel("Time in hours") 

72 for ax in axs: 

73 mpcplot.make_grid(ax) 

74 ax.set_xlim(0, 3600 * 6) 

75 

76 # predictions 

77 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=2) 

78 (ax1, ax2) = axs 

79 # P_el 

80 ax1.set_ylabel("$P_{el}$ in kW") 

81 res.df_simulation["P_el"].plot(ax=ax1, color=mpcplot.EBCColors.dark_grey) 

82 mpc_at_time_step( 

83 data=res.df_neg_flex, time_step=9000, variable="P_el" 

84 ).ffill().plot( 

85 ax=ax1, 

86 drawstyle="steps-post", 

87 label="neg", 

88 linestyle="--", 

89 color=mpcplot.EBCColors.red, 

90 ) 

91 mpc_at_time_step( 

92 data=res.df_pos_flex, time_step=9000, variable="P_el" 

93 ).ffill().plot( 

94 ax=ax1, 

95 drawstyle="steps-post", 

96 label="pos", 

97 linestyle="--", 

98 color=mpcplot.EBCColors.blue, 

99 ) 

100 mpc_at_time_step( 

101 data=res.df_baseline, time_step=9000, variable="P_el" 

102 ).ffill().plot( 

103 ax=ax1, 

104 drawstyle="steps-post", 

105 label="base", 

106 linestyle="--", 

107 color=mpcplot.EBCColors.dark_grey, 

108 ) 

109 ax1.legend() 

110 ax1.vlines(9000, ymin=-1000, ymax=5000, colors="black") 

111 ax1.vlines(9900, ymin=-1000, ymax=5000, colors="black") 

112 ax1.vlines(10800, ymin=-1000, ymax=5000, colors="black") 

113 ax1.vlines(18000, ymin=-1000, ymax=5000, colors="black") 

114 ax1.set_ylim(-0.1, 1) 

115 

116 # mdot 

117 ax2.set_ylabel(r"$\dot{m}$ in kg/s") 

118 res.df_simulation["mDot"].plot(ax=ax2, color=mpcplot.EBCColors.dark_grey) 

119 mpc_at_time_step( 

120 data=res.df_neg_flex, time_step=9000, variable="mDot" 

121 ).ffill().plot( 

122 ax=ax2, 

123 drawstyle="steps-post", 

124 label="neg", 

125 linestyle="--", 

126 color=mpcplot.EBCColors.red, 

127 ) 

128 mpc_at_time_step( 

129 data=res.df_pos_flex, time_step=9000, variable="mDot" 

130 ).ffill().plot( 

131 ax=ax2, 

132 drawstyle="steps-post", 

133 label="pos", 

134 linestyle="--", 

135 color=mpcplot.EBCColors.blue, 

136 ) 

137 mpc_at_time_step( 

138 data=res.df_baseline, time_step=9900, variable="mDot" 

139 ).ffill().plot( 

140 ax=ax2, 

141 drawstyle="steps-post", 

142 label="base", 

143 linestyle="--", 

144 color=mpcplot.EBCColors.dark_grey, 

145 ) 

146 ax2.legend() 

147 ax2.vlines(9000, ymin=0, ymax=500, colors="black") 

148 ax2.vlines(9900, ymin=0, ymax=500, colors="black") 

149 ax2.vlines(10800, ymin=0, ymax=500, colors="black") 

150 ax2.vlines(18000, ymin=0, ymax=500, colors="black") 

151 

152 ax2.set_ylim(0, 0.06) 

153 

154 x_ticks = np.arange(0, 3600 * 6 + 1, 3600) 

155 x_tick_labels = [int(tick / 3600) for tick in x_ticks] 

156 ax2.set_xticks(x_ticks) 

157 ax2.set_xticklabels(x_tick_labels) 

158 ax2.set_xlabel("Time in hours") 

159 for ax in axs: 

160 mpcplot.make_grid(ax) 

161 ax.set_xlim(0, 3600 * 6) 

162 

163 # flexibility 

164 # get only the first prediction time of each time step 

165 energy_flex_neg = res.df_indicator.xs("negative_energy_flex", axis=1).droplevel(1).dropna() 

166 energy_flex_pos = res.df_indicator.xs("positive_energy_flex", axis=1).droplevel(1).dropna() 

167 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=1) 

168 ax1 = axs[0] 

169 ax1.set_ylabel(r"$\epsilon$ in kWh") 

170 energy_flex_neg.plot(ax=ax1, label="neg") 

171 energy_flex_pos.plot(ax=ax1, label="pos") 

172 energy_flex_neg.plot(ax=ax1, label="neg", color=mpcplot.EBCColors.red) 

173 energy_flex_pos.plot(ax=ax1, label="pos", color=mpcplot.EBCColors.blue) 

174 

175 ax1.legend() 

176 

177 x_ticks = np.arange(0, 3600 * 6 + 1, 3600) 

178 x_tick_labels = [int(tick / 3600) for tick in x_ticks] 

179 ax1.set_xticks(x_ticks) 

180 ax1.set_xticklabels(x_tick_labels) 

181 ax1.set_xlabel("Time in hours") 

182 for ax in axs: 

183 mpcplot.make_grid(ax) 

184 ax.set_xlim(0, 3600 * 6) 

185 

186 plt.show()