Skip to content

Commit 7592c6c

Browse files
authored
Format with black
1 parent 8db3c34 commit 7592c6c

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

examples/plant_location.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
# by using a different backend.
1616
if sys.platform == "darwin": # OS X
1717
import matplotlib as mpl
18-
mpl.use('Agg')
18+
19+
mpl.use("Agg")
1920
del mpl
2021

22+
import matplotlib.pyplot as plt
2123
from math import sqrt, log
2224
from itertools import product
2325
from mip import Model, xsum, minimize, OptimizationStatus
@@ -35,14 +37,40 @@
3537
C = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3638

3739
# position of clients
38-
pc = {1: (94, 10), 2: (57, 26), 3: (74, 44), 4: (27, 51), 5: (78, 30), 6: (23, 30),
39-
7: (20, 72), 8: (3, 27), 9: (5, 39), 10: (51, 1)}
40+
pc = {
41+
1: (94, 10),
42+
2: (57, 26),
43+
3: (74, 44),
44+
4: (27, 51),
45+
5: (78, 30),
46+
6: (23, 30),
47+
7: (20, 72),
48+
8: (3, 27),
49+
9: (5, 39),
50+
10: (51, 1),
51+
}
4052

4153
# demands
4254
d = {1: 302, 2: 273, 3: 275, 4: 266, 5: 287, 6: 296, 7: 297, 8: 310, 9: 302, 10: 309}
4355

44-
dist = {(f, c): round(sqrt((pf[f][0] - pc[c][0]) ** 2 + (pf[f][1] - pc[c][1]) ** 2), 1)
45-
for (f, c) in product(F, C) }
56+
# plotting possible plant locations
57+
for i, p in pf.items():
58+
plt.scatter((p[0]), (p[1]), marker="^", color="purple", s=50)
59+
plt.text((p[0]), (p[1]), "$f_%d$" % i)
60+
61+
# plotting location of clients
62+
for i, p in pc.items():
63+
plt.scatter((p[0]), (p[1]), marker="o", color="black", s=15)
64+
plt.text((p[0]), (p[1]), "$c_{%d}$" % i)
65+
66+
plt.text((20), (78), "Region 1")
67+
plt.text((70), (78), "Region 2")
68+
plt.plot((50, 50), (0, 80))
69+
70+
dist = {
71+
(f, c): round(sqrt((pf[f][0] - pc[c][0]) ** 2 + (pf[f][1] - pc[c][1]) ** 2), 1)
72+
for (f, c) in product(F, C)
73+
}
4674

4775
m = Model()
4876

@@ -67,7 +95,7 @@
6795
D = 6 # nr. of discretization points, increase for more precision
6896
v = [c[f] * (v / (D - 1)) for v in range(D)] # points
6997
# non-linear function values for points in v
70-
vn = [0 if k == 0 else 1520 * log(v[k]) for k in range(D)]
98+
vn = [0 if k == 0 else 1520 * log(v[k]) for k in range(D)]
7199
# w variables
72100
w = [m.add_var() for v in range(D)]
73101
m += xsum(w) == 1 # convexification
@@ -83,15 +111,26 @@
83111

84112
# objective function
85113
m.objective = minimize(
86-
xsum(dist[i, j] * x[i, j] for (i, j) in product(F, C)) + xsum(y[i] for i in F) )
114+
xsum(dist[i, j] * x[i, j] for (i, j) in product(F, C)) + xsum(y[i] for i in F)
115+
)
87116

88117
m.optimize()
89118

119+
plt.savefig("location.pdf")
120+
90121
if m.num_solutions:
91122
print("Solution with cost {} found.".format(m.objective_value))
92123
print("Facilities capacities: {} ".format([z[f].x for f in F]))
93124
print("Facilities cost: {}".format([y[f].x for f in F]))
94-
125+
126+
# plotting allocations
127+
for i, j in [(i, j) for (i, j) in product(F, C) if x[(i, j)].x >= 1e-6]:
128+
plt.plot(
129+
(pf[i][0], pc[j][0]), (pf[i][1], pc[j][1]), linestyle="--", color="darkgray"
130+
)
131+
132+
plt.savefig("location-sol.pdf")
133+
95134
# sanity checks
96135
opt = 99733.94905406
97136
if m.status == OptimizationStatus.OPTIMAL:

0 commit comments

Comments
 (0)