-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiment_profiles.py
More file actions
282 lines (256 loc) · 7.91 KB
/
Copy pathexperiment_profiles.py
File metadata and controls
282 lines (256 loc) · 7.91 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import numpy as np
from cobyqa import minimize as cobyqa_minimize
from optiprofiler import benchmark
from pdfo import pdfo as pdfo_minimize
from pybobyqa import solve as pybobyqa_minimize
from scipy.optimize import Bounds, LinearConstraint, NonlinearConstraint, minimize as scipy_minimize
def uobyqa(fun, x0):
"""
Solve an unconstrained optimization problem using UOBYQA.
"""
res = pdfo_minimize(fun, x0, method='uobyqa', options={
"maxfev": 500 * x0.size,
})
return res.x
def newuoa(fun, x0):
"""
Solve an unconstrained optimization problem using NEWUOA.
"""
res = pdfo_minimize(fun, x0, method='newuoa', options={
"maxfev": 500 * x0.size,
})
return res.x
def bobyqa(fun, x0, lb=None, ub=None):
"""
Solve a bound-constrained optimization problem using BOBYQA.
"""
bounds = _build_bounds(lb, ub)
res = pdfo_minimize(fun, x0, method='bobyqa', bounds=bounds, options={
"maxfev": 500 * x0.size,
})
return res.x
def pybobyqa(fun, x0, lb=None, ub=None):
"""
Solve a bound-constrained optimization problem using Py-BOBYQA.
"""
res = pybobyqa_minimize(fun, x0, bounds=(lb, ub), maxfun=500 * x0.size)
return res.x
def lincoa(fun, x0, lb=None, ub=None, a_ub=None, b_ub=None, a_eq=None, b_eq=None):
"""
Solve a linearly constrained optimization problem using LINCOA.
"""
bounds = _build_bounds(lb, ub)
constraints = _build_linear_constraints(a_ub, b_ub, a_eq, b_eq)
res = pdfo_minimize(fun, x0, method='lincoa', bounds=bounds, constraints=constraints, options={
"maxfev": 500 * x0.size,
})
return res.x
def cobyla(fun, x0, lb=None, ub=None, a_ub=None, b_ub=None, a_eq=None, b_eq=None, c_ub=None, c_eq=None):
"""
Solve a nonlinearly constrained optimization problem using COBYLA.
"""
bounds = _build_bounds(lb, ub)
constraints = _build_linear_constraints(a_ub, b_ub, a_eq, b_eq)
constraints += _build_nonlinear_constraints(c_ub, c_eq, x0)
res = scipy_minimize(fun, x0, method='cobyla', bounds=bounds, constraints=constraints, options={
"maxiter": 500 * x0.size,
})
return res.x
def cobyqa(fun, x0, lb=None, ub=None, a_ub=None, b_ub=None, a_eq=None, b_eq=None, c_ub=None, c_eq=None):
"""
Solve a nonlinearly constrained optimization problem using COBYQA.
"""
bounds = _build_bounds(lb, ub)
constraints = _build_linear_constraints(a_ub, b_ub, a_eq, b_eq)
constraints += _build_nonlinear_constraints(c_ub, c_eq, x0)
res = cobyqa_minimize(fun, x0, bounds=bounds, constraints=constraints, options={
"maxfev": 500 * x0.size,
})
return res.x
def _build_bounds(lb, ub):
"""
Build the bound constraints.
"""
if lb is None or ub is None:
return None
return Bounds(lb, ub)
def _build_linear_constraints(a_ub, b_ub, a_eq, b_eq):
"""
Build the linear constraints.
"""
constraints = []
if a_ub is not None and b_ub is not None:
if b_ub.size > 0:
constraints.append(LinearConstraint(a_ub, -np.inf, b_ub))
if a_eq is not None and b_eq is not None:
if b_eq.size > 0:
constraints.append(LinearConstraint(a_eq, b_eq, b_eq))
return constraints
def _build_nonlinear_constraints(c_ub, c_eq, x0):
"""
Build the nonlinear constraints.
"""
constraints = []
if c_ub is not None:
c_ub_x0 = c_ub(x0)
if c_ub_x0.size > 0:
constraints.append(NonlinearConstraint(c_ub, -np.inf, np.zeros_like(c_ub_x0)))
if c_eq is not None:
c_eq_x0 = c_eq(x0)
if c_eq_x0.size > 0:
constraints.append(NonlinearConstraint(c_eq, np.zeros_like(c_eq_x0), np.zeros_like(c_eq_x0)))
return constraints
if __name__ == '__main__':
# Run the benchmark on all unconstrained problems with up to 50 variables.
benchmark(
[cobyqa, newuoa, cobyla],
solver_names=['COBYQA', 'NEWUOA', 'COBYLA'],
benchmark_id='out_unconstrained',
maxdim=50,
)
benchmark(
[cobyqa, newuoa, cobyla],
solver_names=['COBYQA', 'NEWUOA', 'COBYLA'],
benchmark_id='out_unconstrained',
maxdim=50,
feature_name='noisy',
)
# Run the benchmark on all bound-constrained problems with up to 50 variables.
benchmark(
[cobyqa, bobyqa, cobyla],
solver_names=['COBYQA', 'BOBYQA', 'COBYLA'],
benchmark_id='out_bound-constrained',
ptype='b',
maxdim=50,
maxb=np.inf,
project_x0=True,
)
benchmark(
[cobyqa, bobyqa, cobyla],
solver_names=['COBYQA', 'BOBYQA', 'COBYLA'],
benchmark_id='out_bound-constrained',
ptype='b',
maxdim=50,
minb=1,
maxb=np.inf,
feature_name='unrelaxable_constraints',
project_x0=True,
)
benchmark(
[cobyqa, bobyqa, cobyla],
solver_names=['COBYQA', 'BOBYQA', 'COBYLA'],
benchmark_id='out_bound-constrained',
ptype='b',
maxdim=50,
maxb=np.inf,
feature_name='noisy',
project_x0=True,
)
# Run the benchmark on all linearly constrained problems with up to 50 variables and 5000 constraints.
benchmark(
[cobyqa, lincoa, cobyla],
solver_names=['COBYQA', 'LINCOA', 'COBYLA'],
benchmark_id='out_linearly-constrained',
ptype='l',
maxdim=50,
maxb=np.inf,
maxlcon=5000,
project_x0=True,
)
benchmark(
[cobyqa, lincoa, cobyla],
solver_names=['COBYQA', 'LINCOA', 'COBYLA'],
benchmark_id='out_linearly-constrained',
ptype='l',
maxdim=50,
minb=1,
maxb=np.inf,
maxlcon=5000,
feature_name='unrelaxable_constraints',
project_x0=True,
)
benchmark(
[cobyqa, lincoa, cobyla],
solver_names=['COBYQA', 'LINCOA', 'COBYLA'],
benchmark_id='out_linearly-constrained',
ptype='l',
maxdim=50,
maxb=np.inf,
maxlcon=5000,
feature_name='noisy',
project_x0=True,
)
# Run the benchmark on all nonlinearly constrained problems with up to 50 variables and 5000 constraints.
benchmark(
[cobyqa, cobyla],
solver_names=['COBYQA', 'COBYLA'],
benchmark_id='out_nonlinearly-constrained',
ptype='n',
maxdim=50,
maxb=np.inf,
maxlcon=5000,
maxnlcon=5000,
project_x0=True,
)
benchmark(
[cobyqa, cobyla],
solver_names=['COBYQA', 'COBYLA'],
benchmark_id='out_nonlinearly-constrained',
ptype='n',
maxdim=50,
minb=1,
maxb=np.inf,
maxlcon=5000,
maxnlcon=5000,
feature_name='unrelaxable_constraints',
project_x0=True,
)
benchmark(
[cobyqa, cobyla],
solver_names=['COBYQA', 'COBYLA'],
benchmark_id='out_nonlinearly-constrained',
ptype='n',
maxdim=50,
maxb=np.inf,
maxlcon=5000,
maxnlcon=5000,
feature_name='noisy',
project_x0=True,
)
# Run the benchmark on all problems with up to 50 variables and 5000 constraints.
benchmark(
[cobyqa, cobyla],
solver_names=['COBYQA', 'COBYLA'],
benchmark_id='out_all',
ptype='ubln',
maxdim=50,
maxb=np.inf,
maxlcon=5000,
maxnlcon=5000,
project_x0=True,
)
benchmark(
[cobyqa, cobyla],
solver_names=['COBYQA', 'COBYLA'],
benchmark_id='out_all',
ptype='ubln',
maxdim=50,
minb=1,
maxb=np.inf,
maxlcon=5000,
maxnlcon=5000,
feature_name='unrelaxable_constraints',
project_x0=True,
)
benchmark(
[cobyqa, cobyla],
solver_names=['COBYQA', 'COBYLA'],
benchmark_id='out_all',
ptype='ubln',
maxdim=50,
maxb=np.inf,
maxlcon=5000,
maxnlcon=5000,
feature_name='noisy',
project_x0=True,
)