martinbiel / StochasticPrograms.jl

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

Optimal value: 0.0 in all examples #4

Closed bandayachul closed 4 years ago

bandayachul commented 4 years ago

Hi, I have noticed that you have recently updated this packages. I have tried running the examples given here: https://github.com/martinbiel/StochasticPrograms.jl/blob/master/docs/src/manual/examples.md

For both examples, I get zeros as optimal values. Is this a bug in the package?

odow commented 4 years ago

I don't get zeros, but I do get an error:

julia> optimize!(farmer)
ERROR: UndefVarError: only not defined
Stacktrace:
 [1] substitute_variables(::Function, ::AffineDecisionFunction{Float64}) at /Users/oscar/.julia/packages/StochasticPrograms/t8mlY/src/types/decisions/functions/affine.jl:400
 [2] bridged_function(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::AffineDecisionFunction{Float64}) at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Bridges/bridge_optimizer.jl:1234
 [3] set(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::MathOptInterface.ObjectiveFunction{AffineDecisionFunction{Float64}}, ::AffineDecisionFunction{Float64}) at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Bridges/bridge_optimizer.jl:711
 [4] _pass_attributes(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}, ::Bool, ::MathOptInterface.Utilities.IndexMap, ::Array{MathOptInterface.AbstractModelAttribute,1}, ::Tuple{}, ::Tuple{}, ::Tuple{}, ::typeof(MathOptInterface.set)) at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Utilities/copy.jl:148
 [5] pass_attributes at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Utilities/copy.jl:112 [inlined]
 [6] pass_attributes at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Utilities/copy.jl:111 [inlined]
 [7] default_copy_to(::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}, ::Bool) at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Utilities/copy.jl:337
 [8] #automatic_copy_to#113 at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Utilities/copy.jl:15 [inlined]
 [9] #automatic_copy_to at ./none:0 [inlined]
 [10] #copy_to#3 at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Bridges/bridge_optimizer.jl:268 [inlined]
 [11] (::MathOptInterface.var"#kw##copy_to")(::NamedTuple{(:copy_names,),Tuple{Bool}}, ::typeof(MathOptInterface.copy_to), ::MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, ::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}) at ./none:0
 [12] attach_optimizer(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}) at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Utilities/cachingoptimizer.jl:149
 [13] optimize!(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}) at /Users/oscar/.julia/packages/MathOptInterface/bygN7/src/Utilities/cachingoptimizer.jl:185
 [14] optimize!(::DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:wheat, :corn, :beets),Tuple{Float64,Float64,Float64}}},1}}}, ::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}, ::Array{Float64,1}) at /Users/oscar/.julia/packages/StochasticPrograms/t8mlY/src/methods/deterministic_equivalent/optimization.jl:11
 [15] #optimize!#490(::StochasticPrograms.Crash.None, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(optimize!), ::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(:Crops, :Cost, :Budget),Tuple{Array{Symbol,1},Dict{Symbol,Int64},Int64}}},StochasticPrograms.Stage{NamedTuple{(:Crops, :Required, :PurchasePrice, :SellPrice),Tuple{Array{Symbol,1},Dict{Symbol,Int64},Dict{Symbol,Int64},Dict{Symbol,Int64}}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:wheat, :corn, :beets),Tuple{Float64,Float64,Float64}}},1}}}}) at /Users/oscar/.julia/packages/StochasticPrograms/t8mlY/src/methods/api.jl:209
 [16] optimize!(::StochasticProgram{2,Tuple{StochasticPrograms.Stage{NamedTuple{(:Crops, :Cost, :Budget),Tuple{Array{Symbol,1},Dict{Symbol,Int64},Int64}}},StochasticPrograms.Stage{NamedTuple{(:Crops, :Required, :PurchasePrice, :SellPrice),Tuple{Array{Symbol,1},Dict{Symbol,Int64},Dict{Symbol,Int64},Dict{Symbol,Int64}}}}},DeterministicEquivalent{2,1,Tuple{Array{Scenario{NamedTuple{(:wheat, :corn, :beets),Tuple{Float64,Float64,Float64}}},1}}}}) at /Users/oscar/.julia/packages/StochasticPrograms/t8mlY/src/methods/api.jl:201
 [17] top-level scope at REPL[14]:1

Edit: seems like this is a Julia 1.4 thing. So we should either fix or bump the combat: https://github.com/martinbiel/StochasticPrograms.jl/blob/0d9350c9c071eb6678e717e5e68a664f1f0a540f/Project.toml#L33

bandayachul commented 4 years ago

I don't get any errors but optimal values are always zero. The deterministic equivalent formulation is incorrect (reproduced below for the farmer problem).

Deterministic equivalent problem
Min 79.33333333333333 y₁[wheat] + 70 y₁[corn] - 56.666666666666664 w₁[wheat] - 50 w₁[corn] - 12 w₁[beets] - 3.333333333333333 w₁[extra_beets] + 79.33333333333333 y₂[wheat] + 70 y₂[corn] - 56.666666666666664 w₂[wheat] - 50 w₂[corn] - 12 w₂[beets] - 3.333333333333333 w₂[extra_beets] + 79.33333333333333 y₃[wheat] + 70 y₃[corn] - 56.666666666666664 w₃[wheat] - 50 w₃[corn] - 12 w₃[beets] - 3.333333333333333 w₃[extra_beets]
Subject to ....
odow commented 4 years ago

@martinbiel is on vacation, not sure when he gets back.

martinbiel commented 4 years ago

Hey,

I am aware of the compatibility issue, but did not have time to sort it out before going on vacation. I will push a hotfix for 1.0 when I get back.

What Julia version and OS do you get the zero objective value? Did you copy the example verbatim? I could not reproduce it on 1.4.

bandayachul commented 4 years ago

I have tried again just now and it seems to be working, but running the example I get Profit (objective value) of -108390 instead of 108390.

I am not sure why I was getting a zero objective value earlier, though I copied the example verbatim.

I am using Julia version 1.4.2 and Windows 10 OS.

martinbiel commented 4 years ago

In a minimize cost formulation of the farmer problem the optimal value will be negative, so this result is expected. If you convert it into maximization form the result will be positive.

martinbiel commented 4 years ago

The compatibility issues are now fixed. Closing this.