-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
313 lines (256 loc) · 9.06 KB
/
functions.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import numpy as np
from scipy.sparse import csc_matrix
"""# Objective Functions
"""
#Basis generator.
#Generates a set of n-orthonormal vectors.
def rvs(dim=3):
random_state = np.random
H = np.eye(dim)
D = np.ones((dim,))
for n in range(1, dim):
x = random_state.normal(size=(dim-n+1,))
D[n-1] = np.sign(x[0])
x[0] -= D[n-1]*np.sqrt((x*x).sum())
# Householder transformation
Hx = (np.eye(dim-n+1) - 2.*np.outer(x, x)/(x*x).sum())
mat = np.eye(dim)
mat[n-1:, n-1:] = Hx
H = np.dot(H, mat)
# Fix the last sign such that the determinant is 1
D[-1] = (-1)**(1-(dim % 2))*D.prod()
# Equivalent to np.dot(np.diag(D), H) but faster, apparently
H = (D*H.T).T
return H
#Generate a random PSD quadratic with eigenvalues between certain numbers.
def randomPSDGenerator(dim, Mu, L):
eigenval = np.zeros(dim)
eigenval[0] = Mu
eigenval[-1] = L
eigenval[1:-1] = np.random.uniform(Mu, L, dim - 2)
M = np.zeros((dim, dim))
A = rvs(dim)
for i in range(dim):
M += eigenval[i]*np.outer(A[i], A[i])
return M
#Random PSD matrix with a given sparsity.
def randomPSDGeneratorSparse(dim, sparsity):
mask = np.random.rand(dim,dim)> (1- sparsity)
mat = np.random.normal(size = (dim,dim))
Aux = np.multiply(mat, mask)
return np.dot(Aux.T, Aux) + np.identity(dim)
def calculateEigenvalues(M):
from scipy.linalg import eigvalsh
dim = len(M)
L = eigvalsh(M, eigvals = (dim - 1,dim - 1))[0]
Mu = eigvalsh(M, eigvals = (0,0))[0]
return L, Mu
#Takes a random PSD matrix generated by the functions above and uses them as a function.
class funcQuadratic:
import numpy as np
def __init__(self, size, matrix, vector, Mu, L):
self.len = size
self.M = matrix.copy()
self.b = vector.copy()
self.L = L
self.Mu = Mu
return
def dim(self):
return self.len
#Evaluate function.
def fEval(self, x):
return 0.5*np.dot(x, self.M.dot(x)) + np.dot(self.b, x)
#Evaluate gradient.
def fEvalGrad(self, x):
return self.M.dot(x) + self.b
#Line Search.
def lineSearch(self, grad, d):
return -np.dot(grad, d)/np.dot(d, self.M.dot(d))
#Return largest eigenvalue.
def largestEig(self):
return self.L
#Return smallest eigenvalue.
def smallestEig(self):
return self.Mu
#Return largest eigenvalue.
def returnM(self):
return self.M
#Return smallest eigenvalue.
def returnb(self):
return self.b
class funcQuadraticDiag:
import numpy as np
def __init__(self, size, xOpt, Mu = 1.0, L = 2.0):
self.len = size
self.matdim = int(np.sqrt(size))
self.eigenval = np.zeros(size)
self.eigenval[0] = Mu
self.eigenval[-1] = L
self.eigenval[1:-1] = np.random.uniform(Mu, L, size - 2)
self.L = L
self.Mu = Mu
self.xOpt = xOpt
self.b = - np.multiply(self.xOpt, self.eigenval)
return
def dim(self):
return self.len
#Evaluate function.
def fEval(self, x):
return 0.5*np.dot(x, np.multiply(self.eigenval,x)) + np.dot(self.b, x)
#Evaluate gradient.
def fEvalGrad(self, x):
return np.multiply(x, self.eigenval) + self.b
#Return largest eigenvalue.
def largestEig(self):
return self.L
#Return smallest eigenvalue.
def smallestEig(self):
return self.Mu
#Line Search.
def lineSearch(self, grad, d):
return -np.dot(grad, d)/np.dot(d, np.multiply(self.eigenval, d))
#Return largest eigenvalue.
def returnM(self):
return self.eigenval
#Return smallest eigenvalue.
def returnb(self):
return self.b
#Function used in NAGD
class funcSimplexLambdaNormalizedEigen:
#Assemble the matrix from the active set.
def __init__(self, activeSet, z, A, L, Mu):
from scipy.sparse.linalg import eigsh
self.len = len(activeSet)
Mat = np.zeros((len(activeSet[0]), self.len))
self.c = Mu*A + L - Mu
self.b = np.zeros(len(activeSet))
for i in range(0, self.len):
Mat[:, i] = activeSet[i]
self.b[i] = -np.dot(z, activeSet[i])
self.b /= self.c
self.M = np.dot(np.transpose(Mat),Mat)
#Create a sparse matrix from the data.
self.M = csc_matrix(self.M)
if(self.M.shape == (1,1)):
self.L = 1.0
self.Mu = 1.0
else:
self.L = eigsh(self.M, 1, which='LM', return_eigenvectors = False)[0]
self.Mu = eigsh(self.M, 1, sigma=1.0e-10, which='LM', return_eigenvectors = False)[0]
return
def fEval(self, x):
return 0.5*np.dot(x.T, self.M.dot(x)) + np.dot(self.b, x)
def fEvalGrad(self, x):
return self.M.dot(x) + self.b
#Line Search.
def lineSearch(self, grad, d):
return -np.dot(grad, d)/np.dot(d, np.dot(self.M, d))
def returnM(self):
return self.M
def largestEig(self):
return self.L
def smallestEig(self):
return self.Mu
def FWGap(self, x):
grad = self.fEvalGrad(x)
v = np.zeros(len(x))
minVert = np.argmin(grad)
v[minVert] = 1.0
return np.dot(grad, x - v)
def returnlen(self):
return self.len
def update(self, activeSet, z, A, L, Mu):
self.c = Mu*A + L - Mu
self.b = np.zeros(len(activeSet))
for i in range(0, len(activeSet)):
self.b[i] = -np.dot(z, activeSet[i])
self.b /= self.c
return
#Function that will be used in the optimization problem.
#Creates a random quadratic with a positive semidefinite matrix.
class funcAccelScheme:
import numpy as np
#Input the initial point, the dimension of the set and the Q matrix.
#Check that the initial matrix is positive semidefinite.
def __init__(self, size, matrix, vector, L, Mu):
self.len = size
self.matdim = int(np.sqrt(size))
self.M = matrix.copy()
self.y = np.zeros(size, dtype = float)
self.kappa = 0.0
self.b = vector.copy()
self.L = L
self.Mu = Mu
def sety(self, yVal):
self.y = yVal
return
def setKappa(self, kappaVal):
self.kappa = kappaVal
return
def fEval(self, x):
return 0.5*np.dot(x, self.M.dot(x)) + np.dot(self.b, x) + self.kappa/2 * np.dot(x - self.y, x - self.y)
def fEvalBaseProblem(self, x):
return 0.5*np.dot(x, self.M.dot(x)) + np.dot(self.b, x)
def fEvalGrad(self, x):
return self.M.dot(x) + self.b + self.kappa * (x - self.y)
def fEvalGradBaseProblem(self, x):
return self.M.dot(x) + self.b
#Line Search.
def lineSearch(self, grad, d):
return -np.dot(grad, d)/(np.dot(d, self.M.dot(d)) + self.kappa*np.dot(d,d))
def FWGap(self, x, feasReg):
grad = self.fEvalGrad(x)
v = feasReg.LPOracle(grad)
return np.dot(grad, (x - v))
def FWGapBaseProblem(self, x, feasReg):
grad = self.fEvalGradBaseProblem(x)
v = feasReg.LPOracle(grad)
return np.dot(grad, (x - v))
def largestEig(self):
return self.L
def smallestEig(self):
return self.Mu
#Function that will be used in the optimization problem.
#Creates a random quadratic with a positive semidefinite matrix.
class funcAccelSchemeDiag:
import numpy as np
#Input the initial point, the dimension of the set and the Q matrix.
#Check that the initial matrix is positive semidefinite.
def __init__(self, size, matrix, vector, L, Mu):
self.len = size
self.matdim = int(np.sqrt(size))
self.M = matrix
self.y = np.zeros(size, dtype = float)
self.kappa = 0.0
self.b = vector
self.L = L
self.Mu = Mu
def sety(self, yVal):
self.y = yVal
return
def setKappa(self, kappaVal):
self.kappa = kappaVal
return
def fEval(self, x):
return 0.5*np.dot(x,np.multiply(self.M,x)) + np.dot(self.b, x) + self.kappa/2 * np.dot(x - self.y, x - self.y)
def fEvalBaseProblem(self, x):
return 0.5*np.dot(x,np.multiply(self.M,x)) + np.dot(self.b, x)
def fEvalGrad(self, x):
return np.multiply(x, self.M) + self.b + self.kappa * (x - self.y)
def fEvalGradBaseProblem(self, x):
return np.multiply(x, self.M) + self.b
def FWGap(self, x, feasReg):
grad = self.fEvalGrad(x)
v = feasReg.LPOracle(grad)
return np.dot(grad, (x - v))
def FWGapBaseProblem(self, x, feasReg):
grad = self.fEvalGradBaseProblem(x)
v = feasReg.LPOracle(grad)
return np.dot(grad, (x - v))
#Line Search.
def lineSearch(self, grad, d):
return -np.dot(grad, d)/np.dot(d, np.multiply(self.M, d))
def largestEig(self):
return self.L
def smallestEig(self):
return self.Mu