Coverage for examples/OneRoom_CIA/main_cia_flex.py: 33%

98 statements  

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

1import matplotlib.pyplot as plt 

2from matplotlib.ticker import FormatStrFormatter 

3from pathlib import Path 

4from agentlib.utils.multi_agent_system import LocalMASAgency 

5from agentlib_mpc.utils.plotting.mpc import plot_mpc 

6import numpy as np 

7import agentlib_mpc.utils.plotting.basic as mpcplot 

8from agentlib_mpc.utils.analysis import load_sim, load_mpc 

9from agentlib_mpc.utils.analysis import mpc_at_time_step 

10from agentlib_flexquant.generate_flex_agents import FlexAgentGenerator 

11import logging 

12import pandas as pd 

13from agentlib_flexquant.utils.interactive import Dashboard 

14 

15# Set the log-level 

16logging.basicConfig(level=logging.WARN) 

17until = 12000 

18 

19time_of_activation = 1500 

20 

21ENV_CONFIG = {"rt": False, "factor": 0.01, "t_sample": 10} 

22 

23 

24def run_example(until=until, with_plots=False): 

25 results = [] 

26 mpc_config = "mpc_and_sim/simple_cia_mpc.json" 

27 sim_config = "mpc_and_sim/simple_cia_sim.json" 

28 predictor_config = "predictor/predictor_config.json" 

29 flex_config = "flex_configs/flexibility_agent_config.json" 

30 agent_configs = [sim_config, predictor_config] 

31 

32 config_list = FlexAgentGenerator( 

33 flex_config=flex_config, mpc_agent_config=mpc_config 

34 ).generate_flex_agents() 

35 agent_configs.extend(config_list) 

36 

37 mas = LocalMASAgency( 

38 agent_configs=agent_configs, env=ENV_CONFIG, variable_logging=False 

39 ) 

40 

41 mas.run(until=until) 

42 results = mas.get_results(cleanup=False) 

43 

44 if with_plots: 

45 # disturbances 

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

47 ax1 = axs[0] 

48 # load 

49 ax1.set_ylabel("$dot{Q}_{Room}$ in W") 

50 results["SimAgent"]["room"]["load"].plot(ax=ax1) 

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

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

53 ax1.set_xticks(x_ticks) 

54 ax1.set_xticklabels(x_tick_labels) 

55 ax1.set_xlabel("Time in hours") 

56 for ax in axs: 

57 mpcplot.make_grid(ax) 

58 ax.set_xlim(0, 3600 * 6) 

59 

60 # room temp 

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

62 ax1 = axs[0] 

63 # T out 

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

65 results["SimAgent"]["room"]["T_upper"].plot(ax=ax1, color="0.5") 

66 results["SimAgent"]["room"]["T_out"].plot(ax=ax1, color=mpcplot.EBCColors.dark_grey) 

67 mpc_at_time_step( 

68 data=results["myMPCAgent"]["Baseline"], time_step=time_of_activation, variable="T" 

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

70 mpc_at_time_step( 

71 data=results["NegFlexMPC"]["NegFlexMPC"], time_step=time_of_activation, variable="T" 

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

73 mpc_at_time_step( 

74 data=results["PosFlexMPC"]["PosFlexMPC"], time_step=time_of_activation, variable="T" 

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

76 

77 ax1.legend() 

78 ax1.vlines(time_of_activation, ymin=0, ymax=500, colors="black") 

79 ax1.vlines(time_of_activation + 300, ymin=0, ymax=500, colors="black") 

80 ax1.vlines(time_of_activation + 600, ymin=0, ymax=500, colors="black") 

81 ax1.vlines(time_of_activation + 3000, ymin=0, ymax=500, colors="black") 

82 

83 ax1.set_ylim(289, 299) 

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

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

86 ax1.set_xticks(x_ticks) 

87 ax1.set_xticklabels(x_tick_labels) 

88 ax1.set_xlabel("Time in hours") 

89 for ax in axs: 

90 mpcplot.make_grid(ax) 

91 ax.set_xlim(0, 3600 * 6) 

92 

93 # predictions 

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

95 ax1 = axs[0] 

96 # P_el 

97 ax1.set_ylabel("$P_{el}$ in W") 

98 results["SimAgent"]["room"]["P_el"].plot(ax=ax1, color=mpcplot.EBCColors.dark_grey) 

99 mpc_at_time_step( 

100 data=results["NegFlexMPC"]["NegFlexMPC"], time_step=time_of_activation, variable="P_el" 

101 ).ffill().plot( 

102 ax=ax1, 

103 drawstyle="steps-post", 

104 label="neg", 

105 linestyle="--", 

106 color=mpcplot.EBCColors.red, 

107 ) 

108 mpc_at_time_step( 

109 data=results["PosFlexMPC"]["PosFlexMPC"], time_step=time_of_activation, variable="P_el" 

110 ).ffill().plot( 

111 ax=ax1, 

112 drawstyle="steps-post", 

113 label="pos", 

114 linestyle="--", 

115 color=mpcplot.EBCColors.blue, 

116 ) 

117 mpc_at_time_step( 

118 data=results["myMPCAgent"]["Baseline"], time_step=time_of_activation, variable="P_el" 

119 ).ffill().plot( 

120 ax=ax1, 

121 drawstyle="steps-post", 

122 label="base", 

123 linestyle="--", 

124 color=mpcplot.EBCColors.dark_grey, 

125 ) 

126 ax1.legend() 

127 ax1.vlines(time_of_activation, ymin=0, ymax=500, colors="black") 

128 ax1.vlines(time_of_activation + 300, ymin=0, ymax=500, colors="black") 

129 ax1.vlines(time_of_activation + 600, ymin=0, ymax=500, colors="black") 

130 ax1.vlines(time_of_activation + 3000, ymin=0, ymax=500, colors="black") 

131 

132 # flexibility 

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

134 ind_res = results["FlexibilityIndicator"]["FlexibilityIndicator"] 

135 energy_flex_neg = ind_res.xs("negative_energy_flex", axis=1).droplevel(1).dropna() 

136 energy_flex_pos = ind_res.xs("positive_energy_flex", axis=1).droplevel(1).dropna() 

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

138 ax1 = axs[0] 

139 ax1.set_ylabel("$epsilon$ in kWh") 

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

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

142 ax1.yaxis.set_major_formatter(FormatStrFormatter("%.4f")) 

143 

144 ax1.legend() 

145 

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

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

148 ax1.set_xticks(x_ticks) 

149 ax1.set_xticklabels(x_tick_labels) 

150 ax1.set_xlabel("Time in hours") 

151 for ax in axs: 

152 mpcplot.make_grid(ax) 

153 ax.set_xlim(0, 3600 * 6) 

154 # plt.show() 

155 

156 Dashboard( 

157 flex_config="flex_configs/flexibility_agent_config.json", 

158 simulator_agent_config="mpc_and_sim/simple_cia_sim.json", 

159 results=results 

160 ).show() 

161 

162 return results 

163 

164if __name__ == "__main__": 

165 run_example(until, with_plots=True)