Skip to content

Commit 0ae4382

Browse files
committed
updated controller
1 parent 3adcf99 commit 0ae4382

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

tutorials/scenario_theta.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def controller(
5656
return actions
5757

5858

59+
def rule_based_controller(state):
60+
actions = np.zeros(2)
61+
actions = state/2.0
62+
return actions
63+
64+
5965
# Configure matplotlib style to make pretty plots
6066
plt.style.use("seaborn-v0_8-whitegrid")
6167

@@ -106,45 +112,42 @@ def controller(
106112
controlled_depth.index = env_controlled.data_log["simulation_time"]
107113
controlled_depth = controlled_depth.resample("15min").mean()
108114

115+
# Rule based controller
116+
env_rule_controlled = pystorms.scenarios.theta()
117+
done = False
109118

110-
textstr = "Controlled Performance = {:.2f} \nUncontrolled 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'] = []
139123

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()
141137

138+
139+
fig, ax = plt.subplots(1, 3, sharey=True)
142140
controlled_depth[['P1']].plot(ax=ax[0])
143141
uncontrolled_depth[['P1']].plot(ax=ax[0])
142+
env_rule_controlled_depth[['P1']].plot(ax=ax[0])
144143

145144
controlled_depth[['P2']].plot(ax=ax[1])
146145
uncontrolled_depth[['P2']].plot(ax=ax[1])
146+
env_rule_controlled_depth[['P2']].plot(ax=ax[1])
147147

148148
controlled_flows.plot(ax=ax[2])
149149
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

Comments
 (0)