public Population TournamentSelection() { Population parents = new Population(); int nonFeasibleAllowed = (int)(Count * (1.0 - GA_GT.feasibleRate)); int nonFeasibleInParents = 0; while (parents.Count != Count) { int x = GA_GT.random.Next() % Count; int y = GA_GT.random.Next() % Count; while (y == x) { y = GA_GT.random.Next() % Count; } Individual chosen = null; if (getIndividual(x).fitness > getIndividual(y).fitness) { chosen = getIndividual(x); } else { chosen = getIndividual(y); } if (!chosen.isFeasible) { if (nonFeasibleInParents == nonFeasibleAllowed) { chosen = GetBestFeasible(); } else { nonFeasibleInParents++; } } parents.Add(chosen); } return(parents); }
public Individual RunGA_GT() { population = new Population(); population.RandomPopulation(cheaterRate, chromosomeLength, populationSize); maxFitness = knapsackList.MaxFitness(); //maxFitness only calculated once (sum of the values of the objects) maxPayoff = gameModel.maxPayoff; population.Evaluation(); for (int epoch = 0; epoch < numberOfEpochs; epoch++) { Population parents = population.TournamentSelection(); Population offspring = population.UniformCrossover(parents);//population.TwoPointsCrossover(parents); offspring.Mutation(); parents.Sort(); offspring.Sort(); // Another way of choosing the new generation: //population.Clear(); //population.AddRange(parents); //population.AddRange(offspring); //population.Sort(); //population.RemoveRange(populationSize, population.Count - populationSize); //// TODO: number of cheaters should be less than a cheater rate? or some other value? //population.Clear(); //for (int i = 0; i < populationSize; i++) //{ // //population.Add(parents.getIndividual(i)); // population.Add(offspring.getIndividual(i)); //} population.Clear(); int nonFeasibleAllowed = (int)(populationSize * (1.0 - feasibleRate)); int nonFeasibleInPopulation = 0; for (int i = 0; i < offspring.Count; i++) { if (!offspring.getIndividual(i).isFeasible) { if (nonFeasibleInPopulation == nonFeasibleAllowed) { continue; } else { population.Add(offspring.getIndividual(i)); nonFeasibleInPopulation++; } } else { population.Add(offspring.getIndividual(i)); } } if (population.Count < populationSize) { for (int i = 0; i < parents.Count; i++) { if (population.Count == populationSize) { break; } if (parents.getIndividual(i).isFeasible) { population.Add(parents.getIndividual(i)); } } } population.GetBestFeasible().ShowValue(); population.Evaluation(); } population.Sort(); population.Show(); Console.ReadLine(); return(population.GetBestFeasible()); }
public Population UniformCrossover(Population parents) { Population offspring = new Population(); List <int> unused = new List <int>(); for (int i = 0; i < parents.Count; i++) { unused.Add(i); } int chromosomeSize = getChromosomeSize(); for (int i = 0; i < parents.Count; i++) { if (!unused.Contains(i)) { continue; } unused.Remove(i); int randomIndex = unused.ElementAt(GA_GT.random.Next() % unused.Count); unused.Remove(randomIndex); if (GA_GT.random.NextDouble() >= GA_GT.crossoverRate) { offspring.Add(parents.getIndividual(i)); offspring.Add(parents.getIndividual(randomIndex)); } else { Chromosome ch1 = new Chromosome(chromosomeSize); Chromosome ch2 = new Chromosome(chromosomeSize); for (int j = 0; j < chromosomeSize; j++) { if (GA_GT.random.NextDouble() < 0.5) { ch1[j] = parents.getIndividual(i)[j]; ch2[j] = parents.getIndividual(randomIndex)[j]; } else { ch1[j] = parents.getIndividual(randomIndex)[j]; ch2[j] = parents.getIndividual(i)[j]; } } Individual ind1 = new Individual(ch1); ind1.strategy = parents.getIndividual(i).strategy; ind1.Update(GA_GT.knapsackList); Individual ind2 = new Individual(ch2); ind2.strategy = parents.getIndividual(randomIndex).strategy; ind2.Update(GA_GT.knapsackList); offspring.Add(ind1); offspring.Add(ind2); } } return(offspring); }
public Population UniformCrossover(Population parents) { Population offspring = new Population(); List<int> unused = new List<int>(); for (int i = 0; i < parents.Count; i++) { unused.Add(i); } int chromosomeSize = getChromosomeSize(); for (int i = 0; i < parents.Count; i++) { if (!unused.Contains(i)) continue; unused.Remove(i); int randomIndex = unused.ElementAt(GA_GT.random.Next() % unused.Count); unused.Remove(randomIndex); if (GA_GT.random.NextDouble() >= GA_GT.crossoverRate) { offspring.Add(parents.getIndividual(i)); offspring.Add(parents.getIndividual(randomIndex)); } else { Chromosome ch1 = new Chromosome(chromosomeSize); Chromosome ch2 = new Chromosome(chromosomeSize); for (int j = 0; j < chromosomeSize; j++) { if (GA_GT.random.NextDouble() < 0.5) { ch1[j] = parents.getIndividual(i)[j]; ch2[j] = parents.getIndividual(randomIndex)[j]; } else { ch1[j] = parents.getIndividual(randomIndex)[j]; ch2[j] = parents.getIndividual(i)[j]; } } Individual ind1 = new Individual(ch1); ind1.strategy = parents.getIndividual(i).strategy; ind1.Update(GA_GT.knapsackList); Individual ind2 = new Individual(ch2); ind2.strategy = parents.getIndividual(randomIndex).strategy; ind2.Update(GA_GT.knapsackList); offspring.Add(ind1); offspring.Add(ind2); } } return offspring; }
public Population TournamentSelection() { Population parents = new Population(); int nonFeasibleAllowed = (int)(Count * (1.0 - GA_GT.feasibleRate)); int nonFeasibleInParents = 0; while (parents.Count != Count) { int x = GA_GT.random.Next() % Count; int y = GA_GT.random.Next() % Count; while (y == x) { y = GA_GT.random.Next() % Count; } Individual chosen = null; if (getIndividual(x).fitness > getIndividual(y).fitness) { chosen = getIndividual(x); } else { chosen = getIndividual(y); } if (!chosen.isFeasible) { if (nonFeasibleInParents == nonFeasibleAllowed) { chosen = GetBestFeasible(); } else { nonFeasibleInParents++; } } parents.Add(chosen); } return parents; }
public Individual RunGA_GT() { population = new Population(); population.RandomPopulation(cheaterRate, chromosomeLength, populationSize); maxFitness = knapsackList.MaxFitness(); //maxFitness only calculated once (sum of the values of the objects) maxPayoff = gameModel.maxPayoff; population.Evaluation(); for (int epoch = 0; epoch < numberOfEpochs; epoch++) { Population parents = population.TournamentSelection(); Population offspring = population.UniformCrossover(parents);//population.TwoPointsCrossover(parents); offspring.Mutation(); parents.Sort(); offspring.Sort(); // Another way of choosing the new generation: //population.Clear(); //population.AddRange(parents); //population.AddRange(offspring); //population.Sort(); //population.RemoveRange(populationSize, population.Count - populationSize); //// TODO: number of cheaters should be less than a cheater rate? or some other value? //population.Clear(); //for (int i = 0; i < populationSize; i++) //{ // //population.Add(parents.getIndividual(i)); // population.Add(offspring.getIndividual(i)); //} population.Clear(); int nonFeasibleAllowed = (int)(populationSize * (1.0 - feasibleRate)); int nonFeasibleInPopulation = 0; for (int i = 0; i < offspring.Count; i++) { if (!offspring.getIndividual(i).isFeasible) { if (nonFeasibleInPopulation == nonFeasibleAllowed) continue; else { population.Add(offspring.getIndividual(i)); nonFeasibleInPopulation++; } } else { population.Add(offspring.getIndividual(i)); } } if (population.Count < populationSize) { for (int i = 0; i < parents.Count; i++) { if (population.Count == populationSize) break; if (parents.getIndividual(i).isFeasible) population.Add(parents.getIndividual(i)); } } population.GetBestFeasible().ShowValue(); population.Evaluation(); } population.Sort(); population.Show(); Console.ReadLine(); return population.GetBestFeasible(); }