Wednesday, August 11, 2010

Genetic Algorithm with F# - Fitness

Fitness is the most important part of the GA, it applies the pressure to individual and guide the population's evolutionary direction. I always image I could apply different fitness function during the different evolution stages. Now I can use the following function to do so:

let maxFitness population fitnessF =
    let best = Seq.maxBy (fun (c:ChromosomeType)->c.Fitness(fitnessF)) population

I pass in the fitness function fitnessF every time.  Although the F# can know the type most of the time, but sometimes you have to tell a function what the parameter type is. In the function above, I use the maximum value as optimum, while some people prefer the minimum. If you like minimum better, just change the Seq.maxBy to Seq.minBy

No comments: