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, Testfunction 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 ω fix(ξ, ω) return 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 returnendstock_example()-------------------------------------------------------------------
SDDP.jl (c) Oscar Dowson and contributors, 2017-26
-------------------------------------------------------------------
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.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.536222e+00 -1.474689e+00 7.528019e-02 1050 1
20 -1.246103e+00 -1.471565e+00 1.151230e-01 1600 1
30 -1.641016e+00 -1.471360e+00 2.018671e-01 2650 1
40 -1.491526e+00 -1.471206e+00 2.449582e-01 3200 1
50 -1.438375e+00 -1.471117e+00 3.366332e-01 4250 1
60 -1.655987e+00 -1.471077e+00 3.819201e-01 4800 1
70 -1.331043e+00 -1.471077e+00 4.752121e-01 5850 1
80 -1.656557e+00 -1.471075e+00 5.224581e-01 6400 1
81 -1.438375e+00 -1.471075e+00 5.273440e-01 6455 1
-------------------------------------------------------------------
status : simulation_stopping
total time (s) : 5.273440e-01
total solves : 6455
best bound : -1.471075e+00
numeric issues : 0
-------------------------------------------------------------------