Getting started: a penalty proxy for a parametric LP
This example trains a small neural network to predict solutions of a parametric economic-dispatch LP, using the self-supervised PenaltyMethod (no solver needed for training), and evaluates it against solver optima. It runs on a CPU in a few seconds.
using LearningToOptimize
using JuMP, HiGHS, Flux, RandomThe parametric problem
Three generators with costs 1, 3 and 2 serve two demands d (parameters); capacities are variable bounds.
model = Model()
@variable(model, d[1:2] in MOI.Parameter.([1.0, 2.0]))
@variable(model, 0 <= g[1:3] <= 4)
@constraint(model, g[1] + g[2] == d[1])
@constraint(model, g[3] == d[2])
@objective(model, Min, g[1] + 3g[2] + 2g[3])
problem = ParametricProblem(model)ParametricProblem{LinearConic}: 2 parameters, 3 variables, 8 constraints (8 dual rows)The problem class is detected from the model and the layouts are fixed:
problem_class(problem),
num_parameters(problem),
num_decision_variables(problem),
num_duals(problem)(LinearConic(), 2, 3, 8)Method and pipeline
L2OPipeline checks that the method supports the problem class and prepares the evaluator used by the loss.
method = PenaltyMethod(; ρ_eq = 20.0, ρ_ineq = 20.0)
pipe = L2OPipeline(problem, method)L2OPipeline(PenaltyMethod on ParametricProblem{LinearConic}: 2 parameters, 3 variables, 8 constraints (8 dual rows); FullSpaceDecomposition, MatrixEvaluator)Data
Training data are only parameter samples; a smaller test set is solved with HiGHS for reference.
rng = Random.MersenneTwister(1)
sampler = BoxSampler([0.5, 1.0], [1.5, 2.0])
train = generate_data(pipe; sampler, n = 128, rng)
test = generate_data(pipe; sampler, n = 32, rng, optimizer = HiGHS.Optimizer)L2ODataset{Float64}: 32 samples; labels: primal, objective, statusTraining
proxy = Flux.f64(Chain(Dense(2 => 32, relu), Dense(32 => 3)))
history = train_flux!(
pipe,
proxy,
train;
epochs = 200,
batchsize = 32,
rng,
optimiser = Flux.Optimisers.Adam(1e-2),
)
history[[1, 50, 100, 200]]4-element Vector{Float64}:
131.9091119671691
4.061439150378536
3.9555661596343743
3.9395753435918097Evaluation
prediction = complete(pipe, test.inputs, proxy(test.inputs))
report = evaluate(pipe, test.inputs, prediction)EvaluationReport
objective mean = 3.713814880651589 (32 instances)
max_violation mean = 0.050249854550249974 (32 instances)
max_equality_violation mean = 0.049003079217167796 (32 instances)
max_inequality_violation mean = 0.049498625333205766 (32 instances)
Mean absolute optimality gap and worst constraint violation:
sum(abs.(report.objective .- test.objective)) / 32, maximum(report.max_violation)(0.2222472002709256, 0.06105113796089179)The penalty loss only discourages violations, so the proxy lands close to, but not exactly on, the feasible set. The report separates equality and inequality (here: capacity bound) violations:
maximum(report.max_equality_violation), maximum(report.max_inequality_violation)(0.06105113796089179, 0.057415105928500985)Fraction of test instances whose worst violation is below 1e-2 (often none at this training budget). Methods with a completion step (DC3, DLL, …) are the ones that restore feasibility or dual feasibility exactly.
count(<=(1e-2), report.max_violation) / 320.0Repair layers
Repair layers wrap any primal method. BoundedOutput maps the network output into the variable bounds during training and inference, so the bounds hold by construction. Here the only inequalities are the capacity bounds, so their largest violation is exactly zero:
bounded = BoundedOutput(method)
bpipe = L2OPipeline(problem, bounded)
bproxy = Flux.f64(Chain(Dense(2 => 32, relu), Dense(32 => 3)))
train_flux!(
bpipe,
bproxy,
train;
epochs = 200,
batchsize = 32,
rng,
optimiser = Flux.Optimisers.Adam(1e-2),
)
bpred = complete(bpipe, test.inputs, bproxy(test.inputs))
maximum(evaluate(pipe, test.inputs, bpred).max_inequality_violation)0.0ProjectionRepair projects the prediction onto the feasible set of each instance at inference time (a small QP per instance). The result is feasible up to the solver tolerance, and since the projection onto a convex set is nonexpansive it is never farther from the optimal solution than the raw prediction:
repaired = L2OPipeline(problem, ProjectionRepair(bounded, HiGHS.Optimizer))
rpred = complete(repaired, test.inputs, bproxy(test.inputs))
maximum(evaluate(pipe, test.inputs, rpred).max_violation)0.0Mean optimality gap before and after the repair:
gap(pred) = sum(abs.(evaluate(pipe, test.inputs, pred).objective .- test.objective)) / 32
gap(bpred), gap(rpred)(0.10756463441711299, 0.028289497860173124)