Alternative forward models

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

This example demonstrates how to train convex and non-convex models.

This example uses the following packages:

using SDDP
import Ipopt
import PowerModels
import Test

Formulation

For our model, we build a simple optimal power flow model with a single hydro-electric generator.

The formulation of our optimal power flow problem depends on model_type, which must be one of the PowerModels formulations.

(To run locally, download pglib_opf_case5_pjm.m and update filename appropriately.)

function build_model(model_type)
    filename = joinpath(@__DIR__, "pglib_opf_case5_pjm.m")
    data = PowerModels.parse_file(filename)
    return SDDP.PolicyGraph(
        SDDP.UnicyclicGraph(0.95);
        sense = :Min,
        lower_bound = 0.0,
        optimizer = Ipopt.Optimizer,
    ) do sp, t
        power_model = PowerModels.instantiate_model(
            data,
            model_type,
            PowerModels.build_opf;
            jump_model = sp,
        )
        # Now add hydro power models. Assume that generator 5 is hydro, and the
        # rest are thermal.
        pg = power_model.var[:it][:pm][:nw][0][:pg][5]
        sp[:pg] = pg
        @variable(sp, x >= 0, SDDP.State, initial_value = 10.0)
        @variable(sp, deficit >= 0)
        @constraint(sp, balance, x.out == x.in - pg + deficit)
        @stageobjective(sp, objective_function(sp) + 1e6 * deficit)
        SDDP.parameterize(sp, [0, 2, 5]) do ω
            return SDDP.set_normalized_rhs(balance, ω)
        end
        return
    end
end
build_model (generic function with 1 method)

Training a convex model

We can build and train a convex approximation of the optimal power flow problem.

The problem with the convex model is that it does not accurately simulate the true dynamics of the problem. Therefore, it under-estimates the true cost of operation.

convex = build_model(PowerModels.DCPPowerModel)
SDDP.train(convex; iteration_limit = 10)
-------------------------------------------------------------------
         SDDP.jl (c) Oscar Dowson and contributors, 2017-24
-------------------------------------------------------------------
problem
  nodes           : 1
  state variables : 1
  scenarios       : Inf
  existing cuts   : false
options
  solver          : serial mode
  risk measure    : SDDP.Expectation()
  sampling scheme : SDDP.InSampleMonteCarlo
subproblem structure
  VariableRef                             : [20, 20]
  AffExpr in MOI.EqualTo{Float64}         : [13, 13]
  AffExpr in MOI.Interval{Float64}        : [6, 6]
  VariableRef in MOI.GreaterThan{Float64} : [14, 14]
  VariableRef in MOI.LessThan{Float64}    : [11, 11]
numerical stability report
  matrix range     [1e+00, 2e+02]
  objective range  [1e+00, 1e+06]
  bounds range     [4e-01, 6e+00]
  rhs range        [5e-01, 5e+00]
-------------------------------------------------------------------
 iteration    simulation      bound        time (s)     solves  pid
-------------------------------------------------------------------
         1   2.499385e+06  3.431525e+04  2.968891e-01        79   1
         7   4.194010e+05  3.314696e+05  1.645088e+00       309   1
        10   9.355757e+05  4.032218e+05  3.054426e+00       522   1
-------------------------------------------------------------------
status         : iteration_limit
total time (s) : 3.054426e+00
total solves   : 522
best bound     :  4.032218e+05
simulation ci  :  4.781635e+05 ± 4.726264e+05
numeric issues : 0
-------------------------------------------------------------------

To more accurately simulate the dynamics of the problem, a common approach is to write the cuts representing the policy to a file, and then read them into a non-convex model:

SDDP.write_cuts_to_file(convex, "convex.cuts.json")
non_convex = build_model(PowerModels.ACPPowerModel)
SDDP.read_cuts_from_file(non_convex, "convex.cuts.json")

Now we can simulate non_convex to evaluate the policy.

result = SDDP.simulate(non_convex, 1)
1-element Vector{Vector{Dict{Symbol, Any}}}:
 [Dict(:bellman_term => 386311.14891475544, :noise_term => 2, :node_index => 1, :stage_objective => 17580.8140470822, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 385783.9020469458, :noise_term => 5, :node_index => 1, :stage_objective => 17580.81404705669, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 390178.85291148606, :noise_term => 2, :node_index => 1, :stage_objective => 17580.814047386837, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.44370053074, :noise_term => 0, :node_index => 1, :stage_objective => 19041.639614536653, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.44379494566, :noise_term => 2, :node_index => 1, :stage_objective => 23580.69728424989, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 555443.0836042488, :noise_term => 0, :node_index => 1, :stage_objective => 27420.553515402946, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.4436988885, :noise_term => 5, :node_index => 1, :stage_objective => 18341.120853354514, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396105.19682454073, :noise_term => 5, :node_index => 1, :stage_objective => 17580.81405352389, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.4437948744, :noise_term => 2, :node_index => 1, :stage_objective => 22626.186122831416, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.44379494566, :noise_term => 2, :node_index => 1, :stage_objective => 23580.69745473333, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 555443.0836042488, :noise_term => 0, :node_index => 1, :stage_objective => 27420.553515402946, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 660451.0385834568, :noise_term => 0, :node_index => 1, :stage_objective => 417861.01048793254, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.443795172, :noise_term => 2, :node_index => 1, :stage_objective => 26648.751893737688, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396105.1969205994, :noise_term => 5, :node_index => 1, :stage_objective => 17580.814053523904, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 450033.07089778956, :noise_term => 0, :node_index => 1, :stage_objective => 27420.553515441443, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 660451.0385834568, :noise_term => 0, :node_index => 1, :stage_objective => 96512.55846087166, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.443795172, :noise_term => 2, :node_index => 1, :stage_objective => 26648.751893737688, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.44379494566, :noise_term => 2, :node_index => 1, :stage_objective => 23580.697455267167, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 555443.0836042488, :noise_term => 0, :node_index => 1, :stage_objective => 27420.553515402946, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 396632.4436988885, :noise_term => 5, :node_index => 1, :stage_objective => 18341.120853354514, :objective_state => nothing, :belief => Dict(1 => 1.0))]

A problem with reading and writing the cuts to file is that the cuts have been generated from trial points of the convex model. Therefore, the policy may be arbitrarily bad at points visited by the non-convex model.

Training a non-convex model

We can also build and train a non-convex formulation of the optimal power flow problem.

The problem with the non-convex model is that because it is non-convex, SDDP.jl may find a sub-optimal policy. Therefore, it may over-estimate the true cost of operation.

non_convex = build_model(PowerModels.ACPPowerModel)
SDDP.train(non_convex; iteration_limit = 10)
result = SDDP.simulate(non_convex, 1)
1-element Vector{Vector{Dict{Symbol, Any}}}:
 [Dict(:bellman_term => 401962.04070816084, :noise_term => 0, :node_index => 1, :stage_objective => 17578.228609457008, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 406060.69524468895, :noise_term => 2, :node_index => 1, :stage_objective => 17578.25344020066, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 423815.2900698862, :noise_term => 0, :node_index => 1, :stage_objective => 27420.553501798644, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 465120.2309484749, :noise_term => 0, :node_index => 1, :stage_objective => 27420.553505886604, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 406403.06224685337, :noise_term => 5, :node_index => 1, :stage_objective => 19308.50283455514, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 406403.06231587334, :noise_term => 2, :node_index => 1, :stage_objective => 23580.69732513869, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 405903.2631040996, :noise_term => 5, :node_index => 1, :stage_objective => 17578.253440169035, :objective_state => nothing, :belief => Dict(1 => 1.0))]

Combining convex and non-convex models

To summarize, training with the convex model constructs cuts at points that may never be visited by the non-convex model, and training with the non-convex model may construct arbitrarily poor cuts because a key assumption of SDDP is convexity.

As a compromise, we can train a policy using a combination of the convex and non-convex models; we'll use the non-convex model to generate trial points on the forward pass, and we'll use the convex model to build cuts on the backward pass.

convex = build_model(PowerModels.DCPPowerModel)
A policy graph with 1 nodes.
 Node indices: 1
non_convex = build_model(PowerModels.ACPPowerModel)
A policy graph with 1 nodes.
 Node indices: 1

To do so, we train convex using the SDDP.AlternativeForwardPass forward pass, which simulates the model using non_convex, and we use SDDP.AlternativePostIterationCallback as a post-iteration callback, which copies cuts from the convex model back into the non_convex model.

SDDP.train(
    convex;
    forward_pass = SDDP.AlternativeForwardPass(non_convex),
    post_iteration_callback = SDDP.AlternativePostIterationCallback(non_convex),
    iteration_limit = 10,
)
-------------------------------------------------------------------
         SDDP.jl (c) Oscar Dowson and contributors, 2017-24
-------------------------------------------------------------------
problem
  nodes           : 1
  state variables : 1
  scenarios       : Inf
  existing cuts   : false
options
  solver          : serial mode
  risk measure    : SDDP.Expectation()
  sampling scheme : SDDP.InSampleMonteCarlo
subproblem structure
  VariableRef                             : [20, 20]
  AffExpr in MOI.EqualTo{Float64}         : [13, 13]
  AffExpr in MOI.Interval{Float64}        : [6, 6]
  VariableRef in MOI.GreaterThan{Float64} : [14, 14]
  VariableRef in MOI.LessThan{Float64}    : [11, 11]
numerical stability report
  matrix range     [1e+00, 2e+02]
  objective range  [1e+00, 1e+06]
  bounds range     [4e-01, 6e+00]
  rhs range        [5e-01, 5e+00]
-------------------------------------------------------------------
 iteration    simulation      bound        time (s)     solves  pid
-------------------------------------------------------------------
         1   2.503297e+06  1.188134e+05  4.175172e-01        57   1
         2   2.730522e+06  1.760510e+05  2.071615e+00       219   1
         4   1.041981e+06  3.464967e+05  3.752242e+00       366   1
         6   1.802918e+06  3.593268e+05  6.556773e+00       654   1
         7   1.734476e+06  4.231736e+05  9.025089e+00       891   1
        10   4.283898e+05  4.234884e+05  1.112433e+01       999   1
-------------------------------------------------------------------
status         : iteration_limit
total time (s) : 1.112433e+01
total solves   : 999
best bound     :  4.234884e+05
simulation ci  :  1.196390e+06 ± 6.078298e+05
numeric issues : 0
-------------------------------------------------------------------

In practice, if we were to simulate non_convex now, we should obtain a better policy than either of the two previous approaches.