Stochastic programming UndefVarError

I am looking at this package StochasticPrograms and I tried to run the farmer example given on the link below. I am getting an error message: “UndefVarError: @stochastic_model not defined”.

This line in the code may be causing the error:

farmer_model = @stochastic_model begin

Could this be due to that the syntax in the example is old and which has been deprecated? Is there a way to fix it or an updated example available?

Do you have using StochasticPrograms at the top of your file?

You can see the latest stochastic programs documentation here:

What type of stochastic programs are you trying to solve?

cc @martinbiel

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:
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
        @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)
    @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)
        @uncertain ξ::YieldScenario = begin
        @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)
ξ₁ = 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, [ξ₁,ξ₂,ξ₃])
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.

It looks like StochasticPrograms hasn’t been updated to the latest version of JuMP.

Open an issue on

As a work-around, you can install the correct versions as follows:

import Pkg
# 1) Create a new environment
# or some other path, e.g., "C:/Users/Oscar/Documents/StochasticPrograms"

# 2) Install packages in this order

# 3) 
using StochasticPrograms

# ...

Thank you