PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
gcp.py
Go to the documentation of this file.
1 
3 """
4 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
5 """
6 
7 from pyscipopt import Model, quicksum
8 
9 
10 def gcp(V, E, K):
11  """gcp -- model for minimizing the number of colors in a graph
12  Parameters:
13  - V: set/list of nodes in the graph
14  - E: set/list of edges in the graph
15  - K: upper bound on the number of colors
16  Returns a model, ready to be solved.
17  """
18  model = Model("gcp")
19  x, y = {}, {}
20  for k in range(K):
21  y[k] = model.addVar(vtype="B", name="y(%s)" % k)
22  for i in V:
23  x[i, k] = model.addVar(vtype="B", name="x(%s,%s)" % (i, k))
24 
25  for i in V:
26  model.addCons(quicksum(x[i, k] for k in range(K)) == 1, "AssignColor(%s)" % i)
27 
28  for (i, j) in E:
29  for k in range(K):
30  model.addCons(x[i, k] + x[j, k] <= y[k], "NotSameColor(%s,%s,%s)" % (i, j, k))
31 
32  model.setObjective(quicksum(y[k] for k in range(K)), "minimize")
33 
34  model.data = x
35  return model
36 
37 
38 def gcp_low(V, E, K):
39  """gcp_low -- model for minimizing the number of colors in a graph
40  (use colors with low indices)
41  Parameters:
42  - V: set/list of nodes in the graph
43  - E: set/list of edges in the graph
44  - K: upper bound to the number of colors
45  Returns a model, ready to be solved.
46  """
47  model = Model("gcp - low colors")
48  x, y = {}, {}
49  for k in range(K):
50  y[k] = model.addVar(vtype="B", name="y(%s)" % k)
51  for i in V:
52  x[i, k] = model.addVar(vtype="B", name="x(%s,%s)" % (i, k))
53 
54  for i in V:
55  model.addCons(quicksum(x[i, k] for k in range(K)) == 1, "AssignColor(%s)" % i)
56 
57  for (i, j) in E:
58  for k in range(K):
59  model.addCons(x[i, k] + x[j, k] <= y[k], "NotSameColor(%s,%s,%s)" % (i, j, k))
60 
61  for k in range(K - 1):
62  model.addCons(y[k] >= y[k + 1], "LowColor(%s)" % k)
63 
64  model.setObjective(quicksum(y[k] for k in range(K)), "minimize")
65 
66  model.data = x
67  return model
68 
69 
70 def gcp_sos(V, E, K):
71  """gcp_sos -- model for minimizing the number of colors in a graph
72  (use sos type 1 constraints)
73  Parameters:
74  - V: set/list of nodes in the graph
75  - E: set/list of edges in the graph
76  - K: upper bound to the number of colors
77  Returns a model, ready to be solved.
78  """
79  model = Model("gcp - sos constraints")
80 
81  x, y = {}, {}
82  for k in range(K):
83  y[k] = model.addVar(vtype="B", name="y(%s)" % k)
84  for i in V:
85  x[i, k] = model.addVar(vtype="B", name="x(%s,%s)" % (i, k))
86 
87  for i in V:
88  model.addCons(quicksum(x[i, k] for k in range(K)) == 1, "AssignColor(%s)" % i)
89  model.addConsSOS1([x[i, k] for k in range(K)])
90 
91  for (i, j) in E:
92  for k in range(K):
93  model.addCons(x[i, k] + x[j, k] <= y[k], "NotSameColor(%s,%s,%s)" % (i, j, k))
94 
95  for k in range(K - 1):
96  model.addCons(y[k] >= y[k + 1], "LowColor(%s)" % k)
97 
98  model.setObjective(quicksum(y[k] for k in range(K)), "minimize")
99 
100  model.data = x
101  return model
102 
103 
104 import random
105 
106 
107 def make_data(n, prob):
108  """make_data: prepare data for a random graph
109  Parameters:
110  - n: number of vertices
111  - prob: probability of existence of an edge, for each pair of vertices
112  Returns a tuple with a list of vertices and a list edges.
113  """
114  V = range(1, n + 1)
115  E = [(i, j) for i in V for j in V if i < j and random.random() < prob]
116  return V, E
117 
118 
119 if __name__ == "__main__":
120  random.seed(1)
121  V, E = make_data(20, .5)
122  K = 10 # upper bound to the number of colors
123  print("n,K=", len(V), K)
124 
125  model = gcp_low(V, E, K)
126  model.optimize()
127  print("Optimal value:", model.getObjVal())
128  x = model.data
129 
130  color = {}
131  for i in V:
132  for k in range(K):
133  if model.getVal(x[i, k]) > 0.5:
134  color[i] = k
135  print("colors:", color)
136 
137  import time, sys
138 
139  models = [gcp, gcp_low, gcp_sos]
140  cpu = {}
141  N = 25 # number of observations
142  print("#size\t%s\t%s\t%s" % tuple(m.__name__ for m in models))
143  for size in range(250):
144  print(size, "\t", )
145  K = size
146  for prob in [0.1]:
147  for m in models:
148  name = m.__name__
149  if not (name, size - 1, prob) in cpu or cpu[
150  name, size - 1, prob] < 100: # cpu.has_key((name,size-1,prob))
151  cpu[name, size, prob] = 0.
152  for t in range(N):
153  tinit = time.time()
154  random.seed(t)
155  V, E = make_data(size, prob)
156  model = m(V, E, K)
157  model.hideOutput() # silent mode
158  model.optimize()
159  assert model.getObjVal() >= 0 and model.getObjVal() <= K
160  tend = time.time()
161  cpu[name, size, prob] += tend - tinit
162  cpu[name, size, prob] /= N
163  else:
164  cpu[name, size, prob] = "-"
165  print(cpu[name, size, prob], "\t", )
166  print()
167  sys.stdout.flush()
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
gcp.gcp_low
def gcp_low(V, E, K)
Definition: gcp.py:38
gcp.make_data
def make_data(n, prob)
Definition: gcp.py:107
kmedian.m
int m
Definition: kmedian.py:74
gcp.gcp_sos
def gcp_sos(V, E, K)
Definition: gcp.py:70
gcp.gcp
def gcp(V, E, K)
Definition: gcp.py:10