StochDynamicProgramming: the stock problem

This tutorial was generated using Literate.jl. Download the source as a .jl file. Download the source as a .ipynb file.

This example comes from StochDynamicProgramming.jl.

using SDDP, HiGHS, Test

function stock_example()
    model = SDDP.PolicyGraph(
        SDDP.LinearGraph(5);
        lower_bound = -2,
        optimizer = HiGHS.Optimizer,
    ) do sp, stage
        @variable(sp, 0 <= state <= 1, SDDP.State, initial_value = 0.5)
        @variable(sp, 0 <= control <= 0.5)
        @variable(sp, ξ)
        @constraint(sp, state.out == state.in - control + ξ)
        SDDP.parameterize(sp, 0.0:1/30:0.3) do ω
            return JuMP.fix(ξ, ω)
        end
        @stageobjective(sp, (sin(3 * stage) - 1) * control)
    end
    SDDP.train(model; log_frequency = 10)
    @test SDDP.calculate_bound(model) ≈ -1.471 atol = 0.001
    simulation_results = SDDP.simulate(model, 1_000)
    @test length(simulation_results) == 1_000
    μ = SDDP.Statistics.mean(
        sum(data[:stage_objective] for data in simulation) for
        simulation in simulation_results
    )
    @test μ ≈ -1.471 atol = 0.05
    return
end

stock_example()
-------------------------------------------------------------------
         SDDP.jl (c) Oscar Dowson and contributors, 2017-24
-------------------------------------------------------------------
problem
  nodes           : 5
  state variables : 1
  scenarios       : 1.00000e+05
  existing cuts   : false
options
  solver          : serial mode
  risk measure    : SDDP.Expectation()
  sampling scheme : SDDP.InSampleMonteCarlo
subproblem structure
  VariableRef                             : [5, 5]
  AffExpr in MOI.EqualTo{Float64}         : [1, 1]
  VariableRef in MOI.EqualTo{Float64}     : [1, 1]
  VariableRef in MOI.GreaterThan{Float64} : [3, 3]
  VariableRef in MOI.LessThan{Float64}    : [2, 3]
numerical stability report
  matrix range     [1e+00, 1e+00]
  objective range  [3e-01, 2e+00]
  bounds range     [5e-01, 2e+00]
  rhs range        [0e+00, 0e+00]
-------------------------------------------------------------------
 iteration    simulation      bound        time (s)     solves  pid
-------------------------------------------------------------------
        10  -1.442965e+00 -1.475770e+00  6.435084e-02      1050   1
        20  -1.559536e+00 -1.471842e+00  1.003690e-01      1600   1
        30  -1.633260e+00 -1.471526e+00  1.785779e-01      2650   1
        40  -1.512908e+00 -1.471277e+00  2.178040e-01      3200   1
        50  -1.504160e+00 -1.471172e+00  3.005619e-01      4250   1
        60  -1.352909e+00 -1.471159e+00  3.433371e-01      4800   1
        70  -1.520847e+00 -1.471079e+00  4.289780e-01      5850   1
        80  -1.340949e+00 -1.471075e+00  4.746630e-01      6400   1
        90  -1.260376e+00 -1.471075e+00  5.632350e-01      7450   1
       100  -1.573293e+00 -1.471075e+00  6.120729e-01      8000   1
       110  -1.364263e+00 -1.471075e+00  6.621048e-01      8550   1
       120  -1.572199e+00 -1.471075e+00  7.135398e-01      9100   1
       130  -1.548012e+00 -1.471075e+00  7.654879e-01      9650   1
       140  -1.462561e+00 -1.471075e+00  8.176889e-01     10200   1
       150  -1.154072e+00 -1.471075e+00  8.725500e-01     10750   1
       160  -1.512908e+00 -1.471075e+00  9.286950e-01     11300   1
       170  -1.486600e+00 -1.471075e+00  9.857719e-01     11850   1
       180  -1.405274e+00 -1.471075e+00  1.040097e+00     12400   1
       187  -1.281632e+00 -1.471075e+00  1.121772e+00     12785   1
-------------------------------------------------------------------
status         : simulation_stopping
total time (s) : 1.121772e+00
total solves   : 12785
best bound     : -1.471075e+00
simulation ci  : -1.474018e+00 ± 2.599111e-02
numeric issues : 0
-------------------------------------------------------------------