Skip to main content

NonlinearConstraintHelper

Struct NonlinearConstraintHelper 

Source
pub struct NonlinearConstraintHelper {
    pub fun: VectorConstraintFn,
    pub lb: Array1<f64>,
    pub ub: Array1<f64>,
}
Expand description

SciPy-like nonlinear constraint helper: vector-valued fun(x) with lb <= fun(x) <= ub.

Fields§

§fun: VectorConstraintFn

Vector-valued constraint function.

§lb: Array1<f64>

Lower bounds for each constraint component.

§ub: Array1<f64>

Upper bounds for each constraint component.

Implementations§

Source§

impl NonlinearConstraintHelper

Source

pub fn apply_to(&self, cfg: &mut DEConfig, weight_ineq: f64, weight_eq: f64)

Apply helper by emitting penalty closures per component. lb <= f_i(x) <= ub becomes two inequalities: f_i(x)-ub <= 0 and lb - f_i(x) <= 0. If lb==ub, emit an equality penalty for f_i(x)-lb.

Examples found in repository?
examples/optde_nonlinear_constraints.rs (line 39)
8fn main() {
9    // Himmelblau as objective, but with nonlinear constraints to demonstrate helper
10    let himmelblau =
11        |x: &Array1<f64>| (x[0] * x[0] + x[1] - 11.0).powi(2) + (x[0] + x[1] * x[1] - 7.0).powi(2);
12
13    // Bounds
14    let bounds = [(-6.0, 6.0), (-6.0, 6.0)];
15
16    // Nonlinear vector function f(x) with 2 components
17    // 1) Circle-ish constraint: x0^2 + x1^2 <= 10  -> f0(x) = x0^2 + x1^2,  lb=-inf, ub=10
18    // 2) Sum equality: x0 + x1 = 1  -> f1(x) = x0 + x1,  lb=1, ub=1
19    let fun =
20        Arc::new(|x: &Array1<f64>| Array1::from(vec![x[0] * x[0] + x[1] * x[1], x[0] + x[1]]));
21    let lb = Array1::from(vec![-f64::INFINITY, 1.0]);
22    let ub = Array1::from(vec![10.0, 1.0]);
23    let nlc = NonlinearConstraintHelper { fun, lb, ub };
24
25    // Strategy parsing from string
26    let strategy = Strategy::from_str("best1exp").unwrap_or(Strategy::Best1Exp);
27
28    let mut cfg = DEConfigBuilder::new()
29        .seed(456)
30        .maxiter(800)
31        .popsize(30)
32        .strategy(strategy)
33        .recombination(0.9)
34        .crossover(Crossover::Exponential)
35        .build()
36        .expect("popsize must be >= 4");
37
38    // Apply nonlinear constraints with penalties
39    nlc.apply_to(&mut cfg, 1e3, 1e3);
40
41    let rep = differential_evolution(&himmelblau, &bounds, cfg).expect("optimization failed");
42    println!(
43        "success={} message=\"{}\"\nbest f={:.6e}\nbest x={:?}",
44        rep.success, rep.message, rep.fun, rep.x
45    );
46}

Trait Implementations§

Source§

impl Clone for NonlinearConstraintHelper

Source§

fn clone(&self) -> NonlinearConstraintHelper

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

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