private List <Individual> Krzyzuj(CrossMethod crossMethod, List <Individual> population, int a, int b) { var twoRandomIndividuals = GetTwoRandomIndividuals(population); switch (crossMethod) { case CrossMethod.ONE_POINT: return(_krzyzowanieJednopunktowe.Krzyzuj(twoRandomIndividuals[0], twoRandomIndividuals[1], a, b)); case CrossMethod.TWO_POINT: return(_krzyzowanieDwupunktowe.Krzyzuj(twoRandomIndividuals[0], twoRandomIndividuals[1], a, b)); case CrossMethod.JEDNORODNE: return(_krzyzowanieJednorodne.Krzyzuj(twoRandomIndividuals[0], twoRandomIndividuals[1], a, b)); default: throw new NotImplementedException(); } }
private ICross GetCrossMethod(CrossMethod crossMethod) { switch (crossMethod) { case (CrossMethod.OnePoint): return(new OnePointCross()); case (CrossMethod.TwoPoints): return(new TwoPointCross()); case (CrossMethod.ThreePoints): return(new ThreePointCross()); case (CrossMethod.Homogeneous): return(new HomogeneousCross()); default: throw new ArgumentException("Cross method was not foud."); } }
public List <Individual> Execute(int a, int b, int populationAmount, int numberOfBits, int epochsAmount, double bestAndTournamentChomosomeAmount, double eliteStrategyAmount, double crossProbability, double mutationProbability, double inversionProbability, SelectionMethod selectionMethod, CrossMethod crossMethod, MutationMethod mutationMethod, bool maximization) { //Poczatkowa populacja var population = _individualGenerator.GenerateList(populationAmount, numberOfBits, a, b); DataSaver dataSaver = new DataSaver(); dataSaver.createFile(); var bestIndividuals = new List <Individual>(); for (int i = 0; i < epochsAmount; i++) { //Selekcja var afterSelection = Wyselekcjuj(selectionMethod, population, bestAndTournamentChomosomeAmount, maximization); var newPopulation = new List <Individual>(); // strategia elitarna - wez X procent najlepszych do nowej populacji newPopulation.AddRange(_selekcjaNajlepszych.Select(afterSelection, eliteStrategyAmount, maximization)); //Mutacja, krzyzowanie i inwersja while (newPopulation.Count < population.Count) { if (ShouldPerformAction(crossProbability) && newPopulation.Count + 1 < population.Count) { newPopulation.AddRange(Krzyzuj(crossMethod, afterSelection, a, b)); } if (ShouldPerformAction(mutationProbability) && newPopulation.Count < population.Count) { newPopulation.Add(Mutuj(mutationMethod, afterSelection, a, b)); } if (ShouldPerformAction(inversionProbability) && newPopulation.Count < population.Count) { newPopulation.Add(Inwersuj(afterSelection, a, b)); } } bestIndividuals.AddRange(_selekcjaNajlepszych.Select(newPopulation, 1.0 / (newPopulation.Count), maximization)); // dodanie najlepszego do listy dataSaver.saveIndividualsFromEpoche(i, population); population = newPopulation; } dataSaver.saveChart(bestIndividuals); return(population); }