-
Notifications
You must be signed in to change notification settings - Fork 22
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
feat: add flexibility for solvers besides Gurobi #99
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
"""Convert a dict with string keys to a NamedTuple, for python-eqsue kwargs splatting""" | ||
function symbolize(d::Dict{String,Any})::NamedTuple | ||
return (; (Symbol(k) => v for (k,v) in d)...) | ||
end | ||
|
||
|
||
function new_model(factory_like)::Union{JuMP.Model, JuMP.MOI.AbstractOptimizer} | ||
if isa(factory_like, Gurobi.Env) | ||
return JuMP.direct_model(Gurobi.Optimizer(factory_like)) | ||
else | ||
return JuMP.Model(factory_like) | ||
Comment on lines
+7
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this implies JuMP treats Gurobi differently but all the other solvers in the same way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to use the |
||
end | ||
end | ||
|
||
|
||
""" | ||
interval_loop(env, model_kwargs, solver_kwargs, interval, n_interval, | ||
interval_loop(factory_like, model_kwargs, solver_kwargs, interval, n_interval, | ||
start_index, inputfolder, outputfolder) | ||
|
||
Given: | ||
- a Gurobi environment `env` | ||
- optimizer instantiation object `factory_like`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering about the term There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's either an optimization factory or a Gurobi environment. |
||
either a Gurobi environment or something that can be passed to JuMP.Model | ||
- a dictionary of model keyword arguments `model_kwargs` | ||
- a dictionary of solver keyword arguments `solver_kwargs` | ||
- an interval length `interval` (hours) | ||
|
@@ -15,7 +31,7 @@ Given: | |
Build a model, and run through the intervals, re-building the model and/or | ||
re-setting constraint right-hand-side values as necessary. | ||
""" | ||
function interval_loop(env::Gurobi.Env, model_kwargs::Dict, | ||
function interval_loop(factory_like, model_kwargs::Dict, | ||
solver_kwargs::Dict, interval::Int, | ||
n_interval::Int, start_index::Int, | ||
inputfolder::String, outputfolder::String) | ||
|
@@ -45,21 +61,19 @@ function interval_loop(env::Gurobi.Env, model_kwargs::Dict, | |
if storage_enabled | ||
model_kwargs["storage_e0"] = storage.sd_table.InitialStorage | ||
end | ||
m_kwargs = (; (Symbol(k) => v for (k,v) in model_kwargs)...) | ||
m = JuMP.direct_model(Gurobi.Optimizer(env)) | ||
m = new_model(factory_like) | ||
JuMP.set_optimizer_attributes(m, pairs(solver_kwargs)...) | ||
m, voi = _build_model(m; m_kwargs...) | ||
m, voi = _build_model(m; symbolize(model_kwargs)...) | ||
elseif i == 2 | ||
# Build a model with an initial ramp constraint | ||
model_kwargs["initial_ramp_enabled"] = true | ||
model_kwargs["initial_ramp_g0"] = pg0 | ||
if storage_enabled | ||
model_kwargs["storage_e0"] = storage_e0 | ||
end | ||
m_kwargs = (; (Symbol(k) => v for (k,v) in model_kwargs)...) | ||
m = JuMP.direct_model(Gurobi.Optimizer(env)) | ||
m = new_model(factory_like) | ||
JuMP.set_optimizer_attributes(m, pairs(solver_kwargs)...) | ||
m, voi = _build_model(m; m_kwargs...) | ||
m, voi = _build_model(m; symbolize(model_kwargs)...) | ||
else | ||
# Reassign right-hand-side of constraints to match profiles | ||
bus_demand = _make_bus_demand(case, interval_start, interval_end) | ||
|
@@ -125,33 +139,33 @@ function interval_loop(env::Gurobi.Env, model_kwargs::Dict, | |
results = get_results(f, voi, model_kwargs["case"]) | ||
break | ||
elseif ((status in numeric_statuses) | ||
& isa(factory_like, Gurobi.Env) | ||
& !("BarHomogeneous" in keys(solver_kwargs))) | ||
# if BarHomogeneous is not enabled, enable it and re-build | ||
# if Gurobi, and BarHomogeneous is not enabled, enable it and re-solve | ||
solver_kwargs["BarHomogeneous"] = 1 | ||
println("enable BarHomogeneous") | ||
JuMP.set_optimizer_attribute(m, "BarHomogeneous", 1) | ||
elseif ((status in infeasible_statuses) | ||
& !("load_shed_enabled" in keys(model_kwargs))) | ||
# if load shed not enabled, enable it and re-build the model | ||
model_kwargs["load_shed_enabled"] = true | ||
m_kwargs = (; (Symbol(k) => v for (k,v) in model_kwargs)...) | ||
println("rebuild with load shed") | ||
m = JuMP.direct_model(Gurobi.Optimizer(env)) | ||
m = new_model(factory_like) | ||
JuMP.set_optimizer_attributes(m, pairs(solver_kwargs)...) | ||
m, voi = _build_model(m; m_kwargs...) | ||
m, voi = _build_model(m; symbolize(model_kwargs)...) | ||
intervals_without_loadshed = 0 | ||
elseif !("BarHomogeneous" in keys(solver_kwargs)) | ||
# if BarHomogeneous is not enabled, enable it and re-build | ||
elseif (isa(factory_like, Gurobi.Env) | ||
& !("BarHomogeneous" in keys(solver_kwargs))) | ||
# if Gurobi, and BarHomogeneous is not enabled, enable it and re-solve | ||
solver_kwargs["BarHomogeneous"] = 1 | ||
println("enable BarHomogeneous") | ||
JuMP.set_optimizer_attribute(m, "BarHomogeneous", 1) | ||
elseif !("load_shed_enabled" in keys(model_kwargs)) | ||
model_kwargs["load_shed_enabled"] = true | ||
m_kwargs = (; (Symbol(k) => v for (k,v) in model_kwargs)...) | ||
println("rebuild with load shed") | ||
m = JuMP.direct_model(Gurobi.Optimizer(env)) | ||
m = new_model(factory_like) | ||
JuMP.set_optimizer_attributes(m, pairs(solver_kwargs)...) | ||
m, voi = _build_model(m; m_kwargs...) | ||
m, voi = _build_model(m; symbolize(model_kwargs)...) | ||
intervals_without_loadshed = 0 | ||
else | ||
# Something has gone very wrong | ||
|
@@ -199,10 +213,9 @@ function interval_loop(env::Gurobi.Env, model_kwargs::Dict, | |
# delete! will work here even if the key is not present | ||
delete!(solver_kwargs, "BarHomogeneous") | ||
delete!(model_kwargs, "load_shed_enabled") | ||
m_kwargs = (; (Symbol(k) => v for (k,v) in model_kwargs)...) | ||
m = JuMP.direct_model(Gurobi.Optimizer(env)) | ||
m = new_model(factory_like) | ||
JuMP.set_optimizer_attributes(m, pairs(solver_kwargs)...) | ||
m, voi = _build_model(m; m_kwargs...) | ||
m, voi = _build_model(m; symbolize(model_kwargs)...) | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to do similar thing for other solvers or this is something to explore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know that other solvers have a similar 'environment' feature that needs to be managed.