-
Notifications
You must be signed in to change notification settings - Fork 0
/
noLandWaterPath.py
285 lines (218 loc) · 8.72 KB
/
noLandWaterPath.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import igraph
import countries as COS
import graph as MCM_Graph
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import pprint
import tabulate
import math
import random
import pickle
import sys
## load pickeled iGraph
graph = igraph.Graph()
graph = graph.Read_Pickle('MCM_Graph.p')
endPrioritization = 'HMDCost'
pathPrioritization = 'HMDCost'
print 'Begining. Prioritizing on ', pathPrioritization
print 'Land/water nope toggle...'
numFailures = 0
numTimesNoPath = 0
removeEdgeList = []
for edge in graph.es:
if edge['TransitMethod'] != 'land':
# print edge.index
removeEdgeList.append( edge.index )
# edgeID = graph.get_eid( edge['Source'], edge['Target'] )
# print 'Deleting ', edge['SourceCo'], edge['TargetCo']
# graph.delete_edges( edge )
# else:
# print edge
graph.delete_edges(removeEdgeList)
#removeVertexList = []
#for vertex in graph.vs:
# if vertex['label'] == 'Morocco':
# print graph.adjacent( vertex )
# if len( graph.adjacent( vertex ) ) < 1:
## graph.delete_vertices( vertex )
# removeVertexList.append( vertex.index )
#graph.delete_vertices( removeVertexList )
#for vertex in graph.vs :
# vertex["size"] = 8
#layout = graph.layout("kk")
#igraph.plot(graph)
def resourcesCalculate(vertex, update=True):
''' Validated by Jessie hand math.'''
resources = ( vertex['GNI']* \
(1 - vertex['Unemployment']) * \
vertex['Education'] * \
vertex['LifeExp'] * \
vertex['GDPHealth'] ) * 10**(-7)
if update==True:
vertex['Resources'] = resources
return resources
def costFuncMSIMCalculate(edge, update=True):
'''
Calculate the cost for a given edge using a heavily modified Spacial
Interaction Model ("Jessie" model).
Option to automatically update the "Cost" attribute for the edge.
Default behavior.
INPUT: edge : indivigual igraph edge object.
*update : Boolian. If True, execution will update the Cost attribute
of the given edge.
RETURNS: cost : The evaluated value of the cost function.
'''
source = edge['Source']
target = edge['Target']
## Set land/sea toggle value
if edge['TransitMethod']=='land': landval = 0.75
elif edge['TransitMethod']=='sea': landval = 1
else: print 'WARNING: No transit method defined for: ', edge
if graph.vs[target]['NumRefs'] > graph.vs[target]['RefCap']:
val = 0.00000001
if update==True:
edge['Cost'] = val
edge['HMDCost'] = 10000/float(val)
return val
if edge['TargetCo'] in COS.endCountryList():
## Equation D case (end countries):
pt1 = edge['Safety'] * \
graph.vs[target]['SafetyCo'] * \
graph.vs[target]['Resources'] * \
graph.vs[target]['NumRefs'] * \
graph.vs[target]['natPop'] * \
landval
den = edge['Distance']**.5 *\
edge['MoneyCost'] * \
graph.vs[source]['Resources'] *\
graph.vs[source]['natPop'] * \
graph.vs[source]['SafetyCo']
endmult = 1 - \
( graph.vs[target]['NumRefs'] / \
float( graph.vs[target]['RefCap'] ) )
val = pt1 * endmult / den
if graph.vs[target]['NumRefs'] > graph.vs[target]['RefCap']:
val = 0.00000001
if update==True:
edge['Cost'] = val
edge['HMDCost'] = 10000/float(val)
return val
if update==True:
edge['Cost'] = val
edge['HMDCost'] = 10000/float(val)
return val
elif edge['TargetCo'] in COS.transitionCoList():
## Equation C case (Transition countries):
pt1 = edge['Safety'] * \
graph.vs[target]['SafetyCo'] * \
graph.vs[target]['Resources'] * \
graph.vs[target]['NumRefs'] * \
landval
den = edge['Distance'] * \
edge['MoneyCost'] * \
resourcesCalculate( graph.vs[source], update=False ) * \
graph.vs[source]['SafetyCo']
endmult = 1 - \
( graph.vs[target]['NumRefs'] / \
float( graph.vs[target]['RefCap'] ) )
val = pt1 * endmult / den
#
# if graph.vs[target]['NumRefs'] > graph.vs[target]['RefCap']:
# val = 0.00000001
if update==True:
edge['Cost'] = val
if val < .0000001:
edge['HMDCost'] = .0000001
else: edge['HMDCost'] = 10000/float(val)
return val
def pickEndCountry(metric):
endCountries = COS.nordicCountries() + COS.westernEuropeCountries()
endCoLabels = []
endCoWeight = []
for vertex in graph.vs:
if vertex['label'] in endCountries:
# print vertex
endCoLabels.append(vertex['ID'])
# endCoWeight.append( vertex[metric] )
# print len(endCoLabels)
# print len(endCoWeight)
endCoWeight = [ i/sum(endCoWeight) for i in endCoWeight ]
return np.random.choice(endCoLabels)#, p=endCoWeight)
def pickStartCountry():
## Move refs from all countries:
if graph.vs[0]['NumRefs']<10000:
coIDs = range( 38 )
numRefsCos = []
for coid in coIDs:
# graph.vs[coid]['NumRefs']
numRefsCos.append( graph.vs[coid]['NumRefs'] )
originCoWeight = [ i/float(sum(numRefsCos)) for i in numRefsCos ]
# print originCoWeight
return np.random.choice(coIDs, p=originCoWeight)
## Move Refs only from Origin and Transition countries
else:
coList = COS.originCoList() + COS.transitionCoList()
coIDs = []#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 18, 19, 30, 31, 32, 33, 34, 35, 36, 37, 38]
numRefsCos = []
for vertex in graph.vs:
if vertex['label'] in coList:
numRefsCos.append( vertex['NumRefs'] )
coIDs.append( vertex.index )
# for coid in coIDs:
# graph.vs[coid]['NumRefs']
# numRefsCos.append( graph.vs[coid]['NumRefs'] )
originCoWeight = [ i/float(sum(numRefsCos)) for i in numRefsCos ]
# print originCoWeight
return np.random.choice(coIDs, p=originCoWeight)
numRefsOverTime = {}
for vertex in graph.vs:
numRefsOverTime[ vertex['label'] ] = [ vertex['NumRefs'] ]
print pickStartCountry()
for timeStep in range(7000000):
# print 'TS: ', timeStep
# try:
## Update graph properties:
for vertex in graph.vs:
numRefsOverTime[ vertex['label'] ].append( vertex['NumRefs'] )
for vertex in graph.vs:
resourcesCalculate(vertex)
for edge in graph.es :
#print edge
costFuncMSIMCalculate(edge)
desiredEndCo = pickEndCountry(endPrioritization)
# print 'Des end: ', desiredEndCo
startCo = pickStartCountry()
# try:
pathToEndCo = graph.get_shortest_paths(startCo, desiredEndCo, weights = pathPrioritization, output='vpath')[0]
numStepsToMove = 1
if len(pathToEndCo) >numStepsToMove:
# print pathToEndCo
graph.vs[ pathToEndCo[0] ]['NumRefs'] = \
graph.vs[ pathToEndCo[0] ]['NumRefs'] - 300
graph.vs[ pathToEndCo[1] ]['NumRefs'] = \
graph.vs[ pathToEndCo[1] ]['NumRefs'] + 300
# except:
numTimesNoPath = numTimesNoPath + 1
if (timeStep % 10000)==0:
for vertex in graph.vs :
vertex["size"] = int( vertex['NumRefs']*.00012 )
layout = graph.layout("kk")
igraph.plot(graph, \
'NoLandRoutes-prioritize-GraphT'+str(timeStep)+'.png', layout=layout )#, **visual_style)
# except:
# print 'Exception occured!'
# print 'There was not a path ', numTimesNoPath, ' times.'
# pickle.dump( numRefsOverTime, open( 'NoLandRoutes-'+pathPrioritization+".p", "wb" ) )
# numFailures = numFailures + 1
# if numFailures >20:
# print 'Hi'
# sys.exit('Too many failures occured to proceed. Ended at timestep: '+str(timeStep))
print 'There was not a path ', numTimesNoPath, ' times.'
for vertex in graph.vs :
vertex["size"] = int( vertex['NumRefs']*.00012 )
layout = graph.layout("kk")
#igraph.plot(graph, \
# 'path-'+pathPrioritization+'--edge-'+endPrioritization+'Graph'+'.png', \
# layout=layout )#, **visual_style)
MCM_Graph.plotResultsFromList(numRefsOverTime, ['Syria', 'UK', 'France', 'Sweden', 'Spain'])