Fitness

We would like to give a score to how well our programs perform. This can be done by introducing a fitness function. The fitness function is arguably the most important part of evolutionary programming. Without a good fitness function, you likely will not get results you are hoping for.

Nadezhda

For our cockroach a Score will be nothing more than an alias for an integer.

1 pub type Score = i32;

A ScoreCard is a trait that can be implemented to assign a score to a program in an environment.

1 pub trait ScoreCard {
2     fn score(&self, program: Program, environment: Environment) -> Score;
3 }

We could create a dummy struct Config and implement ScoreCard for. A score would be given as the squared distance of the cockroach from the food location.

 1 impl ScoreCard for Config {
 2     fn score (&self, program: Program, environment: Environment) -> Score {
 3         let result = program.evaluate_on(environment);
 4         let score = 
 5             (result.cockroach_location - result.food_location)
 6             .abs()
 7             .pow(2);
 8             
 9         score
10     }
11 }

Exercises

  1. Determine the program length of a Nadezhda program and include it in the score.
  2. How would you extend the Nadezhda ScoreCard to include weight factors for the different parts of the score?