11using OptimalTransportNetworks
22using Random
3- using LinearAlgebra
43using Plots
54
65# Initialize parameters
76param = init_parameters (K = 100 , labor_mobility = false )
87width, height = 13 , 13
98
109# Create graph
11- param, g = create_graph (param, width, height, type = " map" )
10+ param, g0 = create_graph (param, width, height, type = " map" )
1211
1312# Set fundamentals
14- Random. seed! (5 )
15- minpop = minimum (param[:Lj ])
16- param[:Zjn ] = fill (minpop, g[:J ], 1 ) # matrix of productivity
17- param[:Lj ] = fill (1e-6 , g[:J ]) # matrix of population
13+ param[:Zjn ] = ones (param[:J ], 1 ) .* 1e-3 # matrix of productivity (not 0 to avoid numerical glitches)
14+ param[:Lj ] = ones (param[:J ]) .* 1e-3 # matrix of population
1815
19- Ni = find_node (g, 5 , 5 ) # center
16+ Ni = find_node (g0, ceil (width / 2 ), ceil (height / 2 ) ) # center
2017param[:Zjn ][Ni] = 1 # more productive node
2118param[:Lj ][Ni] = 1 # more productive node
2219
20+ Random. seed! (5 )
2321nb_cities = 20 # draw a number of random cities in space
2422for i in 1 : nb_cities- 1
2523 newdraw = false
2624 while newdraw == false
2725 j = round (Int, 1 + rand () * (g[:J ] - 1 ))
28- if param[:Lj ][j] <= minpop
26+ if param[:Lj ][j] <= 1e-3
2927 newdraw = true
3028 param[:Lj ][j] = 1
3129 end
3230 end
3331end
3432
3533param[:hj ] = param[:Hj ] ./ param[:Lj ]
36- param[:hj ][param[:Lj ] .== 1e-6 ] .= 1
34+ param[:hj ][param[:Lj ] .== 1e-3 ] .= 1
3735
3836# Draw geography
3937z = zeros (g[:J ]) # altitude of each node
40-
4138geographies = Dict ()
4239geographies[:blank ] = (z = z, obstacles = nothing )
43- g = apply_geography (g, geographies[:blank ])
4440
45- param0 = param # store initial params
46- g0 = g # store initial graph
41+ g = apply_geography (g0, geographies[:blank ])
4742
4843# Blank geography
4944results = Dict (:blank => optimal_network (param, g))
@@ -53,13 +48,12 @@ mountain_size = 0.75
5348mountain_height = 1
5449mount_x = 10
5550mount_y = 10
56- geographies[:mountain ] = (z = mountain_height * exp .(- ((g [:x ] .- mount_x). ^ 2 .+ (g [:y ] .- mount_y). ^ 2 ) / (2 * mountain_size^ 2 )),
51+ geographies[:mountain ] = (z = mountain_height * exp .(- ((g0 [:x ] .- mount_x). ^ 2 .+ (g0 [:y ] .- mount_y). ^ 2 ) / (2 * mountain_size^ 2 )),
5752 obstacles = nothing )
58- g = apply_geography (g , geographies[:mountain ], alpha_up_i = 10 , alpha_down_i = 10 )
53+ g = apply_geography (g0 , geographies[:mountain ], alpha_up_i = 10 , alpha_down_i = 10 )
5954results[:mountain ] = optimal_network (param, g)
6055
6156# Adding river and access by land
62- g = g0
6357geographies[:river ] = (z = geographies[:mountain ]. z,
6458 obstacles = [6 + (1 - 1 )* width 6 + (2 - 1 )* width;
6559 6 + (2 - 1 )* width 6 + (3 - 1 )* width;
@@ -69,13 +63,14 @@ geographies[:river] = (z = geographies[:mountain].z,
6963 11 + (5 - 1 )* width 12 + (5 - 1 )* width;
7064 12 + (5 - 1 )* width 13 + (5 - 1 )* width])
7165
72- g = apply_geography (g, geographies[:river ], across_abstacle_delta_i = Inf ,
66+ g = apply_geography (g0, geographies[:river ],
67+ along_abstacle_delta_i = 1 ,
68+ along_abstacle_delta_tau = 1 ,
7369 alpha_up_i = 10 , alpha_down_i = 10 )
7470results[:river ] = optimal_network (param, g)
7571
7672# Reinit and put another river and bridges
77- g = g0
78- geographies[:bridges ] = (z = mountain_height * exp .(- ((g[:x ] .- mount_x). ^ 2 .+ (g[:y ] .- mount_y). ^ 2 ) / (2 * mountain_size^ 2 )),
73+ geographies[:bridges ] = (z = mountain_height * exp .(- ((g0[:x ] .- mount_x). ^ 2 .+ (g0[:y ] .- mount_y). ^ 2 ) / (2 * mountain_size^ 2 )),
7974 obstacles = [6 + (1 - 1 )* width 6 + (2 - 1 )* width;
8075 6 + (2 - 1 )* width 6 + (3 - 1 )* width;
8176 6 + (3 - 1 )* width 7 + (4 - 1 )* width;
@@ -86,13 +81,13 @@ geographies[:bridges] = (z = mountain_height * exp.(-((g[:x] .- mount_x).^2 .+ (
8681 11 + (5 - 1 )* width 12 + (5 - 1 )* width;
8782 12 + (5 - 1 )* width 13 + (5 - 1 )* width])
8883
89- g = apply_geography (g, geographies[:bridges ], alpha_up_i = 10 , alpha_down_i = 10 ,
90- across_abstacle_delta_i = 2 , along_obstacle_delta_i = Inf )
84+ g = apply_geography (g0, geographies[:bridges ], alpha_up_i = 10 , alpha_down_i = 10 ,
85+ across_abstacle_delta_i = 2 ,
86+ along_abstacle_delta_tau = 1 )
9187results[:bridges ] = optimal_network (param, g)
9288
9389# Allowing for water transport
94- g = g0
95- geographies[:water_transport ] = (z = mountain_height * exp .(- ((g[:x ] .- mount_x). ^ 2 .+ (g[:y ] .- mount_y). ^ 2 ) / (2 * mountain_size^ 2 )),
90+ geographies[:water_transport ] = (z = mountain_height * exp .(- ((g0[:x ] .- mount_x). ^ 2 .+ (g0[:y ] .- mount_y). ^ 2 ) / (2 * mountain_size^ 2 )),
9691 obstacles = [6 + (1 - 1 )* width 6 + (2 - 1 )* width;
9792 6 + (2 - 1 )* width 6 + (3 - 1 )* width;
9893 6 + (3 - 1 )* width 7 + (4 - 1 )* width;
@@ -103,8 +98,10 @@ geographies[:water_transport] = (z = mountain_height * exp.(-((g[:x] .- mount_x)
10398 11 + (5 - 1 )* width 12 + (5 - 1 )* width;
10499 12 + (5 - 1 )* width 13 + (5 - 1 )* width])
105100
106- g = apply_geography (g, geographies[:water_transport ], alpha_up_i = 10 , alpha_down_i = 10 ,
107- across_abstacle_delta_i = 2 , along_obstacle_delta_i = 0.5 )
101+ g = apply_geography (g0, geographies[:water_transport ], alpha_up_i = 10 , alpha_down_i = 10 ,
102+ across_abstacle_delta_i = 2 ,
103+ along_obstacle_delta_i = 0.5 ,
104+ along_abstacle_delta_tau = 1 )
108105results[:water_transport ] = optimal_network (param, g)
109106
110107# Increasing returns to transport
@@ -123,7 +120,7 @@ for s in simulation
123120 plots[i] = plot_graph (g, results[Symbol (s)][:Ijk ],
124121 geography = geographies[Symbol (s)], obstacles = obstacles[i] == " on" ,
125122 mesh = true , mesh_transparency = 0.2 ,
126- node_sizes = results[Symbol (s)][:cj ] .* (param[:Lj ] .> minpop ),
123+ node_sizes = results[Symbol (s)][:cj ] .* (param[:Lj ] .> 1e-3 ),
127124 node_shades = param[:Zjn ],
128125 edge_min_thickness = 1.5 )
129126 title! (plots[i], " Geography $(s) " )
0 commit comments