SLDP: example 2

This example is derived from Section 4.3 of the paper: Ahmed, S., Cabral, F. G., & da Costa, B. F. P. (2019). Stochastic Lipschitz Dynamic Programming. Optimization Online. PDF

using SDDP
import HiGHS
import Test

function sldp_example_two(; first_stage_integer::Bool = true, N = 2)
    model = SDDP.LinearPolicyGraph(
        stages = 2,
        lower_bound = -100.0,
        optimizer = HiGHS.Optimizer,
    ) do sp, t
        @variable(sp, 0 <= x[1:2] <= 5, SDDP.State, initial_value = 0.0)
        if t == 1
            if first_stage_integer
                @variable(sp, 0 <= u[1:2] <= 5, Int)
                @constraint(sp, [i = 1:2], u[i] == x[i].out)
            end
            @stageobjective(sp, -1.5 * x[1].out - 4 * x[2].out)
        else
            @variable(sp, 0 <= y[1:4] <= 1, Bin)
            @variable(sp, ω[1:2])
            @stageobjective(sp, -16 * y[1] - 19 * y[2] - 23 * y[3] - 28 * y[4])
            @constraint(
                sp,
                2 * y[1] + 3 * y[2] + 4 * y[3] + 5 * y[4] <= ω[1] - x[1].in
            )
            @constraint(
                sp,
                6 * y[1] + 1 * y[2] + 3 * y[3] + 2 * y[4] <= ω[2] - x[2].in
            )
            steps = range(5, stop = 15, length = N)
            SDDP.parameterize(sp, [[i, j] for i in steps for j in steps]) do φ
                return JuMP.fix.(ω, φ)
            end
        end
    end
    if get(ARGS, 1, "") == "--write"
        # Run `$ julia sldp_example_two.jl --write` to update the benchmark
        # model directory
        model_dir = joinpath(@__DIR__, "..", "..", "..", "benchmarks", "models")
        SDDP.write_to_file(
            model,
            joinpath(model_dir, "sldp_example_two_$(N).sof.json.gz");
            test_scenarios = 30,
        )
        return
    end
    SDDP.train(model, iteration_limit = 100, log_frequency = 10)
    bound = SDDP.calculate_bound(model)

    if N == 2
        Test.@test bound <= -57.0
    elseif N == 3
        Test.@test bound <= -59.33
    elseif N == 6
        Test.@test bound <= -61.22
    end
    return
end

sldp_example_two(N = 2)
sldp_example_two(N = 3)
sldp_example_two(N = 6)
------------------------------------------------------------------------------
          SDDP.jl (c) Oscar Dowson and SDDP.jl contributors, 2017-23

Problem
  Nodes           : 2
  State variables : 2
  Scenarios       : 4.00000e+00
  Existing cuts   : false
  Subproblem structure                      : (min, max)
    Variables                               : (7, 11)
    VariableRef in MOI.LessThan{Float64}    : (4, 7)
    VariableRef in MOI.ZeroOne              : (4, 4)
    AffExpr in MOI.LessThan{Float64}        : (2, 2)
    VariableRef in MOI.GreaterThan{Float64} : (5, 7)
    AffExpr in MOI.EqualTo{Float64}         : (2, 2)
    VariableRef in MOI.Integer              : (2, 2)
Options
  Solver          : serial mode
  Risk measure    : SDDP.Expectation()
  Sampling scheme : SDDP.InSampleMonteCarlo

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

 Iteration    Simulation       Bound         Time (s)    Proc. ID   # Solves
       10   -9.800000e+01  -5.809615e+01   4.629993e-02          1         70
       20   -4.000000e+01  -5.809615e+01   1.005969e-01          1        140
       30   -4.700000e+01  -5.809615e+01   1.564970e-01          1        210
       40   -4.700000e+01  -5.809615e+01   2.125411e-01          1        280
       50   -9.800000e+01  -5.809615e+01   2.696650e-01          1        350
       60   -4.700000e+01  -5.809615e+01   3.276501e-01          1        420
       70   -4.000000e+01  -5.809615e+01   3.872130e-01          1        490
       80   -9.800000e+01  -5.809615e+01   4.471290e-01          1        560
       90   -4.700000e+01  -5.809615e+01   5.095720e-01          1        630
      100   -4.000000e+01  -5.809615e+01   5.715380e-01          1        700

Terminating training
  Status         : iteration_limit
  Total time (s) : 5.715380e-01
  Total solves   : 700
  Best bound     : -5.809615e+01
  Simulation CI  : -5.775500e+01 ± 5.020600e+00
------------------------------------------------------------------------------
------------------------------------------------------------------------------
          SDDP.jl (c) Oscar Dowson and SDDP.jl contributors, 2017-23

Problem
  Nodes           : 2
  State variables : 2
  Scenarios       : 9.00000e+00
  Existing cuts   : false
  Subproblem structure                      : (min, max)
    Variables                               : (7, 11)
    VariableRef in MOI.LessThan{Float64}    : (4, 7)
    VariableRef in MOI.ZeroOne              : (4, 4)
    AffExpr in MOI.LessThan{Float64}        : (2, 2)
    VariableRef in MOI.GreaterThan{Float64} : (5, 7)
    AffExpr in MOI.EqualTo{Float64}         : (2, 2)
    VariableRef in MOI.Integer              : (2, 2)
Options
  Solver          : serial mode
  Risk measure    : SDDP.Expectation()
  Sampling scheme : SDDP.InSampleMonteCarlo

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

 Iteration    Simulation       Bound         Time (s)    Proc. ID   # Solves
       10   -9.800000e+01  -6.196125e+01   5.038500e-02          1        120
       20   -4.000000e+01  -6.196125e+01   1.033340e-01          1        240
       30   -9.800000e+01  -6.196125e+01   1.574709e-01          1        360
       40   -6.300000e+01  -6.196125e+01   2.139759e-01          1        480
       50   -4.000000e+01  -6.196125e+01   2.703199e-01          1        600
       60   -4.700000e+01  -6.196125e+01   3.291941e-01          1        720
       70   -4.000000e+01  -6.196125e+01   3.899620e-01          1        840
       80   -7.500000e+01  -6.196125e+01   4.518461e-01          1        960
       90   -4.700000e+01  -6.196125e+01   5.157990e-01          1       1080
      100   -4.700000e+01  -6.196125e+01   6.279740e-01          1       1200

Terminating training
  Status         : iteration_limit
  Total time (s) : 6.279740e-01
  Total solves   : 1200
  Best bound     : -6.196125e+01
  Simulation CI  : -5.872500e+01 ± 4.056491e+00
------------------------------------------------------------------------------
------------------------------------------------------------------------------
          SDDP.jl (c) Oscar Dowson and SDDP.jl contributors, 2017-23

Problem
  Nodes           : 2
  State variables : 2
  Scenarios       : 3.60000e+01
  Existing cuts   : false
  Subproblem structure                      : (min, max)
    Variables                               : (7, 11)
    VariableRef in MOI.LessThan{Float64}    : (4, 7)
    VariableRef in MOI.ZeroOne              : (4, 4)
    AffExpr in MOI.LessThan{Float64}        : (2, 2)
    VariableRef in MOI.GreaterThan{Float64} : (5, 7)
    AffExpr in MOI.EqualTo{Float64}         : (2, 2)
    VariableRef in MOI.Integer              : (2, 2)
Options
  Solver          : serial mode
  Risk measure    : SDDP.Expectation()
  Sampling scheme : SDDP.InSampleMonteCarlo

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

 Iteration    Simulation       Bound         Time (s)    Proc. ID   # Solves
       10   -5.900000e+01  -6.546793e+01   7.119417e-02          1        390
       20   -6.300000e+01  -6.546793e+01   1.438541e-01          1        780
       30   -5.900000e+01  -6.546793e+01   2.163682e-01          1       1170
       40   -8.200000e+01  -6.546793e+01   2.891581e-01          1       1560
       50   -5.900000e+01  -6.546793e+01   3.640802e-01          1       1950
       60   -7.900000e+01  -6.546793e+01   4.386451e-01          1       2340
       70   -7.500000e+01  -6.546793e+01   5.136170e-01          1       2730
       80   -5.400000e+01  -6.546793e+01   5.905762e-01          1       3120
       90   -7.900000e+01  -6.546793e+01   6.679041e-01          1       3510
      100   -6.300000e+01  -6.546793e+01   7.477801e-01          1       3900

Terminating training
  Status         : iteration_limit
  Total time (s) : 7.477801e-01
  Total solves   : 3900
  Best bound     : -6.546793e+01
  Simulation CI  : -6.135500e+01 ± 3.087406e+00
------------------------------------------------------------------------------

This page was generated using Literate.jl.