|
| 1 | +//! Closest Vector Problem (CVP) implementation. |
| 2 | +//! |
| 3 | +//! Given a lattice basis B and target vector t, find integer coefficients x |
| 4 | +//! minimizing ‖Bx - t‖₂. |
| 5 | +
|
| 6 | +use crate::models::optimization::VarBounds; |
| 7 | +use crate::registry::{FieldInfo, ProblemSchemaEntry}; |
| 8 | +use crate::traits::{OptimizationProblem, Problem}; |
| 9 | +use crate::types::{Direction, SolutionSize}; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | + |
| 12 | +inventory::submit! { |
| 13 | + ProblemSchemaEntry { |
| 14 | + name: "ClosestVectorProblem", |
| 15 | + module_path: module_path!(), |
| 16 | + description: "Find the closest lattice point to a target vector", |
| 17 | + fields: &[ |
| 18 | + FieldInfo { name: "basis", type_name: "Vec<Vec<T>>", description: "Basis matrix B as column vectors" }, |
| 19 | + FieldInfo { name: "target", type_name: "Vec<f64>", description: "Target vector t" }, |
| 20 | + FieldInfo { name: "bounds", type_name: "Vec<VarBounds>", description: "Integer bounds per variable" }, |
| 21 | + ], |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Closest Vector Problem (CVP). |
| 26 | +/// |
| 27 | +/// Given a lattice basis B ∈ R^{m×n} and target t ∈ R^m, |
| 28 | +/// find integer x ∈ Z^n minimizing ‖Bx - t‖₂. |
| 29 | +/// |
| 30 | +/// Variables are integer coefficients with explicit bounds for enumeration. |
| 31 | +/// The configuration encoding follows ILP: config[i] is an offset from bounds[i].lower. |
| 32 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 33 | +pub struct ClosestVectorProblem<T> { |
| 34 | + /// Basis matrix B stored as n column vectors, each of dimension m. |
| 35 | + basis: Vec<Vec<T>>, |
| 36 | + /// Target vector t ∈ R^m. |
| 37 | + target: Vec<f64>, |
| 38 | + /// Integer bounds per variable for enumeration. |
| 39 | + bounds: Vec<VarBounds>, |
| 40 | +} |
| 41 | + |
| 42 | +impl<T> ClosestVectorProblem<T> { |
| 43 | + /// Create a new CVP instance. |
| 44 | + /// |
| 45 | + /// # Arguments |
| 46 | + /// * `basis` - n column vectors of dimension m |
| 47 | + /// * `target` - target vector of dimension m |
| 48 | + /// * `bounds` - integer bounds per variable (length n) |
| 49 | + /// |
| 50 | + /// # Panics |
| 51 | + /// Panics if basis/bounds lengths mismatch or dimensions are inconsistent. |
| 52 | + pub fn new(basis: Vec<Vec<T>>, target: Vec<f64>, bounds: Vec<VarBounds>) -> Self { |
| 53 | + let n = basis.len(); |
| 54 | + assert_eq!( |
| 55 | + bounds.len(), |
| 56 | + n, |
| 57 | + "bounds length must match number of basis vectors" |
| 58 | + ); |
| 59 | + let m = target.len(); |
| 60 | + for (i, col) in basis.iter().enumerate() { |
| 61 | + assert_eq!( |
| 62 | + col.len(), |
| 63 | + m, |
| 64 | + "basis vector {i} has length {}, expected {m}", |
| 65 | + col.len() |
| 66 | + ); |
| 67 | + } |
| 68 | + Self { |
| 69 | + basis, |
| 70 | + target, |
| 71 | + bounds, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Number of basis vectors (lattice dimension n). |
| 76 | + pub fn num_basis_vectors(&self) -> usize { |
| 77 | + self.basis.len() |
| 78 | + } |
| 79 | + |
| 80 | + /// Dimension of the ambient space (m). |
| 81 | + pub fn ambient_dimension(&self) -> usize { |
| 82 | + self.target.len() |
| 83 | + } |
| 84 | + |
| 85 | + /// Access the basis matrix. |
| 86 | + pub fn basis(&self) -> &[Vec<T>] { |
| 87 | + &self.basis |
| 88 | + } |
| 89 | + |
| 90 | + /// Access the target vector. |
| 91 | + pub fn target(&self) -> &[f64] { |
| 92 | + &self.target |
| 93 | + } |
| 94 | + |
| 95 | + /// Access the variable bounds. |
| 96 | + pub fn bounds(&self) -> &[VarBounds] { |
| 97 | + &self.bounds |
| 98 | + } |
| 99 | + |
| 100 | + /// Convert a configuration (offsets from lower bounds) to integer values. |
| 101 | + fn config_to_values(&self, config: &[usize]) -> Vec<i64> { |
| 102 | + config |
| 103 | + .iter() |
| 104 | + .enumerate() |
| 105 | + .map(|(i, &c)| { |
| 106 | + let lo = self.bounds.get(i).and_then(|b| b.lower).unwrap_or(0); |
| 107 | + lo + c as i64 |
| 108 | + }) |
| 109 | + .collect() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl<T> Problem for ClosestVectorProblem<T> |
| 114 | +where |
| 115 | + T: Clone |
| 116 | + + Into<f64> |
| 117 | + + crate::variant::VariantParam |
| 118 | + + Serialize |
| 119 | + + for<'de> Deserialize<'de> |
| 120 | + + std::fmt::Debug |
| 121 | + + 'static, |
| 122 | +{ |
| 123 | + const NAME: &'static str = "ClosestVectorProblem"; |
| 124 | + type Metric = SolutionSize<f64>; |
| 125 | + |
| 126 | + fn dims(&self) -> Vec<usize> { |
| 127 | + self.bounds |
| 128 | + .iter() |
| 129 | + .map(|b| { |
| 130 | + b.num_values().expect( |
| 131 | + "CVP brute-force enumeration requires all variables to have finite bounds", |
| 132 | + ) |
| 133 | + }) |
| 134 | + .collect() |
| 135 | + } |
| 136 | + |
| 137 | + fn evaluate(&self, config: &[usize]) -> SolutionSize<f64> { |
| 138 | + let values = self.config_to_values(config); |
| 139 | + let m = self.ambient_dimension(); |
| 140 | + let mut diff = vec![0.0f64; m]; |
| 141 | + for (i, &x_i) in values.iter().enumerate() { |
| 142 | + for (j, b_ji) in self.basis[i].iter().enumerate() { |
| 143 | + diff[j] += x_i as f64 * b_ji.clone().into(); |
| 144 | + } |
| 145 | + } |
| 146 | + for (d, t) in diff.iter_mut().zip(self.target.iter()) { |
| 147 | + *d -= t; |
| 148 | + } |
| 149 | + let norm = diff.iter().map(|d| d * d).sum::<f64>().sqrt(); |
| 150 | + SolutionSize::Valid(norm) |
| 151 | + } |
| 152 | + |
| 153 | + fn variant() -> Vec<(&'static str, &'static str)> { |
| 154 | + crate::variant_params![T] |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +impl<T> OptimizationProblem for ClosestVectorProblem<T> |
| 159 | +where |
| 160 | + T: Clone |
| 161 | + + Into<f64> |
| 162 | + + crate::variant::VariantParam |
| 163 | + + Serialize |
| 164 | + + for<'de> Deserialize<'de> |
| 165 | + + std::fmt::Debug |
| 166 | + + 'static, |
| 167 | +{ |
| 168 | + type Value = f64; |
| 169 | + |
| 170 | + fn direction(&self) -> Direction { |
| 171 | + Direction::Minimize |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +crate::declare_variants! { |
| 176 | + ClosestVectorProblem<i32> => "exp(num_basis_vectors)", |
| 177 | + ClosestVectorProblem<f64> => "exp(num_basis_vectors)", |
| 178 | +} |
| 179 | + |
| 180 | +#[cfg(test)] |
| 181 | +#[path = "../../unit_tests/models/optimization/closest_vector_problem.rs"] |
| 182 | +mod tests; |
0 commit comments