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: VectorConstraintFnVector-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
impl NonlinearConstraintHelper
Sourcepub fn apply_to(&self, cfg: &mut DEConfig, weight_ineq: f64, weight_eq: f64)
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
impl Clone for NonlinearConstraintHelper
Source§fn clone(&self) -> NonlinearConstraintHelper
fn clone(&self) -> NonlinearConstraintHelper
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 NonlinearConstraintHelper
impl !RefUnwindSafe for NonlinearConstraintHelper
impl Send for NonlinearConstraintHelper
impl Sync for NonlinearConstraintHelper
impl Unpin for NonlinearConstraintHelper
impl UnsafeUnpin for NonlinearConstraintHelper
impl !UnwindSafe for NonlinearConstraintHelper
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