PySCIPOpt
Python Interface to the SCIP Optimization Suite
gpp.py
Go to the documentation of this file.
1 ##@file gpp.py
2 #@brief model for the graph partitioning problem
3 """
4 Copyright (c) by Joao Pedro PEDROSO, Masahiro MURAMATSU and Mikio KUBO, 2012
5 """
6 
7 from pyscipopt import Model, quicksum, multidict
8 
9 def gpp(V,E):
10  """gpp -- model for the graph partitioning problem
11  Parameters:
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.
15  """
16  model = Model("gpp")
17 
18  x = {}
19  y = {}
20  for i in V:
21  x[i] = model.addVar(vtype="B", name="x(%s)"%i)
22  for (i,j) in E:
23  y[i,j] = model.addVar(vtype="B", name="y(%s,%s)"%(i,j))
24 
25  model.addCons(quicksum(x[i] for i in V) == len(V)/2, "Partition")
26 
27  for (i,j) in E:
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))
30 
31  model.setObjective(quicksum(y[i,j] for (i,j) in E), "minimize")
32 
33  model.data = x
34  return model
35 
36 
37 def gpp_qo(V,E):
38  """gpp_qo -- quadratic optimization model for the graph partitioning problem
39  Parameters:
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.
43  """
44  model = Model("gpp")
45 
46  x = {}
47  for i in V:
48  x[i] = model.addVar(vtype="B", name="x(%s)"%i)
49 
50  model.addCons(quicksum(x[i] for i in V) == len(V)/2, "Partition")
51 
52  model.setObjective(quicksum(x[i]*(1-x[j]) + x[j]*(1-x[i]) for (i,j) in E), "minimize")
53 
54  model.data = x
55  return model
56 
57 
58 def gpp_qo_ps(V,E):
59  """gpp_qo_ps -- quadratic optimization, positive semidefinite model for the graph partitioning problem
60  Parameters:
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.
64  """
65  model = Model("gpp")
66 
67  x = {}
68  for i in V:
69  x[i] = model.addVar(vtype="B", name="x(%s)"%i)
70 
71  model.addCons(quicksum(x[i] for i in V) == len(V)/2, "Partition")
72 
73  model.setObjective(quicksum((x[i] - x[j]) * (x[i] - x[j]) for (i,j) in E), "minimize")
74 
75  model.data = x
76  return model
77 
78 
79 def gpp_soco(V,E):
80  """gpp -- model for the graph partitioning problem in soco
81  Parameters:
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.
85  """
86  model = Model("gpp model -- soco")
87 
88  x,s,z = {},{},{}
89  for i in V:
90  x[i] = model.addVar(vtype="B", name="x(%s)"%i)
91  for (i,j) in E:
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))
94 
95  model.addCons(quicksum(x[i] for i in V) == len(V)/2, "Partition")
96 
97  for (i,j) in E:
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))
101 
102  # # triangle inequalities (seem to make model slower)
103  # for i in V:
104  # for j in V:
105  # for k in V:
106  # if (i,j) in E and (j,k) in E and (i,k) in E:
107  # print("\t***",(i,j,k)
108  # model.addCons(z[i,j] + z[j,k] + z[i,k] <= 2, "T1(%s,%s,%s)"%(i,j,k))
109  # model.addCons(z[i,j] + s[j,k] + s[i,k] <= 2, "T2(%s,%s,%s)"%(i,j,k))
110  # model.addCons(s[i,j] + s[j,k] + z[i,k] <= 2, "T3(%s,%s,%s)"%(i,j,k))
111  # model.addCons(s[i,j] + z[j,k] + s[i,k] <= 2, "T4(%s,%s,%s)"%(i,j,k))
112 
113  model.setObjective(quicksum(z[i,j] for (i,j) in E), "minimize")
114 
115  model.data = x,s,z
116  return model
117 
118 
119 import random
120 def make_data(n,prob):
121  """make_data: prepare data for a random graph
122  Parameters:
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.
126  """
127  V = range(1,n+1)
128  E = [(i,j) for i in V for j in V if i < j and random.random() < prob]
129  return V,E
130 
131 
132 if __name__ == "__main__":
133  random.seed(1)
134  V,E = make_data(4,.5)
135  print("edges:",E)
136 
137  print("\n\n\nStandard model:")
138  model = gpp(V,E)
139  model.optimize()
140  print("Optimal value:", model.getObjVal())
141  x = model.data
142  print("partition:")
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])
145 
146  print("\n\n\nQuadratic optimization")
147  model = gpp_qo(V,E)
148  model.optimize()
149  model.writeProblem("gpp_qo.lp")
150  print("Optimal value:", model.getObjVal())
151  x = model.data
152  print("partition:")
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])
155 
156  print("\n\n\nQuadratic optimization - positive semidefinite")
157  model = gpp_qo_ps(V,E)
158  model.optimize()
159  model.writeProblem("gpp_qo.lp")
160  print("Optimal value:", model.getObjVal())
161  x = model.data
162  print("partition:")
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])
165 
166  print("\n\n\nSecond order cone optimization")
167  model = gpp_soco(V,E)
168  model.optimize()
169  model.writeProblem("tmp.lp")
170  status = model.getStatus()
171  if status == "optimal":
172  print("Optimal value:", model.getObjVal())
173  x,s,z = model.data
174  print("partition:")
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])
177 
178  for (i,j) in s:
179  print("(%s,%s)\t%s\t%s" % (i,j,model.getVal(s[i,j]),model.getVal(z[i,j])))
Definition: gpp.py:1