Yes, using StochasticPrograms
is at the top of the file.
I am using the latest documentation, but I think the example is using syntax which has been been deprecated, e.g.
using GLPKMathProgInterface
optimize!(farmer, solver = GLPKSolverLP())
I have given my code below, which is replaces these two lines in the farmer example:
https://martinbiel.github.io/StochasticPrograms.jl/stable/manual/examples/
But, I still get “UndefVarError: @stochastic_model not defined” error.
using JuMP
using GLPK
using StochasticPrograms
farmer_model = @stochastic_model begin
@stage 1 begin
@parameters begin
Crops = [:wheat, :corn, :beets]
Cost = Dict(:wheat=>150, :corn=>230, :beets=>260)
Budget = 500
end
@variable(model, x[c = Crops] >= 0)
@objective(model, Min, sum(Cost[c]*x[c] for c in Crops))
@constraint(model, sum(x[c] for c in Crops) <= Budget)
end
@stage 2 begin
@decision x
@parameters begin
Purchased = [:wheat, :corn]
Sold = [:wheat, :corn, :bquota, :bextra]
Required = Dict(:wheat=>200, :corn=>240, :beets=>0)
PurchasePrice = Dict(:wheat=>238, :corn=>210)
SellPrice = Dict(:wheat=>170, :corn=>150, :bquota=>36, :bextra=>10)
end
@uncertain ξ::YieldScenario = begin
wheat::Float64
corn::Float64
beets::Float64
end
@variable(model, y[p = Purchased] >= 0)
@variable(model, w[s = Sold] >= 0)
@objective(model, Min, sum( PurchasePrice[p] * y[p] for p = Purchased) - sum( SellPrice[s] * w[s] for s in Sold))
@constraint(model, const_minreq[p=Purchased],
ξ[p] * x[p] + y[p] - w[p] >= Required[p])
@constraint(model, const_minreq_beets,
ξ[:beets] * x[:beets] - w[:bquota] - w[:bextra] >= Required[:beets])
@constraint(model, const_aux, w[:bquota] <= 6000)
end
end
ξ₁ = YieldScenario(3.0, 3.6, 24.0, probability = 1/3)
ξ₂ = YieldScenario(2.5, 3.0, 20.0, probability = 1/3)
ξ₃ = YieldScenario(2.0, 2.4, 16.0, probability = 1/3)
farmer = instantiate(farmer_model, [ξ₁,ξ₂,ξ₃])
print(farmer)
optimize!(farmer,with_optimizer(GLPK.Optimizer))
x = optimal_decision(farmer, :x)
println("Wheat: $(x[:wheat])")
println("Corn: $(x[:corn])")
println("Beets: $(x[:beets])")
println("Profit: $(optimal_value(farmer))")
Currently, I am going through the examples of the various stochastic programs given. But I will be looking to solve two stage stochastic programming problems with uncertain variables both in the objective function and constraints.