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   1.349689e+06  6.802521e+04  2.391739e-01        59   1
         2   1.849928e+06  1.515651e+05  1.433726e+00       266   1
         5   1.096400e+05  3.141242e+05  2.504071e+00       451   1
        10   3.495977e+04  3.999832e+05  3.346527e+00       574   1
-------------------------------------------------------------------
status         : iteration_limit
total time (s) : 3.346527e+00
total solves   : 574
best bound     :  3.999832e+05
simulation ci  :  4.853634e+05 ± 3.937107e+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 => 378710.70365788473, :noise_term => 5, :node_index => 1, :stage_objective => 17578.21759205396, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 382544.4675771784, :noise_term => 2, :node_index => 1, :stage_objective => 17578.21759205447, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 389238.9701674246, :noise_term => 0, :node_index => 1, :stage_objective => 17578.217592057757, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 388781.62608028186, :noise_term => 5, :node_index => 1, :stage_objective => 17578.21759205721, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 392568.42327117117, :noise_term => 0, :node_index => 1, :stage_objective => 21658.6235086594, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 460552.2533035918, :noise_term => 0, :node_index => 1, :stage_objective => 106456.0753034653, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 392568.4232713633, :noise_term => 2, :node_index => 1, :stage_objective => 25459.383040291836, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 460552.2533035918, :noise_term => 0, :node_index => 1, :stage_objective => 106456.07543104464, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 460552.25330359186, :noise_term => 0, :node_index => 1, :stage_objective => 737983.7565790084, :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 => 416719.068688981, :noise_term => 0, :node_index => 1, :stage_objective => 21328.778774669918, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 416074.89465132996, :noise_term => 5, :node_index => 1, :stage_objective => 17619.588952488386, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 417495.74574136594, :noise_term => 2, :node_index => 1, :stage_objective => 21433.374713683155, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 416719.06832292717, :noise_term => 5, :node_index => 1, :stage_objective => 17693.22075486607, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 422277.4066621865, :noise_term => 0, :node_index => 1, :stage_objective => 21433.374713684298, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 428077.5024049643, :noise_term => 0, :node_index => 1, :stage_objective => 21433.375552565707, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 423189.25151001965, :noise_term => 5, :node_index => 1, :stage_objective => 21433.374713684436, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 429180.14258201665, :noise_term => 0, :node_index => 1, :stage_objective => 21433.375552565765, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 481094.2780357883, :noise_term => 0, :node_index => 1, :stage_objective => 27420.553515466632, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 688260.5891296784, :noise_term => 0, :node_index => 1, :stage_objective => 118205.12737057797, :objective_state => nothing, :belief => Dict(1 => 1.0))  …  Dict(:bellman_term => 430196.79496208613, :noise_term => 0, :node_index => 1, :stage_objective => 22404.899230949388, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 430196.7949630039, :noise_term => 2, :node_index => 1, :stage_objective => 23580.69745375704, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 424941.8323067371, :noise_term => 5, :node_index => 1, :stage_objective => 21433.374713684894, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 430196.7949623641, :noise_term => 0, :node_index => 1, :stage_objective => 22759.00341408801, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 430196.79496300395, :noise_term => 2, :node_index => 1, :stage_objective => 23580.69745409137, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 430196.79496300395, :noise_term => 2, :node_index => 1, :stage_objective => 23580.697454861092, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 430196.79496300395, :noise_term => 2, :node_index => 1, :stage_objective => 23580.697454861092, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 424941.8323067371, :noise_term => 5, :node_index => 1, :stage_objective => 21433.37471368489, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 420295.3396472763, :noise_term => 5, :node_index => 1, :stage_objective => 21433.374713684007, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 416719.06858416507, :noise_term => 5, :node_index => 1, :stage_objective => 20394.7110611079, :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   8.775941e+04  8.166930e+04  1.364672e-01        18   1
         3   1.005374e+06  3.589197e+05  1.790807e+00       189   1
         6   2.171219e+06  3.996909e+05  3.180997e+00       324   1
        10   5.720741e+05  4.138901e+05  4.654569e+00       471   1
-------------------------------------------------------------------
status         : iteration_limit
total time (s) : 4.654569e+00
total solves   : 471
best bound     :  4.138901e+05
simulation ci  :  4.617907e+05 ± 4.160698e+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.