# Historical simulation in SDDP.jl

**URL:** https://discourse.julialang.org/t/historical-simulation-in-sddp-jl/109473
**Category:** Optimization (Mathematical)
**Tags:** question, sddp
**Created:** [January 30, 2024, 9:13pm UTC](https://discourse.julialang.org/t/historical-simulation-in-sddp-jl/109473 "2024-01-30T21:13:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Sorooshsa](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Sorooshsa](https://discourse.julialang.org/u/Sorooshsa)
#### Post date: [January 30, 2024, 9:13pm UTC](https://discourse.julialang.org/t/historical-simulation-in-sddp-jl/109473/1 "2024-01-30T21:13:13Z")

</div>

Hi,  
I did simulation in SDDP.jl using historical data. It works; however, I could not retrieve the optimal solutions anymore by simply putting [:variable] as an argument in SDDP.simulate.  
What should we do in historical simulation to obtain the optimal solution?

---

<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: [January 30, 2024, 9:14pm UTC](https://discourse.julialang.org/t/historical-simulation-in-sddp-jl/109473/2 "2024-01-30T21:14:21Z")

</div>

> I could not retrieve the optimal solutions anymore by simply putting [:variable] as an argument in SDDP.simulate

You should be able to.

Do you have a reproducible example?

---

<div class="post-metadata">

### Author: ![Sorooshsa](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Sorooshsa](https://discourse.julialang.org/u/Sorooshsa)
#### Post date: [January 30, 2024, 9:44pm UTC](https://discourse.julialang.org/t/historical-simulation-in-sddp-jl/109473/3 "2024-01-30T21:44:58Z")

</div>

```julia
using SDDP, HiGHS
T = Array{Float64,2}[[1.0]', [0.75 0.25], [0.75 0.25; 0.25 0.75]]
Ω = [
    (inflow = 0.0, fuel_multiplier = 1.5),
    (inflow = 50.0, fuel_multiplier = 1.0),
    (inflow = 100.0, fuel_multiplier = 0.75),
]

model = SDDP.MarkovianPolicyGraph(
    transition_matrices = Array{Float64,2}[
        [1.0]',
        [0.75 0.25],
        [0.75 0.25; 0.25 0.75],
    ],
    sense = :Min,
    lower_bound = 0.0,
    optimizer = HiGHS.Optimizer,
) do subproblem, node
    # Unpack the stage and Markov index.
    t, markov_state = node
    # Define the state variable.
    @variable(subproblem, 0 <= volume <= 200, SDDP.State, initial_value = 200)
    # Define the control variables.
    @variables(subproblem, begin
        thermal_generation >= 0
        hydro_generation >= 0
        hydro_spill >= 0
        inflow
    end)
    # Define the constraints
    @constraints(
        subproblem,
        begin
            volume.out == volume.in + inflow - hydro_generation - hydro_spill
            thermal_generation + hydro_generation == 150.0
        end
    )
    # Note how we can use `markov_state` to dispatch an `if` statement.
    probability = if markov_state == 1 # wet climate state
        [1 / 6, 1 / 3, 1 / 2]
    else # dry climate state
        [1 / 2, 1 / 3, 1 / 6]
    end

    fuel_cost = [50.0, 100.0, 150.0]
    SDDP.parameterize(subproblem, Ω, probability) do ω
        JuMP.fix(inflow, ω.inflow)
        @stageobjective(
            subproblem,
            ω.fuel_multiplier * fuel_cost[t] * thermal_generation
        )
    end
end

SDDP.train(model, time_limit = 10)

simulations = SDDP.simulate(
    model,
    sampling_scheme = SDDP.Historical([
        ((1, 1), Ω[1]),
        ((2, 2), Ω[3]),
        ((3, 1), Ω[2]),
    ]), [:hydro_spill])

```

I took this code from:

> **[Markovian policy graphs · SDDP.jl](https://sddp.dev/stable/tutorial/markov_uncertainty/#Markovian-policy-graphs)**
>
> Documentation for SDDP.jl.

, and just put [:hydro\_spill] at the end of the code. I just run it and got this error:

```julia
MethodError: no method matching simulate(::SDDP.PolicyGraph{Tuple{Int64, Int64}}, ::Vector{Symbol}; sampling_scheme::SDDP.Historical{Tuple{Int64, Int64}, NamedTuple{(:inflow, :fuel_multiplier), Tuple{Float64, Float64}}})

```

---

<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: [January 30, 2024, 10:01pm UTC](https://discourse.julialang.org/t/historical-simulation-in-sddp-jl/109473/4 "2024-01-30T22:01:33Z")

</div>

`sampling_scheme` is a keyword argument. You need to pass the required positional arguments first:

```julia
simulations = SDDP.simulate(
    model,
    1,
    [:hydro_spill];
    sampling_scheme = SDDP.Historical([
        ((1, 1), Ω[1]),
        ((2, 2), Ω[3]),
        ((3, 1), Ω[2]),
    ]),
)

```
