/// <summary> /// Runs a random walk of n steps along a landscape defined by a crossover operator and returns its results. /// </summary> /// <param name="steps">Number of steps.</param> /// <param name="searchOperator">Operator defining a neighbourhood.</param> /// <returns></returns> public RandomWalk RandomWalk(int steps, CrossoverOperator searchOperator) { RandomWalk statistics = new RandomWalk(steps); Chromosome parent1 = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem); Chromosome parent2 = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem); bool firstParent = RandomGeneratorThreadSafe.NextBool(); Chromosome parent = (firstParent ? parent1 : parent2), child; GatherData(parent, 0, statistics); const int minPopSize = 8, maxPopSize = 15; int popSize = RandomGeneratorThreadSafe.NextInt(minPopSize, maxPopSize); Chromosome[] supportPopulation = new Chromosome[popSize]; for (int i = 0; i < popSize; ++i) { supportPopulation[i] = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem); } IGene[] childGenes1, childGenes2; for (int i = 0; i < steps; ++i) { searchOperator.Run(parent1.Genes, parent2.Genes, out childGenes1, out childGenes2); child = ChromosomeFactory.MakeChromosome(Problem, RandomGeneratorThreadSafe.NextBool() ? childGenes1 : childGenes2); GatherData(child, i, statistics); parent1 = child; parent2 = supportPopulation[RandomGeneratorThreadSafe.NextInt(popSize)]; } return(statistics); }
public void Initialize(int poolSize) { _pool = new List <C>(); _poolSize = poolSize; for (int i = 0; i < _poolSize; i++) { _pool.Add(ChromosomeFactory.CreateChromosome()); } }
public GeneticNeuronalNetwork(int populationSize, List <Article> products, int score, int percentSelection) { _populationSize = populationSize; _products = products; _score = score; _percentSelection = percentSelection; _chromosomes = new List <Chromosome>(); _factory = new ChromosomeFactory(); }
/// <summary> /// Runs a random walk of n steps along a landscape defined by a mutation operator and returns its results. /// </summary> /// <param name="steps">Number of steps.</param> /// <param name="searchOperator">Operator defining a neighbourhood.</param> /// <returns></returns> public RandomWalk RandomWalk(int steps, MutationOperator searchOperator) { RandomWalk statistics = new RandomWalk(steps); Chromosome currentSolution = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem); GatherData(currentSolution, 0, statistics); for (int i = 1; i < steps; ++i) { searchOperator.Run(ref currentSolution); GatherData(currentSolution, i, statistics); } return(statistics); }
public void ClonePopulation_ChangingCloneDosntChangeOriginal() { var population = (new double[] { 1, 1, 1, 1 }).ToPopulation("Chromosomes"); var populationClone = population.Clone(); var fakeChromosome = ChromosomeFactory.CreateChromosome(10, "New"); populationClone.GetChromosomes()[0] = fakeChromosome; foreach (var chromosome in population.GetChromosomes()) { Assert.AreNotEqual(fakeChromosome, chromosome); } }
public void ChangeHistoryLastGeneration_AlgorithmPopulationNotChanged() { var expectedPopulation = new double[] { 1, 1 }; var engine = new TestGeneticSearchEngineBuilder(2, 10, expectedPopulation).IncludeAllHistory().Build(); var fakeChromosome = ChromosomeFactory.CreateChromosome(10, "ChangedChromosome"); var result = engine.Next(); result.History[result.History.Count - 1][0] = fakeChromosome; result = engine.GetCurrentPopulation(); foreach (var chromosome in result.Population.GetChromosomes()) { Assert.AreNotEqual(fakeChromosome, chromosome); } }
public DeploymentModel Run(DeploymentModel initialDeploymentModel) { var initialChromosome = ChromosomeFactory.Create(initialDeploymentModel); var population = InitialPopulationCreator.CreateInitialPopulation(initialChromosome, MinPopulationSize, MaxPopulationSize); while (!TerminationCondition.HasReached(population, CurrentState)) { var parents = SelectionStrategyStrategy.SelectChromosomes(MinPopulationSize, population).ToArray(); var offspring = Cross(parents).ToArray(); offspring = Mutate(offspring).ToArray(); population = ReinsertionStrategy.Reinsert(offspring, population); foreach (var deployment in population.Deployments) { deployment.Fitness = FitnessEvaluator.Evaluate(deployment, Workload); } CurrentState.UpdateOnGenerationRan(population); } return(population.BestDeployment.ToDeploymentModel()); }