pub struct ConstantMutation { /* private fields */ }Expand description
Constant mutation (Poli, Langdon and McPhee 2008, sec. 5.2.2, after Schoenauer et al. 1996):
one constant, chosen uniformly, is perturbed by normal noise of standard deviation sigma
times the width of its type’s range, mirrored at the ends of the range. Each call changes one
constant, as each change is a separate mutation there.
It applies to constants of Constants::uniform and Constants::integers with more than
one value (integers are rounded), and of Constants::normal, whose noise is sigma times
the deviation, without ends to mirror at; the constants of a Constants::choice have no range, and
PointMutation draws them anew. The constant always changes: the noise is drawn again
while it leaves the value as it is (for integers, while it rounds to the same value), up to 64
times, then the value is drawn uniformly from the others. A tree without such a constant is
left unchanged. The cheap tuning of constants: it keeps the tree’s shape and moves a constant
a little.
use genoxide::gp::{Constants, ConstantMutation, Gp, PrimitiveSet};
use genoxide::prelude::*;
#[derive(Clone, Copy, Debug)]
enum Op {
Mul,
X,
}
let mut set = PrimitiveSet::builder();
let real = set.new_type("real");
set.function("mul", Op::Mul, [real, real], real)
.terminal("x", Op::X, real)
.constants(real, Constants::uniform(-1.0..=1.0)?);
let gp = Gp::builder(set.build(real)?).build()?;
let mut tree = gp.primitives().parse("mul(x, 0.5)")?;
ConstantMutation::gaussian(0.05)?.mutate(&gp, &mut tree, &mut StreamRng::seed_from_u64(1));
assert_ne!(tree, gp.primitives().parse("mul(x, 0.5)")?);
assert!(gp.validate(&tree).is_ok()); // still within [-1, 1]Implementations§
Source§impl ConstantMutation
impl ConstantMutation
Sourcepub fn gaussian(sigma: f64) -> Result<Self>
pub fn gaussian(sigma: f64) -> Result<Self>
Normal noise of standard deviation sigma (positive and finite) times the width of the
constant’s range, or the deviation of Constants::normal constants, e.g. 0.1.
§Errors
Error::InvalidSetting (setting constant_sigma) for a sigma that isn’t positive
and finite.
Trait Implementations§
Source§impl Clone for ConstantMutation
impl Clone for ConstantMutation
impl Copy for ConstantMutation
Source§impl Debug for ConstantMutation
impl Debug for ConstantMutation
Source§impl<'de> Deserialize<'de> for ConstantMutation
Available on crate feature serde only.
impl<'de> Deserialize<'de> for ConstantMutation
serde only.Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<ConstantMutation> for TreeMutation
impl From<ConstantMutation> for TreeMutation
Source§fn from(mutation: ConstantMutation) -> Self
fn from(mutation: ConstantMutation) -> Self
Source§impl PartialEq for ConstantMutation
impl PartialEq for ConstantMutation
Source§impl Serialize for ConstantMutation
Available on crate feature serde only.
impl Serialize for ConstantMutation
serde only.impl StructuralPartialEq for ConstantMutation
Auto Trait Implementations§
impl Freeze for ConstantMutation
impl RefUnwindSafe for ConstantMutation
impl Send for ConstantMutation
impl Sync for ConstantMutation
impl Unpin for ConstantMutation
impl UnsafeUnpin for ConstantMutation
impl UnwindSafe for ConstantMutation
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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> ⓘ
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> ⓘ
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