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

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

    fn new_fittest_found(&mut self) { ... }

This trait has to be implemented for the user defined struct. In order to share common data between all individuals use Arc. See TSP and OCR exmaples.

TODO: add serialization, see

Required Methods

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.

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.

This method resets each individual to an initial state. For example in the "queens" case it would reset the queens position randomly (or all in the first row).

Provided Methods

This method is called whenever a new fittest individual is found. It is usefull when you want to provide some additional information or do some statistics. It is optional and the default implementation does nothing.