-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_individual_migration.py
218 lines (205 loc) · 6.24 KB
/
run_individual_migration.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import argparse
import logging
import os
import numpy as np
from config import (
MOCK_SIM_NAME,
SIMULATOR_NAMES,
NUM_CONTROL_NODES,
MAX_ANGLE,
NUM_SAMPLED_POINTS,
AGENT_TYPES,
)
from envs.beamng.config import MAP_SIZE
from factories import make_test_generator, make_env, make_agent
from global_log import GlobalLog
from test_generators.mapelites.config import (
FEATURE_COMBINATIONS,
TURNS_COUNT_FEATURE_NAME,
CURVATURE_FEATURE_NAME,
)
from test_generators.mapelites.mapelites import MapElites
from utils.randomness import set_random_seed
from utils.report_utils import load_individual_report
parser = argparse.ArgumentParser()
parser.add_argument(
"--folder", help="Path of the folder where the logs are", type=str, default="logs"
)
parser.add_argument(
"--filepath",
help="Paths of the folders where the reports are",
type=str,
required=True,
)
# run arguments
parser.add_argument(
"--env-name",
help="Should be the name of the third simulator",
type=str,
choices=SIMULATOR_NAMES,
)
parser.add_argument(
"--donkey-exe-path",
help="Path to the donkey simulator executor",
type=str,
default=None,
)
parser.add_argument(
"--udacity-exe-path",
help="Path to the udacity simulator executor",
type=str,
default=None,
)
parser.add_argument(
"--beamng-user-path", help="Beamng user path", type=str, default=None
)
parser.add_argument(
"--beamng-home-path", help="Beamng home path", type=str, default=None
)
parser.add_argument("--seed", help="Random seed", type=int, default=-1)
parser.add_argument(
"--add-to-port", help="Modify default simulator port", type=int, default=-1
)
parser.add_argument(
"--headless", help="Headless simulation", action="store_true", default=False
)
parser.add_argument(
"--agent-type", help="Agent type", type=str, choices=AGENT_TYPES, default="random"
)
parser.add_argument(
"--test-generator",
help="Which test generator to use",
type=str,
choices=["random"],
default="random",
)
parser.add_argument(
"--model-path",
help="Path to agent model with extension (only if agent_type == 'supervised')",
type=str,
default=None,
)
parser.add_argument(
"--predict-throttle",
help="Predict steering and throttle. Model to load must have been trained using an output dimension of 2",
action="store_true",
default=False,
)
parser.add_argument(
"--feature-combination",
help="Feature combination",
type=str,
choices=FEATURE_COMBINATIONS,
default="{}-{}".format(TURNS_COUNT_FEATURE_NAME, CURVATURE_FEATURE_NAME),
)
parser.add_argument(
"--collect-images",
help="Collect images during execution",
action="store_true",
default=False,
)
# cyclegan options
parser.add_argument(
"--cyclegan-experiment-name",
type=str,
default=None,
help="name of the experiment. It decides where to store samples and models",
)
parser.add_argument(
"--gpu-ids",
type=str,
default="-1",
help="gpu ids: e.g. 0 0,1,2, 0,2. use -1 for CPU",
)
parser.add_argument(
"--cyclegan-checkpoints-dir", type=str, default=None, help="models are saved here"
)
parser.add_argument(
"--cyclegan-epoch",
type=str,
default=-1,
help="which epoch to load? set to latest to use latest cached model",
)
args = parser.parse_args()
if __name__ == "__main__":
logg = GlobalLog("run_individual_migration")
# load probability map by default
report = load_individual_report(filepath=os.path.join(args.folder, args.filepath))
individuals = [
individual
for feature_bin in report.keys()
for individual in report[feature_bin]
]
if args.seed == -1:
args.seed = np.random.randint(2**30 - 1)
set_random_seed(seed=args.seed)
test_generator = make_test_generator(
generator_name=args.test_generator,
map_size=MAP_SIZE,
simulator_name=args.env_name,
agent_type=args.agent_type,
num_control_nodes=NUM_CONTROL_NODES,
max_angle=MAX_ANGLE,
num_spline_nodes=NUM_SAMPLED_POINTS,
)
env = make_env(
simulator_name=args.env_name,
seed=args.seed,
port=args.add_to_port,
test_generator=test_generator,
donkey_exe_path=args.donkey_exe_path,
udacity_exe_path=args.udacity_exe_path,
beamng_home=args.beamng_home_path,
beamng_user=args.beamng_user_path,
headless=args.headless,
beamng_autopilot=args.agent_type == "autopilot",
cyclegan_experiment_name=args.cyclegan_experiment_name,
gpu_ids=args.gpu_ids,
cyclegan_checkpoints_dir=args.cyclegan_checkpoints_dir,
cyclegan_epoch=args.cyclegan_epoch,
)
agent = make_agent(
env_name=args.env_name,
env=env,
model_path=args.model_path,
agent_type=args.agent_type,
predict_throttle=args.predict_throttle,
fake_images=args.cyclegan_experiment_name is not None
and args.cyclegan_checkpoints_dir is not None
and args.cyclegan_epoch != -1,
)
logg.info("Disabling Shapely logs")
for id in ["shapely.geos"]:
l = logging.getLogger(id)
l.setLevel(logging.CRITICAL)
l.disabled = True
mapelites = MapElites(
env=env,
env_name=args.env_name,
agent=agent,
filepath=args.folder,
min_angle=0,
max_angle=1, # to pass the assertion
mutation_extent=0,
population_size=0,
mock_evaluator=args.env_name == MOCK_SIM_NAME,
iteration_runtime=0,
test_generator=test_generator,
individual_migration=True,
feature_combination=args.feature_combination,
port=args.add_to_port,
donkey_exe_path=args.donkey_exe_path,
udacity_exe_path=args.udacity_exe_path,
beamng_home=args.beamng_home_path,
beamng_user=args.beamng_user_path,
headless=args.headless,
collect_images=args.collect_images,
cyclegan_experiment_name=args.cyclegan_experiment_name,
gpu_ids=args.gpu_ids,
cyclegan_checkpoints_dir=args.cyclegan_checkpoints_dir,
cyclegan_epoch=args.cyclegan_epoch,
)
# load probability map by default
mapelites.execute_individuals_and_place_in_map(
individuals=individuals, occupation_map=False
)