Thank you @odow and @jd-foster for the previous answers. I am trying to code up a relatively simple dispatch model. Since this is a network model, ideally the results i expect is dispatch of conventionals and output of renewables (wind, solar, etc) across the zones and over time. As you pointed out, my issue seems to be with indexing, but i’m not sure where exactly.
using JuMP, Clp, DataFrames, CSV
using Base: Symbol
#Define sets
P= plants.index
N = nodes.index
N = nodes.index
T = demand_2018.index
L= lines.index
#Specifying all plant types
C = filter(r -> any(occursin.(["conventional"], r.plant_type)), readDataDFs[:plants]).index
ROR = filter(r -> any(occursin.(["ROR"], r.plant_type)), readDataDFs[:plants]).index #Run-of-river
SOL = filter(r -> any(occursin.(["solar"], r.plant_type)), readDataDFs[:plants]).index #solar
WND = filter(r -> any(occursin.(["wind"], r.plant_type)), readDataDFs[:plants]).index #wind
dispatch_model = Model(Clp.Optimizer)
#Output variables
@variables(dispatch_model, begin
GEN[T, C] >=0 #Conventional plant generation
FLOW[T, L] #Line power flow
RUNOFRIVER[T,ROR] >=0 #Run-of-River
SOLAR[T,SOL] >=0
WIND[T,WND] >=0
VoltageAngle[T, N] #Voltage angle
end)
#Objective function
@objective(dispatch_model,
Min,
sum(sum(GEN[t, c]*data[:plants][:cost][c] for c in C) for t in T))
#conventional plant capacity constraint
@constraint(dispatch_model,
CapConstraint[t in T, c in C],
GEN[t,c] <= data[:plants][:g_max][c]
)
#I have issues with renewable generation constraints
#Run-of-river generation constraint
@constraint(dispatch_model,
RORGeneration_new[t in T, n in N, r in ROR],
RUNOFRIVER[t,n,r] <= data[:ror_cf][t] * data[:plants][:g_max][r][n]
)
#Wind generation constraint
@constraint(dispatch_model,
WNDGeneration[t in T, n in N, w in WND],
WIND[t,n,w] <= data[:wind_cf][n][t] * data[:plants][:g_max][w][n]
)
#Solar generation constraint
@constraint(dispatch_model,
SOLARGeneration[t in T, n in N, s in SOL],
SOLAR[t,n,s] <= data[:solar_cf][n][t] * data[:plants][:g_max][s][n]
)
...
Initial error messsage for Wind
ERROR: KeyError: key (1, "ZONE_A", "ZONE_A_WIND") not found
Stacktrace:
[1] to_index(A::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{String, Int64}}}}, idx::Tuple{Int64, String, String})
@ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\DenseAxisArray.jl:257
[2] getindex(::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{String, Int64}}}}, ::Int64, ::String, ::String)
@ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\DenseAxisArray.jl:266
[3] macro expansion
@ C:\Users\mbahe\.julia\packages\MutableArithmetics\8xkW3\src\rewrite.jl:279 [inlined]
[4] macro expansion
@ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\macros.jl:676 [inlined]
[5] (::var"#44#45")(t::Int64, n::String, w::String)
@ Main C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\macro.jl:194
[6] #38
@ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:105 [inlined]
[7] iterate
@ .\generator.jl:47 [inlined]
[8] collect(itr::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}}, JuMP.Containers.var"#38#39"{var"#44#45"}})
@ Base .\array.jl:678
[9] map(f::Function, A::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}})
@ Base .\abstractarray.jl:2323
[10] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}}, #unused#::Type{JuMP.Containers.DenseAxisArray})
@ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:105
[11] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}})
@ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:66
[12] macro expansion
@ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\macros.jl:142 [inlined]
[13] top-level scope
I am aware that the variables are not defined the same as the constraints. I attempted to make them similar,
@variables(dispatch_model, begin
GEN[T, C] >=0 #Conventional plant generation
FLOW[T, L] #Line power flow
RUNOFRIVER[T,N,ROR] >=0 #Run-of-River
SOLAR[T,N,SOL] >=0
WIND[T,N,WND] >=0
VoltageAngle[T, N] #Voltage angle
end)
#Wind generation constraint
@constraint(dispatch_model,
WNDGeneration[t in T, n in N, w in WND],
WIND[t,n,w] <= data[:wind_cf][n][t] * data[:plants][:g_max][w][n]
)
but I get the following error message (example of wind).
ERROR: KeyError: key "ZONE_A" not found
Stacktrace:
[1] getindex(h::Dict{Symbol, Dict{Int64, V} where V}, key::String)
@ Base .\dict.jl:482
[2] macro expansion
@ C:\Users\mbahe\.julia\packages\MutableArithmetics\8xkW3\src\rewrite.jl:279 [inlined]
[3] macro expansion
@ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\macros.jl:676 [inlined]
[4] (::var"#42#43")(t::Int64, n::String, w::String)
@ Main C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\macro.jl:194
[5] #38
@ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:105 [inlined]
[6] iterate
@ .\generator.jl:47 [inlined]
[7] collect(itr::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}}, JuMP.Containers.var"#38#39"{var"#42#43"}})
@ Base .\array.jl:678
[8] map(f::Function, A::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}})
@ Base .\abstractarray.jl:2323
[9] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}}, #unused#::Type{JuMP.Containers.DenseAxisArray})
@ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:105
[10] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, WeakRefStrings.StringVector{String}, WeakRefStrings.StringVector{String}}})
@ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:66
[11] macro expansion
@ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\macros.jl:142 [inlined]
I would appreciate any help on this issue.
Thanks