-
Notifications
You must be signed in to change notification settings - Fork 0
/
cplex_MPI_final.py
202 lines (175 loc) · 5.74 KB
/
cplex_MPI_final.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import print_function
import numpy as np
from math import floor, fabs
import sys
import cplex
from cplex.exceptions import CplexError
import matplotlib.pyplot as plt
import pandas as pd
import multiprocessing
# example: python cplex_final.py x.csv y.csv result.csv
# X = np.loadtxt(str(sys.argv[1]))
# Y = np.loadtxt(str(sys.argv[2]))
# result = []
class cplex_CPS:
def __init__(self, X, Y,filepath,filename):
self.X = X
self.Y = Y
self.result = []
self.filepath = filepath
self.filename = filename
def run_cplex(self,loc_X,loc_Y,m=30,M=50):
N = loc_X.shape[0]
C = []
for i in range(N):
c = []
for j in range(N):
c.append(np.sqrt((loc_X[i]-loc_X[j])**2+(loc_Y[i]-loc_Y[j])**2))
C.append(c)
print('Number of people for assignment')
print(len(loc_Y))
print('Number of potential social center')
print(len(loc_X))
num_facilities = len(loc_X)
num_clients = len(loc_Y)
model = cplex.Cplex()
model.parameters.mip.tolerances.mipgap.set(0.2)
model.set_log_stream(None)
model.set_error_stream(None)
model.set_warning_stream(None)
model.set_results_stream(None)
# Create one binary variable for each individual. The variables model
# whether this individual is social center or not
model.variables.add(obj = np.zeros(N),
lb=[0] * num_facilities,
ub=[1] * num_facilities,
types=["B"] * num_facilities)
center = []
# for c in range(num_clients):
# supply.append([])
for f in range(num_facilities):
center.append(f)
# Create one binary variable for each facility/client pair. The variables
# model whether a client is served by a facility.
for c in range(num_clients):
model.variables.add(obj=C[c],
lb=[0] * num_facilities,
ub=[1] * num_facilities,
types=["B"] * num_facilities)
# Create corresponding indices for later use
supply = []
for c in range(num_clients):
supply.append([])
for f in range(num_facilities):
supply[c].append((c + 1) * (num_facilities) + f)
# Each client must be assigned to exactly one location
for c in range(num_clients):
assignment_constraint = cplex.SparsePair(ind=[supply[c][f] for f in
range(num_facilities)],
val=[1.0] * num_facilities)
model.linear_constraints.add(lin_expr=[assignment_constraint],
senses=["E"],
rhs=[1])
# The number of clients assigned to a facility must be less than the
# capacity of the facility, and clients must be assigned to an open
# facility
for f in range(num_facilities):
index = [f]
value = [-M*center[f]]
for c in range(num_clients):
index.append(supply[c][f])
value.append(1.0*center[f])
capacity_constraint = cplex.SparsePair(ind=index, val=value)
model.linear_constraints.add(lin_expr=[capacity_constraint],
senses=["L"],
rhs=[0])
for f in range(num_facilities):
index = [f]
value = [-m*center[f]]
for c in range(num_clients):
index.append(supply[c][f])
value.append(1.0*center[f])
capacity_constraint = cplex.SparsePair(ind=index, val=value)
model.linear_constraints.add(lin_expr=[capacity_constraint],
senses=["G"],
rhs=[0])
# Our objective is to minimize cost. Fixed and variable costs
# have been set when variables were created.
model.objective.set_sense(model.objective.sense.minimize)
model.solve()
solution = model.solution
C = solution.get_values()[0:300]
# print(sum(C))
Z = solution.get_values()[300:90300]
# print(sum(Z))
Z = np.array(Z)
Z = np.reshape(Z, (300,300))
return C,Z
def worker(self,loc_X,loc_Y,i,send_end):
'''worker function'''
C,Z = self.run_cplex(loc_X,loc_Y)
count = 0
com = np.zeros(len(loc_X))
for j in range(len(C)):
if int(C[j]!=0):
c_index = count + 1
count +=1
for k in range(len(Z[:,j])):
if Z[k,j]!=0:
com[k] = int(c_index)
result = [com,i]
# print('parallel')
send_end.send(result)
def parallel(self):
jobs = []
pipe_list = []
for i in xrange(len(self.X)/300):
recv_end, send_end = multiprocessing.Pipe(False)
loc_X = self.X[i*300:(i+1)*300]
loc_Y = self.Y[i*300:(i+1)*300]
p = multiprocessing.Process(target=self.worker, args=(loc_X,loc_Y, i,send_end))
jobs.append(p)
pipe_list.append(recv_end)
p.start()
# print('check step 1')
for proc in jobs:
proc.join()
# print('done')
# print('check step 2')
result_list = [x.recv() for x in pipe_list]
# print('check step 3')
for result in result_list:
self.result.append(result)
# print('check step 4')
def create_DF(self):
total_commmunities = 0
communities = []
for i in xrange(len(self.X)/300):
for j in xrange(len(self.X)/300):
if self.result[j][1] == i:
communities.append(self.result[j][0])
total = 0
self.C = []
for community in communities:
for j in range(len(community)):
self.C.append(community[j]+total)
total = total + max(community)
self.DF = pd.DataFrame({'X':self.X,'Y':self.Y,"C":self.C})
def run(self):
self.parallel()
self.create_DF()
self.DF.to_csv(self.filepath +'/'+ self.filename,index=False)