import numpy as np; from numpy import asarray, zeros
if __name__ == "__main__" :
duplicate_fig_52_game = False
if duplicate_fig_52_game :
# Duplicate the game matrix for Figure 52 (for debugging this code):
#
# The distance between each city:
dist = asarray( [ [ 0, 9, 15 ], [ 9, 0, 12 ], [ 15, 12, 0 ] ] )
city_population = [ 50, 30, 20 ]
else :
# The distance between each city:
dist = asarray( [ [ 0, 5, 10, 15 ], [ 5, 0, 5, 10 ], [ 10, 5, 0, 5 ], [ 15, 10, 5, 0 ] ] )
city_population = [ 20, 40, 20, 20 ]
proportion_captured = [ 0.8, 0.6, 0.4 ] # for Ex 11
#proportion_captured = [ 0.9, 0.75, 0.6 ] # for Ex 12
n_cities = len(city_population)
# Note:
#
# The larger company is player R
# The smaller company is player C
#
game_matrix = zeros( (n_cities,n_cities) )
for loc_R in range(n_cities):
for loc_C in range(n_cities):
proportion_for_R = 0.
if duplicate_fig_52_game :
for ci in range(n_cities) :
if dist[ci,loc_R] < dist[ci,loc_C] : # city ci is closer to R buisness goes to R
proportion_for_R += city_population[ci]
elif dist[ci,loc_R] == dist[ci,loc_C] : # city ci is equidistant between R and C we split the population equally
proportion_for_R += 0.5 * city_population[ci]
else : # city ci is close to C buisness goes to C
proportion_for_R += 0.
#endfor
else :
for ci in range(n_cities) :
if dist[ci,loc_R] < dist[ci,loc_C] : # city ci is closer to R
proportion_for_R += proportion_captured [0] * city_population[ci]
elif dist[ci,loc_R] == dist[ci,loc_C] : # city ci is equidistant between R and C
proportion_for_R += proportion_captured[1] * city_population[ci]
else : # city ci is close to C
proportion_for_R += proportion_captured[2] * city_population[ci]
#endfor
#endif
game_matrix[loc_R,loc_C] = proportion_for_R
#endfor
#endfor
print game_matrix