-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.py
97 lines (78 loc) · 2.48 KB
/
problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import random
import math
import matplotlib.pyplot as plt
import numpy as np
def min_one(x):
return sum(x)
def sphere(x, convert=None):
if convert is None:
return sum(x ** 2)
else:
return sum(convert(x) ** 2)
def hub_location_allocation(select):
select = np.array(select)
model = np.load('hub_model.npz')
xc = model['xc']
yc = model['yc']
d = model['d']
xs = model['xs']
ys = model['ys']
c = model['c']
length_matrix = model['length_matrix']
c_num = len(xc)
s_num = len(xs)
z1 = 0
for i in range(c_num):
j = np.argmin(length_matrix[i] / select)
z1 += d[i] * length_matrix[i][j]
z2 = sum(select * c)
w1 = 1
w2 = 1
cost = w1 * z1 + w2 * z2
return cost
def show_hub(select):
# plt.close()
select = np.array(select)
model = np.load('hub_model.npz')
xc = model['xc']
yc = model['yc']
xs = model['xs']
ys = model['ys']
length_matrix = model['length_matrix']
c_num = len(xc)
for i in range(c_num):
j = np.argmin(length_matrix[i] / select)
plt.plot([xc[i], xs[j]], [yc[i], ys[j]])
plt.plot(xc, yc, 'bo')
plt.plot(xs[select == 1], ys[select == 1], 'rs')
plt.plot(xs[select == 0], ys[select == 0], 'rs', markerfacecolor='white')
# plt.show(block=False)
plt.show()
def get_distance(p1, p2):
z = p1 - p2
return math.sqrt(z[0] ** 2 + z[1] ** 2)
def create_model():
cnum = 40 # client number
snum = 20 # server number
x = np.random.uniform(0, 99, cnum + snum)
y = np.random.uniform(0, 99, cnum + snum)
sc = np.arange(cnum + snum)
ci = random.sample(list(sc), cnum)
si = np.setdiff1d(sc, ci)
xc = x[ci]
yc = y[ci]
d = np.random.randint(1, 99, cnum) # demand for each client
xs = x[si]
ys = y[si]
c = np.random.randint(8000, 12000, snum) # installation cost for each server
length_matrix = np.zeros((cnum, snum))
for i in range(cnum):
for j in range(snum):
cpoint = np.array([xc[i], yc[i]])
spoint = np.array([xs[j], ys[j]])
length_matrix[i][j] = get_distance(cpoint, spoint)
np.savez('hub_model', xc=xc, yc=yc, d=d, xs=xs, ys=ys, c=c, length_matrix=length_matrix)
# test
# hub_location_allocation(np.array([1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1]))
# show_hub([1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1])
# print(show_hub([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0]))