use crate::core::constraint::LinearEqualityConstraints;
use crate::core::math::{Dot, MatTransposeVec, MatVec, NormSquared, ScaledAdd};
use crate::core::problem::{CostFunction, Gradient};
pub struct AugmentedLagrangian<'a, P, V> {
problem: &'a P,
lambda: &'a V,
rho: f64,
}
impl<'a, P, V> AugmentedLagrangian<'a, P, V> {
pub fn new(problem: &'a P, lambda: &'a V, rho: f64) -> Self {
Self {
problem,
lambda,
rho,
}
}
pub fn rho(&self) -> f64 {
self.rho
}
}
impl<P, V, M> CostFunction for AugmentedLagrangian<'_, P, V>
where
P: CostFunction<Param = V, Output = f64> + LinearEqualityConstraints<Param = V, Matrix = M>,
M: MatVec<V>,
V: ScaledAdd<f64> + Dot + NormSquared,
{
type Param = V;
type Output = f64;
type Error = <P as CostFunction>::Error;
fn cost(&self, x: &V) -> Result<f64, Self::Error> {
let mut c = self.problem.a().matvec(x);
c.scaled_add(-1.0, self.problem.b());
Ok(self.problem.cost(x)? + self.lambda.dot(&c) + 0.5 * self.rho * c.norm_squared())
}
}
impl<P, V, M> Gradient for AugmentedLagrangian<'_, P, V>
where
P: CostFunction<Param = V, Output = f64>
+ Gradient<Gradient = V>
+ LinearEqualityConstraints<Param = V, Matrix = M>,
M: MatVec<V> + MatTransposeVec<V>,
V: ScaledAdd<f64> + Dot + NormSquared + Clone,
{
type Gradient = V;
fn gradient(&self, x: &V) -> Result<V, <Self as CostFunction>::Error> {
let mut c = self.problem.a().matvec(x);
c.scaled_add(-1.0, self.problem.b());
let mut w = self.lambda.clone();
w.scaled_add(self.rho, &c);
let mut g = self.problem.gradient(x)?;
let constraint_grad = self.problem.a().mat_transpose_vec(&w);
g.scaled_add(1.0, &constraint_grad);
Ok(g)
}
}
#[cfg(all(test, feature = "nalgebra"))]
mod tests {
use super::*;
use nalgebra::{DMatrix, DVector};
struct Probe {
a: DMatrix<f64>,
b: DVector<f64>,
}
impl Probe {
fn new() -> Self {
Self {
a: DMatrix::from_row_slice(1, 2, &[1.0, 1.0]),
b: DVector::from_vec(vec![2.0]),
}
}
}
impl CostFunction for Probe {
type Param = DVector<f64>;
type Output = f64;
type Error = std::convert::Infallible;
fn cost(&self, x: &DVector<f64>) -> Result<f64, std::convert::Infallible> {
Ok(0.5 * x.dot(x))
}
}
impl Gradient for Probe {
type Gradient = DVector<f64>;
fn gradient(&self, x: &DVector<f64>) -> Result<DVector<f64>, std::convert::Infallible> {
Ok(x.clone())
}
}
impl LinearEqualityConstraints for Probe {
type Matrix = DMatrix<f64>;
fn a(&self) -> &DMatrix<f64> {
&self.a
}
fn b(&self) -> &DVector<f64> {
&self.b
}
}
#[test]
fn cost_matches_closed_form() {
let p = Probe::new();
let lambda = DVector::from_vec(vec![0.5]);
let rho = 2.0;
let al = AugmentedLagrangian::new(&p, &lambda, rho);
let x = DVector::from_vec(vec![0.3, -0.4]);
let c = (0.3 - 0.4) - 2.0;
let expected = 0.125 + 0.5 * c + 0.5 * rho * c * c;
assert!((al.cost(&x).unwrap() - expected).abs() < 1e-12);
}
#[test]
fn cost_is_finite_at_infeasible_points() {
let p = Probe::new();
let lambda = DVector::from_vec(vec![0.0]);
let al = AugmentedLagrangian::new(&p, &lambda, 1.0);
let x = DVector::from_vec(vec![100.0, 100.0]);
assert!(al.cost(&x).unwrap().is_finite());
}
#[test]
fn gradient_agrees_with_finite_differences() {
let p = Probe::new();
let lambda = DVector::from_vec(vec![0.7]);
let al = AugmentedLagrangian::new(&p, &lambda, 1.3);
let x = DVector::from_vec(vec![0.3, -0.4]);
let analytic = al.gradient(&x).unwrap();
let h = 1e-6;
for j in 0..2 {
let mut xp = x.clone();
let mut xm = x.clone();
xp[j] += h;
xm[j] -= h;
let fd = (al.cost(&xp).unwrap() - al.cost(&xm).unwrap()) / (2.0 * h);
assert!(
(analytic[j] - fd).abs() < 1e-5,
"component {j}: analytic {} vs fd {}",
analytic[j],
fd
);
}
}
}