4 Copyright (c) by Joao Pedro PEDROSO, Masahiro MURAMATSU and Mikio KUBO, 2012
6 from pyscipopt
import Model, quicksum
10 """weber: model for solving the single source weber problem using soco.
13 - x[i]: x position of customer i
14 - y[i]: y position of customer i
15 - w[i]: weight of customer i
16 Returns a model, ready to be solved.
19 model = Model(
"weber")
21 X, Y, z, xaux, yaux = {}, {}, {}, {}, {}
22 X = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"X")
23 Y = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"Y")
25 z[i] = model.addVar(vtype=
"C", name=
"z(%s)" % (i))
26 xaux[i] = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"xaux(%s)" % (i))
27 yaux[i] = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"yaux(%s)" % (i))
30 model.addCons(xaux[i] * xaux[i] + yaux[i] * yaux[i] <= z[i] * z[i],
"MinDist(%s)" % (i))
31 model.addCons(xaux[i] == (x[i] - X),
"xAux(%s)" % (i))
32 model.addCons(yaux[i] == (y[i] - Y),
"yAux(%s)" % (i))
34 model.setObjective(
quicksum(w[i] * z[i]
for i
in I),
"minimize")
44 """creates example data set"""
49 x[i] = random.randint(0, 100)
50 y[i] = random.randint(0, 100)
51 w[i] = random.randint(1, 5)
55 if __name__ ==
"__main__":
62 print(
"%s\t%8s\t%8s\t%8s" % (
"i",
"x[i]",
"y[i]",
"w[i]"))
64 print(
"%s\t%8g\t%8g\t%8g" % (i, x[i], y[i], w[i]))
70 print(
"Optimal value=", model.getObjVal())
71 print(
"Selected position:", )
72 print(
"\t", (round(model.getVal(X)), round(model.getVal(Y))))
75 print(
"%s\t%8s" % (
"i",
"z[i]"))
77 print(
"%s\t%8g" % (i, model.getVal(z[i])))
81 import matplotlib.pyplot
as P
87 G.add_nodes_from([
"D"])
91 position[i] = (x[i], y[i])
92 position[
"D"] = (round(model.getVal(X)), round(model.getVal(Y)))
94 NX.draw(G, pos=position, node_size=200, node_color=
"g", nodelist=I)
95 NX.draw(G, pos=position, node_size=400, node_color=
"w", nodelist=[
"D"], alpha=0.5)
100 print(
"install 'networkx' and 'matplotlib' for plotting")
104 """weber -- model for solving the weber problem using soco (multiple source version).
106 - I: set of customers
107 - J: set of potential facilities
108 - x[i]: x position of customer i
109 - y[i]: y position of customer i
110 - w[i]: weight of customer i
111 Returns a model, ready to be solved.
113 M = max([((x[i] - x[j]) ** 2 + (y[i] - y[j]) ** 2)
for i
in I
for j
in I])
114 model = Model(
"weber - multiple source")
115 X, Y, v, u = {}, {}, {}, {}
116 xaux, yaux, uaux = {}, {}, {}
118 X[j] = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"X(%s)" % j)
119 Y[j] = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"Y(%s)" % j)
121 v[i, j] = model.addVar(vtype=
"C", name=
"v(%s,%s)" % (i, j))
122 u[i, j] = model.addVar(vtype=
"B", name=
"u(%s,%s)" % (i, j))
123 xaux[i, j] = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"xaux(%s,%s)" % (i, j))
124 yaux[i, j] = model.addVar(lb=-model.infinity(), vtype=
"C", name=
"yaux(%s,%s)" % (i, j))
125 uaux[i, j] = model.addVar(vtype=
"C", name=
"uaux(%s,%s)" % (i, j))
128 model.addCons(
quicksum(u[i, j]
for j
in J) == 1,
"Assign(%s)" % i)
130 model.addCons(xaux[i, j] * xaux[i, j] + yaux[i, j] * yaux[i, j] <= v[i, j] * v[i, j],
131 "MinDist(%s,%s)" % (i, j))
132 model.addCons(xaux[i, j] == (x[i] - X[j]),
"xAux(%s,%s)" % (i, j))
133 model.addCons(yaux[i, j] == (y[i] - Y[j]),
"yAux(%s,%s)" % (i, j))
134 model.addCons(uaux[i, j] >= v[i, j] - M * (1 - u[i, j]),
"uAux(%s,%s)" % (i, j))
136 model.setObjective(
quicksum(w[i] * uaux[i, j]
for i
in I
for j
in J),
"minimize")
138 model.data = X, Y, v, u
142 if __name__ ==
"__main__":
149 X, Y, w, z = model.data
150 print(
"Optimal value:", model.getObjVal())
151 print(
"Selected positions:")
153 print(
"\t", (model.getVal(X[j]), model.getVal(Y[j])))
154 for (i, j)
in sorted(w.keys()):
155 print(
"\t", (i, j), model.getVal(w[i, j]), model.getVal(z[i, j]))
158 edges = [(i, j)
for (i, j)
in z
if model.getVal(z[i, j]) > EPS]
161 import networkx
as NX
162 import matplotlib.pyplot
as P
168 G.add_nodes_from(
"%s" % j
for j
in J)
170 G.add_edge(i,
"%s" % j)
174 position[i] = (x[i], y[i])
176 position[
"%s" % j] = (model.getVal(X[j]), model.getVal(Y[j]))
179 NX.draw(G, position, node_color=
"g", nodelist=I)
180 NX.draw(G, position, node_color=
"w", nodelist=[
"%s" % j
for j
in J])
183 print(
"install 'networkx' and 'matplotlib' for plotting")