4 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
7 from pyscipopt
import Model, quicksum
11 """gcp -- model for minimizing the number of colors in a graph
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.
21 y[k] = model.addVar(vtype=
"B", name=
"y(%s)" % k)
23 x[i, k] = model.addVar(vtype=
"B", name=
"x(%s,%s)" % (i, k))
26 model.addCons(
quicksum(x[i, k]
for k
in range(K)) == 1,
"AssignColor(%s)" % i)
30 model.addCons(x[i, k] + x[j, k] <= y[k],
"NotSameColor(%s,%s,%s)" % (i, j, k))
32 model.setObjective(
quicksum(y[k]
for k
in range(K)),
"minimize")
39 """gcp_low -- model for minimizing the number of colors in a graph
40 (use colors with low indices)
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.
47 model = Model(
"gcp - low colors")
50 y[k] = model.addVar(vtype=
"B", name=
"y(%s)" % k)
52 x[i, k] = model.addVar(vtype=
"B", name=
"x(%s,%s)" % (i, k))
55 model.addCons(
quicksum(x[i, k]
for k
in range(K)) == 1,
"AssignColor(%s)" % i)
59 model.addCons(x[i, k] + x[j, k] <= y[k],
"NotSameColor(%s,%s,%s)" % (i, j, k))
61 for k
in range(K - 1):
62 model.addCons(y[k] >= y[k + 1],
"LowColor(%s)" % k)
64 model.setObjective(
quicksum(y[k]
for k
in range(K)),
"minimize")
71 """gcp_sos -- model for minimizing the number of colors in a graph
72 (use sos type 1 constraints)
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.
79 model = Model(
"gcp - sos constraints")
83 y[k] = model.addVar(vtype=
"B", name=
"y(%s)" % k)
85 x[i, k] = model.addVar(vtype=
"B", name=
"x(%s,%s)" % (i, k))
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)])
93 model.addCons(x[i, k] + x[j, k] <= y[k],
"NotSameColor(%s,%s,%s)" % (i, j, k))
95 for k
in range(K - 1):
96 model.addCons(y[k] >= y[k + 1],
"LowColor(%s)" % k)
98 model.setObjective(
quicksum(y[k]
for k
in range(K)),
"minimize")
108 """make_data: prepare data for a random graph
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.
115 E = [(i, j)
for i
in V
for j
in V
if i < j
and random.random() < prob]
119 if __name__ ==
"__main__":
123 print(
"n,K=", len(V), K)
127 print(
"Optimal value:", model.getObjVal())
133 if model.getVal(x[i, k]) > 0.5:
135 print(
"colors:", color)
139 models = [gcp, gcp_low, gcp_sos]
142 print(
"#size\t%s\t%s\t%s" % tuple(m.__name__
for m
in models))
143 for size
in range(250):
149 if not (name, size - 1, prob)
in cpu
or cpu[
150 name, size - 1, prob] < 100:
151 cpu[name, size, prob] = 0.
159 assert model.getObjVal() >= 0
and model.getObjVal() <= K
161 cpu[name, size, prob] += tend - tinit
162 cpu[name, size, prob] /= N
164 cpu[name, size, prob] =
"-"
165 print(cpu[name, size, prob],
"\t", )