forked from LORER-MTL/ocotools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem.py
282 lines (220 loc) · 8.04 KB
/
Problem.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
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
import cvxpy as cp
from Barrier import *
class Problem:
"""
An OCO problem. Gives access to the function value, gradient and Hessian at any point in its domain.
Some have built-in update functions that represent the time-varying element of the problem.
"""
def __init__(self, f: cp.Objective, grad):
self.f = f
self.grad = grad
def increment(self):
pass
def optimal(self):
pass
class LinConsProblem(Problem):
def __init__(self, n=1):
super().__init__(self)
self.n = n
self.A = np.zeros((0, n))
self.b = np.zeros((0, 1))
self.baseB = self.b
def getA(self):
return self.A
def getb(self):
return self.b
def setA(self, A):
self.A = A
def setb(self, b):
self.b = b
self.baseB = b
def violation(self, x):
return np.linalg.norm([email protected])
class LinearProgram(LinConsProblem):
def __init__(self, n=None, A=None, b=None, C=None, d=None, c=None):
super().__init__(n)
if A is not None: self.setA(A)
if b is not None: self.setb(b)
self.C = np.zeros((0, n)) if C is None else C
self.d = np.zeros((0, 1)) if d is None else d
self.c = np.zeros((1, n)) if c is None else c
self.grad = self.gradfunc
self.f = self.func
def getC(self):
return self.C
def getd(self):
return self.d
def getc(self):
return self.C
def setC(self, C):
self.C = C
def setc(self, c):
self.c = c
def setd(self, d):
self.d = d
def violation(self, x):
return super().violation(x)+np.linalg.norm(np.max([email protected], 0))
def loss(self, x):
return self.c@x
def gradfunc(self, x):
return self.c.T
def func(self, x):
return self.c.dot(x.T)
def getH(self, x):
return np.zeros((self.n, self.n))
def optimal(self):
prob, x = self.optProblem()
prob.solve()
return prob.value, x.value
def optProblem(self):
x = cp.Variable((self.n, 1))
obj = cp.Minimize(self.c@x)
constraints = [self.A@x==self.b.T, self.C@x <= self.d.T]
prob = cp.Problem(obj, constraints)
return prob, x
class OCOMPC(LinearProgram):
def __init__(self, n=1, A=None, b=None, C=None, d=None, c=None):
super().__init__(n, A, b, C, d, c)
self.setBarrier()
def setBarrier(self):
self.barr = BarrSum(self.n)
for col in range(self.C.shape[0]):
self.barr += LinLogBar(self.C[[col], :], self.d[:,[col]])
def barrGradHess(self, x):
return self.barr.grad(x), self.barr.hess(x)
def sigmoid(x):
return 1/(1+np.e**-(x))
def sigmoidGrad(x):
return sigmoid(x)**2 *np.e**-(x)
def sigmoidHess(x):
return np.e**(-x)*(2*np.e**(-x)*sigmoid(x)**3)-sigmoidGrad(x)
class NetFlow(LinConsProblem):
def __init__(self, n=1):
super().__init__(n)
self.f = self.loss
self.grad = self.gradFct
self.scaling = np.ones((n, 1))
self.alpha = np.ones((n, 1))
self.beta = np.zeros((n, 1))
def setA(self, conjMatrix):
pass
def loss(self, x):
return np.sum(self.scaling*
(sigmoid(self.alpha*x+self.beta)+sigmoid(-self.alpha*x)))
def gradFct(self, x):
return self.alpha*(sigmoidGrad(self.alpha*x+self.beta)-sigmoidGrad(-self.alpha*x))
def hessFct(self, x):
return self.alpha**2*(sigmoidHess((self.alpha*x+self.beta)*np.identity(self.n))+
sigmoidHess(-self.alpha*x*np.identity(self.n)))
def getH(self, x):
return self.hessFct(x)
class ConvNetFlow(LinConsProblem):
def __init__(self, n=1):
super().__init__(n)
self.f = self.loss
self.grad = self.gradFct
self.c = np.ones((n, 1))
self.alpha = np.ones((n, 1))
self.beta = np.zeros((n, 1))
self.T = 1
def setLossParams(self, alpha, beta, c):
self.alpha = alpha
self.beta = beta
self.c = c
def loss(self, x):
return np.sum(self.alpha*x**2+self.beta*x+self.c)
def gradFct(self, x):
return self.alpha*2*x+self.beta
def hessFct(self, x):
return np.identity(self.n) *2* self.alpha
def getH(self, x):
return self.hessFct(x)
def optimal(self):
x = cp.Variable((self.n, 1))
cost = cp.sum(cp.multiply(self.alpha, x**2)+cp.multiply(self.beta,x)+self.c)
constraints = []
for row in range(self.A.shape[0]):
constraints.append(cp.sum(cp.multiply(self.A[row].reshape(self.n,1), x)) == self.b[row])
prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve(solver="GUROBI")
return prob.value, x.value
def optProblem(self):
x = cp.Variable((self.n, 1))
cost = cp.sum(cp.multiply(self.alpha, x**2)+cp.multiply(self.beta,x)+self.c)
constraints = []
for row in range(self.A.shape[0]):
constraints.append(cp.sum(cp.multiply(self.A[row].reshape(self.n,1), x)) == self.b[row])
prob = cp.Problem(cp.Minimize(cost), constraints)
return prob
def randomIncrement(self):
self.alpha = np.random.sample(self.alpha.shape)*5
def subIncrement(self):
self.T += 1
self.alpha = np.ones(self.alpha.shape) + np.random.sample(self.alpha.shape)*10/self.T
def increment(self):
self.randomIncrement()
class ExpNetFlow(LinConsProblem):
def __init__(self, n=1):
super().__init__(n)
self.f = self.loss
self.grad = self.gradFct
self.c = np.ones((n, 1))
self.alpha = np.ones((n, 1))
self.beta = np.ones((n, 1))/10
self.T = 1
def setLossParams(self, alpha, beta, c):
self.alpha = alpha
self.beta = beta
self.c = c
def loss(self, x):
return np.sum(self.alpha*np.e**(self.beta*x)+self.c)
def gradFct(self, x):
return self.alpha*self.beta*np.e**(self.beta*x)
def hessFct(self, x):
return np.identity(self.n) * self.alpha*self.beta**2*np.e**(self.beta*x)
def getH(self, x):
return self.hessFct(x)
def optimal(self):
x = cp.Variable((self.n, 1))
cost = cp.sum(cp.multiply(self.alpha, cp.exp(cp.multiply(x, self.beta)))+self.c)
constraints = []
for row in range(self.A.shape[0]):
constraints.append(cp.sum(cp.multiply(self.A[row].reshape(self.n,1), x)) == self.b[row])
prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve()
return prob.value, x.value
def randomIncrement(self, scaling=100):
self.alpha = np.random.sample(self.alpha.shape)*scaling
self.beta = np.random.sample(self.beta.shape)/10+1/10
def subIncrement(self, scaling=100):
self.alpha = np.random.sample(self.alpha.shape)*scaling/np.sqrt((self.T+20)**1.75)+1
self.beta = np.random.sample(self.beta.shape)/10/np.sqrt((self.T+20)**1.75)+1/10
self.b = self.baseB+np.random.sample(self.baseB.shape)*scaling/np.sqrt((self.T+20)**1.75)
self.T += 1
def increment(self):
self.subIncrement()
class QLCP(Problem):
def __init__(self, n):
super().__init__()
self.n = n
self.increment()
def increment(self):
Q = np.random.random((self.n, self.n))
Q = (Q + Q.T)/2
def f(x):
return float(x.T@Q@x)
def grad(x):
return 2*Q@x
self.f = f
self.grad = grad
self.H = Q
m = np.random.randint(1,5)
self.A = np.random.random((m, self.n))
self.b = np.random.random((m, 1))
def getA(self):
return self.A
def getb(self):
return self.b
def getH(self, x):
return self.H