Tree

Trait Tree 

Source
pub trait Tree
where Self: Sized + Debug + Clone,
{ 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§

Source

type Environment

Type of input when evaluating the tree.

Source

type Action

The type the tree will evaluate to.

Required Methods§

Source

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).

Source

fn leaf<R: Rng>(tg: &mut TreeGen<'_, R>, current_depth: usize) -> BoxTree<Self>

Generate a leaf node (a node without any Tree children).

Source

fn count_children(&mut self) -> usize

Count Self children of this node.

Source

fn children(&self) -> Vec<&BoxTree<Self>>

Get children of this node.

Source

fn children_mut(&mut self) -> Vec<&mut BoxTree<Self>>

Get mutable children of this node.

Source

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§

Source

fn tree<R: Rng>(tg: &mut TreeGen<'_, R>) -> BoxTree<Self>

Generate a new tree within the bounds specified by TreeGen.

Source

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?
examples/snake.rs (line 165)
163    fn branch<R: Rng>(tg: &mut TreeGen<R>, current_depth: usize) -> BoxTree<Self> {
164        let direction = TurnDirection::rand(tg);
165        let true_ = Self::child(tg, current_depth + 1);
166        let false_ = Self::child(tg, current_depth + 1);
167        if tg.gen() {
168            IfDanger(direction, true_, false_).into()
169        } else {
170            IfFood(direction, true_, false_).into()
171        }
172    }
More examples
Hide additional 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.

Implementors§