martinbiel / StochasticPrograms.jl

Julia package for formulating and analyzing stochastic recourse models.
MIT License
75 stars 25 forks source link

Error during the instantiation of a program #31

Closed enri07 closed 3 years ago

enri07 commented 3 years ago

Hi,

I have build a two-stage stochastic model using the package StochasticPrograms and now I am trying to instantiate this program. The model is a little bit complicated so it is difficult to give all the information about it, but to summarize I have defined a new type of scenario made by two arrays of dictionaries providing the expectation function as described in the manual. Then I created an array of possible scenarios and all seems to work fine, I'm able to run the command expected on this array and it works as I was expecting. The problem appears when I try to run:

instantiate(model_user, possible_scenarios, optimizer = LShaped.Optimizer)

In this case, the following message is raised:

ERROR: LoadError: MethodError: no method matching build_variable(::JuMP.var"#_error#108"{LineNumberNode}, ::JuMP.Containers.SparseAxisArray{Any,3,Tuple{Any,Any,Any}}, ::StochasticPrograms.DecisionSet)
Closest candidates are:
  build_variable(::Function, ::VariableInfo, ::Any...; kwargs...) at /home/user/.julia/packages/JuMP/Xrr7O/src/macros.jl:1341
  build_variable(::Function, ::ScalarVariable, ::StochasticPrograms.DecisionSet) at /home/user/.julia/packages/StochasticPrograms/SjnV4/src/types/decisions/macros.jl:4
  build_variable(::Function, ::ScalarVariable, ::MathOptInterface.AbstractScalarSet) at /home/user/.julia/packages/JuMP/Xrr7O/src/macros.jl:1354
  ...
Stacktrace:
 [1] macro expansion at /home/user/.julia/packages/JuMP/Xrr7O/src/macros.jl:142 [inlined]
 [2] #121 at /home/user/File_stoch_nuovo/Single_user_vf.jl:63 [inlined]
 [3] (::var"#121#329")(::Model, ::NamedTuple{(:price_MG_P, :price_MG_N_V, :price_MG_N_F, :price_F, :c_UP),Tuple{Dict{Int64,Float64},Dict{Int64,Float64},Dict{Int64,Float64},Float64,Dict{String,Float64}}}, ::Scenario_Load_Renewable) at ./none:0
 [4] generate_proxy!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(:price_MG_P, :price_MG_N_V, :price_MG_N_F, :price_F, :c_UP),Tuple{Dict{Int64,Float64},Dict{Int64,Float64},Dict{Int64,Float64},Float64,Dict{String,Float64}}}}},StageDecompositionStructure{2,1,Tuple{StochasticPrograms.ScenarioProblems{Scenario_Load_Renewable}}}}) at /home/user/.julia/packages/StochasticPrograms/SjnV4/src/methods/generation.jl:104
 [5] generate!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(:price_MG_P, :price_MG_N_V, :price_MG_N_F, :price_F, :c_UP),Tuple{Dict{Int64,Float64},Dict{Int64,Float64},Dict{Int64,Float64},Float64,Dict{String,Float64}}}}},StageDecompositionStructure{2,1,Tuple{StochasticPrograms.ScenarioProblems{Scenario_Load_Renewable}}}}) at /home/user/.julia/packages/StochasticPrograms/SjnV4/src/methods/generation.jl:143
 [6] instantiate(::StochasticModel{2,Tuple{StageParameters{NamedTuple{(),Tuple{}}},StageParameters{NamedTuple{(:price_MG_P, :price_MG_N_V, :price_MG_N_F, :price_F, :c_UP),Tuple{Dict{Int64,Float64},Dict{Int64,Float64},Dict{Int64,Float64},Float64,Dict{String,Float64}}}}}}, ::Array{Scenario_Load_Renewable,1}; instantiation::UnspecifiedInstantiation, optimizer::Type{T} where T, defer::Bool, direct_model::Bool, kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/user/.julia/packages/StochasticPrograms/SjnV4/src/methods/api.jl:37
 [7] top-level scope at /home/user/File_stoch_nuovo/Single_user_vf.jl:239
 [8] include(::String) at ./client.jl:439
 [9] top-level scope at REPL[1]:1
in expression starting at /home/user/File_stoch_nuovo/Single_user_vf.jl:239

Also, I tried to run the command instantiate with different optimizer but nothing changed. Is there someone who could help me please? Thank you for your help.

odow commented 3 years ago

The model is a little bit complicated so it is difficult to give all the information about it

It's impossible to debug without an example.

Create a minimal working example by simplifing what you have. Comment out variables and constraints until the error disappears. Try removing data.

https://stackoverflow.com/help/minimal-reproducible-example

It looks like this might be something to do with your @decision line.

enri07 commented 3 years ago

Thank you for your help. I'm trying to simplify the problem as you suggested.

As regards the @decision variable, is there a problem in the following declaration?

@decision(model, 0 <= x_us[u=user_set, a=users_data[u].asset_names] <= users_data[u].max_capacity[a]/users_data[u].size_asset[a], Int)

I used the same sintax allowed for JuMP variables. In this case user_set and users_data[u].asset_names are respectively finite sets of integers and strings.

enri07 commented 3 years ago

Thank you very much for your previous advice. By analyzing one at a time all the variables, I was able to find that the mistake was the declaration of a @recourse in the second stage that was indexed on an actual empty set. By removing it, all works fine and I'm also able to optimize the problem. I think that I can close this issue.