-
Notifications
You must be signed in to change notification settings - Fork 0
/
feasibleRegions.py
288 lines (246 loc) · 9.81 KB
/
feasibleRegions.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
import numpy as np
from auxiliaryFunctions import maxVertex
"""# LP Oracles"""
#Birkhoff Polytope feasible region.
class BirkhoffPolytope:
def __init__(self, dim):
self.dim = dim
self.matdim = int(np.sqrt(dim))
def LPOracle(self, x):
from scipy.optimize import linear_sum_assignment
objective = x.reshape((self.matdim, self.matdim))
matching = linear_sum_assignment(objective)
solution = np.zeros((self.matdim, self.matdim))
solution[matching] = 1
return solution.reshape(self.dim)
def initialPoint(self):
return np.identity(self.matdim).flatten()
#Input is the vector over which we calculate the inner product.
def AwayOracle(self, grad, activeVertex):
return maxVertex(grad, activeVertex)
#Birkhoff Polytope feasible region.
class probabilitySimplexPolytope:
def __init__(self, dim):
self.dim = dim
def LPOracle(self, x):
v = np.zeros(len(x), dtype = float)
v[np.argmin(x)] = 1.0
return v
#Input is the vector over which we calculate the inner product.
def AwayOracle(self, grad, x):
aux = np.multiply(grad, np.sign(x))
indices = np.where(x > 0.0)[0]
v = np.zeros(len(x), dtype = float)
indexMax = indices[np.argmax(aux[indices])]
v[indexMax] = 1.0
return v, indexMax
def initialPoint(self):
v = np.zeros(self.dim)
v[0] = 1.0
return v
"""LP model based on Gurobi solver."""
from gurobipy import GRB, read, Column
###############################
# Algorithm Configuration
###############################
run_config = {
'solution_only': True,
'verbosity': 'normal',
'OutputFlag': 0,
'dual_gap_acc': 1e-06,
'runningTimeLimit': None,
'use_LPSep_oracle': True,
'max_lsFW': 100000,
'strict_dropSteps': True,
'max_stepsSub': 100000,
'max_lsSub': 100000,
'LPsolver_timelimit': 100000,
'K': 1
}
"""## Gurobi LP Solver
Used for the MIPLIB and the Birkhoff Example.
"""
class GurobiPolytope:
"""LP model implemented via Gurobi."""
def __init__(self, modelFilename, addCubeConstraints=False, transform_to_equality=False):
model = read(modelFilename)
model.setParam('OutputFlag', False)
model.params.TimeLimit = run_config['LPsolver_timelimit']
model.params.threads = 4
model.params.MIPFocus = 0
model.update()
if addCubeConstraints:
counter = 0
for v in model.getVars():
model.addConstr(v <= 1, 'unitCubeConst' + str(counter))
counter += 1
model.update()
if transform_to_equality:
for c in model.getConstrs():
sense = c.sense
if sense == GRB.GREATER_EQUAL:
model.addVar(obj=0, name="ArtN_" + c.constrName, column=Column([-1], [c]))
if sense == GRB.LESS_EQUAL:
model.addVar(obj=0, name="ArtP_" + c.constrName, column=Column([1], [c]))
c.sense = GRB.EQUAL
model.update()
self.dimension = len(model.getVars())
self.model = model
return
"""
To find the total number of constraints in a model: model.NumConstrs
To return the constraints of a model: model.getConstrs()
To add a single constraint to the model model.addConstr(model.getVars()[-1] == 0, name = 'newConstraint1')
If we want to delete the last constraint that was added we do: model.remove(model.getConstrs()[-1])
"""
def LPOracle(self, cc):
"""Find good solution for cc with optimality callback."""
m = self.model
for it, v in enumerate(m.getVars()):
v.setAttr(GRB.attr.Obj, cc[it])
#Update the model with the new atributes.
m.update()
m.optimize(lambda mod, where: fakeCallback(mod, where, GRB.INFINITY))
# Status checking
status = m.getAttr(GRB.Attr.Status)
if status == GRB.INF_OR_UNBD or \
status == GRB.INFEASIBLE or \
status == GRB.UNBOUNDED:
assert False, "The model cannot be solved because it is infeasible or unbounded"
if status != GRB.OPTIMAL:
print(status)
assert False, "Optimization was stopped."
#Store the solution that will be outputted.
solution = np.array([v.x for v in m.getVars()], dtype=float)[:]
#Check that the initial number of constraints and the final number is the same.
return solution
def initialPoint(self):
print("Finding Initial Point.")
return self.LPOracle(np.zeros(self.dimension))
#Input is the vector over which we calculate the inner product.
def AwayOracle(self, grad, activeVertex):
return maxVertex(grad, activeVertex)
def dim(self):
return self.dimension
def fakeCallback(model, where, value):
ggEps = 1e-08
if where == GRB.Callback.MIPSOL:
# x = model.cbGetSolution(model.getVars())
# logging.info 'type of x: ' + str(type(x))
obj = model.cbGet(GRB.Callback.MIPSOL_OBJ)
if obj < value - ggEps:
print('early termination with objective value :{}'.format(obj))
print('which is better than {}'.format(value - ggEps))
# model.terminate()
if where == GRB.Callback.MIP:
objBnd = model.cbGet(GRB.Callback.MIP_OBJBND)
if objBnd >= value + ggEps:
# model.terminate()
pass
import networkx as nx
import matplotlib.pyplot as plt
import math
#Randomly generated Graphs
#p is the redirection probability.
def generateRandomGraph(n, p):
DG = nx.gnr_graph(n, p)
return DG
#Two vertices in the end.
#m represents the number of layers
#s represents the number of nodes per layer.
def generateStructuredGraph(layers, nodesPerLayer):
m = layers
s = nodesPerLayer
DG = nx.DiGraph()
DG.add_nodes_from(range(0, m*s+ 1))
#Add first edges between source
DG.add_edges_from([(0,x + 1) for x in range(s)])
#Add all the edges in the subsequent layers.
for i in range(m - 1):
DG.add_edges_from([(x + 1 + s*i , y + 1 + s*(i + 1)) for x in range(s) for y in range(s)])
DG.add_edges_from([( x + 1 + s*(m - 1) , m*s + 1) for x in range(s)])
return DG
"""
If typegraph = "Structured":
param1 = number of layers
param2 = number of nodes per layer.
Otherwise:
Growing network with redirection (GNR) digraph
param1 = number of nodes
param2 = The redirection probability.
Can draw the graph with the command nx.draw()
"""
class flowPolytope:
"""Shortest path problem on a DAG."""
def __init__(self, param1, param2, typeGraph = "Structured"):
#Generate the type of graph that we want
if(typeGraph == "Structured"):
self.graph = generateStructuredGraph(param1, param2)
else:
self.graph = generateRandomGraph(param1, param2)
#Sort the graph in topological order
self.topologicalSort = list(nx.topological_sort(self.graph))
self.dictIndices = self.constructDictionaryIndices(self.graph)
return
#Given a graph,a dictionary, a set of weights and a topological order.
def LPOracle(self, weight):
d = math.inf*np.ones(nx.number_of_nodes(self.graph))
d[self.topologicalSort[0]] = 0.0
p = -np.ones(nx.number_of_nodes(self.graph), dtype = int)
for u in self.topologicalSort:
for v in self.graph.neighbors(u):
self.relax(u, v, d, weight, p)
pathAlg = [self.topologicalSort[-1]]
while(pathAlg[-1] != self.topologicalSort[0]):
pathAlg.append(p[pathAlg[-1]])
pathAlg.reverse()
#Reconstruc the vertex.
outputVect = np.zeros(nx.number_of_edges(self.graph))
for i in range(len(pathAlg) - 1):
outputVect[self.dictIndices[(pathAlg[i], pathAlg[i + 1])]] = 1.0
return outputVect
def relax(self, i, j, dVect, wVect, pVect):
if dVect[j] > dVect[i] + wVect[self.dictIndices[(i, j)]]:
dVect[j] = dVect[i] + wVect[self.dictIndices[(i, j)]]
pVect[j] = i
return
#Bellman-Ford algorithm for shortest path.
def LPOracleBellmanFord(self, weight):
self.weight = weight.copy()
pathAlg = nx.bellman_ford_path(self.graph, self.topologicalSort[0], self.topologicalSort[-1], self.func)
#Reconstruct the vertex.
outputVect = np.zeros(nx.number_of_edges(self.graph))
for i in range(len(pathAlg) - 1):
outputVect[self.dictIndices[(pathAlg[i], pathAlg[i + 1])]] = 1.0
return outputVect
#Function that returns the values of the weights.
def func(self, u, v, wVect):
return self.weight[self.dictIndices[(v, u)]]
#Given a DAG, returns a mapping from the edges to indices from 0 to N
#where N represents the number of Edges.
def constructDictionaryIndices(self, graph):
#Construct a dictionary of the indices
dictionary = {}
itCount = 0
for i in graph.edges:
dictionary[i] = itCount
itCount += 1
return dictionary
def dim(self):
return self.dimension
def initialPoint(self):
print("Finding Initial Point.")
aux = np.random.rand(self.dim())
return self.LPOracle(aux)
def dim(self):
return self.graph.number_of_edges()
def plot(self):
nx.draw(self.graph)
plt.show()
def returnEdges(self):
return self.graph.edges()
def topologicalOrdering(self):
return self.topologicalSort
#Input is the vector over which we calculate the inner product.
def AwayOracle(self, grad, activeVertex):
return maxVertex(grad, activeVertex)