private void CreateNewGenerationWithTournamentSelection() { Cromozom tempFirstCromozom; Cromozom tempSecondCromozom; newCromozomsPopulation = new List <Cromozom> (); List <Cromozom> listaNoua = new List <Cromozom> (); for (int i = 0; i < 5; i++) { listaNoua.Add(cromozomsPopulation[rand.Next(0, cromozomsPopulation.Count)]); } listaNoua = listaNoua.OrderByDescending(s => s.Fitness).ToList(); ApplyCrossover(listaNoua[0], listaNoua[1]); foreach (var cromozom in newCromozomsPopulation) { if (rand.NextDouble() >= geneticAlgorithmOptions.MutationOccurance) { cromozom.Mutate(); } } tempFirstCromozom = newCromozomsPopulation [0]; tempSecondCromozom = newCromozomsPopulation [1]; Cromozom[] temporaryCromozomArray = new Cromozom[cromozomsPopulation.Count]; cromozomsPopulation.CopyTo(temporaryCromozomArray); newCromozomsPopulation = temporaryCromozomArray.ToList(); newCromozomsPopulation.Add(tempFirstCromozom); newCromozomsPopulation.Add(tempSecondCromozom); newCromozomsPopulation = newCromozomsPopulation.OrderBy(s => s.Fitness).ToList(); newCromozomsPopulation.RemoveRange(0, 2); }
public List <Cromozom> Start(GeneticAlgorithmOptions geneticAlgorithmOptions) { this.geneticAlgorithmOptions = geneticAlgorithmOptions; newCromozomsPopulation = new List <Cromozom> (); elitistPercent = geneticAlgorithmOptions.ElitesPercentage; currentGeneration = 0; Initialize algorithm = new Initialize(); cromozomsPopulation = algorithm.GeneratePopulation(geneticAlgorithmOptions); int benchmarkCounter = 1; while (currentGeneration < geneticAlgorithmOptions.NumberOfGenerations) { IOFunctions.ClearFiles("./", "ta.log*"); ClearPastFitness(); benchmarkCounter = 1; foreach (var benchmark in geneticAlgorithmOptions.Benchmarks) { for (int i = 0; i < cromozomsPopulation.Count; i++) { ExecutaComandaSimulator(i, benchmark, cromozomsPopulation[i].Configuration.Memory, cromozomsPopulation[i].Configuration.OptimizationLevel); proc.WaitForExit(); CalculateFitness(i, benchmarkCounter); } benchmarkCounter++; } Cromozom[] cromozomPopulationCopy = new Cromozom [cromozomsPopulation.Count]; cromozomsPopulation.CopyTo(cromozomPopulationCopy); AppendHistory(cromozomPopulationCopy.ToList(), currentGeneration); if (IsNotLastIteration(geneticAlgorithmOptions.NumberOfGenerations)) { if (geneticAlgorithmOptions.SelectionMode == AlgorithmSelectionMode.Elitist) { CreateNewGenerationWithElitistSelection(); } if (geneticAlgorithmOptions.SelectionMode == AlgorithmSelectionMode.Tournament) { CreateNewGenerationWithTournamentSelection(); } cromozomsPopulation = newCromozomsPopulation; IOFunctions.ClearFiles("vex/configurations/", "*cfg"); for (int i = 0; i < cromozomsPopulation.Count; i++) { IOFunctions.CreateConfigFile(cromozomsPopulation [i], i.ToString()); } } currentGeneration++; } return(cromozomsPopulation); }