Skip to content

Commit

Permalink
Add option to use model names in the functions
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada committed Dec 3, 2024
1 parent 7c5dc82 commit d6f982d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/run_spineopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ A new Spine database is created at `url_out` if one doesn't exist.
- `mip_solver=nothing`: a MIP solver to use if no MIP solver specified in the DB.
- `lp_solver=nothing`: a LP solver to use if no LP solver specified in the DB.
- `use_direct_model::Bool=false`: whether or not to use `JuMP.direct_model` to build the `Model` object.
- `use_model_names::Bool=false`: whether or not to use the names in the model.
- `optimize::Bool=true`: whether or not to optimise the model (useful for running tests).
- `update_names::Bool=false`: whether or not to update variable and constraint names after the model rolls
(expensive).
Expand Down Expand Up @@ -88,6 +89,7 @@ function run_spineopt(
mip_solver=nothing,
lp_solver=nothing,
use_direct_model=false,
use_model_names=false,
optimize=true,
update_names=false,
alternative="",
Expand All @@ -107,6 +109,7 @@ function run_spineopt(
mip_solver=mip_solver,
lp_solver=lp_solver,
use_direct_model=use_direct_model,
use_model_names=use_model_names,
optimize=optimize,
update_names=update_names,
alternative=alternative,
Expand All @@ -126,6 +129,7 @@ function _run_spineopt(
mip_solver,
lp_solver,
use_direct_model,
use_model_names,
log_level,
alternative,
kwargs...,
Expand All @@ -137,7 +141,7 @@ function _run_spineopt(
println("[SpineInterface version $si_ver (git hash: $si_git_hash)]")
t_start = now()
@log log_level 1 "Execution started at $t_start"
m = prepare_spineopt(url_in; upgrade, filters, templates, mip_solver, lp_solver, use_direct_model, log_level)
m = prepare_spineopt(url_in; upgrade, filters, templates, mip_solver, lp_solver, use_direct_model, use_model_names, log_level)
f(m)
run_spineopt!(m, url_out; log_level, alternative, kwargs...)
t_end = now()
Expand Down Expand Up @@ -193,6 +197,7 @@ or a `Dict` (e.g. manually created or parsed from a json file).
- `mip_solver`
- `lp_solver`
- `use_direct_model`
- `use_model_names`
See [run_spineopt](@ref) for the description of the keyword arguments.
"""
Expand All @@ -205,6 +210,7 @@ function prepare_spineopt(
mip_solver=nothing,
lp_solver=nothing,
use_direct_model=false,
use_model_names=false,
)
@log log_level 0 "Reading input data from $(_real_url(url_in))..."
_check_version(url_in; log_level, upgrade)
Expand Down Expand Up @@ -236,7 +242,7 @@ function prepare_spineopt(
end
end
end
create_model(mip_solver, lp_solver, use_direct_model)
create_model(mip_solver, lp_solver, use_direct_model, use_model_names)
end

function _init_data_from_db(url_in, log_level, upgrade, templates, filters, scenario="")
Expand Down Expand Up @@ -326,20 +332,21 @@ function run_spineopt!(
end

"""
create_model(mip_solver, lp_solver, use_direct_model)
create_model(mip_solver, lp_solver, use_direct_model, use_model_names)
A `JuMP.Model` extended to be used with SpineOpt.
`mip_solver` and `lp_solver` are 'optimizer factories' to be passed to `JuMP.Model` or `JuMP.direct_model`;
`use_direct_model` is a `Bool` indicating whether `JuMP.Model` or `JuMP.direct_model` should be used.
`use_model_names` is a `Bool` indicating whether the names in the model should be used.
"""
function create_model(mip_solver, lp_solver, use_direct_model)
function create_model(mip_solver, lp_solver, use_direct_model, use_model_names)
instance = first(model())
mip_solver = _mip_solver(instance, mip_solver)
lp_solver = _lp_solver(instance, lp_solver)
m_mp = if model_type(model=instance) === :spineopt_benders
m_mp = Base.invokelatest(_do_create_model, mip_solver, use_direct_model)
m_mp.ext[:spineopt] = SpineOptExt(instance, lp_solver, m_mp)
JuMP.set_string_names_on_creation(m_mp, false)
JuMP.set_string_names_on_creation(m_mp, use_model_names)
m_mp
end
model_by_stage = OrderedDict()
Expand All @@ -349,7 +356,7 @@ function create_model(mip_solver, lp_solver, use_direct_model)
end
m = Base.invokelatest(_do_create_model, mip_solver, use_direct_model)
m.ext[:spineopt] = SpineOptExt(instance, lp_solver, m_mp, model_by_stage)
JuMP.set_string_names_on_creation(m, false)
JuMP.set_string_names_on_creation(m, use_model_names)
m
end

Expand Down

0 comments on commit d6f982d

Please sign in to comment.