Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add direct_model to create_model #1067

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""
create_model!(energy_problem; kwargs...)

Create the internal model of an [`TulipaEnergyModel.EnergyProblem`](@ref).
Any keyword argument will be passed to the underlyting [`create_model`](@ref).
Create the internal model of a [`TulipaEnergyModel.EnergyProblem`](@ref).
Any keyword argument will be passed to the underlying [`create_model`](@ref).
"""
function create_model!(energy_problem; kwargs...)
energy_problem.model = @timeit to "create_model" create_model(
Expand Down Expand Up @@ -32,10 +32,18 @@
profiles,
model_parameters;
write_lp_file = false,
enable_names = true
enable_names = true,
direct_model = false,
optimizer_with_attributes = optimizer_with_attributes(HiGHS.Optimizer),
)

Create the energy model manually. We recommend using [`create_model!`](@ref) instead.

If `enable_names = false` then variables and constraints in the model will not be assigned names, which improves speed but reduces the readability of log messages.
For more information, see [`JuMP.set_string_names_on_creation`](https://jump.dev/JuMP.jl/stable/api/JuMP/#set_string_names_on_creation).

If `direct_model = true` then a JuMP `direct_model` will be created using `optimizer_with_attributes`, which has memory improvements.
For more information, see [`JuMP.direct_model`](https://jump.dev/JuMP.jl/stable/api/JuMP/#direct_model).
"""
function create_model(
connection,
Expand All @@ -46,9 +54,15 @@
model_parameters;
write_lp_file = false,
enable_names = true,
direct_model = false,
#optimizer_with_attributes = optimizer_with_attributes(HiGHS.Optimizer),
)
## Model
model = JuMP.Model()
if direct_model
model = JuMP.direct_model(optimizer_with_attributes)

Check warning on line 62 in src/create-model.jl

View check run for this annotation

Codecov / codecov/patch

src/create-model.jl#L62

Added line #L62 was not covered by tests
else
model = JuMP.Model()
end

JuMP.set_string_names_on_creation(model, enable_names)

Expand Down
2 changes: 2 additions & 0 deletions src/solve-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ end
Solve the JuMP model. The `optimizer` argument should be an MILP solver from the JuMP
list of [supported solvers](https://jump.dev/JuMP.jl/stable/installation/#Supported-solvers).
By default we use HiGHS.
Note: If `create_model(; direct_model = true)` the `optimizer` cannot be changed in `solve_model`, but can be changed in the `optimizer_with_attributes` argument of [`create_model`](@ref).
For more information, see the [JuMP documentation](https://jump.dev/JuMP.jl/stable/api/JuMP/#direct_model).

The keyword argument `parameters` should be passed as a list of `key => value` pairs.
These can be created manually, obtained using [`default_parameters`](@ref), or read from a file
Expand Down
Loading