odow / SDDP.jl

A JuMP extension for Stochastic Dual Dynamic Programming
https://sddp.dev
Other
305 stars 61 forks source link

Runtime specified variable naming #752

Closed dannyopts closed 3 months ago

dannyopts commented 3 months ago

Hi,

I have written some functions to create variables and ran into a few issues which make me think I must be doing something wrong.

These functions are called multiple times for a single subproblem and I have found that I cant use the "standard" JuMP syntax for defining variables

@JuMP.variable(sp, my_var_name[i=mode.sns])

because the symbol my_var_name will be used to name the variable on the model (not the runtime value of my_var_name). I will then get name collisions on the second call of this function.

Instead I have used an anonymous variable like so:

@JuMP.variable(sp, [i=mode.sns], base_name=my_var_name)

This is fine until I try to the access the variables in SDDP.simulate.

SDDP.simulate(model, 1, [:CompressedH2Store_p]; skip_undefined_variables=true)

which results in CompressedH2Store_p being given the value NaN, even though CompressedH2Store_p is a variable that was added to the model (via @JuMP.variable(sp, [i=mode.sns], base_name="CompressedH2Store_p"))

I dug around a bit and it seems that the variables are added to the model CompressedH2Store_p_1, CompressedH2Store_p_2 etc but are not inserted into the object_dictionary.

As a temporary work around I am adding variables to the object_dictionary after they are created, although this seems like I'm fighting with the tools rather than using them as intended.

function add_to_model(
    sp, 
    component::Component{Store},
    mode::OperationalOptimisationMode
)::Component{Store}
    ....
    definition = component.definition
    base_name = definition.name * "_p"
    p = @JuMP.variable(sp, [i=mode.sns], base_name=base_name)
    JuMP.object_dictionary(sp)[Symbol(base_name)] = p

Is there a better way to add variables to a model with a runtime defined names?

dannyopts commented 3 months ago

Found the answer that this is the expected way to do this: https://discourse.julialang.org/t/using-anonymous-and-or-symbols-to-create-dynamic-varible-names/83095/3 https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467

closing since this is clearly the intended way