4 minimize the total (weighted) travel cost for servicing
5 a set of customers from k facilities.
7 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
12 from pyscipopt
import Model, quicksum
16 """kmedian -- minimize total cost of servicing customers from k facilities
19 - J: set of potential facilities
20 - c[i,j]: cost of servicing customer i from facility j
21 - k: number of facilities to be used
22 Returns a model, ready to be solved.
25 model = Model(
"k-median")
29 y[j] = model.addVar(vtype=
"B", name=
"y(%s)" % j)
31 x[i, j] = model.addVar(vtype=
"B", name=
"x(%s,%s)" % (i, j))
34 model.addCons(
quicksum(x[i, j]
for j
in J) == 1,
"Assign(%s)" % i)
36 model.addCons(x[i, j] <= y[j],
"Strong(%s,%s)" % (i, j))
37 model.addCons(
quicksum(y[j]
for j
in J) == k,
"Facilities")
39 model.setObjective(
quicksum(c[i, j] * x[i, j]
for i
in I
for j
in J),
"minimize")
46 """return distance of two points"""
47 return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
51 """creates example data set"""
55 x = [random.random()
for i
in range(max(m, n))]
56 y = [random.random()
for i
in range(max(m, n))]
60 x = [random.random()
for i
in range(n + m)]
61 y = [random.random()
for i
in range(n + m)]
65 c[i, j] =
distance(x[i], y[i], x[j], y[j])
70 if __name__ ==
"__main__":
82 edges = [(i, j)
for (i, j)
in x
if model.getVal(x[i, j]) > EPS]
83 facilities = [j
for j
in y
if model.getVal(y[j]) > EPS]
85 print(
"Optimal value:", model.getObjVal())
86 print(
"Selected facilities:", facilities)
87 print(
"Edges:", edges)
88 print(
"max c:", max([c[i, j]
for (i, j)
in edges]))
92 import matplotlib.pyplot
as P
97 facilities =
set(j
for j
in J
if model.getVal(y[j]) > EPS)
98 other =
set(j
for j
in J
if j
not in facilities)
99 client =
set(i
for i
in I
if i
not in facilities
and i
not in other)
100 G.add_nodes_from(facilities)
101 G.add_nodes_from(client)
102 G.add_nodes_from(other)
107 for i
in range(len(x_pos)):
108 position[i] = (x_pos[i], y_pos[i])
110 NX.draw(G, position, with_labels=
False, node_color=
"w", nodelist=facilities)
111 NX.draw(G, position, with_labels=
False, node_color=
"c", nodelist=other, node_size=50)
112 NX.draw(G, position, with_labels=
False, node_color=
"g", nodelist=client, node_size=50)
115 print(
"install 'networkx' and 'matplotlib' for plotting")