Two stage stochastic

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?

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,

1 Like

Hi @HosseinF

Take a read of: Please read: make it easier to help you.

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

1 Like

Hi @odow @goerch ,

Thanks for your prompt response.

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

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

Reading through the documentation, Decision API · StochasticPrograms.jl, I think you need

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

@odow
Many thanks! It works!

2 Likes

@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?

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;

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

1 Like

Thanks.

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

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:

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

1 Like