private static Chromosome[] ParentSelection(ref MatingPool matingPool) { int firstRandom; int secondRandom; Chromosome candidate1; Chromosome candidate2; Chromosome[] parents = new Chromosome[2]; for (int i = 0; i < 2; i++) { firstRandom = Helpers.GetRandomInt(0, PARENT_COUNT); secondRandom = Helpers.GetRandomInt(0, PARENT_COUNT); // Make sure arbitrarily selected two choromosomes not the same ones... while (firstRandom == secondRandom) { secondRandom = Helpers.GetRandomInt(0, PARENT_COUNT); } // ...so candidate1 is not identical to candidate2. candidate1 = matingPool[firstRandom]; candidate2 = matingPool[secondRandom]; parents[i] = candidate1.Fitness <= candidate2.Fitness ? candidate1 : candidate2; } return(parents); }
public static void Run() { //Debug.WriteLine("PM | " + parentMatingPool); // Phase 1 childrenMatingPool = new MatingPool(); // Phase 2 for (int i = 0; i < PARENT_COUNT / 2; i++) { childrenMatingPool.Add(CrossOver()); } //Debug.WriteLine(childrenMatingPool.ToString()); // Phase 3 for (int i = 0; i < PARENT_COUNT; i++) { childrenMatingPool[i].Mutate(30); } //Debug.WriteLine("CH | " + childrenMatingPool.ToString()); // Phase 4 const int PARENT_COUNT_TO_BE_DELETED = PARENT_COUNT - 2; const int CHILDREN_COUNT_TO_BE_DELETED = PARENT_COUNT - 4; List <int> fitnessValues = new List <int>(PARENT_COUNT); for (int i = 0; i < PARENT_COUNT; i++) { fitnessValues.Add((int)parentMatingPool[i].Fitness); } int indexToBeDeleted = -1; for (int i = 0; i < PARENT_COUNT_TO_BE_DELETED; i++) { indexToBeDeleted = fitnessValues.IndexOf(fitnessValues.Max()); fitnessValues[indexToBeDeleted] = -1; parentMatingPool._chromosomes[indexToBeDeleted].Clear(); } fitnessValues.Clear(); for (int i = 0; i < PARENT_COUNT; i++) { fitnessValues.Add((int)childrenMatingPool[i].Fitness); } indexToBeDeleted = -1; for (int i = 0; i < CHILDREN_COUNT_TO_BE_DELETED; i++) { indexToBeDeleted = fitnessValues.IndexOf(fitnessValues.Max()); fitnessValues[indexToBeDeleted] = -1; childrenMatingPool._chromosomes[indexToBeDeleted].Clear(); } // Combine survived parents and children int lastpos = 0; for (uint k = 0; k < PARENT_COUNT; k++) { if (parentMatingPool._chromosomes[k].Fitness != null) { continue; } while (childrenMatingPool._chromosomes[lastpos].Fitness == null) { lastpos++; } parentMatingPool._chromosomes[k] = new Chromosome(childrenMatingPool._chromosomes[lastpos++].genes); } //Debug.WriteLine("PA | " + parentMatingPool.ToString()); }