Skip to content

Commit 8c0f420

Browse files
committed
Also adjusting naming convention.
1 parent f7c8e79 commit 8c0f420

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

code/11_GE_simulation_trans_african/optimal_trans_african_networks_largest_pcities.jl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ using OptimalTransportNetworks
44
include("../helpers/helpers.jl")
55

66
# Read Undirected Graph
7-
graph = CSV.read("data/transport_network/trans_african/fastest_routes_graph_edges.csv", DataFrame)
8-
histogram(graph.ug_cost, bins=100)
7+
edges = CSV.read("data/transport_network/trans_african/fastest_routes_graph_edges.csv", DataFrame)
8+
histogram(edges.ug_cost, bins=100)
99
# Adjusting
10-
graph.distance /= 1000 # Convert to km
11-
graph.border_dist /= 1000 # Convert to km
12-
graph.duration /= 60 # Convert to hours
13-
graph.total_cost = graph.ug_cost / 1e6 # Convert to millions
10+
edges.distance /= 1000 # Convert to km
11+
edges.border_dist /= 1000 # Convert to km
12+
edges.duration /= 60 # Convert to hours
13+
edges.total_cost = edges.ug_cost / 1e6 # Convert to millions
1414

15-
n = maximum([maximum(graph.from), maximum(graph.to)])
15+
n = maximum([maximum(edges.from), maximum(edges.to)])
1616

1717
# Create Adjacency Matrix
1818
adj_matrix = falses(n, n)
19-
for i in 1:size(graph, 1)
20-
adj_matrix[graph.from[i], graph.to[i]] = adj_matrix[graph.to[i], graph.from[i]] = true
19+
for i in 1:size(edges, 1)
20+
adj_matrix[edges.from[i], edges.to[i]] = adj_matrix[edges.to[i], edges.from[i]] = true
2121
end
2222

2323
# Read Nodes Data
@@ -28,24 +28,24 @@ describe(nodes)
2828

2929
# Create Infrastructure Matrix: Following Graff (2024) = average speed in km/h: length of route is accounted for in cost function
3030
infra_matrix = zeros(n, n)
31-
for i in 1:size(graph, 1)
32-
speed = graph.distance[i] / graph.duration[i]
33-
infra_matrix[graph.from[i], graph.to[i]] = infra_matrix[graph.to[i], graph.from[i]] = speed
31+
for i in 1:size(edges, 1)
32+
speed = edges.distance[i] / edges.duration[i]
33+
infra_matrix[edges.from[i], edges.to[i]] = infra_matrix[edges.to[i], edges.from[i]] = speed
3434
end
35-
describe(graph.distance ./ graph.duration)
35+
describe(edges.distance ./ edges.duration)
3636

3737
# Create Iceberg Trade Cost Matrix: Following Graff (2024)
38-
# graph.distance += graph.border_dist # uncomment to enable frictions
38+
# edges.distance += edges.border_dist # uncomment to enable frictions
3939
iceberg_matrix = zeros(n, n)
40-
for i in 1:size(graph, 1)
41-
iceberg_matrix[graph.from[i], graph.to[i]] = iceberg_matrix[graph.to[i], graph.from[i]] = 0.1158826 * log(graph.distance[i] / 1.609)
40+
for i in 1:size(edges, 1)
41+
iceberg_matrix[edges.from[i], edges.to[i]] = iceberg_matrix[edges.to[i], edges.from[i]] = 0.1158826 * log(edges.distance[i] / 1.609)
4242
end
4343
iceberg_matrix[iceberg_matrix .< 0] .= 0
4444

4545
# Create Infrastructure Building Cost Matrix: Following Graff (2024)
4646
infra_building_matrix = zeros(n, n)
47-
for i in 1:size(graph, 1)
48-
infra_building_matrix[graph.from[i], graph.to[i]] = infra_building_matrix[graph.to[i], graph.from[i]] = graph.total_cost[i]
47+
for i in 1:size(edges, 1)
48+
infra_building_matrix[edges.from[i], edges.to[i]] = infra_building_matrix[edges.to[i], edges.from[i]] = edges.total_cost[i]
4949
end
5050

5151
# Basic characteristics of the economy
@@ -153,13 +153,13 @@ end
153153
res_nodes |> CSV.write("results/transport_network/GE/trans_african/nodes_results_$(filename).csv")
154154

155155
# Saving: Graph / Edges
156-
res_graph = deepcopy(graph)
157-
res_graph.Ijk_orig = res_to_vec(infra_matrix, graph)
158-
res_graph.Ijk = res_to_vec(res_opt[:Ijk], graph)
156+
res_edges = deepcopy(edges)
157+
res_edges.Ijk_orig = res_to_vec(infra_matrix, edges)
158+
res_edges.Ijk = res_to_vec(res_opt[:Ijk], edges)
159159
for n in 1:N
160-
res_graph[!, Symbol("Qjk_$(n)")] = res_to_vec(res_opt[:Qjkn][:,:,n], graph)
160+
res_edges[!, Symbol("Qjk_$(n)")] = res_to_vec(res_opt[:Qjkn][:,:,n], edges)
161161
end
162-
res_graph |> CSV.write("results/transport_network/GE/trans_african/edges_results_$(filename).csv")
162+
res_edges |> CSV.write("results/transport_network/GE/trans_african/edges_results_$(filename).csv")
163163

164164

165165

code/11_GE_simulation_trans_african/optimal_trans_african_networks_largest_pcities.m

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
% With all_routes, need to set beta = 1 and CrossGoodCongestion 'off' to use efficient dual solution
99

1010
% Read Undirected Graph
11-
graph = readtable("data/transport_network/trans_african/fastest_routes_graph_edges.csv");
12-
% histogram(graph.ug_cost, 'BinWidth', 100);
11+
edges = readtable("data/transport_network/trans_african/fastest_routes_graph_edges.csv");
12+
% histogram(edges.ug_cost, 'BinWidth', 100);
1313
% Adjusting
14-
graph.distance = graph.distance / 1000; % Convert to km
15-
graph.border_dist = graph.border_dist / 1000; % Convert to km
16-
graph.duration = graph.duration / 60; % Convert to hours
17-
graph.total_cost = graph.ug_cost / 1e6; % Convert to millions
14+
edges.distance = edges.distance / 1000; % Convert to km
15+
edges.border_dist = edges.border_dist / 1000; % Convert to km
16+
edges.duration = edges.duration / 60; % Convert to hours
17+
edges.total_cost = edges.ug_cost / 1e6; % Convert to millions
1818

19-
n = max(max(graph.from), max(graph.to));
19+
n = max(max(edges.from), max(edges.to));
2020

2121
% Create Adjacency Matrix
2222
adj_matrix = false(n, n);
23-
for i = 1:height(graph)
24-
adj_matrix(graph.from(i), graph.to(i)) = true;
25-
adj_matrix(graph.to(i), graph.from(i)) = true;
23+
for i = 1:height(edges)
24+
adj_matrix(edges.from(i), edges.to(i)) = true;
25+
adj_matrix(edges.to(i), edges.from(i)) = true;
2626
end
2727

2828
% Read Nodes Data
@@ -33,27 +33,27 @@
3333

3434
% Create Infrastructure Matrix: Following Graff (2024) = average speed in km/h: length of route is accounted for in cost function
3535
infra_matrix = zeros(n, n);
36-
for i = 1:height(graph)
37-
speed = graph.distance(i) / graph.duration(i);
38-
infra_matrix(graph.from(i), graph.to(i)) = speed;
39-
infra_matrix(graph.to(i), graph.from(i)) = speed;
36+
for i = 1:height(edges)
37+
speed = edges.distance(i) / edges.duration(i);
38+
infra_matrix(edges.from(i), edges.to(i)) = speed;
39+
infra_matrix(edges.to(i), edges.from(i)) = speed;
4040
end
41-
summary(graph.distance ./ graph.duration);
41+
summary(edges.distance ./ edges.duration);
4242

4343
% Create Iceberg Trade Cost Matrix: Following Graff (2024)
44-
% graph.distance = graph.distance + graph.border_dist; % uncomment to enable frictions
44+
% edges.distance = edges.distance + edges.border_dist; % uncomment to enable frictions
4545
iceberg_matrix = zeros(n, n);
46-
for i = 1:height(graph)
47-
iceberg_matrix(graph.from(i), graph.to(i)) = 0.1158826 * log(graph.distance(i) / 1.609);
48-
iceberg_matrix(graph.to(i), graph.from(i)) = 0.1158826 * log(graph.distance(i) / 1.609);
46+
for i = 1:height(edges)
47+
iceberg_matrix(edges.from(i), edges.to(i)) = 0.1158826 * log(edges.distance(i) / 1.609);
48+
iceberg_matrix(edges.to(i), edges.from(i)) = 0.1158826 * log(edges.distance(i) / 1.609);
4949
end
5050
iceberg_matrix(iceberg_matrix < 0) = 0;
5151

5252
% Create Infrastructure Building Cost Matrix: Following Graff (2024)
5353
infra_building_matrix = zeros(n, n);
54-
for i = 1:height(graph)
55-
infra_building_matrix(graph.from(i), graph.to(i)) = graph.total_cost(i);
56-
infra_building_matrix(graph.to(i), graph.from(i)) = graph.total_cost(i);
54+
for i = 1:height(edges)
55+
infra_building_matrix(edges.from(i), edges.to(i)) = edges.total_cost(i);
56+
infra_building_matrix(edges.to(i), edges.from(i)) = edges.total_cost(i);
5757
end
5858

5959
% Basic characteristics of the economy
@@ -110,36 +110,36 @@
110110
'a', a, 'sigma', sigma, 'N', N, 'alpha', alpha, 'beta', beta, 'gamma', gamma, 'rho', rho, ...
111111
'verbose', 'on', 'K', K, 'TolKappa', 1e-5);
112112

113-
[param, g] = create_graph(param,[],[],'X',nodes.lon,'Y',nodes.lat,'Type','custom','Adjacency',adj_matrix);
113+
[param, graph] = create_graph(param,[],[],'X',nodes.lon,'Y',nodes.lat,'Type','custom','Adjacency',adj_matrix);
114114

115115
param.Lj = population;
116116
param.Zjn = productivity;
117117
% param.omegaj = weights; % In case want to give more weight to specific locations
118118
param.Hj = population .* (1-alpha); % TG: I normalise this because the general utility function has a (h_j/(1-alpha))^(1-alpha) thing with it
119119

120-
g.delta_i = infra_building_matrix;
121-
g.delta_tau = iceberg_matrix;
120+
graph.delta_i = infra_building_matrix;
121+
graph.delta_tau = iceberg_matrix;
122122

123123
% Naming conventions: if IRS -> 'cgc_irs' or 'irs_na' without annealing; if alpha = 0.1 -> add '_alpha01'; if with_ports = false -> add '_noport'; if frictions -> add '_bc' (border cost)
124124
filename = '22g_10b_fixed_cgc_sigma38_rho0'; % adjust if sigma != 1.5
125125
fprintf('File extension: %s\n', filename);
126126

127127
% Solve allocation from existing infrastructure
128128
strcat("Started P_stat on ", datestr(datetime('now')))
129-
res_stat = solve_allocation(param, g, infra_matrix, true);
129+
res_stat = solve_allocation(param, graph, infra_matrix, true);
130130

131131
% Solve Optimal Network (this can take long - up to 48h)
132132
strcat("Started P_opt on ", datestr(datetime('now')))
133-
res_opt = optimal_network(param, g, infra_matrix, min_mask, max_mask, false);
133+
res_opt = optimal_network(param, graph, infra_matrix, min_mask, max_mask, false);
134134

135135
% Check: should be 1
136-
K_ratio = K / sum(sum(res_opt.Ijk .* g.delta_i));
136+
K_ratio = K / sum(sum(res_opt.Ijk .* graph.delta_i));
137137
disp(K_ratio);
138138

139139
% Plot Network
140-
plot_graph(param, g, res_opt.Ijk, 'Margin', 0.01);
140+
plot_graph(param, graph, res_opt.Ijk, 'Margin', 0.01);
141141
axis equal;
142-
plot_graph(param, g, res_opt.Ijk - infra_matrix, 'Margin', 0.01);
142+
plot_graph(param, graph, res_opt.Ijk - infra_matrix, 'Margin', 0.01);
143143
axis equal;
144144

145145
% Saving: Nodes
@@ -164,10 +164,10 @@
164164
writetable(res_nodes, sprintf('results/transport_network/GE/trans_african/nodes_results_%s.csv', filename));
165165

166166
% Saving: Graph / Edges
167-
res_graph = graph;
168-
res_graph.Ijk_orig = res_to_vec(infra_matrix, graph);
169-
res_graph.Ijk = res_to_vec(res_opt.Ijk, graph);
167+
res_edges = edges;
168+
res_edges.Ijk_orig = res_to_vec(infra_matrix, edges);
169+
res_edges.Ijk = res_to_vec(res_opt.Ijk, edges);
170170
for n = 1:N
171-
res_graph = setfield(res_graph, ['Qjk_', num2str(n)], res_to_vec(res_opt.Qjkn(:,:,n), graph));
171+
res_edges = setfield(res_edges, ['Qjk_', num2str(n)], res_to_vec(res_opt.Qjkn(:,:,n), edges));
172172
end
173-
writetable(res_graph, sprintf('results/transport_network/GE/trans_african/edges_results_%s.csv', filename));
173+
writetable(res_edges, sprintf('results/transport_network/GE/trans_african/edges_results_%s.csv', filename));

0 commit comments

Comments
 (0)