Individual

Struct Individual 

Source
pub struct Individual<T>
where T: Tree,
{ pub tree: BoxTree<T>, /* private fields */ }
Expand description

A genetic individual to mate and mutate in a Genetic Program.

Wraps around a BoxTree and caches useful data.

Fields§

§tree: BoxTree<T>

The contained GP tree, starting at the head.

Implementations§

Source§

impl<T> Individual<T>
where T: Tree,

Source

pub fn new<R: Rng>(tg: &mut TreeGen<'_, R>) -> Individual<T>

Generate a new Tree and individual.

Examples found in repository?
examples/snake.rs (line 255)
251fn main() {
252    let mut rng = OsRng::new().unwrap();
253    let mut tree_gen = TreeGen::full(&mut rng, 1, 4);
254
255    let mut indv1: Individual<SnakeTree> = Individual::new(&mut tree_gen);
256    let mut indv2: Individual<SnakeTree> = Individual::new(&mut tree_gen);
257
258    let mut rng = OsRng::new().unwrap();
259    let crossover = Crossover::one_point();
260    println!("---");
261    println!("{}", indv1);
262    println!("{}", indv2);
263    println!("---");
264    crossover.mate(&mut indv1, &mut indv2, &mut rng);
265    println!("{}", indv1);
266    println!("{}", indv2);
267    println!("---");
268
269    println!("{}", indv1);
270    let mut mutate_rng = OsRng::new().unwrap();
271    let mut tree_gen = TreeGen::full(&mut mutate_rng, 1, 2);
272    let mutation = Mutation::uniform();
273    mutation.mutate(&mut indv1, &mut tree_gen);
274    println!("{}", indv1);
275
276    let mut env = SnakeEnvironment {
277        size: Vector { x: 10, y: 10 },
278        food: Vector { x: 9, y: 9 },
279        snake: vec![Vector { x: 3, y: 3 }, Vector { x: 4, y: 3 }],
280    };
281
282    for _ in 0..200 {
283        let indv: Individual<SnakeTree> = Individual::new(&mut tree_gen);
284
285        let mut score1 = 0;
286        let mut score2 = 0;
287        let mut tick = 100;
288        while tick > 0 {
289            //println!("{:?} {:?}", tick, env.snake);
290            let move_ = indv.tree.evaluate(&env);
291            if env.sense_danger(move_) {
292                break;
293            }
294            if env.sense_food(move_) {
295                score2 += 1;
296                tick += 100;
297                env.food = Vector {
298                    x: rng.gen_range(0, 11),
299                    y: rng.gen_range(0, 11),
300                };
301            }
302            env.perform_movement(move_);
303            score1 += 1;
304            tick -= 1;
305        }
306        if score2 >= 1 {
307            println!("lived={:?} ate={:?}", score1, score2);
308            println!("{}", indv);
309        }
310    }
311}
More examples
Hide additional examples
examples/symbolic_regression.rs (line 168)
150fn main() {
151    let mut rng = OsRng::new().unwrap();
152    let mut tree_gen = TreeGen::full(&mut rng, 1, 4);
153
154    let mut rng = OsRng::new().unwrap();
155    let crossover = Crossover::one_point();
156
157    let mut mutate_rng = OsRng::new().unwrap();
158    let mut mut_tree_gen = TreeGen::full(&mut mutate_rng, 1, 2);
159    let mutation = Mutation::uniform();
160
161    let inputs: Vec<f64> = (-10..11).map(|i| (i as f64) / 10.0).collect();
162    let expecteds: Vec<f64> = inputs.iter()
163        .cloned()
164        .map(|i| i.powi(4) + i.powi(3) + i.powi(2) + i)
165        .collect();
166
167    let mut population: Vec<Individual<Equation>> =
168        (0..200).map(|_| Individual::new(&mut tree_gen)).collect();
169    for round in 0..40 {
170        let mut ranking = BinaryHeap::new();
171        for individual in population.drain(..) {
172            let mut sum_of_squared_errors = 0.0;
173            for i in 0..inputs.len() {
174                let input = inputs[i];
175                let expected = expecteds[i];
176                let output = individual.tree.evaluate(&input);
177                let squared_error = (output - expected).powi(2);
178                sum_of_squared_errors += squared_error;
179            }
180            if !sum_of_squared_errors.is_finite() {
181                sum_of_squared_errors = 100000000000.0;
182            }
183            ranking.push(RankedIndividual(sum_of_squared_errors, individual));
184        }
185
186        let ranking = ranking.into_sorted_vec();
187        //println!("{:?}", ranking);
188
189        println!("=== ROUND {} ===", round);
190        for i in 0..3 {
191            println!("Rank {:?}\n  Range = [-1.0, 1.0]    Step = +0.1\n  Comparing to x^4 + x^3 \
192                      + x^2 + x\n  Sum of squared error = {}\n  Equation = {}",
193                     i,
194                     ranking[i].0,
195                     ranking[i].1);
196        }
197
198        for i in 0..100 {
199            let RankedIndividual(_, mut indv1) = ranking[i].clone();
200            let RankedIndividual(_, mut indv2) = ranking[i + 1].clone();
201
202            population.push(indv1.clone());
203            population.push(indv2.clone());
204
205            crossover.mate(&mut indv1, &mut indv2, &mut rng);
206
207            if rng.gen() {
208                mutation.mutate(&mut indv1, &mut mut_tree_gen);
209            }
210            if rng.gen() {
211                mutation.mutate(&mut indv2, &mut mut_tree_gen);
212            }
213
214            population.push(indv1);
215            population.push(indv2);
216        }
217
218        println!();
219    }
220}
Source

pub fn new_from_tree(boxtree: BoxTree<T>) -> Individual<T>

Create from a Tree.

Source

pub fn nodes_count(&self) -> usize

Get cached number of nodes in tree.

Source

pub fn recalculate_metadata(&mut self)

Update cached metadata such at the number of nodes in the tree.

Trait Implementations§

Source§

impl<T> Clone for Individual<T>
where T: Tree + Clone,

Source§

fn clone(&self) -> Individual<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Individual<T>
where T: Tree + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for Individual<T>
where T: Tree + Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Individual<T>

§

impl<T> RefUnwindSafe for Individual<T>
where T: RefUnwindSafe,

§

impl<T> Send for Individual<T>
where T: Send,

§

impl<T> Sync for Individual<T>
where T: Sync,

§

impl<T> Unpin for Individual<T>

§

impl<T> UnwindSafe for Individual<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.