pub trait Tree{
type Environment;
type Action;
// Required methods
fn branch<R: Rng>(
tg: &mut TreeGen<'_, R>,
current_depth: usize,
) -> BoxTree<Self>;
fn leaf<R: Rng>(
tg: &mut TreeGen<'_, R>,
current_depth: usize,
) -> BoxTree<Self>;
fn count_children(&mut self) -> usize;
fn children(&self) -> Vec<&BoxTree<Self>>;
fn children_mut(&mut self) -> Vec<&mut BoxTree<Self>>;
fn evaluate(&self, env: &Self::Environment) -> Self::Action;
// Provided methods
fn tree<R: Rng>(tg: &mut TreeGen<'_, R>) -> BoxTree<Self> { ... }
fn child<R: Rng>(
tg: &mut TreeGen<'_, R>,
current_depth: usize,
) -> BoxTree<Self> { ... }
}Expand description
Trait to be implemented by Genetic Programs trees.
Required Associated Types§
Sourcetype Environment
type Environment
Type of input when evaluating the tree.
Required Methods§
Sourcefn branch<R: Rng>(
tg: &mut TreeGen<'_, R>,
current_depth: usize,
) -> BoxTree<Self>
fn branch<R: Rng>( tg: &mut TreeGen<'_, R>, current_depth: usize, ) -> BoxTree<Self>
Generate a branch node (a node with at least one Tree child).
Sourcefn leaf<R: Rng>(tg: &mut TreeGen<'_, R>, current_depth: usize) -> BoxTree<Self>
fn leaf<R: Rng>(tg: &mut TreeGen<'_, R>, current_depth: usize) -> BoxTree<Self>
Generate a leaf node (a node without any Tree children).
Sourcefn count_children(&mut self) -> usize
fn count_children(&mut self) -> usize
Count Self children of this node.
Sourcefn children_mut(&mut self) -> Vec<&mut BoxTree<Self>>
fn children_mut(&mut self) -> Vec<&mut BoxTree<Self>>
Get mutable children of this node.
Sourcefn evaluate(&self, env: &Self::Environment) -> Self::Action
fn evaluate(&self, env: &Self::Environment) -> Self::Action
Get indexed child of this node. Number children from 0; suggested to go left-to-right. Used to evaluate the root node of a tree.
Provided Methods§
Sourcefn tree<R: Rng>(tg: &mut TreeGen<'_, R>) -> BoxTree<Self>
fn tree<R: Rng>(tg: &mut TreeGen<'_, R>) -> BoxTree<Self>
Generate a new tree within the bounds specified by TreeGen.
Sourcefn child<R: Rng>(tg: &mut TreeGen<'_, R>, current_depth: usize) -> BoxTree<Self>
fn child<R: Rng>(tg: &mut TreeGen<'_, R>, current_depth: usize) -> BoxTree<Self>
Generate a random new node to go into a tree.
Examples found in repository?
More examples
examples/symbolic_regression.rs (line 32)
31 fn branch<R: Rng>(tg: &mut TreeGen<R>, current_depth: usize) -> BoxTree<Self> {
32 let left = Self::child(tg, current_depth + 1);
33 let right = Self::child(tg, current_depth + 1);
34 match tg.gen_range(0, 7) {
35 0 => Add(left, right),
36 1 => Sub(left, right),
37 2 => Mul(left, right),
38 3 => Div(left, right),
39 4 => Neg(left),
40 5 => Sin(left),
41 6 => Cos(left),
42 _ => unreachable!(),
43 }
44 .into()
45 }Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.