Skip to content

Commit ac00971

Browse files
committed
Make graph largely independent of the parameters structure.
1 parent 6a0d9a7 commit ac00971

2 files changed

Lines changed: 65 additions & 71 deletions

File tree

src/main/create_graph.jl

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,35 @@
22
# using LinearAlgebra
33

44
"""
5-
create_graph(param, w = 11, h = 11; type = "map", kwargs...) -> Dict, Dict
5+
create_graph(param, w = 11, h = 11; type = "map", N = 1, kwargs...) -> Dict
66
77
Initialize the underlying graph, population and productivity parameters.
88
99
# Arguments
10-
- `param::Dict`: Dict that contains the model parameters
11-
- `w::Int64=11`: Number of nodes along the width of the underlying graph (integer)
12-
- `h::Int64=11`: Number of nodes along the height of the underlying graph (integer, odd if triangle)
10+
- `param::Dict`: Dict that contains the model parameters (only needed for checks)
11+
- `w::Int64=11`: Number of nodes along the width of the underlying graph if type != "custom" (integer)
12+
- `h::Int64=11`: Number of nodes along the height of the underlying graph if type != "custom" (integer, odd if triangle)
1313
1414
# Keyword Arguments
1515
- `type::String="map"`: Either "map", "square", "triangle", or "custom"
16-
- `omega::Vector{Float64}`: Vector of Pareto weights for each node or region in partial mobility case (default ones(J or nregions))
17-
- `Zjn::Matrix{Float64}`: J x N matrix of producties per node (j = 1:J) and good (n = 1:N) (default ones(J, N))
1816
- `adjacency::BitMatrix`: J x J Adjacency matrix (only used for custom network)
1917
- `x::Vector{Float64}`: x coordinate (longitude) of each node (only used for custom network)
2018
- `y::Vector{Float64}`: y coordinate (latitude) of each node (only used for custom network)
19+
- `omega::Vector{Float64}`: Vector of Pareto weights for each node or region in partial mobility case (default ones(J or nregions))
20+
- `m::Vector{Float64}=ones(N)`: Vector of weights Nx1 in the cross congestion cost function
21+
- `Zjn::Matrix{Float64}`: J x N matrix of producties per node (j = 1:J) and good (n = 1:N) (default ones(J, N))
2122
- `Lj::Vector{Float64}`: Vector of populations in each node (j = 1:J) (only for fixed labour case)
2223
- `Hj::Vector{Float64}`: Vector of immobile good in each node (j = 1:J) (e.g. housing, default ones(J))
2324
- `Lr::Vector{Float64}`: Vector of populations in each region (r = 1:nregions) (only for partial mobility)
2425
- `region::Vector{Int64}`: Vector indicating region of each location (only for partial mobility)
2526
26-
# Notes
27-
- `create_graph()` will overwrite any parameters `Zjn`, `Lj`, `Hj`, `omega`, `Lr`, already set in the `param` Dict with the default values. So these values should be set inside `create_graph()`, or after `create_graph()` has been called.
28-
2927
# Examples
3028
```julia
31-
param, graph = create_graph(init_parameters())
29+
graph = create_graph()
3230
```
3331
"""
3432
function create_graph(param, w = 11, h = 11; type = "map", kwargs...)
3533

36-
param = namedtuple_to_dict(param)
3734
options = retrieve_options_create_graph(param, w, h, type; kwargs...)
3835

3936
if type == "map"
@@ -46,26 +43,26 @@ function create_graph(param, w = 11, h = 11; type = "map", kwargs...)
4643
graph = create_custom(options[:adjacency], options[:x], options[:y])
4744
end
4845

49-
param[:J] = graph[:J]
50-
param[:Zjn] = haskey(options, :Zjn) ? options[:Zjn] : ones(graph[:J], param[:N])
51-
param[:Hj] = haskey(options, :Hj) ? options[:Hj] : ones(graph[:J])
46+
graph[:N] = param[:N] # Ensure graph has all information
47+
graph[:Zjn] = haskey(options, :Zjn) ? options[:Zjn] : ones(graph[:J], graph[:N])
48+
graph[:Hj] = haskey(options, :Hj) ? options[:Hj] : ones(graph[:J])
5249

5350
if param[:mobility] == false
54-
param[:Lj] = haskey(options, :Lj) ? options[:Lj] : ones(graph[:J]) / graph[:J]
55-
param[:hj] = param[:Hj] ./ param[:Lj]
56-
param[:hj][param[:Lj] .== 0] .= 1
57-
param[:omegaj] = options[:omega]
51+
graph[:Lj] = haskey(options, :Lj) ? options[:Lj] : ones(graph[:J]) / graph[:J]
52+
graph[:hj] = graph[:Hj] ./ graph[:Lj]
53+
graph[:hj][graph[:Lj] .== 0] .= 1
54+
graph[:omegaj] = options[:omega]
5855
elseif param[:mobility] == 0.5
5956
graph[:region] = options[:region]
60-
param[:omegar] = options[:omega]
61-
param[:Lr] = options[:Lr]
62-
param[:nregions] = options[:nregions]
57+
graph[:omegar] = options[:omega]
58+
graph[:Lr] = options[:Lr]
59+
graph[:nregions] = options[:nregions]
6360
end
6461

6562
# Running checks on population / productivity / housing parameters
66-
check_graph_param(param)
63+
check_graph_param(graph, param)
6764

68-
return param, graph
65+
return graph
6966
end
7067

7168
function isadjacency(M)

src/main/init_parameters.jl

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11

2-
# ==============================================================
3-
# OPTIMAL TRANSPORT NETWORKS IN SPATIAL EQUILIBRIUM
4-
# by P. Fajgelbaum, E. Schaal, D. Henricot, C. Mantovani 2017-19
5-
# ================================================= version 1.0.4
62

73
"""
84
init_parameters(; kwargs...) -> Dict
95
10-
Returns a `param` dict with the model parameters.
6+
Returns a `param` dict with the model parameters. These are independent of the graph structure and dimensions.
117
128
# Keyword Arguments
139
- `alpha::Float64=0.5`: Cobb-Douglas coefficient on final good c^alpha * h^(1-alpha)
1410
- `beta::Float64=1`: Parameter governing congestion in transport cost
1511
- `gamma::Float64=1`: Elasticity of transport cost relative to infrastructure
16-
- `K::Float64=1`: Amount of concrete/asphalt
1712
- `sigma::Float64=5`: Elasticity of substitution across goods (CES)
1813
- `rho::Float64=2`: Curvature in utility (c^alpha * h^(1-alpha))^(1-rho)/(1-rho)
1914
- `a::Float64=0.8`: Curvature of the production function L^alpha
20-
- `m::Vector{Float64}=ones(N)`: Vector of weights Nx1 in the cross congestion cost function
21-
- `N::Int64=1`: Number of goods
2215
- `nu::Float64=1`: Elasticity of substitution b/w goods in transport costs if cross-good congestion
16+
- `N::Int64=1`: Number of goods traded in the economy (used for checks in `create_graph()`)
17+
- `K::Float64=1`: Amount of concrete/asphalt
2318
- `labor_mobility::Any=false`: Switch for labor mobility (true/false or 'partial')
2419
- `cross_good_congestion::Bool=false`: Switch for cross-good congestion
2520
- `annealing::Bool=true`: Switch for the use of annealing at the end of iterations (only if gamma > beta)
2621
- `verbose::Bool=true`: Switch to turn on/off text output (from Ipopt or other optimizers)
2722
- `duality::Bool=false`: Switch to turn on/off duality whenever available
2823
- `warm_start::Bool=true`: Use the previous solution as a warm start for the next iteration
29-
- `kappa_min::Float64=1e-5`: Minimum value for road capacities κ
24+
- `kappa_min::Float64=1e-5`: Minimum value for road capacities K
3025
- `min_iter::Int64=20`: Minimum number of iterations
3126
- `max_iter::Int64=200`: Maximum number of iterations
32-
- `tol::Float64=1e-5`: Tolerance for convergence of road capacities κ
27+
- `tol::Float64=1e-5`: Tolerance for convergence of road capacities K
3328
3429
# Optional Parameters
3530
- `optimizer = Ipopt.Optimizer`: Optimizer to be used
@@ -43,7 +38,7 @@ Returns a `param` dict with the model parameters.
4338
param = init_parameters(K = 10, labor_mobility = true)
4439
```
4540
"""
46-
function init_parameters(; alpha = 0.5, beta = 1, gamma = 1, K = 1, sigma = 5, rho = 2, a = 0.8, N = 1, m = ones(N), nu = 1,
41+
function init_parameters(; alpha = 0.5, beta = 1, gamma = 1, sigma = 5, rho = 2, a = 0.8, nu = 1, K = 1,
4742
labor_mobility = false, cross_good_congestion=false, annealing=true,
4843
verbose = true, duality = false, warm_start = true,
4944
kappa_min = 1e-5, min_iter = 20, max_iter = 200, tol = 1e-5, kwargs...)
@@ -65,8 +60,6 @@ function init_parameters(; alpha = 0.5, beta = 1, gamma = 1, K = 1, sigma = 5, r
6560
param[:sigma] = sigma
6661
param[:rho] = rho
6762
param[:a] = a
68-
param[:m] = m
69-
param[:N] = N
7063
param[:nu] = nu
7164
if labor_mobility === "partial" || labor_mobility === 0.5
7265
param[:mobility] = 0.5
@@ -98,57 +91,61 @@ function init_parameters(; alpha = 0.5, beta = 1, gamma = 1, K = 1, sigma = 5, r
9891
param[:F] = (L, a) -> L^a
9992
param[:Fprime] = (L, a) -> a * L^(a - 1)
10093

101-
# CHECK CONSISTENCY WITH ENDOWMENTS/PRODUCTIVITY (if applicable)
102-
check_graph_param(param)
94+
# # CHECK CONSISTENCY WITH ENDOWMENTS/PRODUCTIVITY (if applicable)
95+
# check_graph_param(param)
10396

10497
return param
10598
end
10699

107-
function check_graph_param(param)
108-
109-
## These are graph parameters that should not be in the parameters dict
110-
111-
# if haskey(param, :x) && length(param[:x]) != param[:J]
112-
# @warn "x does not have the right length J = $(param[:J])."
113-
# end
114-
115-
# if haskey(param, :y) && length(param[:y]) != param[:J]
116-
# @warn "y does not have the right length J = $(param[:J])."
117-
# end
118-
119-
# if haskey(param, :adjacency) && size(param[:adjacency]) != (param[:J], param[:J])
120-
# @warn "adjacency matrix does not have the right dimensions J x J."
121-
# end
122-
123-
# if haskey(param, :region) && length(param[:region]) != param[:J]
124-
# @warn "region does not have the right length J = $(param[:J])."
125-
# end
100+
# Check if the parameters are consistent with the graph structure
101+
function check_graph_param(graph, param)
126102

127-
if haskey(param, :omegaj) && length(param[:omegaj]) != param[:J]
128-
@warn "omegaj does not have the right length J = $(param[:J])."
103+
if haskey(param, :omegaj) && !haskey(graph, :omegaj)
104+
graph[:omegaj] = param[:omegaj]
105+
end
106+
if haskey(graph, :omegaj) && length(graph[:omegaj]) != graph[:J]
107+
@warn "omegaj does not have the right length J = $(graph[:J])."
129108
end
130109

131-
if haskey(param, :omegar) && length(param[:omegar]) != param[:nregions]
132-
@warn "omegar does not have the right length nregions = $(param[:nregions])."
110+
if haskey(param, :omegar) && !haskey(graph, :omegar)
111+
graph[:omegar] = param[:omegar]
112+
end
113+
if haskey(graph, :omegar) && length(graph[:omegar]) != graph[:nregions]
114+
@warn "omegar does not have the right length nregions = $(graph[:nregions])."
133115
end
134116

135-
if haskey(param, :Lr) && length(param[:Lr]) != param[:nregions]
136-
@warn "Lr does not have the right length nregions = $(param[:nregions])."
117+
if haskey(param, :Lr) && !haskey(graph, :Lr)
118+
graph[:Lr] = param[:Lr]
119+
end
120+
if haskey(graph, :Lr) && length(graph[:Lr]) != graph[:nregions]
121+
@warn "Lr does not have the right length nregions = $(graph[:nregions])."
137122
end
138123

139-
if haskey(param, :Lj) && param[:mobility] == 0 && length(param[:Lj]) != param[:J]
140-
@warn "Lj does not have the right length J = $(param[:J])."
124+
if haskey(param, :Lj) && !haskey(graph, :Lj) && param[:mobility] == 0
125+
graph[:Lj] = param[:Lj]
126+
end
127+
if haskey(graph, :Lj) && param[:mobility] == 0 && length(graph[:Lj]) != graph[:J]
128+
@warn "Lj does not have the right length J = $(graph[:J])."
141129
end
142130

143-
if haskey(param, :Zjn) && size(param[:Zjn]) != (param[:J], param[:N])
144-
@warn "Zjn does not have the right size J ($(param[:J])) x N ($(param[:N]))."
131+
if haskey(param, :Zjn) && !haskey(graph, :Zjn)
132+
graph[:Zjn] = param[:Zjn]
133+
end
134+
if haskey(graph, :Zjn) && size(graph[:Zjn]) != (graph[:J], graph[:N])
135+
@warn "Zjn does not have the right size J ($(graph[:J])) x N ($(graph[:N]))."
145136
end
146137

147-
if haskey(param, :Hj) && length(param[:Hj]) != param[:J]
148-
@warn "Hj does not have the right length J = $(param[:J])."
138+
if haskey(param, :Hj) && !haskey(graph, :Hj)
139+
graph[:Hj] = param[:Hj]
140+
end
141+
if haskey(graph, :Hj) && length(graph[:Hj]) != graph[:J]
142+
@warn "Hj does not have the right length J = $(graph[:J])."
149143
end
150144

151-
if haskey(param, :hj) && length(param[:hj]) != param[:J]
152-
@warn "hj does not have the right length J = $(param[:J])."
145+
if haskey(param, :hj) && !haskey(graph, :hj)
146+
graph[:hj] = param[:hj]
147+
end
148+
if haskey(graph, :hj) && length(graph[:hj]) != graph[:J]
149+
@warn "hj does not have the right length J = $(graph[:J])."
153150
end
154151
end

0 commit comments

Comments
 (0)