-
Notifications
You must be signed in to change notification settings - Fork 2
/
IPM_algorithm.py
146 lines (133 loc) · 5.35 KB
/
IPM_algorithm.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
import numpy as np
from auxiliary_functions import regularization_selection
from cvxopt import matrix, sparse, solvers
def Train_QP_cvxopt(
function,
testing_function,
equality_constraints=None,
inequality_constraints=None,
show_progress=False,
type_parameter_search="minimization",
maximum_number_iterations_search=100,
maximum_number_iterations_final=100,
options={
"min": 0.0,
"max": 1.0,
"number_evaluations": 200,
"distribution": "uniform",
"inner_iterations": 5000,
},
absolute_tolerance=1.0e-7,
relative_tolerance=1.0e-6,
feasibility_tolernace=1.0e-7,
):
from auxiliary_functions import repeat_along_diag
num_basis_functions = function.number_basis_functions()
dimension = function.number_dimensions()
reference_point = np.random.rand(int(dimension * num_basis_functions))
gradient_val = function.gradient(reference_point)
hessian_matrix = repeat_along_diag(function.hessian(), dimension)
# Get the quadratic and the linear part of the "flattened" problem (i.e. the problem in vector form)
quadratic = hessian_matrix
linear = (
gradient_val
- 0.5 * reference_point.dot(quadratic)
- 0.5 * quadratic.dot(reference_point)
)
# Transform to a problem over the simplex.
simplex_quadratic = np.block([[quadratic, -quadratic], [-quadratic, quadratic]])
simplex_linear = np.hstack((linear, -linear))
simplex_dimensionality = 2 * int(dimension * num_basis_functions)
if equality_constraints is not None:
# Create the constraint matrix.
num_constraints = len(equality_constraints)
constraint_matrix = np.zeros((num_constraints, simplex_dimensionality))
constraint_vector = np.zeros(num_constraints)
for i in range(num_constraints):
for key, value in equality_constraints[i].items():
if key != "constant":
index = int(key.replace("x", ""))
constraint_matrix[i, index] = value
constraint_matrix[
i, index + int(num_basis_functions * dimension)
] = -value
else:
constraint_vector[i] = value
A = sparse(
matrix(
np.vstack((constraint_matrix, np.ones(simplex_dimensionality))),
(1 + num_constraints, simplex_dimensionality),
)
)
else:
A = matrix(np.ones(simplex_dimensionality), (1, simplex_dimensionality))
constraint_vector = None
if inequality_constraints is not None:
# Create the inequality constraint matrix.
num_ineq_constraints = len(inequality_constraints)
ineq_constraint_matrix = np.zeros(
(num_ineq_constraints, simplex_dimensionality)
)
ineq_constraint_vector = np.zeros(num_ineq_constraints)
for i in range(num_ineq_constraints):
for key, value in inequality_constraints[i].items():
if key != "constant":
index = int(key.replace("x", ""))
ineq_constraint_matrix[i, index] = value
ineq_constraint_matrix[
i, index + int(num_basis_functions * dimension)
] = -value
else:
ineq_constraint_vector[i] = value
G = sparse(
matrix(
np.vstack(
(ineq_constraint_matrix, -np.identity(simplex_dimensionality))
),
(simplex_dimensionality + num_ineq_constraints, simplex_dimensionality),
)
)
h = matrix(
np.append(ineq_constraint_vector, np.zeros(simplex_dimensionality)),
(simplex_dimensionality + num_ineq_constraints, 1),
)
else:
G = -sparse(
matrix(
np.identity(simplex_dimensionality),
)
)
h = matrix(np.zeros(simplex_dimensionality), (simplex_dimensionality, 1))
# Create the matrices that will be used in the solution process
P = sparse(matrix(simplex_quadratic))
q = matrix(simplex_linear, (simplex_dimensionality, 1))
# Select the best hyperparameters
solvers.options["show_progress"] = show_progress
solvers.options["absolute_tolerance"] = absolute_tolerance
solvers.options["relative_tolerance"] = relative_tolerance
solvers.options["feasibility_tolernace"] = feasibility_tolernace
alpha = regularization_selection(
lambda x: testing_function.evaluate(
QP_cvxopt(P, q, G, h, A, x, constraint_vector=constraint_vector)
),
type_parameter_search,
options,
show_progress=show_progress,
skip_minimization_check=True,
)
print("Regularization choosen (CVXOPT): ", alpha)
return QP_cvxopt(P, q, G, h, A, alpha, constraint_vector=constraint_vector)
def QP_cvxopt(P, q, G, h, A, l1_regularization, constraint_vector=None):
if constraint_vector is None:
b = matrix(l1_regularization)
else:
b = matrix(
np.append(constraint_vector, l1_regularization).tolist(),
(1 + len(constraint_vector), 1),
"d",
)
sol = solvers.qp(P, q, G, h, A, b)
optimum = np.array(sol["x"])
return (
optimum[: int(len(optimum) / 2)] - optimum[int(len(optimum) / 2) :]
).flatten()