-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiment_profiles.py
More file actions
137 lines (110 loc) · 5.91 KB
/
experiment_profiles.py
File metadata and controls
137 lines (110 loc) · 5.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
import numpy as np
from cobyqa import minimize as cobyqa_minimize
from optiprofiler import set_cutest_problem_options, find_cutest_problems, run_benchmark
from pdfo import pdfo as pdfo_minimize
from pybobyqa import solve as pybobyqa_minimize
from scipy.optimize import Bounds, LinearConstraint, NonlinearConstraint
def uobyqa(fun, x0):
"""
Solve an unconstrained optimization problem using UOBYQA.
"""
res = pdfo_minimize(fun, x0, method='uobyqa')
return res.x
def newuoa(fun, x0):
"""
Solve an unconstrained optimization problem using NEWUOA.
"""
res = pdfo_minimize(fun, x0, method='newuoa')
return res.x
def bobyqa(fun, x0, lb, ub):
"""
Solve a bound-constrained optimization problem using BOBYQA.
"""
bounds = _build_bounds(lb, ub)
res = pdfo_minimize(fun, x0, method='bobyqa', bounds=bounds)
return res.x
def pybobyqa(fun, x0, lb, ub):
"""
Solve a bound-constrained optimization problem using Py-BOBYQA.
"""
res = pybobyqa_minimize(fun, x0, bounds=(lb, ub))
return res.x
def lincoa(fun, x0, lb, ub, a_ub, b_ub, a_eq, b_eq):
"""
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)
return res.x
def cobyla(fun, x0, lb, ub, a_ub, b_ub, a_eq, b_eq, c_ub, c_eq):
"""
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 = pdfo_minimize(fun, x0, method='cobyla', bounds=bounds, constraints=constraints)
return res.x
def cobyqa(fun, x0, lb, ub, a_ub, b_ub, a_eq, b_eq, c_ub, c_eq):
"""
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)
return res.x
def _build_bounds(lb, ub):
"""
Build the bound constraints.
"""
return Bounds(lb, ub)
def _build_linear_constraints(a_ub, b_ub, a_eq, b_eq):
"""
Build the linear constraints.
"""
constraints = []
if b_ub.size > 0:
constraints.append(LinearConstraint(a_ub, -np.inf, b_ub))
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 = []
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)))
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__':
# Consider only problems with up to 50 variables and 5000 constraints.
set_cutest_problem_options(n_max=50, m_max=5000)
# Run the benchmark on all unconstrained problems.
cutest_problem_names = find_cutest_problems('unconstrained')
run_benchmark([cobyqa, newuoa, cobyla], ['COBYQA', 'NEWUOA', 'COBYLA'], cutest_problem_names, benchmark_id='unconstrained')
run_benchmark([cobyqa, newuoa, cobyla], ['COBYQA', 'NEWUOA', 'COBYLA'], cutest_problem_names, feature_name='noisy', benchmark_id='unconstrained')
# Run the benchmark on all bound-constrained problems.
cutest_problem_names = find_cutest_problems('bound')
run_benchmark([cobyqa, bobyqa, pybobyqa, cobyla], ['COBYQA', 'BOBYQA', 'Py-BOBYQA', 'COBYLA'], cutest_problem_names, benchmark_id='bound-constrained', project_x0=True)
run_benchmark([cobyqa, bobyqa, pybobyqa, cobyla], ['COBYQA', 'BOBYQA', 'Py-BOBYQA', 'COBYLA'], cutest_problem_names, feature_name='unrelaxable_constraints', benchmark_id='bound-constrained', project_x0=True, at_least_one_bound_constraint=True)
run_benchmark([cobyqa, bobyqa, pybobyqa, cobyla], ['COBYQA', 'BOBYQA', 'Py-BOBYQA', 'COBYLA'], cutest_problem_names, feature_name='noisy', benchmark_id='bound-constrained', project_x0=True)
# Run the benchmark on all linearly constrained problems.
cutest_problem_names = find_cutest_problems('adjacency linear')
run_benchmark([cobyqa, lincoa, cobyla], ['COBYQA', 'LINCOA', 'COBYLA'], cutest_problem_names, benchmark_id='linearly-constrained', project_x0=True)
run_benchmark([cobyqa, lincoa, cobyla], ['COBYQA', 'LINCOA', 'COBYLA'], cutest_problem_names, feature_name='unrelaxable_constraints', benchmark_id='linearly-constrained', project_x0=True, at_least_one_bound_constraint=True)
run_benchmark([cobyqa, lincoa, cobyla], ['COBYQA', 'LINCOA', 'COBYLA'], cutest_problem_names, feature_name='noisy', benchmark_id='linearly-constrained', project_x0=True)
# Run the benchmark on all nonlinearly constrained problems.
cutest_problem_names = find_cutest_problems('quadratic other')
run_benchmark([cobyqa, cobyla], ['COBYQA', 'COBYLA'], cutest_problem_names, benchmark_id='nonlinearly-constrained')
run_benchmark([cobyqa, cobyla], ['COBYQA', 'COBYLA'], cutest_problem_names, feature_name='unrelaxable_constraints', benchmark_id='nonlinearly-constrained', at_least_one_bound_constraint=True)
run_benchmark([cobyqa, cobyla], ['COBYQA', 'COBYLA'], cutest_problem_names, feature_name='noisy', benchmark_id='nonlinearly-constrained')
# Run the benchmark on all problems.
cutest_problem_names = find_cutest_problems('unconstrained bound adjacency linear quadratic other')
run_benchmark([cobyqa, cobyla], ['COBYQA', 'COBYLA'], cutest_problem_names, benchmark_id='all')
run_benchmark([cobyqa, cobyla], ['COBYQA', 'COBYLA'], cutest_problem_names, feature_name='noisy', benchmark_id='all')