1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
pub mod tree;
mod crossover;
mod mutation;
pub use self::crossover::*;
pub use self::mutation::*;
use rand::Rng;
use std::fmt;
use self::tree::*;
#[derive(Debug, Clone)]
pub struct Individual<T>
where T: Tree
{
pub tree: BoxTree<T>,
nodes_count: usize,
}
impl<T> Individual<T>
where T: Tree
{
pub fn new<R: Rng>(tg: &mut TreeGen<R>) -> Individual<T> {
Self::new_from_tree(T::tree(tg))
}
pub fn new_from_tree(boxtree: BoxTree<T>) -> Individual<T> {
let mut indv = Individual {
tree: boxtree,
nodes_count: 0,
};
indv.recalculate_metadata();
indv
}
pub fn nodes_count(&self) -> usize {
self.nodes_count
}
pub fn recalculate_metadata(&mut self) {
self.nodes_count = self.tree.count_nodes();
}
}
impl<T> fmt::Display for Individual<T>
where T: Tree + fmt::Display
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.tree)
}
}