-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
138 lines (118 loc) · 5.43 KB
/
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
import copy
import importlib
import logging
import pickle
import sys
import gym_db # noqa: F401
from gym_db.common import EnvironmentType
from swirl.experiment import Experiment
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
assert len(sys.argv) == 2, "Experiment configuration file must be provided: main.py path_fo_file.json"
CONFIGURATION_FILE = sys.argv[1]
experiment = Experiment(CONFIGURATION_FILE)
if experiment.config["rl_algorithm"]["stable_baselines_version"] == 2:
from stable_baselines.common.callbacks import EvalCallbackWithTBRunningAverage
from stable_baselines.common.vec_env import DummyVecEnv, SubprocVecEnv, VecNormalize
algorithm_class = getattr(
importlib.import_module("stable_baselines"), experiment.config["rl_algorithm"]["algorithm"]
)
elif experiment.config["rl_algorithm"]["stable_baselines_version"] == 3:
from stable_baselines3.common.callbacks import EvalCallbackWithTBRunningAverage
from stable_baselines3.common.vec_env import DummyVecEnv, SubprocVecEnv, VecNormalize
algorithm_class = getattr(
importlib.import_module("stable_baselines3"), experiment.config["rl_algorithm"]["algorithm"]
)
else:
raise ValueError
experiment.prepare()
ParallelEnv = SubprocVecEnv if experiment.config["parallel_environments"] > 1 else DummyVecEnv
training_env = ParallelEnv(
[experiment.make_env(env_id) for env_id in range(experiment.config["parallel_environments"])]
)
training_env = VecNormalize(
training_env, norm_obs=True, norm_reward=True, gamma=experiment.config["rl_algorithm"]["gamma"], training=True
)
experiment.model_type = algorithm_class
with open(f"{experiment.experiment_folder_path}/experiment_object.pickle", "wb") as handle:
pickle.dump(experiment, handle, protocol=pickle.HIGHEST_PROTOCOL)
model = algorithm_class(
policy=experiment.config["rl_algorithm"]["policy"],
env=training_env,
verbose=2,
seed=experiment.config["random_seed"],
gamma=experiment.config["rl_algorithm"]["gamma"],
tensorboard_log="tensor_log",
policy_kwargs=copy.copy(
experiment.config["rl_algorithm"]["model_architecture"]
), # This is necessary because SB modifies the passed dict.
**experiment.config["rl_algorithm"]["args"],
)
logging.warning(f"Creating model with NN architecture: {experiment.config['rl_algorithm']['model_architecture']}")
experiment.set_model(model)
experiment.compare()
callback_test_env = VecNormalize(
DummyVecEnv([experiment.make_env(0, EnvironmentType.TESTING)]),
norm_obs=True,
norm_reward=False,
gamma=experiment.config["rl_algorithm"]["gamma"],
training=False,
)
test_callback = EvalCallbackWithTBRunningAverage(
n_eval_episodes=experiment.config["workload"]["validation_testing"]["number_of_workloads"],
eval_freq=round(experiment.config["validation_frequency"] / experiment.config["parallel_environments"]),
eval_env=callback_test_env,
verbose=1,
name="test",
deterministic=True,
comparison_performances=experiment.comparison_performances["test"],
)
callback_validation_env = VecNormalize(
DummyVecEnv([experiment.make_env(0, EnvironmentType.VALIDATION)]),
norm_obs=True,
norm_reward=False,
gamma=experiment.config["rl_algorithm"]["gamma"],
training=False,
)
validation_callback = EvalCallbackWithTBRunningAverage(
n_eval_episodes=experiment.config["workload"]["validation_testing"]["number_of_workloads"],
eval_freq=round(experiment.config["validation_frequency"] / experiment.config["parallel_environments"]),
eval_env=callback_validation_env,
best_model_save_path=experiment.experiment_folder_path,
verbose=1,
name="validation",
deterministic=True,
comparison_performances=experiment.comparison_performances["validation"],
)
callbacks = [validation_callback, test_callback]
if len(experiment.multi_validation_wl) > 0:
callback_multi_validation_env = VecNormalize(
DummyVecEnv([experiment.make_env(0, EnvironmentType.VALIDATION, experiment.multi_validation_wl)]),
norm_obs=True,
norm_reward=False,
gamma=experiment.config["rl_algorithm"]["gamma"],
training=False,
)
multi_validation_callback = EvalCallbackWithTBRunningAverage(
n_eval_episodes=len(experiment.multi_validation_wl),
eval_freq=round(experiment.config["validation_frequency"] / experiment.config["parallel_environments"]),
eval_env=callback_multi_validation_env,
best_model_save_path=experiment.experiment_folder_path,
verbose=1,
name="multi_validation",
deterministic=True,
comparison_performances={},
)
callbacks.append(multi_validation_callback)
experiment.start_learning()
model.learn(
total_timesteps=experiment.config["timesteps"],
callback=callbacks,
tb_log_name=experiment.id,
)
experiment.finish_learning(
training_env,
validation_callback.moving_average_step * experiment.config["parallel_environments"],
validation_callback.best_model_step * experiment.config["parallel_environments"],
)
experiment.finish()