# Two stage stochastic

**URL:** https://discourse.julialang.org/t/two-stage-stochastic/78767
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [March 30, 2022, 8:07pm UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767 "2022-03-30T20:07:07Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![HosseinF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hosseinf/32/35059_2.png) [@HosseinF](https://discourse.julialang.org/u/HosseinF)
#### Post date: [March 30, 2022, 8:07pm UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/1 "2022-03-30T20:07:07Z")

</div>

Hello every one. I am new to Julia. I want to extract the second stage decisional variables but it does not work. Does every one know that what is the problem?

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 30, 2022, 9:25pm UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/2 "2022-03-30T21:25:55Z")

</div>

Hi,

no, I don’t. If you could describe your problem in more detail or even provide a M(inimal)W(orking)E(xample) that would probably help.

All the best,

---

<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: [March 30, 2022, 10:34pm UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/3 "2022-03-30T22:34:27Z")

</div>

Hi @HosseinF

Take a read of: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

Are you using StochasticPrograms.jl?

You can access the value of decision variables in the second stage using `value(variable, scenario)`, for example, `value(y, 2)` will access the value of variable `y` in scenario `2`.

Here’s the documentation: [Decision API · StochasticPrograms.jl](https://martinbiel.github.io/StochasticPrograms.jl/stable/manual/decisions/#Solved-problem)

---

<div class="post-metadata">

### Author: ![HosseinF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hosseinf/32/35059_2.png) [@HosseinF](https://discourse.julialang.org/u/HosseinF)
#### Post date: [March 31, 2022, 3:34am UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/4 "2022-03-31T03:34:25Z")

</div>

Hi @odow @goerch ,

Thanks for your prompt response.

This is the code (borrowed from a website as a sample code):

```julia-auto
using Ipopt
using StochasticPrograms

@stochastic_model simple_model begin
    @stage 1 begin
        @decision(simple_model, x1 >= 40)
        @decision(simple_model, x2 >= 20)
        @objective(simple_model, Min, 100*x1 + 150*x2)
        @constraint(simple_model, x1+x2 <= 120)
    end
    @stage 2 begin
        @uncertain q1 q2 d1 d2
        @recourse(simple_model, 0 <= y1 <= d1)
        @recourse(simple_model, 0 <= y2 <= d2)
        @objective(simple_model, Max, q1*y1 + q2*y2)
        @constraint(simple_model, 6*y1 + 10*y2 <= 60*x1)
        @constraint(simple_model, 8*y1 + 5*y2 <= 80*x2)
    end
end;
# Create two scenarios
ξ1 = @scenario q1 = 24.0 q2 = 28.0 d1 = 500.0 d2 = 100.0 probability = 0.4;
ξ2 = @scenario q1 = 28.0 q2 = 32.0 d1 = 300.0 d2 = 300.0 probability = 0.6;
# Instantiate without optimizer
sp = instantiate(simple_model, [ξ1, ξ2], optimizer = Ipopt.Optimizer)
print(sp)
set_optimizer(sp, Ipopt.Optimizer)
optimize!(sp)

# Check termination status
print(termination_status(sp));
print(objective_value(sp));

```

After optimizing the model, I tried to find the optimum results with the help of following commands. The values of the first stage are properly shown but for the second stage, no.

# To print the stage 1 decisional variables (it works for stage 1)

`print("\n params x1, x2:" , value.(all_decision_variables(sp, 1)))`

# To print the stage 2 decisional variables, the following code shows error:

`print("\n params y1, y2:" , value.(all_decision_variables(sp, 2)))`

# I faced to this error:

ERROR: LoadError: y1 is scenario dependent, consider `value(dvar, scenario_index)`.

# I also just tried _value(variable, scenario)_ as:

`value(y1, 2)`

**however I faced to this error** :  
ERROR: LoadError: UndefVarError: y1 not defined

---

<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: [March 31, 2022, 3:46am UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/5 "2022-03-31T03:46:34Z")

</div>

Reading through the documentation, [Decision API · StochasticPrograms.jl](https://martinbiel.github.io/StochasticPrograms.jl/stable/manual/decisions), I think you need

```julia
y1 = sp[2, :y1] # get y1 variable from stage 2
value(y1, 2) # value of y1 in scenario 2

```

---

<div class="post-metadata">

### Author: ![HosseinF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hosseinf/32/35059_2.png) [@HosseinF](https://discourse.julialang.org/u/HosseinF)
#### Post date: [March 31, 2022, 5:12am UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/6 "2022-03-31T05:12:55Z")

</div>

@odow  
Many thanks! It works!

---

<div class="post-metadata">

### Author: ![HosseinF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hosseinf/32/35059_2.png) [@HosseinF](https://discourse.julialang.org/u/HosseinF)
#### Post date: [May 5, 2022, 10:44pm UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/7 "2022-05-05T22:44:20Z")

</div>

@odow  
Hi odow,

Regarding the expressions determined in two-stage stochastic models, I couldn’t access them using a command like simple\_model[2,:exp1]. Is there any way to access the value of these kinds of variables?

For example, we can consider the following sample code. So, how can we access exp1 values after optimization process done?

```julia
using Ipopt
using StochasticPrograms

@stochastic_model simple_model begin
    @stage 1 begin
        @decision(simple_model, x1 >= 40)
        @decision(simple_model, x2 >= 20)
        @objective(simple_model, Min, 100*x1 + 150*x2)
        @constraint(simple_model, x1+x2 <= 120)
    end
    @stage 2 begin
        @uncertain q1 q2 d1 d2
        @recourse(simple_model, y1[t in 1:10] <= d1)
        @recourse(simple_model, y2[t in 1:10] <= d2)
        @expression(simple_model, exp1[t in 1:10], y1[t] + y2[t])
        @objective(simple_model, Max, sum(q1*y1[t] + q2*y2[t] for t in 1:10)
        @constraint(simple_model, cons1[t in 1:10], 6*y1[t] + 10*y2[t] <= 60*x1)
        @constraint(simple_model, cons2[t in 1:10], 8*y1[t] + 5*y2[t] <= 80*x2)
    end
end;

```

---

<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: [May 5, 2022, 10:53pm UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/8 "2022-05-05T22:53:58Z")

</div>

Does something like `simple_model[2][:exp1`] work?

I haven’t really used StochasticPrograms, so I won’t be much help beyond what is in the documentation: [Home · StochasticPrograms.jl](https://martinbiel.github.io/StochasticPrograms.jl/stable/)

---

<div class="post-metadata">

### Author: ![HosseinF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hosseinf/32/35059_2.png) [@HosseinF](https://discourse.julialang.org/u/HosseinF)
#### Post date: [May 6, 2022, 1:59pm UTC](https://discourse.julialang.org/t/two-stage-stochastic/78767/9 "2022-05-06T13:59:42Z")

</div>

Thanks.

I forgot to bring the error I faced when I used `simple_model[2,:exp1]`:

```julia
LoadError: Only decisions and decision constraints can be queried using this syntax. For regular variables and constraints, either annotate the relevant variable with @decision or first query the relevant JuMP subproblem and use the regular `[]` syntax.

```

I checked and see that `simple_model[2][:exp1] ` also has this error:

```julia
LoadError: MethodError: no method matching getindex(::StochasticProgram{2, Tuple{StochasticPrograms.Stage{NamedTuple{(:C_R, :C_B, :y_bar_B, :y_bar_R), NTuple{4, Int64}}}, StochasticPrograms.Stage{NamedTuple{(:time_slots, :C, :times, :SOC_min, :SOC_max, :Bat_Cap, :max_import_power, :Ro, :Etha_c_n, :Etha_d_n, :Delta_T, :M), Tuple{Int64, Dict{Int64, Float64}, UnitRange{Int64}, Float64, Int64, Int64, Int64, Int64, Int64, Int64, Float64, Int64}}}}, DeterministicEquivalent{2, 1, Tuple{Vector{Scenario{JuMP.Containers.DenseAxisArray{Float64, 2, Tuple{Vector{Symbol}, Base.OneTo{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}, JuMP.Containers._AxisLookup{Base.OneTo{Int64}}}}}}}}}, ::Int64)

```

Also, I see that `@expression` is one of JuMP keywords. But, I didn’t find suitable details of how accessing an expression variable in its documentation:  
[JuMP](https://jump.dev/JuMP.jl/stable/)
