@@ -56,6 +56,12 @@ def controller(
56
56
return actions
57
57
58
58
59
+ def rule_based_controller (state ):
60
+ actions = np .zeros (2 )
61
+ actions = state / 2.0
62
+ return actions
63
+
64
+
59
65
# Configure matplotlib style to make pretty plots
60
66
plt .style .use ("seaborn-v0_8-whitegrid" )
61
67
@@ -106,45 +112,42 @@ def controller(
106
112
controlled_depth .index = env_controlled .data_log ["simulation_time" ]
107
113
controlled_depth = controlled_depth .resample ("15min" ).mean ()
108
114
115
+ # Rule based controller
116
+ env_rule_controlled = pystorms .scenarios .theta ()
117
+ done = False
109
118
110
- textstr = "Controlled Performance = {:.2f} \n Uncontrolled Peformance = {:.2f}" .format (
111
- env_controlled .performance (), env_uncontrolled .performance ()
112
- )
113
-
114
- fig = plt .figure (num = 1 , figsize = (20 , 5 ))
115
- ax = plt .gca ()
116
- controlled_flows .plot (ax = ax )
117
- uncontrolled_flows .plot (ax = ax )
118
- ax .text (
119
- 0.20 ,
120
- 0.90 ,
121
- textstr ,
122
- transform = ax .transAxes ,
123
- fontsize = 10 ,
124
- verticalalignment = "center" ,
125
- bbox = dict (boxstyle = "round" , facecolor = "grey" , alpha = 0.2 ),
126
- )
127
- ax .text (
128
- 0.80 ,
129
- 0.73 ,
130
- "Exceedance Threshold" ,
131
- transform = ax .transAxes ,
132
- fontsize = 10 ,
133
- verticalalignment = "center" ,
134
- )
135
- plt .axhline (y = 0.50 , color = "red" )
136
- plt .title ("Scenario Theta: Equal-filling Controller" , loc = "left" )
137
- plt .ylabel ("Flows" )
138
- plt .xlabel ("Time" )
119
+ # Update the datalog to append states
120
+ env_rule_controlled .data_log ["depthN" ] = {}
121
+ env_rule_controlled .data_log ["depthN" ]['P1' ] = []
122
+ env_rule_controlled .data_log ["depthN" ]['P2' ] = []
139
123
140
- fig , ax = plt .subplots (1 , 3 , sharey = True )
124
+ while not done :
125
+ state = env_rule_controlled .state ()
126
+ actions = rule_based_controller (state )
127
+ done = env_rule_controlled .step (actions )
128
+
129
+ env_rule_controlled_flows = pd .DataFrame .from_dict (env_rule_controlled .data_log ["flow" ])
130
+ env_rule_controlled_flows .index = env_rule_controlled .data_log ["simulation_time" ]
131
+ env_rule_controlled_flows = env_rule_controlled_flows .resample ("15min" ).mean ()
132
+ env_rule_controlled_flows = env_rule_controlled_flows .rename (columns = {"8" : "Rule-baseed Controller" })
133
+
134
+ env_rule_controlled_depth = pd .DataFrame .from_dict (env_rule_controlled .data_log ["depthN" ])
135
+ env_rule_controlled_depth .index = env_rule_controlled .data_log ["simulation_time" ]
136
+ env_rule_controlled_depth = env_rule_controlled_depth .resample ("15min" ).mean ()
141
137
138
+
139
+ fig , ax = plt .subplots (1 , 3 , sharey = True )
142
140
controlled_depth [['P1' ]].plot (ax = ax [0 ])
143
141
uncontrolled_depth [['P1' ]].plot (ax = ax [0 ])
142
+ env_rule_controlled_depth [['P1' ]].plot (ax = ax [0 ])
144
143
145
144
controlled_depth [['P2' ]].plot (ax = ax [1 ])
146
145
uncontrolled_depth [['P2' ]].plot (ax = ax [1 ])
146
+ env_rule_controlled_depth [['P2' ]].plot (ax = ax [1 ])
147
147
148
148
controlled_flows .plot (ax = ax [2 ])
149
149
uncontrolled_flows .plot (ax = ax [2 ])
150
- plt .show ()
150
+ env_rule_controlled_flows .plot (ax = ax [2 ])
151
+
152
+ fig .set_size_inches (8.0 , 3.0 )
153
+ plt .savefig ("scenario_theta.svg" , dpi = 1000 )
0 commit comments