-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
95 lines (74 loc) · 3.42 KB
/
part1.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
#Part1CodeFinal
#3133 Part 1
import csv
from pprint import pprint
from gurobipy import *
def csvReader(aFile):
with open(aFile, 'rt') as fin:
reader = csv.reader(fin)
header = next(reader)
return [row for row in reader]
arcList = csvReader('DS9_Network_Arc_Data.csv')
nodeList = csvReader('DS9_Network_Node_Data.csv')
def maxFlow():
#Cleaning up the data:
nodes = [node[0] for node in nodeList]
#list of nodes
demands = {node[0]: node[1] for node in nodeList}
#dictionary where key is node num and value is demand at that node
nodeGroups = {node[0]: node[2] for node in nodeList}
#dictionary where key is node num and value is group id
arcs = [(arc[0], arc[1]) for arc in arcList]
#list of every arc i --> j
decArcs = arcs + [(arc[1],arc[0]) for arc in arcList]
#list every arc i --> j and same arcs j --> i
capacities = {(arc[0],arc[1]): arc[2] for arc in arcList}
#dictionary where key is arc i --> j and value is capacity of that arc
m = Model("Deep Space 9")
m.setParam('OutputFlag', True)
#Creating all of the decisions variables:
#1: Creating arc flow decisions variables
x = m.addVars(decArcs, name = 'energyFlow')
#2: Creating node energy usage decisions variables:
y = m.addVars(nodes, name = 'energyUsage')
#Objective
objectiveJs = []
for arc in arcs:
if arc[0] == '1':
objectiveJs.append(arc[1]) #adds all j's from node '1' --> j to a list
m.setObjective(quicksum(x['1',j] for j in objectiveJs), GRB.MAXIMIZE)
#Constraints:
#made the assumption that the generator always supplies node 1 to demand, doesn't count towards flow
m.addConstr(y['1'] == int(demands['1']), name='generator supply')
#sum of flow on an arc from i --> j + j --> i <= capacity of that arc
for arc in arcs:
m.addConstr(x[arc[0],arc[1]] + x[arc[1],arc[0]] <= int(capacities[(arc[0],arc[1])]), name='capacity')
#nonnegativity constraint:
#arc flow in any direction is nonnegative
for arc in decArcs:
m.addConstr(x[arc[0],arc[1]] >= 0, name='nonnegativity')
#node energy usage is nonnegative
for node in nodes:
m.addConstr(y[node] >= 0, name='nonnegativity')
#energy used at a node is <= energy demanded at a node:
for node in nodes:
m.addConstr(y[node] <= int(demands[node]), name='demands')
#flow constraints:
#1: total energy used == energy output by generator plus energy used at the generator node:
m.addConstr(y['1'] + quicksum(x['1',j] for j in objectiveJs) == quicksum(y[node] for node in nodes))
#2: flow in = flow out + energy used at all nodes
for node in nodes:
nodeArcList = [arc[1] for arc in decArcs if node == arc[0]]
if node != '1':
m.addConstr(y[node] + quicksum(x[node,nout] for nout in nodeArcList) == quicksum(x[nin, node] for nin in nodeArcList))
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:')
for v in m.getVars():
print('%s = %g' % (v.varName, v.x))
#total flow used
return sum([var.x for var in m.getVars() if var.varName[0:7] == 'energyU'])
print('Max Flow is:' + str(maxFlow()))