public Chromosome(Chromosome copy) { this.fitness = copy.fitness; this.distance = copy.distance; this.size = copy.size; this.order = new List <City>(copy.GetOrder()); }
// "Let's do it how they do it on discovery channel." public Chromosome Crossover(Chromosome partner) { var IndexA = Random.Next(0, size); var IndexB = Random.Next(IndexA, size); var selected = order.GetRange(IndexA, IndexB - IndexA + 1); var rest = partner.GetOrder().Except(selected).ToList(); var generation = rest.Take(IndexA) .Concat(selected) .Concat(rest.Skip(IndexA)) .ToList(); return(new Chromosome(size, generation)); }
// Time of the next era. Population NewGeneration(int size) { List <Chromosome> population = new List <Chromosome>(); for (int i = 0; i < size; i++) { var mother = Selection(); var father = Selection(); Chromosome c = mother.Crossover(father); // Grow a few tails foreach (City n in c.GetOrder()) { c = c.Mutate(); } population.Add(c); } return(new Population(population)); }