Skip to content

Commit

Permalink
Added bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
mivanicERS committed Jan 31, 2025
1 parent ef80bdc commit 8ccf9a3
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/GlobalTradeAnalysisProjectModelV7.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mutable struct model_container_struct
parameters
sets
fixed
lower
upper
end

include("./helpers/aggComb.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/extract_model!.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function extract_model!(; model_container)
function extract_model!(model_container)
results = merge(Dict(
String(k) => begin
arrayOut = NamedArray(zeros(map(length, v.axes)), v.axes)
Expand Down
5 changes: 3 additions & 2 deletions src/generate_initial_model.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function generate_initial_model(; hSets, hData, hParameters)
(; sets, parameters, data, fixed) = generate_starting_values(; hSets, hData, hParameters)
mc = model_container_struct(JuMP.Model(Ipopt.Optimizer), data, parameters, sets, fixed)
(; sets, parameters, data, fixed, lower, upper) = generate_starting_values(; hSets, hData, hParameters)
mc = model_container_struct(JuMP.Model(Ipopt.Optimizer), data, parameters, sets, fixed, lower, upper)
build_model!(mc)
initialize_model!(mc)
return mc
end
49 changes: 47 additions & 2 deletions src/generate_starting_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,57 @@ function generate_starting_values(; hSets, hData, hParameters)
end
end for (k) in ["to", "tfe", "tx", "txs", "tm", "tms", "tfd", "tfm", "tpd", "tpm", "tgd", "tgm", "tid", "tim", "tinc", "qesf", "qe", "ppa", "α_qintva", "γ_qintva", "α_qfa", "γ_qfa", "α_qfe", "γ_qfe", "α_qfdqfm", "γ_qfdqfm", "α_qca", "γ_qca", "α_pca", "γ_pca", "σyp", "σyg", "β_qpa", "α_qpdqpm", "γ_qpdqpm", "α_qga", "γ_qga", "α_qgdqgm", "γ_qgdqgm", "α_qia", "γ_qia", "α_qidqim", "γ_qidqim", "α_qxs", "γ_qxs", "α_qtmfsd", "α_qst", "γ_qst", "α_qes2", "γ_qes2", "α_qinv", "δ", "ρ", "pop"]
)

fixed["ppa"][:, :] .= false
## The price of the first commodity in the first region is fixed
fixed["ppa"][comm[1], reg[1]] = true

q_min = 1e-8
q_max = 1e+12
p_min = 1e-8
p_max = 1e+8
t_min = 1e-8
t_max = 1e+2
α_min = 1e-8
α_max = 1
γ_min = 1e-8
ϵ_min = 1e-8
σ_min = 0
y_min = 1e-8
y_max = 1e12
v_min = 0


lower = merge(
Dict(k => q_min for k ["pop", "qint", "qva", "qo", "qfa", "qfe", "qfd", "qfm", "qca", "qc", "qes", "u", "up", "ug", "us", "qpa", "qpd", "qpm", "qga", "qgd", "qgm", "qia", "qid", "qim", "qinv", "qms", "qxs", "qtmfsd", "qtm", "qst", "qds", "qe", "qesf", "walras_sup", "walras_dem", "kb", "ke", "globalcgds"]),
Dict("qsave" => -q_max),
Dict(k => p_min for k ["pca", "pcgdswld", "pcif", "pds", "pe", "peb", "pes", "pfa", "pfactor", "pfd", "pfe", "pfm", "pfob", "pga", "pgd", "pgm", "pgov", "pia", "pid", "pim", "pint", "pinv", "pmds", "pms", "po", "pop", "ppa", "ppd", "ppm", "ppriv", "ps", "psave", "pt", "ptrans", "pva"]),
Dict(k => t_min for k ["tfd", "tfe", "tfm", "tgd", "tgm", "tid", "tim", "tinc", "tm", "tms", "to", "tpd", "tpm", "tx", "txs"]),
Dict(k => α_min for k ["α_pca", "α_qca", "α_qes2", "α_qfa", "α_qfdqfm", "α_qfe", "α_qga", "α_qgdqgm", "α_qia", "α_qidqim", "α_qintva", "α_qinv", "α_qpdqpm", "α_qst", "α_qtmfsd", "α_qxs"]),
Dict(k => γ_min for k ["γ_pca", "γ_qca", "γ_qes2", "γ_qfa", "γ_qfdqfm", "γ_qfe", "γ_qga", "γ_qgdqgm", "γ_qia", "γ_qidqim", "γ_qintva", "γ_qpdqpm", "γ_qst", "γ_qxs"]),
Dict(k => ϵ_min for k ["ϵ_qes2", "ϵ_qfa", "ϵ_qfdqfm", "ϵ_qfe", "ϵ_qga", "ϵ_qgdqgm", "ϵ_qia", "ϵ_qidqim", "ϵ_qintva", "ϵ_qinv", "ϵ_qpdqpm", "ϵ_qxs"]),
Dict(k => σ_min for k ["σ_qxs", "σ_vdf", "σ_vdg", "σ_vdi", "σ_vdp", "σ_vf", "σ_vff", "σ_vg", "σ_vi", "σ_vif", "σ_vp", "σ_vtwr", "σ_ρ", "σsave", "σyg", "σyp"]),
Dict("σ_qinv" => -1),
Dict(k => y_min for k ["fincome", "y", "yp", "yg"]),
Dict(k => v_min for k ["vdfp", "vmfp", "vdpp", "vmpp", "vdgp", "vmgp", "vdip", "vmip", "evfp", "evos", "vfob", "vcif", "vst", "vtwr", "maks", "vkb"]),
Dict(k => 0 for k ["uelas", "uepriv"]),
Dict(β_qpa => 1e-8),
Dict(k => 0 for k ["δ", "ρ"])
)

upper = merge(
Dict(k => q_max for k ["pop", "qint", "qva", "qo", "qfa", "qfe", "qfd", "qfm", "qca", "qc", "qes", "u", "up", "ug", "us", "qpa", "qpd", "qpm", "qga", "qgd", "qgm", "qsave", "qia", "qid", "qim", "qinv", "qms", "qxs", "qtmfsd", "qtm", "qst", "qds", "qe", "qesf", "walras_sup", "walras_dem", "kb", "ke", "globalcgds"]),
Dict(k => p_max for k ["pca", "pcgdswld", "pcif", "pds", "pe", "peb", "pes", "pfa", "pfactor", "pfd", "pfe", "pfm", "pfob", "pga", "pgd", "pgm", "pgov", "pia", "pid", "pim", "pint", "pinv", "pmds", "pms", "po", "pop", "ppa", "ppd", "ppm", "ppriv", "ps", "psave", "pt", "ptrans", "pva"]),
Dict(k => t_max for k ["tfd", "tfe", "tfm", "tgd", "tgm", "tid", "tim", "tinc", "tm", "tms", "to", "tpd", "tpm", "tx", "txs"]),
Dict(k => α_max for k ["α_pca", "α_qca", "α_qes2", "α_qfa", "α_qfdqfm", "α_qfe", "α_qga", "α_qgdqgm", "α_qia", "α_qidqim", "α_qintva", "α_qinv", "α_qpdqpm", "α_qst", "α_qtmfsd", "α_qxs"]),
Dict("σ_qinv" => 1),
Dict(k => y_max for k ["fincome", "y", "yp", "yg"]),
Dict(k => 10 for k ["uelas", "uepriv"]),
Dict(k => 1 for k ["δ", "ρ"])
)


# Calculate calibrated_parameter initial values
return (sets=sets, parameters=parameters, data=data, fixed=fixed)
return (sets=sets, parameters=parameters, data=data, fixed=fixed, lower=lower, upper=upper)

end
19 changes: 15 additions & 4 deletions src/initialize_model!.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function initialize_model!(; model_container)
function initialize_model!(model_container)

free_variables = filter(f -> !is_fixed(f), all_variables(model_container.model))
set_start_value.(free_variables, 1.01)

free_variables = filter(f->!is_fixed(f), all_variables(model_container.model))
set_start_value.(free_variables,1.01)

# First set the starting values and report if anything is missing
for k names(object_dictionary(model_container.model))
Expand All @@ -23,6 +24,16 @@ function initialize_model!(; model_container)
end
# Second unfix all variables
unfix.(JuMP.all_variables(model_container.model)[is_fixed.(JuMP.all_variables(model_container.model))])

# Set lower bounds
for (k,v) model_container.lower
set_lower_bound.(model_container[Symbol(k)], v)
end

for (k,v) model_container.upper
set_lower_bound.(model_container[Symbol(k)], v)
end

# Third fix variables that need to be fixed
for fv keys(model_container.fixed)
if size(model_container.fixed[fv]) == ()
Expand All @@ -36,7 +47,7 @@ function initialize_model!(; model_container)
else
for fvi CartesianIndices(model_container.fixed[fv])
if model_container.fixed[fv][fvi] && is_valid(model_container.model, model_container.model[Symbol(fv)][fvi])
if !isnan( model_container.data[fv][fvi])
if !isnan(model_container.data[fv][fvi])
fix(model_container.model[Symbol(fv)][fvi], model_container.data[fv][fvi]; force=true)
else
fix(model_container.model[Symbol(fv)][fvi], 0; force=true)
Expand Down
8 changes: 4 additions & 4 deletions src/run_model!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ are specified as exogenous (model_container.fixed), (3) solve the model, (4) loa
variable values to the data (model_container.data)
"""
function run_model!(; model_container, max_iter=50, constr_viol_tol=1e-8, bound_push=1e-15)
function run_model!( model_container; max_iter=50, constr_viol_tol=1e-8, bound_push=1e-15)

# initialize the model
initialize_model!(; model_container=model_container)
initialize_model!(model_container)

# solve_model
solve_model!(; model_container=model_container, max_iter=max_iter, constr_viol_tol=constr_viol_tol, bound_push=bound_push)
solve_model!(model_container; max_iter=max_iter, constr_viol_tol=constr_viol_tol, bound_push=bound_push)

# Exttract values
extract_model!(; model_container=model_container)
extract_model!(model_container)

end
4 changes: 2 additions & 2 deletions src/solve_model!.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function solve_model!(; model_container, max_iter, constr_viol_tol, bound_push)
function solve_model!(model_container; max_iter, constr_viol_tol, bound_push)

# Specify the solver parameters
set_attribute(model_container.model, "max_iter", max_iter)
set_attribute(model_container.model, "constr_viol_tol", constr_viol_tol)
Expand Down

0 comments on commit 8ccf9a3

Please sign in to comment.