-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_experiments.py
155 lines (130 loc) · 4.66 KB
/
run_experiments.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
import argparse
import copy
import pickle
import numpy
from metricdiversity.acquisitions.cas import ConstraintActiveSearchService
from metricdiversity.acquisitions.expected_metric_coverage import ExpectedMetricCoverageService
from metricdiversity.acquisitions.mobo import MOBOService
from metricdiversity.test_problems.problems import *
def run_mobo(test_problem, budget=None, reps=1):
problem = test_problem()
if budget is None:
assert problem.budget > 0
budget = problem.budget
all_results = []
problem_name = problem.__class__.__name__
print(f"Running problem {problem_name} for MOBO with budget={budget}.")
for i in range(reps):
print(f"replication {i + 1}/{reps}")
mobo = MOBOService(
parameters=problem.parameters,
constraints=problem.constraints,
num_init_points=len(problem.metric_names) * len(problem.parameter_names),
verbose=False,
)
for _ in range(budget):
suggestion = mobo.create_suggestion()
values = problem.evaluate(suggestion)
observation = {'suggestion': suggestion['id'], 'values': values}
mobo.create_observation(observation)
mobo_points = numpy.array(mobo.get_points())
mobo_values = numpy.array(mobo.get_values())
mobo_feasible_inds = numpy.array(mobo.get_feasible_indices())
all_results.append({
'points': mobo_points,
'values': mobo_values,
'satisfied': mobo_feasible_inds,
})
file_name = f"./results/{problem_name}_MOBO.pkl"
save_results(all_results, file_name)
return
def run_cas(test_problem, budget=None, reps=1):
problem = test_problem()
if budget is None:
assert problem.budget > 0
budget = problem.budget
all_results = []
problem_name = problem.__class__.__name__
print(f"Running problem {problem_name} for CAS with budget={budget}.")
for i in range(reps):
print(f"replication {i + 1}/{reps}")
cas = ConstraintActiveSearchService(
parameters=problem.parameters,
constraints=problem.constraints,
punchout_radius=problem.punchout_radius_param,
num_init_points=len(problem.metric_names) * len(problem.parameter_names),
verbose=False,
)
for _ in range(budget):
suggestion = cas.create_suggestion()
values = problem.evaluate(suggestion)
observation = {'suggestion': suggestion['id'], 'values': values}
cas.create_observation(observation)
cas_points = numpy.array(cas.get_points())
cas_values = numpy.array(cas.get_values())
cas_feasible_inds = numpy.array(cas.get_feasible_indices())
all_results.append({
'points': cas_points,
'values': cas_values,
'satisfied': cas_feasible_inds,
})
file_name = f"./results/{problem_name}_CAS.pkl"
save_results(all_results, file_name)
return
def run_emc(test_problem, budget=None, reps=1):
problem = test_problem()
if budget is None:
assert problem.budget > 0
budget = problem.budget
all_results = []
problem_name = problem.__class__.__name__
print(f"Running problem {problem_name} for EMC with budget={budget}.")
for i in range(reps):
print(f"replication {i + 1}/{reps}")
emc = ExpectedMetricCoverageService(
parameters=problem.parameters,
constraints=problem.constraints,
punchout_radius=problem.punchout_radius_metric_normalized,
num_init_points=len(problem.metric_names) * len(problem.parameter_names),
normalize_y=True,
verbose=False,
)
for _ in range(budget):
suggestion = emc.create_suggestion()
values = problem.evaluate(suggestion)
observation = {'suggestion': suggestion['id'], 'values': values}
emc.create_observation(observation)
emc_points = numpy.array(emc.get_points())
emc_values = numpy.array(emc.get_values())
emc_feasible_inds = numpy.array(emc.get_feasible_indices())
all_results.append({
'points': emc_points,
'values': emc_values,
'satisfied': emc_feasible_inds,
})
file_name = f"./results/{problem_name}_EMC.pkl"
save_results(all_results, file_name)
return
def save_results(results, filename):
with open(filename, 'wb') as file:
pickle.dump(results, file)
print(f"Saved result to {filename}")
def run_problems(args):
budget = args.budget
reps = args.reps
problems = [TwoHumps, EMI]
for problem in problems:
run_mobo(problem, budget, reps)
run_cas(problem, budget, reps)
run_emc(problem, budget, reps)
return
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--budget', type=int, required=False, default=None)
parser.add_argument('--reps', type=int, required=False, default=1)
return parser.parse_args()
def main():
args = parse_args()
run_problems(args)
if __name__ == '__main__':
main()