-
Notifications
You must be signed in to change notification settings - Fork 11
/
PMedianRamziEditFinal.py
executable file
·147 lines (102 loc) · 3.56 KB
/
PMedianRamziEditFinal.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pulp import *
import pandas as pd
import numpy as np
import time
'''
# You can uncomment this code block if you would like to import
# a dictionary of dictionaries and convert into a pandas dataframe
def load_obj(name ):
with open('/home/vagrant/Downloads/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
D = load_obj('dictionary_D')
pandas_D = pd.DataFrame.from_dict(D)
# make sure your demands are the rows in order to run without modifying code
print('print pandas_D')
print(pandas_D)
'''
#
pandas_from_csv = pd.read_csv('/Users/Ramzi/Desktop/JustDistanceNoWeightsWTesla.csv',header=0,index_col='ID')
#print('print pandas_from_csv')
#print(pandas_from_csv)
#get column keys
# test_pd.keys()
# get row keys
# test_pd.index
# Assumes that the column headers are the potential origins/facilities points
# and the rows are the demand points
facilities = pandas_from_csv.keys()
# facilities = pandas_D.keys()
# convert all strings in keys to a list of integers
facilities = list(map(int, facilities))
print('print D keys (origins)')
print(facilities)
demand = pandas_from_csv.index
# demand = pandas_D.index
demand = list(map(int, demand))
print('print demand points')
print(demand)
# start counting time
t1=time.time()
p = 16 # number of locations to optimize to
# decision variables
# This is same as X = LpVariable.dicts('X_%s_%s', (location), cat = 'Binary', lowBound = 0, upBound = 1)
# but shorter and a format speficier is not needed.
# declare facility variables
X = LpVariable.dicts('X',(facilities),0,1,LpInteger)
# declare demand variables
Y = LpVariable.dicts('Y', (demand,facilities),0,1,LpInteger)
# create the LP object, set up as a MINIMIZATION problem
prob = LpProblem('P Median', LpMinimize)
# prob += sum(sum(D[i][j] * Y[i][j] for j in location) for i in demand)
# pandas iloc looks up values by row(i) and column(j)
prob += sum(sum(pandas_from_csv.iloc[i,j] * Y[i][j] for j in facilities) for i in demand)
# set up constraints
# This is same as prob += sum([X[j] for j in location]) == p
prob += lpSum([X[j] for j in facilities]) == p
# For Q-coverage problems, modify the constraint below and instead of '== 1'
# make it equal to the number of facilities that need to service each demand
for i in demand: prob += sum(Y[i][j] for j in facilities) == 1
for i in demand:
for j in facilities:
prob += Y[i][j] <= X[j]
# constraint below is is in example if you want to make location 105 an existing facility
prob += X[0] == 1
prob += X[1] == 1
prob += X[2] == 1
prob += X[3] == 1
prob += X[4] == 1
prob += X[5] == 1
prob += X[6] == 1
prob += X[7] == 1
prob += X[8] == 1
prob += X[9] == 1
prob += X[10] == 1
#remeber index!!!
prob.solve()
# format output
print(' ')
print("Status:",LpStatus[prob.status])
print(' ')
print("Objective: ",value(prob.objective))
print(' ')
for v in prob.variables():
subV = v.name.split('_')
if subV[0] == "X" and v.varValue == 1: print('p-Median Node: ', subV[1])
result = []
'''print(' ')
for v in prob.variables():
subV = v.name.split('_')
if subV[0] == "Y" and v.varValue == 1: print(subV[1], ' is connected to', subV[2])'''
print(' ')
for v in prob.variables():
subV = v.name.split('_')
if subV[0] == "Y" and v.varValue == 1:
result.append((subV[1], ' is connected to', subV[2]))
print(result)
df = pd.DataFrame(np.array(result))
print (df)
df.to_csv('/Users/Ramzi/Dropbox/EdgeListP-medianCode/EdgelistwithNoWeightsTesla.csv', sep=',')
# print out elapsed time
print("Processing time took:",time.time()-t1)