public string Debug(SuperLightGA ga, float[] genome) { int start = Start; string output = ""; for (int i = 0; i < MainClass.NumberOfSteps; i++) { float g1 = ga.ReadGene(genome, i, 0); float g2 = ga.ReadGene(genome, i, 1); int factor = (int)Math.Round(g1 * MaxJumpDistance); if (g2 < 0.5f) // Subtract { output += string.Format("- {0} ", factor); } else // Add { output += string.Format("+ {0} ", factor); } start = Perform( start, ga.ReadGene(genome, i, 0), ga.ReadGene(genome, i, 1) ); if (start == Target) { break; } } return(output + string.Format("= {0} ", start)); }
public static void Main(string[] args) { var jumpyFrog = new JumpFinder(1234); Console.WriteLine("Target: {0}", jumpyFrog.Target); Console.WriteLine("Start: {0}", jumpyFrog.Start); var ga = new SuperLightGA(); // Population of 50. Keep Top-2 from each generation ga.Initialize(50, 2, JumpFinder.MaxSteps, 2); int generations = ga.Run(jumpyFrog, false); Console.WriteLine("Generations: {0}", generations); for (int i = 0; i < ga.SurvivalCount; i++) { Console.WriteLine(" {0}", jumpyFrog.Debug(ga, ga.BestGenomes[i])); Console.WriteLine(" Score: {0}", ga.BestGenomeFitnesses[i]); } Console.ReadKey(); }
public float Evaluate(SuperLightGA ga, float[] genome) { int start = Start; int numberOfSteps; int score = 0; for (numberOfSteps = 0; numberOfSteps < MainClass.NumberOfSteps; numberOfSteps++) { start = Perform( start, ga.ReadGene(genome, numberOfSteps, 0), ga.ReadGene(genome, numberOfSteps, 1) ); if (start == Target) { break; } // Evaluate based on # of jumps and distance from goal score += (numberOfSteps + 1) * Math.Max(0, Math.Abs(Target - start)); } return(score); }