-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFalkHoffmanAlgorithm.py
More file actions
445 lines (400 loc) · 16.4 KB
/
FalkHoffmanAlgorithm.py
File metadata and controls
445 lines (400 loc) · 16.4 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import numpy as np
from pulp import *
import collections
class FalkHoffmanInstance(object):
"""
Solve concave optimization problem on convex solution domain, applying the Falk-Hoffman Algorithm
[FalkHoffman1986]_. Notation inspired by that source.
References
----------
.. [FalkHoffman1986] James E. Falk, Karla L. Hoffman: "Concave Minimization via Collapsing Polytopes",
Operations Research 34.6 (1986). DOI: 10.1287/opre.34.6.919
"""
class solutionTreeNode(object):
"""
Object representing one node in the solution tree
"""
def __init__(self, problemInstance, x, y, w, s):
"""
Constructor
Parameters
----------
problemInstance : FalkHoffmanInstance
owning problem instance
x : numpy.array
node coordinates in the original optimization problem
y : float
distance value (f) for point v
w : numpy.array
feasible point associated with v
s : numpy.array
slack variable vector, corresponding to v
"""
self._problemInstance = problemInstance
# compose simplex tableau solution vector
self._solution = self._problemInstance.combineSolutionVector(x, y, s)
# we now perform a sanity check on the solution vector. In particular, the number of
# 0 values in the solution vector has to be n+1.
k = self._problemInstance.A.shape[1]+1
if np.sum(self._solution == 0) < k:
# partition set of indices into, split by n+1 smallest element
k_smallest_indices = np.argpartition(self._solution, k)
# set the values corresponding to the n+1 smallest values to 0
np.put(self._solution, k_smallest_indices[:k], 0)
# re-convert the solution into three components
self._x, self._y, self._s = self._problemInstance.getSolutionComponents(self._solution)
else: # no adjustment necessary, we will just use the values passed
# store coordinates and values associated with this point
self._x = x
self._w = w
self._y = y
self._s = s
# check if solution is feasible
self._feasible = True if np.isclose(self._y, 0) else False
# define some additional variables
self._children = []
self._f = self._problemInstance.f(w) # calculate target function value at w
# register node with problem instance
self._problemInstance.registerSolutionNode(self)
print 'New Node Created representing ' + str(self._x) + ' with f-Value: ' + str(self._f)
def getLowestTargetFunctionLeaf(self):
"""
Search tree for leaf with lowest value for target function value
"""
if self.isLeaf:
return self
else: # return minimum of direct children responses
currentLeaf = None
currentMinValue = np.inf
for c in self._children:
childBestLeaf = c.getLowestTargetFunctionLeaf()
if childBestLeaf.f < currentMinValue:
currentLeaf = childBestLeaf
currentMinValue = childBestLeaf.f
return currentLeaf
def branchOut(self):
"""
Create all direct child-nodes of the current solution node, performing all checks
on the elgibility of a possible new solution node
"""
# get binary indicator about which solution variables are in the basis
isBaseVector = (self._solution > 0)
# get matrix of Basis and non-basis columns from tableau
t = self._problemInstance.originalTableau
# get Matrix comprising of Basis Vectors and it's inverse, required for pivoting operation
B = t[:, isBaseVector]
B_inv = np.linalg.inv(B)
# calculate non-basis vectors in current simplex tableau
N = B_inv.dot(t[:, ~isBaseVector])
# compute and store current rhs tableau vector to speed up computation
currentRhs = B_inv.dot(self._problemInstance.b)
# Find row index corresponding to y
# first calculate current tableau column corresponding to y
yCol = B_inv.dot(t[:, self._problemInstance.A.shape[1]])
# find only index of yCol close to 1
yRow = np.argmax(np.isclose(yCol, 1))
# iterate through all column indices of non-basis vectors
# N_col will allow us to directly retrieve the corresponding column vector from N, saving
# computation time
for N_col, j in enumerate(np.array(range(t.shape[1]))[~isBaseVector]):
# get corresponding tableau column
col = N[:, N_col]
# implement first check for new nodes: A_ij > 0.
# entry in the entering variable column, in the row corresponding to y has to be > 0
if col[yRow] <= 0:
continue
# calculate possible theta values
thetaVector = np.divide(
currentRhs,
col
)
# create a solution vector for the new solutions
# first for new point v
solution_new_v = self._solution.copy()
solution_new_v[isBaseVector] = currentRhs-np.min(thetaVector[thetaVector > 0])*col
solution_new_v[j] = np.min(thetaVector[thetaVector > 0])
v_x, v_y, v_s = self._problemInstance.getSolutionComponents(solution_new_v)
# if y has improved, we also need to check, if the new x-coordinates are not
# already part of the solution tree
if self._problemInstance.solutionNodeExists(v_x):
continue
# if both tests have been passed, calculate the corresponding w-point
solution_new_w = self._solution.copy()
solution_new_w[isBaseVector] = currentRhs-thetaVector[yRow]*col
solution_new_w[j] = thetaVector[yRow]
w_x, w_y, w_s = self._problemInstance.getSolutionComponents(solution_new_w)
# create child node for this new solution
self._children.append(
FalkHoffmanInstance.solutionTreeNode(
self._problemInstance,
v_x,
v_y,
w_x,
v_s
)
)
def setF(self, newValue):
"""
Allow to externally set target function value.
External changes are only accepted for the root node, where this change is required by the
algorithm.
"""
# only allow changes at root node
if self == self._problemInstance.solutionTreeRoot:
self._f = newValue
isLeaf = property(
lambda self: len(self._children) == 0,
lambda self, newValue: False
)
"""Read only property: Direct child nodes"""
x = property(
lambda self: self._x,
lambda self, newValue: False
)
"""Read-only property: x-Coordinates"""
y = property(
lambda self: self._y,
lambda self, newValue: False
)
"""Read-only property: y-Value (distance to nearest constraint)"""
f = property(
lambda self: self._f,
lambda self, newValue: self.setF(newValue)
)
"""Read/Write access to target function value. External changes to target function value are
only permissible for the root node"""
feasible = property(
lambda self: self._feasible,
lambda self, newValue: False
)
"""Read only property: Is this solution feasible (y==0)"""
def __init__(self, f, A, b):
"""
Parameters
----------
f : callable
Function assumed to be concave. Called with numpy vector, representing a possible solution vector x.
Expcted to return float.
A : numpy.Matrix
m x n Matrix, representing the m inequality constraints (no slack variables). Should include non-negativity
constraints
b : numpy.array
Right-hand-side vector (such that Ax<=b)
"""
# store problem parameters
self._f = f
self._A = np.array(A)
self._b = b
# row wise euclidean norm, c.f. http://stackoverflow.com/a/7741976
self._a = np.sum(np.abs(self._A)**2, axis=-1)**(1./2)
# create original Simplex tableau
self._originalTableau = self.getInitialTableau()
# create dictionary to quickly check the existence of a solution node for a particular coordinate
self._solutionNodeDict = {}
# solve initial CP and create root node of solution tree
x, y, s = self.solveCP()
self._solutionTreeRoot = FalkHoffmanInstance.solutionTreeNode(
self,
x,
y,
x, # v and w are the same point here
s
)
self._solutionTreeRoot.f = np.inf # set target function value artificially to +infty
def registerSolutionNode(self, node):
"""
Make problem aware of the existence of a new node in the solution tree
Parameters
----------
node : solutionTreeNode
"""
self._solutionNodeDict[tuple(node.x)] = node
def solutionNodeExists(self, x):
"""
Check if a solution node to coordinate tuple x exist already.
Parameters
----------
x : numpy.array
coordinates
Returns
-------
c : Boolean
Result
"""
return tuple(x) in self._solutionNodeDict.keys()
def combineSolutionVector(self, x, y, s):
"""
Create combined solution vector, encompassing the solution point (x), the distance value y
and the slack variable vector s.
Parameters
----------
x : numpy.array
y : float
s : numpy.array
Returns
-------
solution : numpy.array
"""
solution = np.append(x, y)
return np.append(solution, s)
def getSolutionComponents(self, solution):
"""
Dissembles a solution point to the simplex tableau v into the components x (point in the original
decision problem), y (distance to constraints), and s (slack variable vector).
Parameters
----------
solution : numpy.float
Returns
-------
x : numpy.array
y : float
s : numpy.array
"""
x = solution[:self._A.shape[1]]
y = solution[self._A.shape[1]]
s = solution[self._A.shape[1]+1:]
return x, y, s
def getInitialTableau(self):
"""
Compose original (before any optimization) simplex tableau (no right-hand side vector)
Returns
-------
t : numpy.matrix
"""
# tableau has has many rows as A and one column for each column of A, y and one slack variable
# for each constraint
t = np.zeros((self._A.shape[0], self._A.shape[1]+1+self._A.shape[0]))
# the left most part of m is A
t[:, :self._A.shape[1]] = self._A
# next row is the length vector a
t[:, self._A.shape[1]] = self._a
# the other columns are an identity matrix
t[:, self._A.shape[1]+1:] = np.identity(self._A.shape[0])
return t
def solve(self, maxK=10000):
"""
Start solver.
Parameters
----------
maxK : int
Maximum number of stages to be computed. Default: 10,000
Returns
-------
y : float
best feasible solution found
l : list
list of numpy arrays, vectors that yield the best found target function value
status : str
Status string indicating the type of solution found. 'optimal' is returned if the algorithm
concluded as planned and optimality of the solution can be guaranteed. 'stopped' is returned
if the algorithm hit the maximum number of stages and was terminated.
"""
k = 0
status = 'stopped'
while k <= maxK:
k += 1 # increase stage counter
n = self._solutionTreeRoot.getLowestTargetFunctionLeaf()
if np.isclose(n.y, 0): # this is a feasible solution, we have finished
status = 'optimal'
break
else:
n.branchOut()
# the tree has stopped for whatever reason. Now we need to find leafs corresponding to
# the optimal target function values
optimalNodes = []
optimalTargetFunctionValue = np.inf
for x, node in self._solutionNodeDict.iteritems():
if node.feasible:
if np.isclose(node.f, optimalTargetFunctionValue):
optimalNodes.append(x)
elif node.f < optimalTargetFunctionValue:
optimalNodes = [x]
optimalTargetFunctionValue = node.f
return optimalTargetFunctionValue, optimalNodes, status
def solveCP(self):
"""
Solve Linear Optimization problem CP.
Returns
-------
x : numpy.array
Optimal point
y : float
maximum achieved sphere radius
s : numpy.array
Slack variable values at optimality
"""
prob = LpProblem("FHAlgorithmCP", LpMaximize)
# create solution variables
x = LpVariable.dicts(
"x",
indexs=range(self._A.shape[1]),
lowBound=0
)
y = LpVariable(
"y",
lowBound=0
)
s = LpVariable.dicts(
"s",
indexs=range(self._A.shape[0]),
lowBound=0
)
# target function: minimize weighted set size of active worksystems
prob += y, "Target Function"
# main constraints (Ax+ay+s = b)
for i in range(self._A.shape[0]):
prob += lpSum(
[
self._A[i, j] * x[j]
for j in range(self._A.shape[1])
]
) + self._a[i]*y + s[i] == self._b[i], 'Constraint ' + str(i)
# make sure y is minimum value (y <= (b_i-A(i)x)/||A(i)|| forall i)
for i in range(self._A.shape[0]):
prob += y <= (
self._b[i] - lpSum(
[
self._A[i, j] * x[j]
for j in range(self._A.shape[1])
]
)
)/self._a[i], 'y-minimization constraint, iteration ' + str(i)
# save and solve LP
prob.writeLP("FHAlgorithmCP.lp")
prob.solve()
if LpStatus[prob.status] == 'Optimal': # solution found
# return x, y, s as arrays/floats respectively
xRet = np.array([v.value() for v in x.values()])
yRet = y.value()
sRet = np.array([v.value() for v in s.values()])
# delete all temporary files created by the solver (lp and mps files)
for f in [f for f in os.listdir(".") if f.endswith(".mps") or f.endswith(".lp") or f.endswith(".sol")]:
os.remove(f)
return xRet, yRet, sRet
else:
assert False, 'Initial CP could not be solved at optimality'
originalTableau = property(
lambda self: self._originalTableau,
lambda self, newValue: False
)
"""read-only access on original simplex tableau"""
f = property(
lambda self: self._f,
lambda self, newValue: False
)
"""read-only access on target function"""
A = property(
lambda self: self._A,
lambda self, newValue: False
)
"""read-only access on constraint matrix"""
b = property(
lambda self: self._b,
lambda self, newValue: False
)
"""read-only access on right-hand side vector"""
solutionTreeRoot = property(
lambda self: self._solutionTreeRoot,
lambda self, newValue: False
)
"""read-only access on root node of solution tree"""