pub struct LinearConstraintHelper {
pub a: Array2<f64>,
pub lb: Array1<f64>,
pub ub: Array1<f64>,
}Expand description
SciPy-like linear constraint helper: lb <= A x <= ub.
Fields§
§a: Array2<f64>Constraint matrix A (m x n).
lb: Array1<f64>Lower bounds vector (m elements).
ub: Array1<f64>Upper bounds vector (m elements).
Implementations§
Source§impl LinearConstraintHelper
impl LinearConstraintHelper
Sourcepub fn apply_to(&self, cfg: &mut DEConfig, weight: f64)
pub fn apply_to(&self, cfg: &mut DEConfig, weight: f64)
Apply helper by merging into DEConfig.linear_penalty (stacking rows if already present)
Examples found in repository?
examples/optde_linear_constraints.rs (line 38)
7fn main() {
8 // Objective: sphere in 2D
9 let sphere = |x: &Array1<f64>| x.iter().map(|v| v * v).sum::<f64>();
10
11 // Bounds
12 let bounds = [(-5.0, 5.0), (-5.0, 5.0)];
13
14 // Linear constraint example: lb <= A x <= ub
15 // 1) x0 + x1 <= 1.0
16 // 2) 0.2 <= x0 - x1 <= 0.4
17 let a = Array2::from_shape_vec((2, 2), vec![1.0, 1.0, 1.0, -1.0]).unwrap();
18 let lb = Array1::from(vec![-f64::INFINITY, 0.2]);
19 let ub = Array1::from(vec![1.0, 0.4]);
20 let lc = LinearConstraintHelper { a, lb, ub };
21
22 // Strategy parsing from string (mirrors SciPy names)
23 let strategy = Strategy::from_str("randtobest1exp").unwrap_or(Strategy::RandToBest1Exp);
24
25 // Build config using the fluent builder
26 let mut cfg = DEConfigBuilder::new()
27 .seed(123)
28 .maxiter(600)
29 .popsize(30)
30 .strategy(strategy)
31 .recombination(0.9)
32 .mutation(Mutation::Range { min: 0.4, max: 1.0 })
33 .crossover(Crossover::Exponential)
34 .build()
35 .expect("popsize must be >= 4");
36
37 // Apply linear constraints with a penalty weight
38 lc.apply_to(&mut cfg, 1e3);
39
40 let rep = differential_evolution(&sphere, &bounds, cfg).expect("optimization failed");
41 println!(
42 "success={} message=\"{}\"\nbest f={:.6e}\nbest x={:?}",
43 rep.success, rep.message, rep.fun, rep.x
44 );
45}Trait Implementations§
Source§impl Clone for LinearConstraintHelper
impl Clone for LinearConstraintHelper
Source§fn clone(&self) -> LinearConstraintHelper
fn clone(&self) -> LinearConstraintHelper
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for LinearConstraintHelper
impl RefUnwindSafe for LinearConstraintHelper
impl Send for LinearConstraintHelper
impl Sync for LinearConstraintHelper
impl Unpin for LinearConstraintHelper
impl UnsafeUnpin for LinearConstraintHelper
impl UnwindSafe for LinearConstraintHelper
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more