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 can_retire validation for multi-stage optimization #683

Merged
merged 7 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/case_runners/case_runner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ function run_genx_case_multistage!(case::AbstractString, mysetup::Dict, optimize
model_dict[t] = generate_model(mysetup, inputs_dict[t], OPTIMIZER)
end

# check that resources do not switch from can_retire = 0 to can_retire = 1 between stages
validate_can_retire_multistage(inputs_dict, mysetup["MultiStageSettingsDict"]["NumStages"])
lbonaldo marked this conversation as resolved.
Show resolved Hide resolved

### Solve model
println("Solving Model")

Expand Down
34 changes: 34 additions & 0 deletions src/model/utility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,37 @@ function by_rid_res(rid::Integer, sym::Symbol, rs::Vector{<:AbstractResource})
f = isdefined(GenX, sym) ? getfield(GenX, sym) : x -> getproperty(x, sym)
return f(r)
end

@doc raw"""
(inputs_dict::Dict, num_stages::Int)

This function validates that all the resources do not switch from havig `can_retire = 0` to `can_retire = 1` during the multi-stage optimization.

# Arguments
- `inputs_dict::Dict`: A dictionary containing the inputs for each stage.
- `num_stages::Int`: The number of stages in the multi-stage optimization.

# Returns
- Throws an error if a resource switches from `can_retire = 0` to `can_retire = 1` between stages.
"""
function validate_can_retire_multistage(inputs_dict::Dict, num_stages::Int)
for stage in 2:num_stages # note: loop starts from 2 because we are comparing stage t with stage t-1
can_retire_current = can_retire.(inputs_dict[stage]["RESOURCES"])
can_retire_previous = can_retire.(inputs_dict[stage - 1]["RESOURCES"])

# Check if any resource switched from can_retire = 0 to can_retire = 1 between stage t-1 and t
if any(can_retire_current .- can_retire_previous .> 0)
# Find the resources that switched from can_retire = 0 to can_retire = 1 and throw an error
retire_switch_ids = findall(can_retire_current .- can_retire_previous .> 0)
resources_switched = inputs_dict[stage]["RESOURCES"][retire_switch_ids]
for resource in resources_switched
@warn "Resource `$(resource_name(resource))` with id = $(resource_id(resource)) switched " *
"from can_retire = 0 to can_retire = 1 between stages $(stage - 1) and $stage"
end
msg = "Current implementation of multi-stage optimization does not allow resources " *
"to switch from can_retire = 0 to can_retire = 1 between stages."
error(msg)
end
end
return nothing
end
70 changes: 70 additions & 0 deletions test/test_multistage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,74 @@ end

test_update_cumulative_min_ret!()

function test_can_retire_validation()
@testset "No resources switch from can_retire = 0 to can_retire = 1" begin
inputs = Dict{Int,Dict}()
lbonaldo marked this conversation as resolved.
Show resolved Hide resolved
lbonaldo marked this conversation as resolved.
Show resolved Hide resolved
inputs[1] = Dict("RESOURCES" => [
GenX.Thermal(Dict(:resource => "thermal", :id => 1,
:can_retire => 1)),
GenX.Vre(Dict(:resource => "vre", :id => 2,
:can_retire => 1)),
GenX.Hydro(Dict(:resource => "hydro", :id => 3,
:can_retire => 1)),
GenX.FlexDemand(Dict(:resource => "flex_demand", :id => 4,
:can_retire => 1))])
inputs[2] = Dict("RESOURCES" => [
GenX.Thermal(Dict(:resource => "thermal", :id => 1,
:can_retire => 0)),
GenX.Vre(Dict(:resource => "vre", :id => 2,
:can_retire => 1)),
GenX.Hydro(Dict(:resource => "hydro", :id => 3,
:can_retire => 1)),
GenX.FlexDemand(Dict(:resource => "flex_demand", :id => 4,
:can_retire => 1))])
inputs[3] = Dict("RESOURCES" => [
GenX.Thermal(Dict(:resource => "thermal", :id => 1,
:can_retire => 0)),
GenX.Vre(Dict(:resource => "vre", :id => 2,
:can_retire => 0)),
GenX.Hydro(Dict(:resource => "hydro", :id => 3,
:can_retire => 1)),
GenX.FlexDemand(Dict(:resource => "flex_demand", :id => 4,
:can_retire => 1))])
@test isnothing(GenX.validate_can_retire_multistage(inputs, 3))
end

@testset "One resource switches from can_retire = 0 to can_retire = 1" begin
inputs = Dict{Int,Dict}()
lbonaldo marked this conversation as resolved.
Show resolved Hide resolved
lbonaldo marked this conversation as resolved.
Show resolved Hide resolved
lbonaldo marked this conversation as resolved.
Show resolved Hide resolved
inputs[1] = Dict("RESOURCES" => [
GenX.Thermal(Dict(:resource => "thermal", :id => 1,
:can_retire => 0)),
GenX.Vre(Dict(:resource => "vre", :id => 2,
:can_retire => 0)),
GenX.Hydro(Dict(:resource => "hydro", :id => 3,
:can_retire => 0)),
GenX.FlexDemand(Dict(:resource => "flex_demand", :id => 4,
:can_retire => 1))])
inputs[2] = Dict("RESOURCES" => [
GenX.Thermal(Dict(:resource => "thermal", :id => 1,
:can_retire => 0)),
GenX.Vre(Dict(:resource => "vre", :id => 2,
:can_retire => 0)),
GenX.Hydro(Dict(:resource => "hydro", :id => 3,
:can_retire => 1)),
GenX.FlexDemand(Dict(:resource => "flex_demand", :id => 4,
:can_retire => 1))])
inputs[3] = Dict("RESOURCES" => [
GenX.Thermal(Dict(:resource => "thermal", :id => 1,
:can_retire => 0)),
GenX.Vre(Dict(:resource => "vre", :id => 2,
:can_retire => 0)),
GenX.Hydro(Dict(:resource => "hydro", :id => 3,
:can_retire => 1)),
GenX.FlexDemand(Dict(:resource => "flex_demand", :id => 4,
:can_retire => 1))])
@test_throws ErrorException GenX.validate_can_retire_multistage(inputs, 3)
end
end

with_logger(ConsoleLogger(stderr, Logging.Error)) do
test_can_retire_validation()
end

end # module TestMultiStage
Loading