-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_sb3.py
50 lines (39 loc) · 1.4 KB
/
train_sb3.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
"""Sample script for training a control policy on the Hopper environment
using stable-baselines3 (https://stable-baselines3.readthedocs.io/en/master/)
Read the stable-baselines3 documentation and implement a training
pipeline with an RL algorithm of your choice between PPO and SAC.
"""
import gym
import numpy
from env import *
from stable_baselines3 import PPO
def main():
train_env = gym.make("CustomHopper-source-v0")
print('State space:', train_env.observation_space) # state-space
print('Action space:', train_env.action_space) # action-space
print('Dynamics parameters:', train_env.get_parameters()) # masses of each link of the Hopper
best_params = { # The best params found with the grid search for the source.
'learning_rate': 0.00078,
'n_steps': 2048,
'batch_size': 64,
'gamma': 0.99,
'clip_range': 0.25
}
model = PPO("MlpPolicy", train_env, verbose=0)
model.learn(total_timesteps=1)
vec_env = model.get_env()
obs = vec_env.reset()
cumul_reward = 0
done = False
i = 0
while not done and i < 10_000:
action, _states = model.predict(obs, deterministic=True)
obs, reward, done, info = vec_env.step(action)
vec_env.render()
cumul_reward += reward
i+=1
print(cumul_reward)
print(i)
model.save("uniform_model.mdl")
if __name__ == '__main__':
main()