-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (69 loc) · 2.83 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
from skopt.space import Real, Integer, Categorical
import signal
import io
import time
import subprocess
import sys
import pathlib
import os
from train_instance import TrainInstance
sys.path.append("HyperParameter-Optimizer/")
from gaussian_process import GaussianProcessSearch
file_dir = str(pathlib.Path(__file__).resolve().parents[0])
def signal_handler(sig, frame):
print('\nSIGINT signal received: killing instances...')
for instance in instances:
instance.kill()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
search_space = [
Real(low=0.01, high=1., name='reproduction_reward'),
Real(low=1e-7, high=1e-4, name='time_step_modifier'),
Real(low=0., high=1e-5, name='pop_reward_modifier'),
]
if __name__ == "__main__":
num_instances = 4
# Paths and files
gpro_input_file = None # Use None to start from zero
env_dir = "envs/"
env_path = os.path.join(env_dir, "optimization_test.x86_64")
log_files_dir = os.path.join(file_dir, "logs/")
output_files_dir = "out_files/"
config_file = "config/chickens.yaml"
gp_search = GaussianProcessSearch(search_space=search_space,
fixed_space={},
evaluator=None,
input_file=gpro_input_file,
output_file='results.csv')
# Instantiate training instances
instances = []
for i in range(num_instances):
instances.append(TrainInstance(port=i,
env_path=env_path,
log_files_dir=log_files_dir,
output_files_dir=output_files_dir,
config_file=config_file))
# Start training all instances
candidates = []
if gpro_input_file is None: # If first points, sample random
candidates = gp_search.get_random_candidate(num_instances)
else:
candidates = gp_search.get_next_candidate(num_instances)
for i in range(num_instances):
instances[i].train(candidates[i])
while(True):
time.sleep(60) # refresh rate in seconds
for i in range(num_instances):
instance = instances[i]
if instance.inactive:
candidate = gp_search.get_next_candidate(1)[0]
instance.train(candidate)
elif instance.is_done():
instance_params = instance.meta_params
instance_result = instance.get_val()
print("instance_params:", instance_params)
print("instance_result:", instance_result)
gp_search.add_point_value(instance_params, instance_result)
gp_search.save_values()
instance.kill()
# print("Instance", i, "has value:", instance.get_val())