I want to create a JUMP energy system model, where different features can be added or not.
So my idea was to use different functions which can be called if needed for adding new equations to the model. Cannot figure out what the problem is.
Here simple code with an error:
using JuMP
m=Model()
function add_storage!(m::Model, Vol_m3::Int64, time_steps::OrdinalRange{Int64,Int64})
@variable(m, M3_storage[time_steps]>=0)
@constraint(m, storageCap[t in time_steps], M3_storage[t]<=Vol_m3)
return m
end
time_steps=(1:4)
add_storage!(m,200,time_steps)
With the print below I see all the variables I wanted.
print(m)
Output:
Feasibility
Subject to
storageCap[1] : M3_storage[1] <= 200.0
storageCap[2] : M3_storage[2] <= 200.0
storageCap[3] : M3_storage[3] <= 200.0
storageCap[4] : M3_storage[4] <= 200.0
M3_storage[1] >= 0.0
M3_storage[2] >= 0.0
M3_storage[3] >= 0.0
M3_storage[4] >= 0.0
But wow, when I add another “feature” and would like to use the already existing model m I get an error:
function add_min_level!(m::Model,Min_m3::Int64,time_steps::OrdinalRange{Int64,Int64})
@constraint(m, storageMin[t in time_steps], M3_storage[t]>=Min_m3)
return m
end
add_min_level!(m,50,time_steps)
Output: UndefVarError: M3_storage not defined
M3_storage[t] can be printet with print(m).
But is not available in the function add_min_level! even with (m::Model) shared as imput parameter?
Could you help me what the problem is and how to resolve it.
Generally what is the best in JUMP to make a flexible optimization Model where you can add or delete easily model componentes
Best Greetings Gerhard