-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_02.py
215 lines (171 loc) · 6.34 KB
/
test_02.py
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
import gym
import gym_dssat_pdi
import torch
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from stable_baselines3 import PPO
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.monitor import Monitor
from gym_dssat_pdi.envs.utils import utils
# helpers for action normalization
def normalize_action(action_space_limits, action):
"""Normalize the action from [low, high] to [-1, 1]"""
low, high = action_space_limits
return 2.0 * ((action - low) / (high - low)) - 1.0
def denormalize_action(action_space_limits, action):
"""Denormalize the action from [-1, 1] to [low, high]"""
low, high = action_space_limits
return low + (0.5 * (action + 1.0) * (high - low))
# Wrapper for easy and uniform interfacing with SB3
class GymDssatWrapper(gym.Wrapper):
def __init__(self, env):
super(GymDssatWrapper, self).__init__(env)
self.action_low, self.action_high = self._get_action_space_bounds()
# using a normalized action space
self.action_space = gym.spaces.Box(low=-1, high=1, shape=(1,), dtype="float32")
# using a vector representation of observations to allow
# easily using SB3 MlpPolicy
self.observation_space = gym.spaces.Box(low=0.0,
high=np.inf,
shape=env.observation_dict_to_array(
env.observation).shape,
dtype="float32"
)
# to avoid annoying problem with Monitor when episodes end and things are None
self.last_info = {}
self.last_obs = None
def _get_action_space_bounds(self):
box = self.env.action_space['anfer']
return box.low, box.high
def _format_action(self, action):
return { 'anfer': action[0] }
def _format_observation(self, observation):
return self.env.observation_dict_to_array(observation)
def reset(self):
return self._format_observation(self.env.reset())
def step(self, action):
# Rescale action from [-1, 1] to original action space interval
denormalized_action = denormalize_action((self.action_low, self.action_high), action)
formatted_action = self._format_action(denormalized_action)
obs, reward, done, info = self.env.step(formatted_action)
# handle `None`s in obs, reward, and info on done step
if done:
obs, reward, info = self.last_obs, 0, self.last_info
else:
self.last_obs = obs
self.last_info = info
formatted_observation = self._format_observation(obs)
return formatted_observation, reward, done, info
def close(self):
return self.env.close()
def seed(self, seed):
self.env.set_seed(seed)
def __del__(self):
self.close()
# Create environment
env_args = {
'run_dssat_location': '/home/jovyan/gym_dssat_pdi/run_dssat',
'mode': 'fertilization',
'seed': 123,
'random_weather': True,
}
env = GymDssatWrapper(gym.make('GymDssatPdi-v0', **env_args))
# Training arguments for PPO agent
ppo_args = {
'batch_size': 128,
'n_steps': 256,
'gamma': 0.95,
'learning_rate': 0.003,
'clip_range': 0.1,
'n_epochs': 20,
'policy_kwargs': dict(
net_arch=[dict(pi=[64, 64], vf=[64, 64])],
activation_fn=torch.nn.Tanh,
ortho_init=False,
),
'seed': 123,
}
# Create the agent
ppo_agent = PPO('MlpPolicy', env, **ppo_args)
# Train for 40k timesteps
print('Training PPO agent...')
#ppo_agent.learn(total_timesteps=40_000)
ppo_agent.learn(total_timesteps=20)
print('Training done')
# Baseline agents for comparison
class NullAgent:
"""
Agent always choosing to do no fertilization
"""
def __init__(self, env):
self.env = env
def predict(self, obs, state=None, episode_start=None, deterministic=None):
action = normalize_action((self.env.action_low, self.env.action_high), [0])
return np.array([action], dtype=np.float32), obs
class ExpertAgent:
"""
Simple agent using policy of choosing fertilization amount based on days after planting
"""
fertilization_dic = {
40: 27,
45: 35,
80: 54,
}
def __init__(self, env, normalize_action=False, fertilization_dic=None):
self.env = env
self.normalize_action = normalize_action
def _policy(self, obs):
dap = int(obs[0][0])
return [self.fertilization_dic[dap] if dap in self.fertilization_dic else 0]
def predict(self, obs, state=None, episode_start=None, deterministic=None):
action = self._policy(obs)
action = normalize_action((self.env.action_low, self.env.action_high), action)
return np.array([action], dtype=np.float32), obs
# evaluation and plotting functions
def evaluate(agent, n_episodes=10):
dir = './logs_test02/'
utils.make_folder(dir)
# Create eval env
eval_args = {
'run_dssat_location': '/home/jovyan/gym_dssat_pdi/run_dssat',
'log_saving_path': './logs_test02/dssat_pdi.log',
'mode': 'fertilization',
'seed': 456,
'random_weather': True,
}
env = Monitor(GymDssatWrapper(gym.make('GymDssatPdi-v0', **eval_args)))
returns, _ = evaluate_policy(
agent, env, n_eval_episodes=n_episodes, return_episode_rewards=True)
env.close()
return returns
def plot_results(labels, returns):
data_dict = {}
for label, data in zip(labels, returns):
data_dict[label] = data
df = pd.DataFrame(data_dict)
ax = sns.boxplot(data=df)
ax.set_xlabel("policy")
ax.set_ylabel("evaluation output")
plt.savefig('results_sb3.pdf')
print("\nThe result is saved in the current working directory as 'results_sb3.pdf'\n")
plt.show()
# evaluate agents
null_agent = NullAgent(env)
print('Evaluating Null agent...')
null_returns = evaluate(null_agent)
print('Done')
print('Evaluating PPO agent...')
ppo_returns = evaluate(ppo_agent)
print('Done')
expert_agent = ExpertAgent(env)
print('Evaluating Expert agent...')
expert_returns = evaluate(expert_agent)
print('Done')
# display results
labels = ['null', 'ppo', 'expert']
returns = [null_returns, ppo_returns, expert_returns]
plot_results(labels, returns)
# Cleanup
env.close()