StochDynamicProgramming: the stock problem

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, iteration_limit = 50, 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 SDDP.jl contributors, 2017-23

Problem
  Nodes           : 5
  State variables : 1
  Scenarios       : 1.00000e+05
  Existing cuts   : false
  Subproblem structure                      : (min, max)
    Variables                               : (5, 5)
    VariableRef in MOI.LessThan{Float64}    : (2, 3)
    VariableRef in MOI.GreaterThan{Float64} : (3, 3)
    AffExpr in MOI.EqualTo{Float64}         : (1, 1)
Options
  Solver          : serial mode
  Risk measure    : SDDP.Expectation()
  Sampling scheme : SDDP.InSampleMonteCarlo

Numerical stability report
  Non-zero Matrix range     [1e+00, 1e+00]
  Non-zero Objective range  [3e-01, 2e+00]
  Non-zero Bounds range     [5e-01, 2e+00]
  Non-zero RHS range        [0e+00, 0e+00]
No problems detected

 Iteration    Simulation       Bound         Time (s)    Proc. ID   # Solves
       10   -1.472998e+00  -1.473983e+00   3.724003e-02          1        550
       20   -1.312622e+00  -1.472269e+00   7.758999e-02          1       1100
       30   -1.238687e+00  -1.471686e+00   1.218660e-01          1       1650
       40   -1.648086e+00  -1.471257e+00   1.654930e-01          1       2200
       50   -1.329903e+00  -1.471136e+00   2.117300e-01          1       2750

Terminating training
  Status         : iteration_limit
  Total time (s) : 2.117300e-01
  Total solves   : 2750
  Best bound     : -1.471136e+00
  Simulation CI  : -1.455143e+00 ± 5.523791e-02
------------------------------------------------------------------------------

This page was generated using Literate.jl.