private void OnUpdate() { // CALC FITNESS for (int i = 0; i < Generations.Length; i++) { for (int j = 0; j < Generations[i].IterationLength - 1; j++) { string initials = Generations[i].GetSegmentValue(j); string initials1 = Generations[i].GetSegmentValue(j + 1); PointF a = Cities[initials]; PointF b = Cities[initials1]; Generations[i].Fitness += Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2)); } } Generations = Generations.OrderBy(p => p.Fitness).ToArray(); BestCreature = Generations[0]; SecondBestCreature = Generations[1]; for (int i = 0; i < Generations.Length; i++) { DNA <String> child = DNA <string> .Combine(BestCreature, SecondBestCreature, DNA <string> .CrossoverStrategy.SimplePlusWhatIsLeft, 2); double rate = (1.0 / Math.Pow(Generations[Generations.Length - 1].Fitness, 2)) * Math.Pow(BestCreature.Fitness, 2); rate *= 0.0045; child.Mutate(rate, 10, DNA <string> .MutationStrategy.SegmentSwap, 2); Generations[i] = new DNA <string>(new LinearConverter()) { FullSequence = child.FullSequence, FullLength = BestCreature.FullLength, SegmentLength = BestCreature.SegmentLength, IterationLength = BestCreature.IterationLength, }; } }