4 Copyright (c) by Joao Pedro PEDROSO, Masahiro MURAMATSU and Mikio KUBO, 2012 7 from pyscipopt
import Model, quicksum, multidict
10 """gpp -- model for the graph partitioning problem 12 - V: set/list of nodes in the graph 13 - E: set/list of edges in the graph 14 Returns a model, ready to be solved. 21 x[i] = model.addVar(vtype=
"B", name=
"x(%s)"%i)
23 y[i,j] = model.addVar(vtype=
"B", name=
"y(%s,%s)"%(i,j))
25 model.addCons(quicksum(x[i]
for i
in V) == len(V)/2,
"Partition")
28 model.addCons(x[i] - x[j] <= y[i,j],
"Edge(%s,%s)"%(i,j))
29 model.addCons(x[j] - x[i] <= y[i,j],
"Edge(%s,%s)"%(j,i))
31 model.setObjective(quicksum(y[i,j]
for (i,j)
in E),
"minimize")
38 """gpp_qo -- quadratic optimization model for the graph partitioning problem 40 - V: set/list of nodes in the graph 41 - E: set/list of edges in the graph 42 Returns a model, ready to be solved. 48 x[i] = model.addVar(vtype=
"B", name=
"x(%s)"%i)
50 model.addCons(quicksum(x[i]
for i
in V) == len(V)/2,
"Partition")
52 model.setObjective(quicksum(x[i]*(1-x[j]) + x[j]*(1-x[i])
for (i,j)
in E),
"minimize")
59 """gpp_qo_ps -- quadratic optimization, positive semidefinite model for the graph partitioning problem 61 - V: set/list of nodes in the graph 62 - E: set/list of edges in the graph 63 Returns a model, ready to be solved. 69 x[i] = model.addVar(vtype=
"B", name=
"x(%s)"%i)
71 model.addCons(quicksum(x[i]
for i
in V) == len(V)/2,
"Partition")
73 model.setObjective(quicksum((x[i] - x[j]) * (x[i] - x[j])
for (i,j)
in E),
"minimize")
80 """gpp -- model for the graph partitioning problem in soco 82 - V: set/list of nodes in the graph 83 - E: set/list of edges in the graph 84 Returns a model, ready to be solved. 86 model = Model(
"gpp model -- soco")
90 x[i] = model.addVar(vtype=
"B", name=
"x(%s)"%i)
92 s[i,j] = model.addVar(vtype=
"C", name=
"s(%s,%s)"%(i,j))
93 z[i,j] = model.addVar(vtype=
"C", name=
"z(%s,%s)"%(i,j))
95 model.addCons(quicksum(x[i]
for i
in V) == len(V)/2,
"Partition")
98 model.addCons((x[i] + x[j] -1)*(x[i] + x[j] -1) <= s[i,j],
"S(%s,%s)"%(i,j))
99 model.addCons((x[j] - x[i])*(x[j] - x[i]) <= z[i,j],
"Z(%s,%s)"%(i,j))
100 model.addCons(s[i,j] + z[i,j] == 1,
"P(%s,%s)"%(i,j))
113 model.setObjective(quicksum(z[i,j]
for (i,j)
in E),
"minimize")
120 def make_data(n,prob):
121 """make_data: prepare data for a random graph 123 - n: number of vertices 124 - prob: probability of existence of an edge, for each pair of vertices 125 Returns a tuple with a list of vertices and a list edges. 128 E = [(i,j)
for i
in V
for j
in V
if i < j
and random.random() < prob]
132 if __name__ ==
"__main__":
134 V,E = make_data(4,.5)
137 print(
"\n\n\nStandard model:")
140 print(
"Optimal value:", model.getObjVal())
143 print([i
for i
in V
if model.getVal(x[i]) >= .5])
144 print([i
for i
in V
if model.getVal(x[i]) < .5])
146 print(
"\n\n\nQuadratic optimization")
149 model.writeProblem(
"gpp_qo.lp")
150 print(
"Optimal value:", model.getObjVal())
153 print([i
for i
in V
if model.getVal(x[i]) >= .5])
154 print([i
for i
in V
if model.getVal(x[i]) < .5])
156 print(
"\n\n\nQuadratic optimization - positive semidefinite")
157 model = gpp_qo_ps(V,E)
159 model.writeProblem(
"gpp_qo.lp")
160 print(
"Optimal value:", model.getObjVal())
163 print([i
for i
in V
if model.getVal(x[i]) >= .5])
164 print([i
for i
in V
if model.getVal(x[i]) < .5])
166 print(
"\n\n\nSecond order cone optimization")
167 model = gpp_soco(V,E)
169 model.writeProblem(
"tmp.lp")
170 status = model.getStatus()
171 if status ==
"optimal":
172 print(
"Optimal value:", model.getObjVal())
175 print([i
for i
in V
if model.getVal(x[i]) >= .5])
176 print([i
for i
in V
if model.getVal(x[i]) < .5])
179 print(
"(%s,%s)\t%s\t%s" % (i,j,model.getVal(s[i,j]),model.getVal(z[i,j])))