forked from holmescao/iGrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sac_main.py
194 lines (162 loc) · 7.14 KB
/
sac_main.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
import argparse
import gym
import os
import numpy as np
import torch
import datetime
import matplotlib.pyplot as plt
from tensorboardX import SummaryWriter
# from logger import Logger
import SAC.sac_module.utils as utils
from TenSim.simulator import PredictModel
from SAC.sac_module.replay_buffer import ReplayBuffer
from SAC.sac_module.sac import SACAgent
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
device = torch.device(
'cuda') if torch.cuda.is_available() else torch.device('cpu')
torch.set_num_threads(1)
# TesnorboardX
writer = SummaryWriter(
logdir='SAC/sac_module/runs/{}_SAC_{}_{}'.format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), 'WUR',
'SAC'))
def sim_env(version, base_tmp_folder):
direcrory = base_tmp_folder+'/models/'
model_dir = direcrory + version
model_path = model_dir + '/model/'
scaler_dir = model_dir + '/scaler/'
ten_env = PredictModel(model1_dir=model_path+'simulator_greenhouse.pkl',
model2_dir=model_path+'simulator_crop_front.pkl',
model3_dir=model_path+'simulator_crop_back.pkl',
scaler1_x=scaler_dir+'greenhouse_x_scaler.pkl',
scaler1_y=scaler_dir+'greenhouse_y_scaler.pkl',
scaler2_x=scaler_dir+'crop_front_x_scaler.pkl',
scaler2_y=scaler_dir+'crop_front_y_scaler.pkl',
scaler3_x=scaler_dir+'crop_back_x_scaler.pkl',
scaler3_y=scaler_dir+'crop_back_y_scaler.pkl',
linreg_dir=model_path+'/PARsensor_regression_paramsters.pkl',
weather_dir=model_path+'/weather.npy')
return ten_env
class NormalizedActions(gym.ActionWrapper):
def action(self, action):
low = self.action_space.low
high = self.action_space.high
action = low + (action + 1.0) * 0.5 * (high - low)
action = np.clip(action, low, high)
action = np.rint(action)
return action
def reverse_action(self, action):
low = self.action_space.low
high = self.action_space.high
action = 2 * (action - low) / (high - low) - 1
action = np.clip(action, low, high)
action = action.astype(int)
return action
def test(args):
seed = 9
utils.set_seed_everywhere(seed)
env = sim_env(args.version, args.base_tmp_folder)
env = NormalizedActions(env)
obs_dim = env.observation_space.shape[0]
act_dim = env.action_space.shape[0]
action_range = [
-1, 1
]
agent = SACAgent(obs_dim=obs_dim, action_dim=act_dim,
action_range=action_range)
agent.load_model("SAC/sac_model/sac_actor_%d_Exp" % args.seed,
"SAC/sac_model/sac_critic_%d_Exp" % args.seed)
obs = env.reset()
done = False
episode_reward = 0
rew_list = []
while not done:
with utils.eval_mode(agent):
action = agent.act(obs, sample=False)
obs, rew, done, _ = env.step(action)
episode_reward += rew
rew_list.append(episode_reward)
plt.plot(rew_list)
plt.show()
def main(args):
utils.set_seed_everywhere(args.seed)
env = sim_env(args.version, args.base_tmp_folder)
env = NormalizedActions(env)
obs_dim = env.observation_space.shape[0]
act_dim = env.action_space.shape[0]
action_range = [
-1, 1
]
agent = SACAgent(obs_dim=obs_dim, action_dim=act_dim,
action_range=action_range)
replay_buffer = ReplayBuffer(env.observation_space.shape,
env.action_space.shape,
50000,
device=device)
step = 0
start_step = 2000
eval_freq = 10
max_test_episode = 0
interval = 4
for i in range(args.total_episode):
episode_reward = 0
obs = env.reset()
done = False
while not done:
if step < start_step:
temp_list = np.random.uniform(-0.1, 0.1, 24//interval)
co2_list = np.random.uniform(-0.4, -0.2, 24//interval)
illu_list = np.array([-1 for _ in range(24//interval)])
irri_list = np.array([-1 for _ in range(24//interval)])
action = np.hstack((temp_list, co2_list, illu_list, irri_list))
else:
with utils.eval_mode(agent):
action = agent.act(obs, sample=True)
if len(replay_buffer) > start_step:
agent.update(replay_buffer, writer, step)
action = action.repeat(interval)
next_obs, reward, done, _ = env.step(action)
done_no_max = float(done)
episode_reward += reward
replay_buffer.add(obs, action, reward, next_obs, done,
done_no_max)
obs = next_obs
step += 1
writer.add_scalar('train/episode_reward', episode_reward, step)
print('step: {}, episode_reward: {}'.format(step, episode_reward))
if i % eval_freq == 0:
print('--------------------------------')
print('start evaluation')
print('--------------------------------')
episode_reward_list = []
evaluate_episodes = 3
for episode in range(evaluate_episodes):
obs = env.reset()
done = False
episode_reward = 0
while not done:
with utils.eval_mode(agent):
action = agent.act(obs, sample=True)
obs, rew, done, _ = env.step(action)
episode_reward += rew
episode_reward_list.append(episode_reward)
print(action)
writer.add_scalar('eval_mean/episode',
np.mean(episode_reward_list), step)
writer.add_scalar('eval_std/episode',
np.std(episode_reward_list), step)
print('episode: {} evaluate value: {} evaluate std: {}'.format(i, np.mean(episode_reward_list),
np.std(episode_reward_list)))
if np.mean(episode_reward_list) > max_test_episode:
max_test_episode = np.mean(episode_reward_list)
agent.save_model(actor_path=args.save_dir+"sac_actor_%d_Exp" % args.seed,
critic_path=args.save_dir+"sac_critic_%d_Exp" % args.seed)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--base_tmp_folder", default="./result", type=str)
parser.add_argument("--save_dir", default="SAC/sac_model/", type=str)
parser.add_argument("--version", default="incremental", type=str)
parser.add_argument("--seed", default=9, type=int)
parser.add_argument("--total_episode", default=8000, type=int)
args = parser.parse_args()
main(args)
# test(args)