public void Mutate() { /* * Mutate by, * 1. Randomalty select men, women or dog (i.e. person) * 2. Randomalty select two triplets * 3. swap the person in two triplets */ int index1 = (int)(GA.NextRandom * m_capacity); int index2 = (int)(GA.NextRandom * m_capacity); Person type = null; switch ((int)(GA.NextRandom * 3)) { case 0: type = new Men(); break; case 1: type = new Women(); break; case 2: type = new Dog(); break; default: Debug.Assert(false); break; } int first = m_triples[index1].Get(type); int second = m_triples[index2].Get(type); m_triples[index1].Set(type, second); m_triples[index2].Set(type, first); }
public void ComputeFitness() { m_fitness = 0; // iterate over all triplets, and sum all preferences differences from optimum foreach (triple_t triple in m_triples) { Men men = (Men)m_men_arr[triple.Men]; Women women = (Women)m_women_arr[triple.Women]; Dog dog = (Dog)m_dogs_arr[triple.Dog]; int fitness = 0; fitness += men.preference_first(triple.Women); fitness += men.preference_second(triple.Dog); fitness += women.preference_first(triple.Men); fitness += women.preference_second(triple.Dog); fitness += dog.preference_first(triple.Men); fitness += dog.preference_second(triple.Women); triple.Fitness = fitness; m_fitness += fitness; } }