Skip to main content

LinearConstraintHelper

Struct LinearConstraintHelper 

Source
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

Source

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

Source§

fn clone(&self) -> LinearConstraintHelper

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LinearConstraintHelper

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V