Skip to content

Commit 0de30ff

Browse files
committed
affine space projetion: new implementation
About: - implementation without modcholesky - using custom Cholesky implementation - all Rust unit tests pass
1 parent a7e09cd commit 0de30ff

3 files changed

Lines changed: 133 additions & 71 deletions

File tree

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ rpmalloc = { version = "0.2", features = [
9898
# epigraph of the squared Euclidean norm
9999
roots = "0.0.8"
100100

101-
# Least squares solver (NOTE: ndarray must be version 0.15 - not 0.16)
102101
ndarray = { version = "=0.17.2", features = ["approx"] }
103102

104103
# jemallocator is an optional feature; it will only be loaded if the feature

‎src/constraints/affine_space.rs‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ use super::Constraint;
22
use crate::matrix_operations;
33
use crate::CholeskyFactoriser;
44

5+
use ndarray::{ArrayView1, ArrayView2};
6+
57
#[derive(Clone)]
68
/// An affine space here is defined as the set of solutions of a linear equation, $Ax = b$,
79
/// that is, $E=\\{x\in\mathbb{R}^n: Ax = b\\}$, which is an affine space. It is assumed that
810
/// the matrix $AA^\intercal$ is full-rank.
911
pub struct AffineSpace {
1012
a_mat: Vec<f64>,
1113
b_vec: Vec<f64>,
12-
l: Vec<f64>,
14+
f: CholeskyFactoriser<f64>,
1315
n_rows: usize,
1416
n_cols: usize,
1517
}
18+
1619
impl AffineSpace {
1720
/// Construct a new affine space given the matrix $A\in\mathbb{R}^{m\times n}$ and
1821
/// the vector $b\in\mathbb{R}^m$
@@ -27,7 +30,6 @@ impl AffineSpace {
2730
///
2831
pub fn new(a: Vec<f64>, b: Vec<f64>) -> Self {
2932
let n_rows = b.len();
30-
println!("n_rows = {}", n_rows);
3133
let n_elements_a = a.len();
3234
assert!(n_rows > 0, "b must not be empty");
3335
assert!(
@@ -36,18 +38,77 @@ impl AffineSpace {
3638
);
3739
let n_cols = n_elements_a / n_rows;
3840
let aat = matrix_operations::mul_a_at(&a, n_rows, n_cols).unwrap();
39-
println!("A = {:?}", a);
40-
println!("AA' = {:?}", aat);
41-
let mut factoriser = CholeskyFactoriser::new(3);
41+
let mut factoriser = CholeskyFactoriser::new(n_rows);
4242
factoriser.factorize(&aat).unwrap();
43-
let l = factoriser.l();
44-
println!("L = {:?}", l);
4543
AffineSpace {
4644
a_mat: a,
4745
b_vec: b,
48-
l: l.to_vec(),
46+
f: factoriser,
4947
n_rows,
5048
n_cols,
5149
}
5250
}
5351
}
52+
53+
54+
impl Constraint for AffineSpace {
55+
/// Projection onto the set $E = \\{x: Ax = b\\}$, which is computed by
56+
/// $$P_E(x) = x - A^\intercal z(x),$$
57+
/// where $z$ is the solution of the linear system
58+
/// $$(AA^\intercal)z = Ax - b,$$
59+
/// which has a unique solution provided $A$ has full row rank. The linear system
60+
/// is solved by computing the Cholesky factorisation of $AA^\intercal$, which is
61+
/// done using ...
62+
///
63+
/// ## Arguments
64+
///
65+
/// - `x`: The given vector $x$ is updated with the projection on the set
66+
///
67+
/// ## Example
68+
///
69+
/// Consider the set $X = \\{x \in \mathbb{R}^4 :Ax = b\\}$, with $A\in\mathbb{R}^{3\times 4}$
70+
/// being the matrix
71+
/// $$A = \begin{bmatrix}0.5 & 0.1& 0.2& -0.3\\\\ -0.6& 0.3& 0 & 0.5 \\\\ 1.0& 0.1& -1& -0.4\end{bmatrix},$$
72+
/// and $b$ being the vector
73+
/// $$b = \begin{bmatrix}1 \\\\ 2 \\\\ -0.5\end{bmatrix}.$$
74+
///
75+
/// ```rust
76+
/// use optimization_engine::constraints::*;
77+
///
78+
/// let a = vec![0.5, 0.1, 0.2, -0.3, -0.6, 0.3, 0., 0.5, 1.0, 0.1, -1.0, -0.4,];
79+
/// let b = vec![1., 2., -0.5];
80+
/// let affine_set = AffineSpace::new(a, b);
81+
/// let mut x = [1., -2., -0.3, 0.5];
82+
/// affine_set.project(&mut x);
83+
/// ```
84+
///
85+
/// The result is stored in `x` and it can be verified that $Ax = b$.
86+
fn project(&self, x: &mut [f64]) {
87+
let m = self.n_rows;
88+
let n = self.n_cols;
89+
90+
assert!(x.len() == n, "x has wrong dimension");
91+
92+
// Step 1: Compute e = Ax - b
93+
let a = ArrayView2::from_shape((self.n_rows, self.n_cols), &self.a_mat)
94+
.expect("invalid A shape");
95+
let x_view = ArrayView1::from(&x[..]);
96+
let b = ArrayView1::from(&self.b_vec[..]);
97+
let e = a.dot(&x_view) - b;
98+
let e_slice: &[f64] = e.as_slice().unwrap();
99+
100+
// Step 2: Solve AA' z = e and compute z
101+
let z = self.f.solve(e_slice).unwrap();
102+
103+
// Step 3: Compute x = x - A'z
104+
let at_z = a.t().dot(&ArrayView1::from(&z[..]));
105+
for (xi, corr) in x.iter_mut().zip(at_z.iter()) {
106+
*xi -= *corr;
107+
}
108+
}
109+
110+
/// Affine sets are convex.
111+
fn is_convex(&self) -> bool {
112+
true
113+
}
114+
}

‎src/constraints/tests.rs‎

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -925,67 +925,69 @@ fn t_epigraph_squared_norm_correctness() {
925925
#[test]
926926
fn t_affine_space() {
927927
let a = vec![
928-
0.5, 0.1, 0.2, -0.3, -0.6, 0.3, 0., 0.5, 1.0, 0.1, -1.0, -0.4,
928+
0.5, 0.1, 0.2, -0.3,
929+
-0.6, 0.3, 0., 0.5,
930+
1.0, 0.1, -1.0, -0.4,
929931
];
930932
let b = vec![1., 2., -0.5];
931-
let _affine_set = AffineSpace::new(a, b);
932-
// let mut x = [1., -2., -0.3, 0.5];
933-
// affine_set.project(&mut x);
934-
// let x_correct = [
935-
// 1.888564346697095,
936-
// 5.629857182200888,
937-
// 1.796204902230790,
938-
// 2.888362906715977,
939-
// ];
940-
// unit_test_utils::assert_nearly_equal_array(
941-
// &x_correct,
942-
// &x,
943-
// 1e-10,
944-
// 1e-12,
945-
// "projection on affine set is wrong",
946-
// );
947-
}
948-
949-
// #[test]
950-
// fn t_affine_space_larger() {
951-
// let a = vec![
952-
// 1.0f64, 1., 1., 0., 0., 0., 1., 1., 1., 0., 0., 0., 1., 1., 1., -1., 4., -1., 0., 2.,
953-
// ];
954-
// let b = vec![1., -2., 3., 4.];
955-
// let affine_set = AffineSpace::new(a, b);
956-
// let mut x = [10., 11., -9., 4., 5.];
957-
// affine_set.project(&mut x);
958-
// let x_correct = [
959-
// 9.238095238095237,
960-
// -0.714285714285714,
961-
// -7.523809523809524,
962-
// 6.238095238095238,
963-
// 4.285714285714288,
964-
// ];
965-
// unit_test_utils::assert_nearly_equal_array(
966-
// &x_correct,
967-
// &x,
968-
// 1e-10,
969-
// 1e-12,
970-
// "projection on affine set is wrong",
971-
// );
972-
// }
973-
//
974-
// #[test]
975-
// fn t_affine_space_single_row() {
976-
// let a = vec![1., 1., 1., 1.];
977-
// let b = vec![1.];
978-
// let affine_set = AffineSpace::new(a, b);
979-
// let mut x = [5., 6., 10., 25.];
980-
// affine_set.project(&mut x);
981-
// let s = x.iter().sum();
982-
// unit_test_utils::assert_nearly_equal(1., s, 1e-12, 1e-14, "wrong sum");
983-
// }
984-
//
985-
// #[test]
986-
// #[should_panic]
987-
// fn t_affine_space_wrong_dimensions() {
988-
// let a = vec![0.5, 0.1, 0.2, -0.3, -0.6, 0.3, 0., 0.5, 1.0, 0.1, -1.0];
989-
// let b = vec![1., 2., -0.5];
990-
// let _ = AffineSpace::new(a, b);
991-
// }
933+
let affine_set = AffineSpace::new(a, b);
934+
let mut x = [1., -2., -0.3, 0.5];
935+
affine_set.project(&mut x);
936+
let x_correct = [
937+
1.888564346697095,
938+
5.629857182200888,
939+
1.796204902230790,
940+
2.888362906715977,
941+
];
942+
unit_test_utils::assert_nearly_equal_array(
943+
&x_correct,
944+
&x,
945+
1e-10,
946+
1e-12,
947+
"projection on affine set is wrong",
948+
);
949+
}
950+
951+
#[test]
952+
fn t_affine_space_larger() {
953+
let a = vec![
954+
1.0f64, 1., 1., 0., 0., 0., 1., 1., 1., 0., 0., 0., 1., 1., 1., -1., 4., -1., 0., 2.,
955+
];
956+
let b = vec![1., -2., 3., 4.];
957+
let affine_set = AffineSpace::new(a, b);
958+
let mut x = [10., 11., -9., 4., 5.];
959+
affine_set.project(&mut x);
960+
let x_correct = [
961+
9.238095238095237,
962+
-0.714285714285714,
963+
-7.523809523809524,
964+
6.238095238095238,
965+
4.285714285714288,
966+
];
967+
unit_test_utils::assert_nearly_equal_array(
968+
&x_correct,
969+
&x,
970+
1e-10,
971+
1e-12,
972+
"projection on affine set is wrong",
973+
);
974+
}
975+
976+
#[test]
977+
fn t_affine_space_single_row() {
978+
let a = vec![1., 1., 1., 1.];
979+
let b = vec![1.];
980+
let affine_set = AffineSpace::new(a, b);
981+
let mut x = [5., 6., 10., 25.];
982+
affine_set.project(&mut x);
983+
let s = x.iter().sum();
984+
unit_test_utils::assert_nearly_equal(1., s, 1e-12, 1e-14, "wrong sum");
985+
}
986+
987+
#[test]
988+
#[should_panic]
989+
fn t_affine_space_wrong_dimensions() {
990+
let a = vec![0.5, 0.1, 0.2, -0.3, -0.6, 0.3, 0., 0.5, 1.0, 0.1, -1.0];
991+
let b = vec![1., 2., -0.5];
992+
let _ = AffineSpace::new(a, b);
993+
}

0 commit comments

Comments
 (0)