-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_search_optimizer.py
198 lines (173 loc) · 7.67 KB
/
sequential_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
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
"""
Implement violation-aware Bayesian optimizer.
"""
import numpy as np
import safeopt
import copy
import vacbo
import util
from keep_default_optimizer import KeepDefaultOpt
DISCOMFORT_THR = 10
optimization_config = {
'eval_budget': 40
}
optimizer_base_config = {
'noise_level': [0.004, 0.2, 0.2],
'kernel_var': 0.1,
'train_noise_level': 1.0,
'problem_name': 'SinglePIRoomEvaluator',
'normalize_input': False
}
VARS_TO_FIX = ['high_on_time', 'high_off_time', 'high_setpoint',
'low_setpoint', 'control_setpoint']
CONTEXTUAL_VARS = ['Q_irr', 'T_out', 'T_init']
def get_no_opt_optimizer(problem_name, optimizer_type, optimizer_config,
init_points_id=0, discomfort_thr=2.0,
vars_to_fix=None, contextual_vars=None,
start_date_time=None, fixed_param=None,
discomfort_weight=0.01, tune_var_scale='log'):
problem_config = util.get_config(
problem_name, gp_kernel='Matern52', init_points_id=init_points_id,
discomfort_thr=discomfort_thr, vars_to_fix=VARS_TO_FIX,
start_eval_time=start_date_time, room_simulator='PCNN',
discomfort_weight=discomfort_weight, tune_PI_scale=tune_var_scale,
contextual_vars=CONTEXTUAL_VARS)
optimizer_config.update(problem_config)
if fixed_param is not None:
problem_config['init_safe_points'] = fixed_param
problem = vacbo.ContextualOptimizationProblem(problem_config)
if optimizer_type == 'no opt':
opt = KeepDefaultOpt(problem, optimizer_config)
total_cost_list = [opt.cumu_vio_cost]
return opt, total_cost_list, problem
def evaluate_seq_control(fixed_param, discomfort_thr,
control_seq, discomfort_weight=0.01,
optimizer_base_config=None,
optimization_config=None,
vars_to_fix=None):
# try fixing one parameter
no_opt_config = copy.deepcopy(optimizer_base_config)
no_opt, no_opt_best_obj_list, no_opt_total_cost_list = \
get_no_opt_optimizer(
no_opt_config['problem_name'], 'no opt', no_opt_config,
discomfort_thr=discomfort_thr, vars_to_fix=vars_to_fix,
fixed_param=fixed_param, discomfort_weight=discomfort_weight)
num_of_control = len(control_seq[:, 0])
for _ in range(num_of_control):
y_obj, constr_vals = no_opt.make_step(
np.expand_dims(control_seq[_, :], axis=0))
simulator = no_opt.opt_problem.simulator
simulator.update_history_dict()
cumulative_discomfort = simulator.cumulative_discomfort
date_time_list = list(simulator.history_dict.keys())
cumulative_energy = sum(
simulator.history_dict_to_list('power room', date_time_list)
) * 0.001 * 0.25
num_of_data = len(date_time_list)
energy_per_day = cumulative_energy / (num_of_data / 96)
ave_discomfort = cumulative_discomfort / num_of_data
return energy_per_day, ave_discomfort, cumulative_energy, \
cumulative_discomfort
class SeqGridSearchOpt:
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
if 'discomfort_weight' in keep_default_config.keys():
self.discomfort_weight = keep_default_config['discomfort_weight']
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 look_ahead_step(self):
current_control_seq = np.atleast_2d(
np.squeeze(np.array(self.query_points_list))
)
total_controller_num = len(self.parameter_set[:, 0])
aug_energy_per_day_list = []
aug_discomfort_per_day_list = []
for controller_id in range(total_controller_num):
new_control = np.expand_dims(
self.parameter_set[controller_id, :], axis=0)
#print(new_control, current_control_seq)
if current_control_seq.shape[1] > 0:
augment_control_seq = np.concatenate(
(current_control_seq, new_control), axis=0)
else:
augment_control_seq = new_control
energy_per_day, ave_discomfort, cumulative_energy, \
cumulative_discomfort = evaluate_seq_control(
new_control, DISCOMFORT_THR, augment_control_seq,
optimizer_base_config=optimizer_base_config,
optimization_config=optimization_config,
vars_to_fix=VARS_TO_FIX)
aug_energy_per_day_list.append(energy_per_day)
aug_discomfort_per_day_list.append(ave_discomfort)
# optimization criterion
discomfort_weight = self.discomfort_weight
aug_energy_per_day_arr = np.array(aug_energy_per_day_list)
aug_discomfort_per_day_arr = np.array(aug_discomfort_per_day_list)
weighted_min_obj = aug_energy_per_day_arr + \
discomfort_weight * aug_discomfort_per_day_arr
print(aug_energy_per_day_arr,
aug_discomfort_per_day_arr,
discomfort_weight,
weighted_min_obj)
opt_id = np.argmin(weighted_min_obj)
opt_controller = np.array(np.expand_dims(
self.parameter_set[opt_id, :], axis=0))
return opt_controller
def make_step(self, evaluate_point=None):
self.current_step += 1
if evaluate_point is None:
x_next = self.look_ahead_step()
else:
x_next = evaluate_point
# Get a measurement from the real system
# print(evaluate_point)
# print(x_next)
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