-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolarLights.py
71 lines (56 loc) · 2.15 KB
/
SolarLights.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
#Solar Lights
import csv
from pprint import pprint
import geopy.distance
from gurobipy import *
#maxRange = 1.36702 #miles == 2.2km: 8 central hubs needed w/ spoke model
print('if max range of each solar light connection is 2 km : ')
maxRange = 1.24274 #miles = 2km: 10 central hubs needed w/ spoke model
def csvReader():
with open('SLights.csv', 'rt') as fin:
reader = csv.reader(fin)
header = next(reader)
uncleanList = [row for row in reader]
cleanList = []
for row in uncleanList:
tempList = [row[0], float(row[1]), float(row[2]), row[3]]
cleanList.append(tempList)
return cleanList
csvList = csvReader()
numList = []
for i in range(len(csvList)):
numList.append([i,(float(csvList[i][1]),float(csvList[i][2]))])
distCompList = []
for i in range(len(numList)):
distList = []
for j in range(len(numList)):
distance = geopy.distance.distance(numList[i][1],numList[j][1]).miles
distList.append(distance)
distCompList.append(distList)
m = Model("Solar Lights")
m.setParam('OutputFlag', True)
x = m.addVars(len(numList), vtype = GRB.BINARY, name='bouys')
varDict = {}
for i in range(len(distCompList)):
varDict[i] = distCompList[i]
m.addConstr(x[i] >= 0, name = 'nonnegativity')
for i in range(len(varDict)):
inList = []
for j in range(len(varDict[i])):
if varDict[i][j] <= maxRange:
inList.append(j)
m.addConstr(quicksum(x[i] for i in inList) >= 1, name = 'bouys')
m.setObjective(quicksum(x[i] for i in range(len(varDict))), GRB.MINIMIZE)
m.optimize()
status_code = {1:'LOADED',2:'OPTIMAL',3:'INFEASIBLE',4:'INF_OR_UNBD',5:'UNBOUNDED'}
status = m.status
print('The optimization status is {}'.format(status_code[status]))
if status == 2:
print('Optimal solution:')
finalList = []
for v in m.getVars():
#print('%s = %g' % (v.varName, v.x))
if v.x == 1.0:
finalList.append(csvList[int(v.varName[v.varName.find('[') + 1:v.varName.find(']')])])
print('Number of solar light central hubs needed:{}'.format(m.objVal))
pprint(finalList)