11
2+ using OptimalTransportNetworks
23using Random
4+ using Plots
35
46# ------------------------------------
57# 4.2 Random Cities with Multiple Goods
68# -------------------------------------
79
8- ngoods= 10
9- width= 9
10- height= 9
10+ ngoods = 10
11+ width = 9
12+ height = 9
1113
1214# Init and Solve network
1315
14- param = init_parameters (TolKappa = 1e-5 , K= 10000 , LaborMobility = " on " , Annealing = " off " )
15- param, g = create_graph (param, width, height, Type = " triangle" )
16+ param = init_parameters (K= 10000 , labor_mobility = true , annealing = false )
17+ param, g = create_graph (param, width, height, type = " triangle" )
1618
1719# set fundamentals
1820
19- param. N = 1 + ngoods
20- param. Zjn = zeros (g. J , param. N ) # matrix of productivity
21+ param[ :N ] = 1 + ngoods
22+ param[ : Zjn] = zeros (g[ :J ] , param[ :N ] ) # matrix of productivity
2123
22- param. Zjn[:,1 ] .= 1 # the entire countryside produces the homogenenous good 1
24+ param[ : Zjn] [:,1 ] .= 1 # the entire countryside produces the homogenenous good 1
2325
2426if ngoods > 0
2527 Ni = find_node (g, 5 , 5 ) # center
26- param. Zjn[Ni, 2 ] = 1 # central node
27- param. Zjn[Ni, 1 ] = 0 # central node
28+ param[ : Zjn] [Ni, 2 ] = 1 # central node
29+ param[ : Zjn] [Ni, 1 ] = 0 # central node
2830
2931 Random. seed! (5 )
3032 for i in 2 : ngoods
3133 newdraw = false
3234 while newdraw == false
33- j = round (Int, 1 + rand () * (g. J - 1 ))
34- if param. Zjn[j, 1 ] > 0
35+ j = round (Int, 1 + rand () * (g[ :J ] - 1 ))
36+ if param[ : Zjn] [j, 1 ] > 0
3537 newdraw = true
36- param. Zjn[j, i+ 1 ] = 1
37- param. Zjn[j, 1 ] = 0
38+ param[ : Zjn] [j, i+ 1 ] = 1
39+ param[ : Zjn] [j, 1 ] = 0
3840 end
3941 end
4042 end
@@ -45,37 +47,28 @@ results = Array{Any}(undef, 2)
4547results[1 ] = optimal_network (param, g)
4648
4749# Nonconvex
48- param. gamma = 2
49- results[2 ] = optimal_network (param, g, results[1 ]. Ijk)
50-
50+ param[:gamma ] = 2
51+ results[2 ] = optimal_network (param, g, I0 = results[1 ][:Ijk ])
5152
5253# Plot results
5354
5455cols = 3 # number of columns
55- rows = ceil ((1 + param. N) / cols)
56-
57- s = [" random_cities_multigoods_convex" , " random_cities_multigoods_nonconvex" ]
56+ rows = Int (ceil ((1 + param[:N ]) / cols))
5857
5958for j in 1 : 2
60- fig = figure ( " Units " => " inches " , " Position " => [ 0 , 0 , 7.5 , 11 ], " Name " => s[j])
61-
59+ # Initialize an empty array to hold the subplots
60+ plots = Vector {Any} (undef, ( 1 + param[ :N ]))
6261 # Plot network
63- subplot (rows, cols, 1 )
64- plot_graph (param, g, results[j]. Ijk, Shades = (results[j]. Lj .- minimum (results[j]. Lj)) ./ (maximum (results[j]. Lj) - minimum (results[j]. Lj)), Sizes = 1 .+ 16 .* (results[j]. Lj ./ mean (results[j]. Lj) .- 1 ), NodeFgColor = [.6 , .8 , 1 ], Transparency = " off" )
65- title (" (a) Transport Network (I_{jk})" , fontweight = " normal" , fontname = " Times" , fontsize = 9 )
66-
67- for i in 1 : param. N
68- subplot (rows, cols, i+ 1 )
69- sizes = 3 .* results[j]. Yjn[:, i] ./ sum (results[j]. Yjn[:, i])
70- shades = param. Zjn[:, i] ./ maximum (param. Zjn[:, i])
71- plot_graph (param, g, results[j]. Qjkn[:, :, i], Arrows = " on" , ArrowScale = 1 , ArrowStyle = " thin" , Nodes = " on" , Sizes = sizes, Shades = shades, NodeFgColor = [.6 , .8 , 1 ], Transparency = " off" )
72- title (" ($Char (97+i)) Shipping (Q^{i}_{jk})" , fontweight = " normal" , fontname = " Times" , fontsize = 9 )
62+ plots[1 ] = plot_graph (g, results[j][:Ijk ], node_shades = results[j][:Lj ], node_sizes = results[j][:Lj ], node_sizes_scale = 40 )
63+ title! (plots[1 ], " (a) Transport Network" )
64+ # Plot goods flows
65+ for i in 1 : param[:N ]
66+ plots[i+ 1 ] = plot_graph (g, results[j][:Qjkn ][:, :, i], edge_color = :brown , arrows = true , arrow_style = " thin" ,
67+ node_sizes = results[j][:Yjn ][:, i], node_sizes_scale = 40 ,
68+ node_shades = param[:Zjn ][:, i])
69+ title! (plots[i+ 1 ], string (' (' , Char (96 + i + 1 ), ' )' , " Flows Good " , i))
7370 end
74-
75- # Save
76- # savefig(fig, s[j] * ".eps")
77- # savefig(fig, s[j] * ".jpg")
71+ # Combine plots
72+ final_plot = plot (plots... , layout = (cols, rows), size = (rows* 400 , cols* 400 ))
73+ display (final_plot)
7874end
79-
80-
81- # Please note that this translation assumes that the functions `init_parameters`, `create_graph`, `find_node`, `optimal_network`, and `plot_graph` have been previously defined in Julia with the same functionality as in the original Matlab code. Also, the `figure` and `subplot` functions are assumed to be from a plotting package like `PyPlot`. The saving of the figures is commented out because the directory to save the figures is not specified.
0 commit comments