-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
273 lines (226 loc) · 9.92 KB
/
main.py
File metadata and controls
273 lines (226 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import torch
import datetime
import argparse
import numpy as np
import datetime
import sys, os
from envs.swat_gym import SWATEnv
from agents.random_agent import RandomAgent
from agents.standard_practice import StandardAgent
from agents.reactive_agent import ReactiveAgent
from agents.ddpg import DDPG, ReplayBuffer
from agents.td3 import TD3
# Runs policy for X episodes and returns average reward
def eval_policy(agent, max_action, seed, eval_episodes=10):
eval_env = SWATEnv()
# eval_env.seed(seed + 100)
avg_reward = 0.
for t in range(eval_episodes):
state, _, done, info = eval_env.reset(seed=seed+100)
while not done:
action = agent.select_action(np.array(state))
state, reward, done, _ = eval_env.step(action)
avg_reward += reward
avg_reward /= eval_episodes
print("---------------------------------------")
print(f"Evaluation over {eval_episodes} episodes: {avg_reward:.3f}")
print("---------------------------------------")
return avg_reward
def train(env, action_dim, agent, memory, seed):
batch_size = 8
max_timesteps = env.max_duration+1
max_action = float(env.action_space.high[0])
# init env
state, _, done, info = env.reset()
gaussian_std = 0.1
# bookkeeping
rewards = []
evaluations = []
mu_loss = []
q_loss = []
for t in range(max_timesteps):
# select action
action = (
agent.select_action(np.array(state))
+ np.random.normal(0, max_action * gaussian_std, size=action_dim)
).clip(0, max_action)
# since max_action is set to fertilizer amount, implicitly clip max irrigation
# action = [action[0], action[1]/3.0]
# perform action in env
next_state, reward, done, info = env.step(action)
# store experience in replay buffer
memory.add(state, action, next_state, reward, done)
# update current state and date
state = next_state
# track rewards
rewards.append(reward)
print(f"Timestep: {t}, reward: {reward}")
# train after collecting enough samples
if memory.size > 2*batch_size:
if algorithm=='TD3':
agent.train(memory, batch_size)
else:
actor_loss, critic_loss = agent.train(memory, batch_size)
mu_loss.append(actor_loss)
q_loss.append(critic_loss)
# evaluate every week
if (t+1)%7==0:
avg_reward = eval_policy(agent, max_action, seed, eval_episodes=10)
evaluations.append(avg_reward)
if done:
print("Episode terminated successfully!")
df = env.show_history()
state, _, done, info = env.reset()
current_date = info[0]
backup_rewards = rewards
rewards = []
# break
rewards = backup_rewards
return rewards, evaluations, df
# Runs policy for X episodes and returns average reward
def eval_random_policy(seed, eval_episodes=10):
eval_env = SWATEnv()
# eval_env.seed(seed + 100)
avg_reward = 0.
for t in range(eval_episodes):
state, _, done, info = eval_env.reset(seed=seed+100)
while not done:
action = env.action_space.sample()
state, reward, done, _ = eval_env.step(action)
avg_reward += reward
avg_reward /= eval_episodes
print("---------------------------------------")
print(f"Evaluation over {eval_episodes} episodes: {avg_reward:.3f}")
print("---------------------------------------")
return avg_reward
def standard_eval_policy(agent, max_action, seed, eval_episodes=10):
eval_env = SWATEnv()
# eval_env.seed(seed + 100)
avg_reward = 0.
for t in range(eval_episodes):
state, _, done, info = eval_env.reset(seed=seed+100)
current_date = datetime.datetime(2021, 4, 15)
while not done:
action = agent.select_action(current_date)
state, reward, done, _ = eval_env.step(action)
avg_reward += reward
current_date += datetime.timedelta(days=1)
avg_reward /= eval_episodes
print("---------------------------------------")
print(f"Evaluation over {eval_episodes} episodes: {avg_reward:.3f}")
print("---------------------------------------")
return avg_reward
def reactive_eval_policy(agent, max_action, seed, eval_episodes=10):
eval_env = SWATEnv()
# eval_env.seed(seed + 100)
avg_reward = 0.
for t in range(eval_episodes):
state, _, done, info = eval_env.reset(seed=seed+100)
current_date = datetime.datetime(2021, 4, 15)
while not done:
action = agent.select_action(current_date, state, info)
state, reward, done, info = eval_env.step(action)
avg_reward += reward
current_date += datetime.timedelta(days=1)
avg_reward /= eval_episodes
print("---------------------------------------")
print(f"Evaluation over {eval_episodes} episodes: {avg_reward:.3f}")
print("---------------------------------------")
return avg_reward
def baseline_train(env, algorithm, agent, seed):
# env.seed(seed + 100)
max_timesteps = env.max_duration+1
max_action = float(env.action_space.high[0])
rewards = []
evaluations = []
# init env
state, _, done, info = env.reset(seed=seed+100)
current_date = datetime.datetime(2021, 4, 15)
for t in range(max_timesteps):
if algorithm=='Standard':
action = agent.select_action(current_date)
elif algorithm=='Reactive':
action = agent.select_action(current_date, state, info)
else: # random
action = env.action_space.sample()
# perform action in env
next_state, reward, done, info = env.step(action)
# update current state and date
state = next_state
current_date += datetime.timedelta(days=1)
# track rewards
rewards.append(reward)
print(f"Timestep: {t}, reward: {reward}")
# evaluate every week
if (t+1)%7==0:
if algorithm=='Standard':
avg_reward = standard_eval_policy(agent, max_action, seed, eval_episodes=10)
elif algorithm=='Reactive':
avg_reward = reactive_eval_policy(agent, max_action, seed, eval_episodes=10)
else:
avg_reward = eval_random_policy(seed)
evaluations.append(avg_reward)
if done:
print("Episode terminated successfully!")
df = env.show_history()
state, _, done, _ = env.reset()
current_date = datetime.datetime(2021, 4, 15)
backup_rewards = rewards
rewards = []
# break
rewards = backup_rewards
return rewards, evaluations, df
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--algorithm", default="DDPG")
args = parser.parse_args()
algos = ['Random', 'Standard', 'Reactive', 'DDPG', 'TD3']
sim_time = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
for seed in range(5):
for algorithm in algos:
print("---------------------------------------")
print(f"Policy: {algorithm}, Seed: {seed}")
print("---------------------------------------")
env = SWATEnv()
# Set seeds
# env.seed(seed)
state, _, done, info = env.reset(seed=seed)
env.action_space.seed(seed)
torch.manual_seed(seed)
np.random.seed(seed)
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
max_action = float(env.action_space.high[0])
print(f"state dim: {state_dim}, action_dim: {action_dim}, max action: {max_action}\n")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Initialize policy, replayer buffer, and noise
if algorithm=='Random':
agent = RandomAgent()
rewards, evals, df = baseline_train(env, algorithm, agent, seed)
elif algorithm=='Standard':
agent = StandardAgent(env.start_date)
rewards, evals, df = baseline_train(env, algorithm, agent, seed)
elif algorithm == 'Reactive':
agent = ReactiveAgent()
rewards, evals, df = baseline_train(env, algorithm, agent, seed)
elif algorithm=='DDPG':
agent = DDPG(state_dim, action_dim, max_action, device)
memory = ReplayBuffer(state_dim, action_dim, device)
rewards, evals, df = train(env, action_dim, agent, memory, seed)
elif algorithm=='TD3':
policy_noise = 0.2*max_action # Noise added to target policy during critic update
noise_clip = 0.5*max_action # Range to clip target policy noise
policy_freq = 2 # Frequency of delayed policy updates
memory = ReplayBuffer(state_dim, action_dim, device)
agent = TD3(state_dim, action_dim, max_action, policy_noise=policy_noise, noise_clip=noise_clip, policy_freq=policy_freq)
rewards, evals, df = train(env, action_dim, agent, memory, seed)
else:
print("Incorrect policy specified. Valid choices = ['Standard', 'Reactive', 'DDPG']")
sys.exit(0)
file_name = f"{algorithm}_{seed}"
savePath = f'./experiments/exp_{sim_time}/{file_name}'
if not os.path.exists(savePath):
os.makedirs(savePath)
np.save(f"{savePath}/{file_name}_rewards", rewards)
np.save(f"{savePath}/{file_name}_evals", evals)
df.to_csv(f"{savePath}/{file_name}_history.csv")