-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_train.py
181 lines (138 loc) · 5.82 KB
/
run_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
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
import os
import sys
import numpy as np
# Add project root to the python path
sys.path.append(os.path.dirname(__file__))
from utils.individual import Models
from evolutionary.neuroevolution import NeuroEvolution
from rl.rlpibb import RlPibb
def neuro_evolution_train(model_type:str, env_type: str, fixed_centres: bool, generations: int, max_steps: int, gen_size: int,
elite_size: int, load_elite: bool=False, alt_cpgs: bool=False, add_noise: bool=False, mean: float=1.0, std: float=0.001):
"""Train a model using neuroevolution"""
try:
# Check model type
if model_type not in ["FC", "CPG-FC", "RBFN-FC", "CPG-RBFN"]:
raise ValueError("Model type not supported")
# Initialize neuroevolution
neuro_evolution = NeuroEvolution(model_type=model_type, env_type=env_type, fixed_centres=fixed_centres, generations=generations, max_steps=max_steps,
gen_size=gen_size, mean=mean, std=std, elite_size=elite_size, load_elite=load_elite, alt_cpgs=alt_cpgs, add_noise=add_noise)
# Run neuroevolution
print("Running Neuro evolution training...")
neuro_evolution.run(verbose=True)
# Get path to save data
model_path = os.path.join(os.getcwd(), "data", model_type)
if not os.path.exists(model_path):
os.makedirs(model_path)
# Save results
print("TRAINING COMPLETED...")
neuro_evolution.save(model_path)
# Close environment
neuro_evolution.env.close()
# Get plots
neuro_evolution.get_plots(model_path, is_show=False)
except KeyboardInterrupt:
print("TRAINING INTERRUPTED !!")
# Get path to save data
model_path = os.path.join(os.getcwd(), "data", env_type, model_type, "not fixed")
if not os.path.exists(model_path):
os.makedirs(model_path)
#Set new path to save files if fixed centers are selected
if fixed_centres:
model_path = os.path.join(os.getcwd(), "data", env_type, model_type, "fixed")
if not os.path.exists(model_path):
os.makedirs(model_path)
elif alt_cpgs and add_noise:
model_path = os.path.join(os.getcwd(), "data", env_type, model_type, "noisy_alt_cpgs")
if not os.path.exists(model_path):
os.makedirs(model_path)
elif alt_cpgs:
model_path = os.path.join(os.getcwd(), "data", env_type, model_type, "alt_cpgs")
if not os.path.exists(model_path):
os.makedirs(model_path)
elif add_noise:
model_path = os.path.join(os.getcwd(), "data", env_type, model_type, "add_noise")
if not os.path.exists(model_path):
os.makedirs(model_path)
# Save data
neuro_evolution.save(model_path)
# Close environment
neuro_evolution.env.close()
# Get plots
neuro_evolution.get_plots(model_path, is_show=False)
# Exit
sys.exit()
def rl_pibb_train(env_type: str, epochs: int, max_steps: int, rollout_size: int, norm_constant: float,
variance: float, decay:float, alt_cpgs: bool, add_noise: bool, test_case: int):
"""Train a model using RL-PIBB"""
try:
# Initialize RL-PIBB
rl_pibb = RlPibb(env_type, epochs, max_steps, rollout_size, norm_constant, variance, decay, alt_cpgs, add_noise, test_case)
# Run RL-PIBB
print("Running RL-PIBB training...")
rl_pibb.run(verbose=True)
# Get path to save data
model_path = os.path.join(os.getcwd(), "data", "RL-PIBB", f"set_{test_case}_alt{int(alt_cpgs)}_noise{int(add_noise)}")
if not os.path.exists(model_path):
os.makedirs(model_path)
# Save results
print("TRAINING COMPLETED...")
rl_pibb.save(model_path)
# Close environment
rl_pibb.env.close()
# Get plots
# rl_pibb.get_plots(model_path, is_show=False)
except KeyboardInterrupt:
print("TRAINING INTERRUPTED !!")
# Get path to save data
model_path = os.path.join(os.getcwd(), "data", "RL-PIBB", f"set_{test_case}_alt{int(alt_cpgs)}_noise{int(add_noise)}")
if not os.path.exists(model_path):
os.makedirs(model_path)
# Save data
rl_pibb.save(model_path)
# Close environment
rl_pibb.env.close()
# Get plots
# rl_pibb.get_plots(model_path, is_show=False)
# Exit
sys.exit()
if __name__ == "__main__":
#Gym environment
env_type = "HalfCheetah-v4"
# env_type = "Walker2d-v4"
## MODEL TYPE
models = Models()
# model_type = models.FC_MODEL
# model_type = models.CPG_FC_MODEL
# model_type = models.RBFN_FC_MODEL
model_type = models.CPG_RBFN_MODEL
# NEUROEVOLUTION PARAMS
fixed_centres = False
load_elite = False
alt_cpgs = True
add_noise = True
generations = 10000
max_steps = 1000
gen_size = 10
elite_size = 10
mean = 0.0
std = 0.01
# Run neuroevolution
neuro_evolution_train(model_type=model_type, env_type=env_type, fixed_centres=fixed_centres, generations=generations, max_steps=max_steps,
gen_size=gen_size, mean=mean, alt_cpgs=alt_cpgs, std=std, elite_size=elite_size, load_elite=load_elite, add_noise=add_noise)
# RL-PIBB PARAMS
epochs = 500
max_steps = 1000
rollout_size = 10
norm_constant = 10.0
variance = 0.05
decay = 0.995
test_case = 1
alt_cpgs = False
add_noise = True
# Read alt_cpgs from command line
if len(sys.argv) > 1:
alt_cpgs = bool(sys.argv[1])
print(f"Alt CPGs: {alt_cpgs}")
print(f"Add noise: {add_noise}")
# Run RL-PIBB
rl_pibb_train(env_type, epochs, max_steps, rollout_size, norm_constant, variance, decay, alt_cpgs, add_noise, test_case)