martinbiel / StochasticPrograms.jl

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

ERROR on the instantiation of a stochastic model #20

Closed gganetlepage closed 3 years ago

gganetlepage commented 3 years ago

I try to do the quick start example, same error as Issue #13 was happening. So, I run pgk> add StochasticPrograms#master and now this error is obtained. The same behavior happens for the farm example. Below versioninfo, status and the code.

Thank you in advance,

julia> versioninfo()
Julia Version 1.5.2
Commit 539f3ce943 (2020-09-23 23:17 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)

(@v1.5) pkg> status
Status `C:\Users\PC\.julia\environments\v1.5\Project.toml`
  [a076750e] CPLEX v0.7.2
  [9961bab8] Cbc v0.7.1
  [e2554f3b] Clp v0.8.3
  [60bf3e95] GLPK v0.14.2
  [2e9cd046] Gurobi v0.9.3
  [7073ff75] IJulia v1.22.0
  [43edad99] InstantiateFromURL v0.5.0
  [b6b21f68] Ipopt v0.6.3
  [4076af6c] JuMP v0.21.4
  [b8f27783] MathOptInterface v0.9.18
  [91a5bcdd] Plots v1.7.3
  [8b8459f2] StochasticPrograms v0.4.1 `https://github.com/martinbiel/StochasticPrograms.jl.git#master`

julia> using StochasticPrograms, GLPK, Ipopt

julia> simple_model = @stochastic_model begin
           @stage 1 begin
               @decision(model, x₁ >= 40)
               @decision(model, x₂ >= 20)
               @objective(model, Min, 100*x₁ + 150*x₂)
               @constraint(model, x₁ + x₂ <= 120)
           end
           @stage 2 begin
               @uncertain q₁ q₂ d₁ d₂
               @variable(model, 0 <= y₁ <= d₁)
               @variable(model, 0 <= y₂ <= d₂)
               @objective(model, Max, q₁*y₁ + q₂*y₂)
               @constraint(model, 6*y₁ + 10*y₂ <= 60*x₁)
               @constraint(model, 8*y₁ + 5*y₂ <= 80*x₂)
           end
       end
Two-Stage Stochastic Model

minimize f₀(x) + �[f(x,ξ)]
  x∈�

where

f(x,ξ) = min  f(y; x, ξ)
              y ∈ � (x, ξ)

julia> ξ₁ = Scenario(q₁ = 24.0, q₂ = 28.0, d₁ = 500.0, d₂ = 100.0, probability = 0.4)
Scenario with probability 0.4
  q₁: 24.0
  q₂: 28.0
  d₁: 500.0
  d₂: 100.0

julia> ξ₂ = Scenario(q₁ = 28.0, q₂ = 32.0, d₁ = 300.0, d₂ = 300.0, probability = 0.6)
Scenario with probability 0.6
  q₁: 28.0
  q₂: 32.0
  d₁: 300.0
  d₂: 300.0

julia> sp = instantiate(simple_model, [ξ₁, ξ₂], optimizer = GLPK.Optimizer)
ERROR: MethodError: no method matching is_coefficient_type(::Type{VectorAffineDecisionFunction{Float64}}, ::Type{Float64})
Closest candidates are:
  is_coefficient_type(::Type{var"#s280"} where var"#s280"<:Union{MathOptInterface.SingleVariable, MathOptInterface.VectorOfVariables}, ::Type) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Utilities\functions.jl:2058
  is_coefficient_type(::Type{var"#s280"} where var"#s280"<:Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}, MathOptInterface.VectorAffineFunction{T}, MathOptInterface.VectorQuadraticFunction{T}}, ::Type{T}) where T at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Utilities\functions.jl:2059
  is_coefficient_type(::Type{var"#s280"} where var"#s280"<:(Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}, MathOptInterface.VectorAffineFunction{T}, MathOptInterface.VectorQuadraticFunction{T}} where T), ::Type) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Utilities\functions.jl:2060
Stacktrace:
 [1] supports_constraint(::Type{MathOptInterface.Bridges.Constraint.ScalarizeBridge{Float64,F,S} where S where F}, ::Type{VectorAffineDecisionFunction{Float64}}, ::Type{MathOptInterface.Nonnegatives}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\Constraint\scalarize.jl:31
 [2] node(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Type{VectorAffineDecisionFunction{Float64}}, ::Type{MathOptInterface.Nonnegatives}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:168
 [3] edge(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Int64, ::Type{MathOptInterface.Bridges.Constraint.NonposToNonnegBridge{Float64,VectorAffineDecisionFunction{Float64},VectorAffineDecisionFunction{Float64}}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:94
 [4] node(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Type{VectorAffineDecisionFunction{Float64}}, ::Type{MathOptInterface.Nonpositives}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:169
 [5] edge(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Int64, ::Type{MathOptInterface.Bridges.Constraint.VectorizeBridge{Float64,VectorAffineDecisionFunction{Float64},MathOptInterface.Nonpositives,AffineDecisionFunction{Float64}}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:94
 [6] node(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Type{AffineDecisionFunction{Float64}}, ::Type{MathOptInterface.LessThan{Float64}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:169
 [7] edge(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Int64, ::Type{MathOptInterface.Bridges.Constraint.GreaterToLessBridge{Float64,AffineDecisionFunction{Float64},AffineDecisionFunction{Float64}}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:94
 [8] node(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Type{AffineDecisionFunction{Float64}}, ::Type{MathOptInterface.GreaterThan{Float64}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:169
 [9] edge(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Int64, ::Type{MathOptInterface.Bridges.Objective.SlackBridge{Float64,AffineDecisionFunction{Float64},AffineDecisionFunction{Float64}}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:101
 [10] node(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::Type{AffineDecisionFunction{Float64}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:190
 [11] bridge_index at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:277 [inlined]
 [12] supports_bridging_objective_function at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\lazy_bridge_optimizer.jl:299 [inlined]
 [13] supports(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::MathOptInterface.ObjectiveFunction{AffineDecisionFunction{Float64}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Bridges\bridge_optimizer.jl:670
 [14] supports(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}, ::MathOptInterface.ObjectiveFunction{AffineDecisionFunction{Float64}}) at C:\Users\PC\.julia\packages\MathOptInterface\VjkSQ\src\Utilities\cachingoptimizer.jl:572
 [15] set_objective_function(::Model, ::AffineDecisionFunction{Float64}) at C:\Users\PC\.julia\packages\JuMP\e0Uc2\src\objective.jl:95
 [16] set_objective_function at C:\Users\PC\.julia\packages\JuMP\e0Uc2\src\objective.jl:110 [inlined]
 [17] set_objective at C:\Users\PC\.julia\packages\JuMP\e0Uc2\src\objective.jl:128 [inlined]
 [18] macro expansion at C:\Users\PC\.julia\packages\JuMP\e0Uc2\src\macros.jl:831 [inlined]
 [19] #3 at .\REPL[4]:5 [inlined]
 [20] (::var"#3#8")(::Model, ::NamedTuple{(),Tuple{}}) at .\none:0
 [21] generate!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(),Tuple{}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}}, ::DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}, ::Int64) at C:\Users\PC\.julia\packages\StochasticPrograms\yFtc7\src\methods\deterministic_equivalent\generation.jl:28
 [22] generate!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(),Tuple{}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}}, ::DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}) at C:\Users\PC\.julia\packages\StochasticPrograms\yFtc7\src\methods\deterministic_equivalent\generation.jl:11
 [23] generate!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(),Tuple{}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}}) at C:\Users\PC\.julia\packages\StochasticPrograms\yFtc7\src\methods\generation.jl:96
 [24] instantiate(::StochasticModel{2,Tuple{StageParameters{NamedTuple{(),Tuple{}}},StageParameters{NamedTuple{(),Tuple{}}}}}, ::Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}; instantiation::StochasticPrograms.UnspecifiedInstantiation, optimizer::Type{T} where T, defer::Bool, direct_model::Bool, kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\PC\.julia\packages\StochasticPrograms\yFtc7\src\methods\api.jl:37
 [25] top-level scope at REPL[7]:1

Edit: added a fenced code block

bigadolfo commented 3 years ago

Hello everyone, The same problem comes to me. I will be very grateful to anyone who can find the solution

Thanks,

martinbiel commented 3 years ago

Hey, StochasticPrograms seems incompatible with the latest MathOptInterface version released a few days ago. Until I have sorted that out, downgrading MOI should remove the error temporarily.

bigadolfo commented 3 years ago

Hello, you mean using MathOptInterface v0.9.17 ?, but that package I don't use. Besides StochasticPrograms, I use GLPK (v0.14.2) as an optimizer. Is GLPK using MathOptInterface??? , I must downgrade the GLPX package....

[c52e3926] Atom v0.12.25 [60bf3e95] GLPK v0.14.2 [e5e0dc1b] Juno v0.8.4 [b8f27783] MathOptInterface v0.9.17 [8b8459f2] StochasticPrograms v0.4.1

martinbiel commented 3 years ago

StochasticPrograms has MOI as a dependency, so you get it automatically. pgk> add StochasticPrograms#master and pkg> add MathOptInterface@0.9.17 should work as a temporary fix.

bigadolfo commented 3 years ago

Thanks , it works. We keep on waiting for the final solution

bigadolfo commented 3 years ago

In this example: https://martinbiel.github.io/StochasticPrograms.jl/dev/manual/examples/

What is the function to obtain the value of variables of tons bought and sold (y, w) -recourses var-? ... I think it is optimal_recourse_decision... but not work...

martinbiel commented 3 years ago

See #15 for a discussion about this.

gganetlepage commented 3 years ago

Hi, thank you, the trick works.

martinbiel commented 3 years ago

Keeping this open until I have fixed the bug.

TheoGuyard commented 3 years ago

Hello ! Seems that fixing MOI by downgrading it (v0.9.17 but also v0.9.16) doesn't work either. Thank you for your work and good luck to fix this !

(stochastic-kep) pkg> status
Status `~/Documents/INSA/5GM/stochastic-kep/Code/Project.toml`
  [60bf3e95] GLPK v0.14.2
  [4076af6c] JuMP v0.21.4
  [b8f27783] MathOptInterface v0.9.17
  [8b8459f2] StochasticPrograms v0.4.1

julia> using StochasticPrograms, GLPK

julia> simple_model = @stochastic_model begin
                  @stage 1 begin
                      @decision(model, x₁ >= 40)
                      @decision(model, x₂ >= 20)
                      @objective(model, Min, 100*x₁ + 150*x₂)
                      @constraint(model, x₁ + x₂ <= 120)
                  end
                  @stage 2 begin
                      @uncertain q₁ q₂ d₁ d₂
                      @variable(model, 0 <= y₁ <= d₁)
                      @variable(model, 0 <= y₂ <= d₂)
                      @objective(model, Max, q₁*y₁ + q₂*y₂)
                      @constraint(model, 6*y₁ + 10*y₂ <= 60*x₁)
                      @constraint(model, 8*y₁ + 5*y₂ <= 80*x₂)
                  end
              end
Two-Stage Stochastic Model

minimize f₀(x) + 𝔼[f(x,ξ)]
  x∈𝒳

where

f(x,ξ) = min  f(y; x, ξ)
              y ∈ 𝒴 (x, ξ)

julia> ξ₁ = Scenario(q₁ = 24.0, q₂ = 28.0, d₁ = 500.0, d₂ = 100.0, probability = 0.4)
Scenario with probability 0.4
  q₁: 24.0
  q₂: 28.0
  d₁: 500.0
  d₂: 100.0

julia> ξ₂ = Scenario(q₁ = 28.0, q₂ = 32.0, d₁ = 300.0, d₂ = 300.0, probability = 0.6)
Scenario with probability 0.6
  q₁: 28.0
  q₂: 32.0
  d₁: 300.0
  d₂: 300.0

julia> sp = instantiate(simple_model, [ξ₁, ξ₂], optimizer = GLPK.Optimizer)
ERROR: MethodError: no method matching copy(::StochasticPrograms.SingleDecisionSet{Float64})
Closest candidates are:
  copy(::Expr) at expr.jl:36
  copy(::Core.CodeInfo) at expr.jl:64
  copy(::BitSet) at bitset.jl:46
  ...
Stacktrace:
 [1] add_constraint(::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}, ::MathOptInterface.SingleVariable, ::StochasticPrograms.SingleDecisionSet{Float64}) at /Users/theoguyard/.julia/packages/MathOptInterface/8Mxwr/src/Utilities/universalfallback.jl:460
 [2] add_constrained_variable(::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}, ::StochasticPrograms.SingleDecisionSet{Float64}) at /Users/theoguyard/.julia/packages/MathOptInterface/8Mxwr/src/variables.jl:88
 [3] add_constrained_variable(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}, ::StochasticPrograms.SingleDecisionSet{Float64}) at /Users/theoguyard/.julia/packages/MathOptInterface/8Mxwr/src/Utilities/cachingoptimizer.jl:289
 [4] add_variable(::Model, ::VariableConstrainedOnCreation{StochasticPrograms.SingleDecisionSet{Float64},ScalarVariable{Int64,Float64,Float64,Float64}}, ::String) at /Users/theoguyard/.julia/packages/StochasticPrograms/g8Qnj/src/types/decisions/macros.jl:25
 [5] macro expansion at /Users/theoguyard/.julia/packages/JuMP/e0Uc2/src/macros.jl:91 [inlined]
 [6] (::var"#15#20")(::Model, ::NamedTuple{(),Tuple{}}) at ./PI�:0
 [7] generate!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(),Tuple{}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}}, ::DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}, ::Int64) at /Users/theoguyard/.julia/packages/StochasticPrograms/g8Qnj/src/methods/deterministic_equivalent/generation.jl:28
 [8] generate!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(),Tuple{}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}}, ::DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}) at /Users/theoguyard/.julia/packages/StochasticPrograms/g8Qnj/src/methods/deterministic_equivalent/generation.jl:11
 [9] generate!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(),Tuple{}}},StochasticPrograms.Stage{NamedTuple{(),Tuple{}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}}}}) at /Users/theoguyard/.julia/packages/StochasticPrograms/g8Qnj/src/methods/generation.jl:92
 [10] instantiate(::StochasticModel{2,Tuple{StageParameters{NamedTuple{(),Tuple{}}},StageParameters{NamedTuple{(),Tuple{}}}}}, ::Array{Scenario{NamedTuple{(:q₁, :q₂, :d₁, :d₂),NTuple{4,Float64}}},1}; instantiation::StochasticPrograms.UnspecifiedInstantiation, optimizer::Type{T} where T, defer::Bool, direct_model::Bool, kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/theoguyard/.julia/packages/StochasticPrograms/g8Qnj/src/methods/api.jl:37
 [11] top-level scope at REPL[15]:1
martinbiel commented 3 years ago

You need to be on the master branch as well. pkg> add StochasticPrograms#master

martinbiel commented 3 years ago

Fixed on master.

Lessvn commented 3 years ago

Hi, Seems the problem still exists as I am getting the same error, for the same toy problem:

using StochasticPrograms using GLPK using Ipopt

simple_model = @stochastic_model begin @stage 1 begin @decision(model, x₁ >= 40) @decision(model, x₂ >= 20) @objective(model, Min, 100x₁ + 150x₂) @constraint(model, x₁ + x₂ <= 120) end @stage 2 begin @uncertain q₁ q₂ d₁ d₂ @variable(model, 0 <= y₁ <= d₁) @variable(model, 0 <= y₂ <= d₂) @objective(model, Max, q₁y₁ + q₂y₂) @constraint(model, 6y₁ + 10y₂ <= 60x₁) @constraint(model, 8y₁ + 5y₂ <= 80x₂) end end

ξ₁ = Scenario(q₁ = 24.0, q₂ = 28.0, d₁ = 500.0, d₂ = 100.0, probability = 0.4) ξ₂ = Scenario(q₁ = 28.0, q₂ = 32.0, d₁ = 300.0, d₂ = 300.0, probability = 0.6) sp = instantiate(simple_model, [ξ₁, ξ₂], optimizer = GLPK.Optimizer)

I have downgraded the MathOptInterface and use StochasticPrograms v0.4.1 #master as you can see below:

(SAA) pkg> status Status C:\Users\Less\Julia\SAA\Project.toml [60bf3e95] GLPK v0.14.2 [b6b21f68] Ipopt v0.6.3 [4076af6c] JuMP v0.21.4 [b8f27783] MathOptInterface v0.9.17 [8b8459f2] StochasticPrograms v0.4.1 #master (https://github.com/martinbiel/StochasticPrograms.jl.git)

julia> versioninfo() Julia Version 1.2.0 Commit c6da87ff4b (2019-08-20 00:03 UTC) Platform Info: OS: Windows (x86_64-w64-mingw32)

Any idea, why this happens?

Thank you

martinbiel commented 3 years ago

With the latest fix, downgrading MOI should no longer be necessary. Maybe you need to re-add master to get the latest commits? I only test on 1.0 and 1.5 as well, so your Julia version might matter. What exact error are you getting?