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-23
-------------------------------------------------------------------
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.397270e+06  8.488724e+04  3.408329e-01        59   1
         3   1.662298e+06  1.884979e+05  1.778558e+00       221   1
         5   8.849549e+05  2.505160e+05  3.550659e+00       415   1
         8   3.425598e+05  3.924556e+05  4.886250e+00       552   1
         9   5.569261e+06  3.974484e+05  6.630143e+00       747   1
        10   3.788097e+06  3.998455e+05  8.286204e+00       910   1
-------------------------------------------------------------------
status         : iteration_limit
total time (s) : 8.286204e+00
total solves   : 910
best bound     :  3.998455e+05
simulation ci  :  1.561889e+06 ± 1.144523e+06
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 => 379164.10641488736, :noise_term => 2, :node_index => 1, :stage_objective => 21433.37542542827, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 380794.2905708711, :noise_term => 2, :node_index => 1, :stage_objective => 21433.37542542828, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 386931.5717243489, :noise_term => 0, :node_index => 1, :stage_objective => 21433.375425428316, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 392476.9602112749, :noise_term => 0, :node_index => 1, :stage_objective => 22212.461392794627, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 392476.96021180734, :noise_term => 2, :node_index => 1, :stage_objective => 23580.69745420577, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 387346.4988666351, :noise_term => 5, :node_index => 1, :stage_objective => 21433.375425428312, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 382216.0375263775, :noise_term => 5, :node_index => 1, :stage_objective => 21433.375425428243, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 377085.5761861199, :noise_term => 5, :node_index => 1, :stage_objective => 21433.375425428276, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 378715.76034210366, :noise_term => 2, :node_index => 1, :stage_objective => 21433.375425428283, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 384853.0414955815, :noise_term => 0, :node_index => 1, :stage_objective => 21433.375425428298, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 379722.580155324, :noise_term => 5, :node_index => 1, :stage_objective => 21433.375425428276, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 374592.1188150666, :noise_term => 5, :node_index => 1, :stage_objective => 21433.375425428076, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 380729.39996854437, :noise_term => 0, :node_index => 1, :stage_objective => 21433.37542542828, :objective_state => nothing, :belief => Dict(1 => 1.0)), Dict(:bellman_term => 386866.6811220222, :noise_term => 0, :node_index => 1, :stage_objective => 21433.375425428312, :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 => 382707.03750669304, :noise_term => 5, :node_index => 1, :stage_objective => 17580.84713142626, :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-23
-------------------------------------------------------------------
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   3.906127e+06  8.473743e+04  7.338309e-01        75   1
         4   1.714670e+05  2.907546e+05  1.855050e+00       147   1
         6   1.286003e+05  3.496458e+05  2.915032e+00       219   1
         7   2.533496e+06  3.756617e+05  5.391655e+00       387   1
         8   6.283696e+05  3.816158e+05  6.682409e+00       474   1
        10   9.600644e+05  3.944612e+05  8.879220e+00       624   1
-------------------------------------------------------------------
status         : iteration_limit
total time (s) : 8.879220e+00
total solves   : 624
best bound     :  3.944612e+05
simulation ci  :  1.038436e+06 ± 8.057129e+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.