-
Notifications
You must be signed in to change notification settings - Fork 0
/
caca.py
106 lines (78 loc) · 2.65 KB
/
caca.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
# -*- coding: utf-8 -*-
from gurobipy import *
############################################################
#### Please run extractor.py before running this script ####
############################################################
print("Optimizing best secure points location")
#### Model iniciation ####
m=Model("P2")
#### Decision Variables ####
B=6000000
M=1000000000000
x={}
y={}
P={}
T={}
for j in idsecure:
#### Integer, Quantity of people in node j ####
T[j]=m.addVar(vtype="I", name="T_%s" %(j), lb=0)
#### Binary, if node j is safe point ####
x[j]=m.addVar(vtype=GRB.BINARY, name="x_%s" %(j))
for i in N:
#### Binary, if people travels from i to j ####
y[i,j]=m.addVar(vtype=GRB.BINARY, name="y_%s_%s" %(i,j))
#### Interger, Flow of people from i to j ####
P[i,j]=m.addVar(vtype="I", name="P_%s_%s" %(i,j), lb=0)
#for i in N:
# for j in idsecure:
# #### Binary, if people travels from i to j ####
# y[i,j]=m.addVar(vtype=GRB.BINARY, name="y_%s_%s" %(i,j))
# #### Interger, Flow of people from i to j ####
# P[i,j]=m.addVar(vtype="I", name="P_%s_%s" %(i,j), lb=0)
m.update()
#### Objective Function ####
m.setObjective(quicksum(P[i,j]*ultimatematrix[i,j] for (i,j) in nodetosecure))
#### Constraints ####
#### Budget ####
m.addConstr(quicksum(C[j]*x[j]+V[j]*T[j] for j in idsecure) + quicksum(D[j]*y[i,j] for (i,j) in nodetosecure) <= B)
#### Demand point distribution ####
for i in N:
m.addConstr(quicksum(y[i,j] for j in idsecure) <= 2)
m.addConstr(quicksum(y[i,j] for j in idsecure) >= 1)
#### Consistency ####
for j in idsecure:
m.addConstr((quicksum(y[i,j] for i in N))/M <= x[j])
for (i,j) in nodetosecure:
m.addConstr(y[i,j] >= ((P[i,j])/M))
#### Total Demand ####
for i in N:
m.addConstr(quicksum(P[i,j] for j in idsecure) == p[i])
#### Total Influx ####
for j in idsecure:
m.addConstr(quicksum(P[i,j] for i in N) == T[j])
m.update()
m.optimize()
import matplotlib.pyplot as plt
import networkx as nx
#### Draw nodes and edges ####
G = nx.Graph()
G.add_nodes_from(N)
#G.add_nodes_from(idsecure)
#for (i,j) in A:
# G.add_edge(i,j)
securenodes=[]
for (i,j) in nodetosecure:
if y[i,j].X==1:
G.add_edge(i,j)
securenodes.append(j)
# print(y[i,j])
G.add_nodes_from(securenodes)
position={}
for i in range(len(nodeswithposition)+1):
if i == 0:
pass
else:
a=nodeswithposition[i]
position[i]=a
nx.draw(G, position, node_color="red",node_size=20, nodelist=N)
nx.draw(G, position, node_color="blue",node_size=70, nodelist=securenodes)