Coverage for examples/OneRoom_SimpleMPC/main_one_room_flex.py: 92%

25 statements  

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

1import logging 

2from agentlib_flexquant.generate_flex_agents import FlexAgentGenerator 

3from agentlib_flexquant.utils.interactive import Dashboard, CustomBound 

4from agentlib.utils.multi_agent_system import LocalMASAgency 

5from plot_results import plot_results 

6 

7# Set the log-level 

8logging.basicConfig(level=logging.WARN) 

9until = 21600 

10 

11ENV_CONFIG = {"rt": False, "factor": 0.01, "t_sample": 60} 

12 

13 

14def run_example(until=until, with_plots=False, with_dashboard=False): 

15 """ Example with market usage 

16 

17 mpc_config:  

18 Sets inputs, outputs, states, and parameters for the MPC agent.  

19 It points to the path of the MPC problem definition file (simple_building.py) and defines the MPC parameters. 

20 sim_config:  

21 Sets inputs, outputs, and states for the simulation agent.  

22 It points to the path of the FMU file and defines the simulation parameters. 

23 predictor_config: 

24 Sets parameters for the predictor agent and points to the path of the predictor formulation file (predictor.py). 

25 flex_config: 

26 Sets various options for the flexibility quantification framework:  

27 - characteristic times for the indicator module (e.g. market time, preparation time, flex event duration) 

28 - options for the cost calculation 

29 - whether to use a constant electricity price or to input a time series sent by the predictor agent 

30 - whether to use a constant feed-in tariff or to input a time series sent by the predictor agent 

31 - if no feed-in is required (e.g. for a house without electricity generation), use a constant feed-in tariff with value 0 

32 - option to correct the cost for stored energy at the end of the prediction horizon  

33 - option to include a market config (points to a market config file)  

34 - options for the flexibility agent generator:  

35 - power variable of the baseline agent  

36 - cost functions of PF-MPC and NF-MPC agents, including custom parameters and variables for the shadow MPCs  

37 - general options such as results paths  

38 

39 Change flex_power_feedback_method in MarketSpecifications to deal with systems with 

40 fast or slow inertia. 

41 See difference in results: when using collocation, power during flex event does not 

42 fully follow offer. When using constant it does. Reason is the fast inertia of the 

43 system. 

44 

45 This examples also introduces the flex_cost_function_appendix field, which allows 

46 for custom cost function terms. These terms are added to the standard objective 

47 of the corresponding shadow MPC (the filed does not exist for the Baseline, as 

48 here you would change the base MPC used for creating the shadow MPCs). You can 

49 change the field in the flexibility_agent_config to see its impact. 

50 

51 The time delay in the prediction plots (power profiles do not follow predictions) 

52 is caused by the collocation points and is only a visual effect. 

53 

54 """ 

55 

56 mpc_config = "mpc_and_sim/simple_model.json" 

57 sim_config = "mpc_and_sim/simple_sim.json" 

58 flex_config = "flex_configs/flexibility_agent_config.json" 

59 agent_configs = [sim_config] 

60 

61 config_list = FlexAgentGenerator( 

62 flex_config=flex_config, mpc_agent_config=mpc_config 

63 ).generate_flex_agents() 

64 agent_configs.extend(config_list) 

65 

66 mas = LocalMASAgency( 

67 agent_configs=agent_configs, env=ENV_CONFIG, variable_logging=False 

68 ) 

69 

70 mas.run(until=until) 

71 results = mas.get_results(cleanup=False) 

72 

73 if with_plots: 

74 # Alternative plots using matplotlib 

75 plot_results(results_data=results) 

76 

77 if with_dashboard: 

78 Dashboard( 

79 flex_config="flex_configs/flexibility_agent_config.json", 

80 simulator_agent_config="mpc_and_sim/simple_sim.json", 

81 results=results 

82 ).show( 

83 custom_bounds=CustomBound( 

84 for_variable="T", 

85 lb_name="T_lower", 

86 ub_name="T_upper" 

87 ) 

88 ) 

89 return results 

90 

91 

92if __name__ == "__main__": 

93 run_example(until, with_plots=False, with_dashboard=True)