-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbundle.py
127 lines (105 loc) · 3.58 KB
/
bundle.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
from typing import Tuple, Union
import numpy as np
import cvxpy as cp
class ProximalBundleMethod:
r"""
Proximal bundle method
The impolementations is based on the figure 7.3 of [1]_ .
Parameters
----------
n: int
number of parameters
alpha: float
proximal parameter
gamma: float
sense: function
max or min
Attributes
----------
x: cvxpy.Variable
read only
custom_constraints: list of cvxpy.constraints.constraint.Constraint
constraints define the feasible region of x.
References
----------
.. [1] Andrzej Ruszczynski, "Nonlinear Optimization", Princeton University Press, 2006.
"""
_SENSE2DEFAULT = {
max: float("inf"),
min: float("-inf")
}
def __init__(
self, n: int, alpha: float = 1, gamma: float = 0.5, sense=max
):
self._alpha = alpha
self._gamma = gamma
self._sense = sense
self._x = cp.Variable(n)
self._y = cp.Variable()
self.custom_constraints = []
self._cuts = []
self._consts = []
self._center = None
self._center_obj = None
self._v = self._SENSE2DEFAULT[self._sense]
@property
def x(self):
return self._x
def _evaluate(self, x) -> float:
if self._sense == max:
op = min
else:
op = max
return op(
(obj + g @ (x - x0) for obj, x0, g in self._cuts),
default=self._SENSE2DEFAULT[self._sense]
)
def _get_cut_const(self, obj, x, grad):
if self._sense == max:
return self._y <= obj - grad @ x + grad @ self._x
else:
return self._y >= obj - grad @ x + grad @ self._x
def step(self, obj, x, subgrad) -> np.ndarray:
r"""
Add cut and get new point.
Parameters
----------
obj: float
objective value
x: numpy.ndarray
subgrad: numpy.ndarray
subgradient at x
Returns
-------
numpy.ndarray
the next point to evaluate
"""
self._add_cut(obj, x, subgrad)
return self._solve()
def _add_cut(self, obj, x, subgrad) -> None:
if self._center is None:
self._consts.append(self._get_cut_const(obj, x, subgrad))
self._cuts.append((obj, x, subgrad))
self._center = x
self._center_obj = obj
else:
tgt = (1 - self._gamma) * self._center_obj + self._gamma * self._evaluate(x)
if (self._sense == max and obj >= tgt) or (self._sense == min and obj <= tgt):
# Serious Step
self._center = x
self._center_obj = obj
if (self._sense == max and obj < self._v) or (self._sense == min and obj > self._v):
self._consts.append(self._get_cut_const(obj, x, subgrad))
self._cuts.append((obj, x, subgrad))
def _solve(self) -> Tuple[np.ndarray, bool]:
if self._sense == max:
obj = cp.Maximize(self._y - 0.5 * self._alpha * cp.sum_squares(self._x - self._center))
else:
obj = cp.Minimize(self._y + 0.5 * self._alpha * cp.sum_squares(self._x - self._center))
prob = cp.Problem(obj, self.custom_constraints + self._consts)
prob.solve()
self._v = self._y.value
idx = [i for i, c in enumerate(self._consts) if c.dual_value > 1e-8]
self._consts = [self._consts[i] for i in idx]
self._cuts = [self._cuts[i] for i in idx]
return self._x.value