-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
142 lines (119 loc) · 5.84 KB
/
train.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
import time
import gym
import wandb
import numpy as np
from stable_baselines3 import SAC
import os
import sys
from pathlib import Path
import random
from stable_baselines3.common.vec_env import DummyVecEnv
import models.denseMlpPolicy
from stable_baselines3.common.evaluation import evaluate_policy
from trackingEnv import TrackingEnv
def make_env(
optimal_distance: float = 0.75,
ts: float = 0.05,
mass_noise: bool = True,
output_delay: bool = True,
sensor_noise: bool = True,
start_noise: bool = True,
move_target: bool = True,
asymmetric_actor_buffer_length: int = 15):
def _init() -> gym.Env:
env = TrackingEnv(
optimal_distance=optimal_distance,
Ts=ts,
sensor_noise=sensor_noise,
mass_noise=mass_noise,
start_noise=start_noise,
output_delay=output_delay,
move_target=move_target,
asymmetric_actor_buffer_length=asymmetric_actor_buffer_length
)
env.seed(random.randint(1, 10000))
return env
return _init
if __name__ == "__main__":
settings = {'num_cpu': 8,
'policy': 'DenseMlpPolicy',
'n_timesteps': 10000,
'WandB': False,
'WandB_project': '<WandB_project>',
'WandB_entity': '<WandB_entity>',
'WandB_API_key': '<WandB_API_key>',
'render': False,
'eval_episodes': 5,
'eval_mode': False,
'optimal_distance': 0.75,
'Ts': 0.05,
'sensor_noise': True,
'mass_noise': True,
'start_noise': True,
'output_delay': True,
'move_target': True,
'asymmetric_actor_buffer_length': 15
}
if settings['eval_mode']:
eval_env = DummyVecEnv([make_env(optimal_distance=settings['optimal_distance'],
ts=settings['Ts'],
sensor_noise=settings['sensor_noise'],
mass_noise=settings['mass_noise'],
start_noise=settings['start_noise'],
output_delay=settings['output_delay'],
move_target=settings['move_target'],
asymmetric_actor_buffer_length=settings['asymmetric_actor_buffer_length'])])
# MODEL TO TEST
model_ID = 1672672721
model_NUMBER = 0
model = SAC.load(os.path.join("experiments/SAC_{}".format(model_ID), "SAC_{}".format(model_NUMBER)), env=eval_env)
# Eval
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=settings['eval_episodes'],
render=settings['render'])
print(f'Mean reward: {mean_reward} +/- {std_reward:.2f}')
else:
t = int(time.time())
# Path for Models
pathname = os.path.dirname(sys.argv[0])
abs_path = os.path.abspath(pathname)
current_path = Path(os.path.join(abs_path, "experiments", "SAC_{}".format(t)))
current_path.mkdir(parents=True, exist_ok=True)
if settings['WandB']:
wandb.login(key=settings['WandB_API_key'])
wandb.init(project=settings['WandB_project'], entity=settings['WandB_entity'],
name="SAC_{}".format(t), config=settings)
# Training VecEnv
vec_env = DummyVecEnv([make_env(optimal_distance=settings['optimal_distance'],
ts=settings['Ts'],
sensor_noise=settings['sensor_noise'],
mass_noise=settings['mass_noise'],
start_noise=settings['start_noise'],
output_delay=settings['output_delay'],
move_target=settings['move_target'],
asymmetric_actor_buffer_length=settings['asymmetric_actor_buffer_length']) for i in range(settings['num_cpu'])])
# Create Model for Training
model = SAC(settings['policy'], vec_env, verbose=1)
# We create a separate environment for evaluation
eval_env = DummyVecEnv([make_env(optimal_distance=settings['optimal_distance'],
ts=settings['Ts'],
sensor_noise=settings['sensor_noise'],
mass_noise=settings['mass_noise'],
start_noise=settings['start_noise'],
output_delay=settings['output_delay'],
move_target=settings['move_target'],
asymmetric_actor_buffer_length=settings['asymmetric_actor_buffer_length'])])
# Save Best Models
best_episodes = np.full((10,), -100.0)
# RL Training
while True:
model.learn(settings['n_timesteps'])
# Eval
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=settings['eval_episodes'], render=settings['render'])
print(f'Mean reward: {mean_reward} +/- {std_reward:.2f}')
if settings['WandB']:
wandb.log({'test': mean_reward})
worst_model = np.argmin(best_episodes)
if mean_reward > best_episodes[worst_model]:
best_episodes[worst_model] = mean_reward
model.save(os.path.join(current_path, "SAC_{}".format(worst_model)))
np.savetxt(os.path.join(current_path, "models_score.csv"), best_episodes, delimiter=",")