/// <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 override void Run(IGene[] genes) { int indexOfElement, indexOfDestination; RandomGeneratorThreadSafe.NextTwoDifferentInts(genes.Length, out indexOfElement, out indexOfDestination); Insert(genes, indexOfElement, indexOfDestination); }
/// <summary> /// Runs the PMX operator. /// The cut points are chosen randomly. /// </summary> /// <param name="parent1">The first parent.</param> /// <param name="parent2">The second parent.</param> /// <param name="child1">The first child.</param> /// <param name="child2">The second child.</param> public override void Run(IGene[] parent1, IGene[] parent2, out IGene[] child1, out IGene[] child2) { int cutPoint1, cutPoint2; RandomGeneratorThreadSafe.NextTwoIntsFirstBigger(1, parent1.Count() - 1, out cutPoint1, out cutPoint2); Run(parent1, parent2, out child1, out child2, cutPoint1, cutPoint2); }
public override void Run(IGene[] genes) { int index1, index2, customerCount = genes.Count(); RandomGeneratorThreadSafe.NextTwoDifferentInts(customerCount, out index1, out index2); Swap(genes, index1, index2); }
public override List <Chromosome> Run(List <Chromosome> chromosomes, Parameters parameters) { int k = 3, index; var chosenOnes = new Chromosome[chromosomes.Count]; var tournament = new Chromosome[k]; Chromosome winner = null; for (int i = 0; i < chromosomes.Count; ++i) { for (int j = 0; j < k; ++j) { index = RandomGeneratorThreadSafe.NextInt(chromosomes.Count); tournament[j] = chromosomes[index]; } //if (parameters.FitnessStrategy == Fitness.FitnessStrategy.MAXIMIZE) winner = tournament[0]; for (int j = 1; j < k; ++j) { if (parameters.Fitness.Get(tournament[j]) < parameters.Fitness.Get(winner)) { winner = tournament[j]; } } // winner = tournament.Aggregate((c1, c2) => parameters.Fitness.Get(c1) < parameters.Fitness.Get(c2) ? c1 : c2); chosenOnes[i] = (Chromosome)winner.Clone(); } return(chosenOnes.ToList()); }
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); }
public override void Run(IGene[] genes) { int startPosition = RandomGeneratorThreadSafe.NextInt(genes.Length - 1); int length = RandomGeneratorThreadSafe.NextInt(genes.Length - startPosition - 2) + 2; Inverse(genes, startPosition, length); }
/// <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); }
public override void Run(ref Chromosome solution) { int indexOfElement, indexOfDestination; RandomGeneratorThreadSafe.NextTwoDifferentInts(solution.Genes.Length, out indexOfElement, out indexOfDestination); Insert(solution.Genes, indexOfElement, indexOfDestination); solution.Refresh(); }
public override void Run(ref Chromosome solution) { int startPosition = RandomGeneratorThreadSafe.NextInt(solution.Genes.Length - 1); int length = RandomGeneratorThreadSafe.NextInt(solution.Genes.Length - startPosition - 2) + 2; Inverse(solution.Genes, startPosition, length); solution.Refresh(); }
/// <summary> /// Runs the swap operator. /// </summary> /// <param name="solution">The referenced solution for a change.</param> public override void Run(ref Chromosome solution) { var genes = solution.Genes; int index1, index2, customerCount = genes.Count(); RandomGeneratorThreadSafe.NextTwoDifferentInts(customerCount, out index1, out index2); Swap(genes, index1, index2); solution.Refresh(); }
public override void Run(IGene[] genes) { int startIndex, destIndex, length; RandomGeneratorThreadSafe.NextTwoDifferentInts(genes.Length - 1, out startIndex, out destIndex); int upperLimit = startIndex > destIndex ? startIndex : destIndex; length = RandomGeneratorThreadSafe.NextInt(1, genes.Length - upperLimit + 1); Displace(genes, startIndex, destIndex, length); }
public override void Run(ref Chromosome solution) { int startIndex, destIndex, length; RandomGeneratorThreadSafe.NextTwoDifferentInts(solution.Genes.Length - 1, out startIndex, out destIndex); int upperLimit = startIndex > destIndex ? startIndex : destIndex; length = RandomGeneratorThreadSafe.NextInt(1, solution.Genes.Length - upperLimit + 1); Displace(solution.Genes, startIndex, destIndex, length); solution.Refresh(); }
public override void Run(IGene[] parent1, IGene[] parent2, out IGene[] child1, out IGene[] child2) { int customerCount = parent1.Count(); IGene[] c1 = new IGene[customerCount]; IGene[] c2 = new IGene[customerCount]; int slicePoint = RandomGeneratorThreadSafe.NextInt(customerCount); DoCycleCrossover(parent1, parent2, c1, c2, slicePoint, true); child1 = c1; child2 = c2; }
public static void Shuffle <T>(this IList <T> list) { int n = list.Count, k; while (n > 1) { --n; k = RandomGeneratorThreadSafe.NextInt(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } }
public override Chromosome RandomNeighbourSolution(Chromosome chromosome) { IGene[] newGenes; Chromosome newSolution, newChromosome; newChromosome = (Chromosome)chromosome.Clone(); newGenes = newChromosome.Genes; int position1, position2; RandomGeneratorThreadSafe.NextTwoDifferentInts(chromosome.Size(), out position1, out position2); IGene swappedGene1 = newGenes[position1]; IGene swappedGene2 = newGenes[position2]; newGenes[position1] = swappedGene2; newGenes[position2] = swappedGene1; newSolution = new Solution((VrptwProblem)chromosome.Problem, newGenes); return(newSolution); }
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> /// Runs the OX operator. /// </summary> /// <param name="parent1">The first parent.</param> /// <param name="parent2">The second parent.</param> /// <param name="child1">The first child.</param> /// <param name="child2">The second child.</param> public override void Run(IGene[] parent1, IGene[] parent2, out IGene[] child1, out IGene[] child2) { int cutPoint1, cutPoint2, customerCount = parent1.Count(); RandomGeneratorThreadSafe.NextTwoIntsFirstBigger(1, customerCount - 1, out cutPoint1, out cutPoint2); IGene[] c1 = new IGene[customerCount]; IGene[] c2 = new IGene[customerCount]; Array.Copy(parent1, cutPoint1, c1, cutPoint1, cutPoint2 - cutPoint1); Array.Copy(parent2, cutPoint1, c2, cutPoint1, cutPoint2 - cutPoint1); int iterations = customerCount - (cutPoint2 - cutPoint1); int parentPosition2 = cutPoint2 - 1, parentPosition1 = cutPoint2 - 1; for (int i = 0; i < iterations; ++i) { int position = (cutPoint2 + i) % customerCount; FillEmptyGenes(position, ref parentPosition2, parent2, c1); FillEmptyGenes(position, ref parentPosition1, parent1, c2); } child1 = c1; child2 = c2; }
/// <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(); } } }
private bool[] PrepareUniform(int geneCount) { return(Enumerable.Repeat(0, geneCount).Select(i => RandomGeneratorThreadSafe.NextBool()).ToArray()); }