private void ga_OnGenerationComplete(object sender, GaEventArgs e) { Console.WriteLine("---Generation Complete---"); Console.WriteLine("Gen: " + e.Generation); Console.WriteLine("Fit: " + e.Population.MaximumFitness + " " + (e.Population.MaximumFitness - lastGenFitness)); lastGenFitness = e.Population.MaximumFitness; //get the best solution var chromosome = e.Population.GetTop(1)[0]; int numOfBytes = chromosome.ToBinaryString().Length / 8; byte[] bytes = new byte[numOfBytes]; for (int i = 0; i < numOfBytes; i++) { bytes[i] = Convert.ToByte(chromosome.ToBinaryString().Substring(8 * i, 8), 2); } Equation eq = Decoder.decodeToEquation(bytes); eq.fitness = e.Population.MaximumFitness; Console.WriteLine("Eq : " + eq.ToString()); form.backgroundWorker1.ReportProgress(Convert.ToInt32(e.Population.MaximumFitness * 100), JsonConvert.SerializeObject(eq)); }
public static double calculateY(Equation eq, double x) { return(eq.d0 * Math.Pow((x + eq.d1), 4) + eq.d2 * Math.Pow((x + eq.d3), 3) + eq.d4 * Math.Pow((x + eq.d5), 2) + eq.d6 * Math.Pow((x + eq.d7), 1) + eq.d8); }