public static void InitPopulation() { population.Clear(); buffer.Clear(); complete = false; for (int i = 0; i < popSize; i++) { GA_Class citizen = new GA_Class(); citizen.fitness = 0; citizen.units = new bool[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int randSelect = rand.Next(0, 2); if (randSelect == 1) { citizen.units[x, y] = true; } else if (randSelect == 2) { citizen.units[x, y] = false; } } } population.Add(citizen); buffer.Add(citizen); } bestResult = population[0].units; }
private static void Mate() { float esize = popSize * eliteRate; int matePos, i1, i2; Elitism(esize); for (int i = (int)esize; i < popSize; i++) { i1 = rand.Next(0, popSize / 2); i2 = rand.Next(0, popSize / 2); matePos = rand.Next() % (width * height); GA_Class child = new GA_Class(); child.units = new bool[width, height]; int m = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (rand.Next() < mutation) { int randBool = rand.Next() % 2; if (randBool == 0) { child.units[x, y] = false; } else { child.units[x, y] = true; } } else if (m <= matePos) { child.units[x, y] = population[i1].units[x, y]; } else { child.units[x, y] = population[i2].units[x, y]; } m++; } } buffer[i] = child; } }