-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_search_optimizer.py
85 lines (73 loc) · 2.99 KB
/
grid_search_optimizer.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
"""
Implement violation-aware Bayesian optimizer.
"""
import numpy as np
import safeopt
import GPy
from scipy.stats import norm
class GridSearchOpt:
def __init__(self, opt_problem, keep_default_config):
self.current_step = 0
self.opt_problem = opt_problem
self.noise_level = keep_default_config['noise_level']
self.current_step = 0
self.cumu_vio_cost = 0
# Bounds on the inputs variable
self.bounds = opt_problem.bounds
self.discret_num_list = opt_problem.discretize_num_list
# set of parameters
self.parameter_set = safeopt.linearly_spaced_combinations(
self.bounds,
self.discret_num_list
)
# Initial safe point
self.x0_arr = opt_problem.init_safe_points
self.query_points_list = []
self.query_point_obj = []
self.query_point_constrs = []
self.S = []
# self.kernel_list = []
init_obj_val_arr, init_constr_val_arr = \
self.get_obj_constr_val(self.x0_arr)
self.init_obj_val_arr = init_obj_val_arr
self.init_constr_val_arr = init_constr_val_arr
self.best_obj = np.min(init_obj_val_arr)
best_obj_id = np.argmin(init_obj_val_arr[:, 0])
self.best_sol = self.x0_arr[best_obj_id, :]
self.evaluate_id = 0
def get_obj_constr_val(self, x_arr, noise=False):
obj_val_arr, constr_val_arr = self.opt_problem.sample_point(x_arr)
return obj_val_arr, constr_val_arr
def plot(self):
# Plot the GP
self.opt.plot(100)
# Plot the true function
y, constr_val = self.get_obj_constr_val(self.parameter_set,
noise=False)
def make_step(self, evaluate_point=None):
self.current_step += 1
if evaluate_point is None:
eval_id = self.evaluate_id
try:
x_next = np.expand_dims(self.parameter_set[eval_id, :], axis=0)
except Exception:
x_next = np.expand_dims(self.parameter_set[0, :], axis=0)
self.evaluate_id = eval_id + 1
else:
x_next = evaluate_point
# Get a measurement from the real system
y_obj, constr_vals = self.get_obj_constr_val(x_next)
self.query_points_list.append(x_next)
self.query_point_obj.append(y_obj)
self.query_point_constrs.append(constr_vals)
vio_cost = self.opt_problem.get_total_violation_cost(constr_vals)
vio_cost = np.squeeze(vio_cost)
if np.all(constr_vals <= 0):
# update best objective if we get a feasible point
if self.best_obj > y_obj[0, 0]:
self.best_sol = x_next
self.best_obj = np.min([y_obj[0, 0], self.best_obj])
violation_cost = self.opt_problem.get_total_violation_cost(constr_vals)
violation_total_cost = np.sum(violation_cost, axis=0)
self.cumu_vio_cost = self.cumu_vio_cost + violation_total_cost
return y_obj, constr_vals