# Stochastic programming UndefVarError

**URL:** https://discourse.julialang.org/t/stochastic-programming-undefvarerror/34608
**Category:** Optimization (Mathematical)
**Created:** [February 13, 2020, 11:58pm UTC](https://discourse.julialang.org/t/stochastic-programming-undefvarerror/34608 "2020-02-13T23:58:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)
#### Post date: [February 13, 2020, 11:58pm UTC](https://discourse.julialang.org/t/stochastic-programming-undefvarerror/34608/1 "2020-02-13T23:58:38Z")

</div>

Hi,  
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:

```julia
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?

> <https://github.com/martinbiel/StochasticPrograms.jl/blob/master/docs/src/manual/examples.md>

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [February 14, 2020, 4:37pm UTC](https://discourse.julialang.org/t/stochastic-programming-undefvarerror/34608/2 "2020-02-14T16:37:17Z")

</div>

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

You can see the latest stochastic programs documentation here: [Examples · StochasticPrograms.jl](https://martinbiel.github.io/StochasticPrograms.jl/stable/manual/examples/)

What type of stochastic programs are you trying to solve?

cc @martinbiel

---

<div class="post-metadata">

### Author: ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)
#### Post date: [February 15, 2020, 12:44am UTC](https://discourse.julialang.org/t/stochastic-programming-undefvarerror/34608/3 "2020-02-15T00:44:26Z")

</div>

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.

```julia
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/](https://martinbiel.github.io/StochasticPrograms.jl/stable/manual/examples/)  
But, I still get “UndefVarError: @stochastic\_model not defined” error.

```julia
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.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [February 15, 2020, 4:40pm UTC](https://discourse.julialang.org/t/stochastic-programming-undefvarerror/34608/4 "2020-02-15T16:40:00Z")

</div>

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

Open an issue on [https://github.com/martinbiel/StochasticPrograms.jl](https://github.com/martinbiel/StochasticPrograms.jl).

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

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

# 2) Install packages in this order
Pkg.add("StochasticPrograms")
Pkg.add("JuMP")
Pkg.add("GLPKMathProgInterface")

# 3) 
using StochasticPrograms

# ...

```

---

<div class="post-metadata">

### Author: ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)
#### Post date: [February 15, 2020, 6:09pm UTC](https://discourse.julialang.org/t/stochastic-programming-undefvarerror/34608/5 "2020-02-15T18:09:02Z")

</div>

Thank you
