public override List <Chromosome> Run(List <Chromosome> chromosomes, Parameters parameters) { chromosomes.Sort(); List <float> distribution = new List <float>(chromosomes.Count); float partialSum = 0.0f; var chosenOnes = new List <Chromosome>(); for (int i = 0; i < chromosomes.Count; ++i) { switch (parameters.FitnessStrategy) { case FitnessStrategy.MINIMIZE: partialSum += 1.0f / parameters.Fitness.Get(chromosomes[i]); break; case FitnessStrategy.MAXIMIZE: partialSum += parameters.Fitness.Get(chromosomes[i]); break; } distribution.Add(partialSum); } for (int i = 0; i < chromosomes.Count; ++i) { float nextRand = (float)RandomGeneratorThreadSafe.NextDouble(partialSum); float chosenOne = distribution.First(d => d >= nextRand); int index = distribution.IndexOf(chosenOne); Chromosome chosenSolution = chromosomes[index]; chosenOnes.Add(chosenSolution); } return(chosenOnes); }
/// <summary> /// Invokes the crossover operations on the next generation. /// </summary> /// <param name="population">The population.</param> /// <param name="parameters">The parameters.</param> /// <returns></returns> protected Population Crossover(IProblem problem, Population population, Parameters parameters) { Population newPopulation = population, childPopulation; foreach (var crossover in parameters.CrossoverOperators) { List <Thread> crossoverThreads = new List <Thread>(); int threadNumber = Environment.ProcessorCount; int tasksPerThread = (population.Size / threadNumber); tasksPerThread += tasksPerThread % 2; newPopulation = Selection(newPopulation, parameters); newPopulation.Randomize(); childPopulation = new Population(population.Size); for (int i = 0; i < threadNumber; ++i) { int start = i * tasksPerThread; int end; if (i == threadNumber - 1) { end = population.Size; } else { end = (i + 1) * tasksPerThread; } Thread thread = new Thread(() => { Chromosome parent1, parent2, child1, child2; IGene[] child1Genes, child2Genes; double nextCrossoverProb; for (int j = start; j < end; j += 2) { nextCrossoverProb = RandomGeneratorThreadSafe.NextDouble(); parent1 = newPopulation[j]; parent2 = newPopulation[j + 1]; if (nextCrossoverProb > parameters.CrossoverProbability) { childPopulation[j] = parent1; childPopulation[j + 1] = parent2; continue; } crossover.Run(parent1.Genes, parent2.Genes, out child1Genes, out child2Genes); child1 = chromosomeFactory.MakeChromosome(problem, parent1.Genes); child2 = chromosomeFactory.MakeChromosome(problem, parent2.Genes); childPopulation[j] = child1; childPopulation[j + 1] = child2; } }); crossoverThreads.Add(thread); thread.Start(); } foreach (Thread thread in crossoverThreads) { thread.Join(); } newPopulation = childPopulation; } return(newPopulation); }
protected void AnnealingStep(ref Chromosome currentSolution, ref Chromosome bestSolution, double temperature) { Chromosome newSolution = Parameters.ChromosomeFactory.RandomNeighbourSolution(currentSolution, Parameters.MutationOperators.First()); float delta = GetDelta(newSolution, currentSolution); if (delta < 0) { SetNewSolutions(newSolution, ref currentSolution, ref bestSolution); } else { double x = RandomGeneratorThreadSafe.NextDouble(); double e = Math.Exp(-delta / temperature); if (x < e) { SetNewSolutions(newSolution, ref currentSolution, ref bestSolution); } } }
private void PrepareUniform(int geneCount, out int[] ones, out int[] zeros) { List <int> onesList = new List <int>(), zerosList = new List <int>(); double nextDouble; for (int i = 0; i < geneCount; ++i) { nextDouble = RandomGeneratorThreadSafe.NextDouble(); if (nextDouble > 0.50000000) { onesList.Add(i); } else { zerosList.Add(i); } } ones = onesList.ToArray(); zeros = zerosList.ToArray(); }
/// <summary> /// Invokes the mutation operations on the next generation. /// </summary> /// <param name="population">The population.</param> /// <param name="parameters">The parameters.</param> /// <returns></returns> protected void Mutation(Population population, Parameters parameters) { foreach (var mutation in parameters.MutationOperators) { List <Thread> mutationThreads = new List <Thread>(); int threadNumber = Environment.ProcessorCount; int tasksPerThread = population.Size / threadNumber; for (int i = 0; i < threadNumber; ++i) { int start = i * tasksPerThread; int end = (i + 1) * tasksPerThread; if (i == threadNumber - 1) { end += population.Size % threadNumber; } Thread thread = new Thread(() => { double nextMutationProb; for (int j = start; j < end; ++j) { nextMutationProb = RandomGeneratorThreadSafe.NextDouble(); if (nextMutationProb > parameters.MutationProbability) { continue; } Chromosome chromosome = population[j]; mutation.Run(ref chromosome); } }); mutationThreads.Add(thread); thread.Start(); } foreach (Thread thread in mutationThreads) { thread.Join(); } } }