public static double ComputeFitness(IInd inputInd) { //Convert StringInd.Value to a string of bits, for each 1 in the bits array add 1 to the fitness, //divide the fitness by the amount of bits Char[] bits = inputInd.ToCharArray(); var fitness = bits.Where(x => x == '1').Sum(x => 1.0); fitness = fitness/bits.Length; return fitness; }
public static IInd Mutation(IInd inpStringInd, double rate) { Char[] inpBits = inpStringInd.ToCharArray(); for (int i = 0; i < inpBits.Length; i++) { var r = Rand.NextDouble(); if (r <= rate) { switch (inpBits[i]) { case '0': inpBits[i] = '1'; break; case '1': inpBits[i] = '0'; break; } } } IInd res = new StringInd(new string(inpBits)); return res; }