Skip to content

Commit 07d40c2

Browse files
committed
Core code used
1 parent ea2bcb0 commit 07d40c2

33 files changed

+1005
-1043
lines changed

.DS_Store

-4 KB
Binary file not shown.

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@ Storm water network is simulated using EPA-SWMM and pyswmm/matswmm. Matswmm has
1111

1212
pyswmm/matswmm makes function calls to a static c library. Hence, we advice gcc-8.0 for pyswmm and gcc-4.2 for matswmm.
1313

14+
## Agents
1415

16+
There are two types of deep rl agents.
17+
1. Centralized controller that can observe multiple states across the network and control the ponds in the network
18+
2. Localised controller that can observe the state of an individual assent and control it
19+
20+
To use these agents to control a storm water network, you would just need to know the ID(e.g. "1D" from the swmm input file).
21+
22+
Refer to the example implementation for further details.
23+
24+
## Data presented in the paper
25+
26+
Weights of the neural network used for plots can be found in **./data**
27+
28+
## Utilities
29+
30+
Source code for generating heatmaps and other plots presented in the paper can be found in **./utilities**
31+
1532
## Further references
1633
1. Reinforcement Learning by Sutton and Barto
1734
2. DQN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

aa_network_uncontrolled_response/uncontrolled_response.py

-44
This file was deleted.

anet_test.py

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import numpy as np
2+
#import matplotlib.pyplot as plt
3+
import swmm
4+
from pond_net import pond_tracker
5+
from dqn_agent import deep_q_agent
6+
from ger_fun import reward_function, epsi_greedy, swmm_track
7+
from ger_fun import build_network, swmm_states
8+
import random
9+
import sys
10+
import os
11+
os.environ['LD_LIBRARY_PATH'] = os.getcwd()
12+
load_model=(sys.argv[3])
13+
write_model=(sys.argv[4])
14+
epi_start=float(sys.argv[1])
15+
epi_end = float(sys.argv[2])
16+
17+
# Nodes as List
18+
NODES_LIS = {'93-49743' : 'OR39',
19+
'93-49868' : 'OR34',
20+
'93-49919' : 'OR44',
21+
'93-49921' : 'OR45',
22+
'93-50074' : 'OR38',
23+
'93-50076' : 'OR46',
24+
'93-50077' : 'OR48',
25+
'93-50081' : 'OR47',
26+
'93-50225' : 'OR36',
27+
'93-90357' : 'OR43',
28+
'93-90358' : 'OR35'}
29+
30+
nodes_controlled = {'93-50077' : 'OR48'}
31+
states_controlled = {'93-50077': ['93-50077']}
32+
controlled_ponds = {}
33+
34+
35+
for i in nodes_controlled.keys():
36+
controlled_ponds[i] = pond_tracker(i,
37+
NODES_LIS[i],
38+
len(states_controlled[i]),
39+
1000000)
40+
41+
all_nodes = [i for i in NODES_LIS.keys()]
42+
con_nodes = [i for i in nodes_controlled.keys()]
43+
uco_nodes = list(set(all_nodes)-set(con_nodes))
44+
45+
action_space = np.linspace(0.0, 10.0, 101)
46+
uncontrolled_ponds = {}
47+
for i in uco_nodes:
48+
uncontrolled_ponds[i] = pond_tracker(i,
49+
NODES_LIS[i],
50+
1, 100)
51+
52+
# Initialize Neural Networks
53+
models_ac = {}
54+
for i in nodes_controlled.keys():
55+
model = target = build_network(len(states_controlled[i]),
56+
len(action_space),
57+
2, 50, 'relu', 0.0)
58+
if load_model != 'initial_run':
59+
model.load_weights(i + load_model)
60+
61+
target.set_weights(model.get_weights())
62+
models_ac[i] = [model, target]
63+
64+
65+
# Simulation Time Steps
66+
episode_count = 198
67+
timesteps = episode_count*14000
68+
time_limit = 14000
69+
epsilon_value = np.linspace(epi_start, epi_end, timesteps+10)
70+
71+
72+
# Initialize Deep Q agents
73+
agents_dqn = {}
74+
for i in nodes_controlled.keys():
75+
temp = deep_q_agent(models_ac[i][0],
76+
models_ac[i][1],
77+
len(states_controlled[i]),
78+
controlled_ponds[i].replay_memory,
79+
epsi_greedy)
80+
agents_dqn[i] = temp
81+
82+
episode_counter = 0
83+
time_sim = 0
84+
rain_duration = ['0005', '0010', '0030','0060','0120','0180','0360','0720','1080','1440']
85+
return_period = ['001','002','005','010','025','050','100']
86+
file_names = []
87+
for i in rain_duration:
88+
for j in return_period:
89+
temp = 'aa_orifices_v3_scs_' + i + 'min_' + j +'yr.inp'
90+
file_names.append(temp)
91+
out = {}
92+
93+
train_data = file_names
94+
# RL Stuff
95+
while time_sim < timesteps:
96+
temp1_name = random.sample(train_data, 1)
97+
inp = 'aa_orifices_v3_scs_' + '0360' + 'min_' + '025' +'yr.inp'
98+
print inp
99+
episode_counter += 1
100+
episode_timer = 0
101+
swmm.initialize(inp)
102+
done = False
103+
for i in nodes_controlled.keys():
104+
controlled_ponds[i].forget_past()
105+
for i in uncontrolled_ponds.keys():
106+
uncontrolled_ponds[i].forget_past()
107+
outflow_track = []
108+
print 'New Simulation: ', episode_counter
109+
temp_gate = 1.0
110+
while episode_timer < time_limit:
111+
episode_timer += 1
112+
time_sim += 1
113+
# Take a look at whats happening
114+
for i in nodes_controlled.keys():
115+
agents_dqn[i].state_vector = swmm_states(states_controlled[i],
116+
swmm.DEPTH)
117+
# Take action
118+
for i in nodes_controlled.keys():
119+
action_step = agents_dqn[i].actions_q(epsilon_value[time_sim],
120+
action_space)
121+
agents_dqn[i].action_vector = action_step/100.0
122+
swmm.modify_setting(controlled_ponds[i].orifice_id,
123+
agents_dqn[i].action_vector)
124+
current_gate = agents_dqn[i].action_vector
125+
126+
# SWMM step
127+
swmm.run_step()
128+
129+
# Receive the new rewards
130+
outflow = swmm.get('ZOF1', swmm.INFLOW, swmm.SI)
131+
water_level = swmm.get('93-50077', swmm.DEPTH, swmm.SI)
132+
outflow_track.append(outflow)
133+
overflows = swmm_states(all_nodes, swmm.FLOODING)
134+
gate_change = np.abs(current_gate - temp_gate)
135+
temp_gate = current_gate
136+
r_temp = reward_function(water_level, outflow, gate_change)
137+
138+
for i in nodes_controlled.keys():
139+
agents_dqn[i].rewards_vector = r_temp
140+
141+
# Observe the new states
142+
for i in nodes_controlled.keys():
143+
agents_dqn[i].state_new_vector = swmm_states(states_controlled[i],
144+
swmm.DEPTH)
145+
# Update Replay Memory
146+
for i in nodes_controlled.keys():
147+
controlled_ponds[i].replay_memory_update(agents_dqn[i].state_vector,
148+
agents_dqn[i].state_new_vector,
149+
agents_dqn[i].rewards_vector,
150+
agents_dqn[i].action_vector,
151+
agents_dqn[i].terminal_vector)
152+
153+
# Track Controlled ponds
154+
for i in controlled_ponds.keys():
155+
temp = swmm_track(controlled_ponds[i], attributes=["depth", "inflow","outflow","flooding"], controlled=True)
156+
temp = np.append(temp, np.asarray([agents_dqn[i].action_vector, agents_dqn[i].rewards_vector]))
157+
controlled_ponds[i].tracker_update(temp)
158+
159+
# Track Uncontrolled ponds
160+
for i in uncontrolled_ponds.keys():
161+
temp = swmm_track(uncontrolled_ponds[i], attributes=["depth", "inflow","outflow","flooding"], controlled=True)
162+
temp = np.append(temp, np.asarray([1.0, 0.0]))
163+
uncontrolled_ponds[i].tracker_update(temp)
164+
165+
# Train
166+
if episode_timer%25 == 0:
167+
for i in controlled_ponds.keys():
168+
agents_dqn[i].train_q(time_sim)
169+
170+
for i in models_ac.keys():
171+
temp = i + write_model
172+
models_ac[i][0].save(temp)
173+
174+
out[episode_counter] = outflow_track
175+
for i in controlled_ponds.keys():
176+
controlled_ponds[i].record_mean()
177+
178+
np.save('controlled_ponds'+ write_model +'.npy', controlled_ponds)
179+
np.save('outflow_last'+write_model, outflow_track)
180+
np.save('mean_rewards'+write_model,controlled_ponds['93-50077'].bookkeeping['mean_rewards'].data())
181+

0 commit comments

Comments
 (0)