Trait darwin_rs::individual::Individual [] [src]

pub trait Individual {
    fn new() -> Self;
    fn mutate(&mut self);
    fn calculate_fitness(&self) -> f64;
}

This trait has to be implemented for the user defined struct. Tip: Use lazy_static to share large data structure between individuals (see TSP example).

Required Methods

fn new() -> Self

This method creates a new individual.

fn mutate(&mut self)

This method mutates the individual. Usually this is a cheap and easy to implement function. In order to improve the simulation, the user can make this function a bit "smarter". This is nicely shown in the tsp and tsp2 example. The tsp2 example contains two types of mutation, tsp just one:

examples/tsp: 1. swap position

examples/tsp2: 1. swap position, 2. rotate (shift) positions

By just adding this one additional mutation type the simulation converges much faster to the optimum. Of course rotation can be "simulated" by a number of swaps, but re-doing all these steps takes time and the chances that these steps are taken in the correct order by just randomly swaping positions are very slim. So just start with one simple mutation function (one operation) and add more and more "smarter" mutation types to the mutate function.

fn calculate_fitness(&self) -> f64

This method calculates the fitness for the individual. Usually this is an expensive operation and a bit more difficult to implement, compared to the mutation method above. The lower the fitness value, the better (healthier) the individual is and the closer the individual is to the perfect solution. This can also correspont to the number of errors like for example in the sudoku or queens problem case.

Implementors