protected override GeneticAlgorithm CreateGA() { m_fitness = new WallBuilderFitness(SecondsForEvaluation / TimeScale); var chromosome = new WallBuilderChromosome(BricksCount, MinPosition, MaxPosition); var crossover = new RandomCrossover() .AddCrossover(new UniformCrossover(), 0.9f) .AddCrossover(new SectionCrossover(chromosome.Length / chromosome.BricksCount, true), 0.1f); var mutation = new RandomMutation() .AddMutation(new FlipBitMutation(), .9f) .AddMutation(new UniformMutation(), .1f); var selection = new EliteSelection(); var population = new Population(NumberOfSimultaneousEvaluations, NumberOfSimultaneousEvaluations, chromosome); var ga = new GeneticAlgorithm(population, m_fitness, selection, crossover, mutation); ga.Termination = new TimeEvolvingTermination(System.TimeSpan.FromDays(1)); ga.TaskExecutor = new ParallelTaskExecutor { MinThreads = population.MinSize, MaxThreads = population.MaxSize * 2 }; ga.GenerationRan += delegate { m_lastPosition = Vector3.zero; }; return(ga); }
public override List <Candidate> Crossover(Candidate parentX, Candidate parentY) { List <Candidate> childrenList = new List <Candidate>(); float chance = (float)RandomCrossover.NextDouble(); if (chance < CrossoverChance) { IntegrityHelper.checkGens(parentX); IntegrityHelper.checkGens(parentY); int startIndex = RandomCrossover.Next(0, parentX.chromoson.Count() - 1); int endIndex = RandomCrossover.Next(0, parentX.chromoson.Count() - 1); //random indexes if (startIndex > endIndex) { int temp = startIndex; startIndex = endIndex; endIndex = temp; } int[,] mappingArray = createMappingArray(parentX, parentY, startIndex, endIndex); int[] childXChromosome = new int[parentX.chromoson.Count()]; int[] childYChromosome = new int[parentX.chromoson.Count()]; FillChromosone(ref childXChromosome, startIndex, mappingArray, 1); FillChromosone(ref childYChromosome, startIndex, mappingArray, 0); fillMissingGens(ref childXChromosome, parentX, startIndex, endIndex); fillMissingGens(ref childYChromosome, parentY, startIndex, endIndex); fillMappedGens(ref childXChromosome, mappingArray, parentX); fillDoubleMapp(ref childXChromosome, mappingArray, parentX); fillMappedGens(ref childYChromosome, mappingArray, parentY); fillDoubleMapp(ref childYChromosome, mappingArray, parentY); Candidate childX = new Candidate(parentX.generation, childXChromosome.ToList(), parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString()); Candidate childY = new Candidate(parentY.generation, childYChromosome.ToList(), parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString()); childX.generation = (parentX.generation + 1); childY.generation = (parentY.generation + 1); childrenList.Add(childX); childrenList.Add(childY); IntegrityHelper.checkGens(childrenList); } else { Candidate childX = new Candidate(parentX.generation, parentX.chromoson, parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString()); Candidate childY = new Candidate(parentY.generation, parentY.chromoson, parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString()); childX.generation = (parentX.generation + 1); childY.generation = (parentY.generation + 1); childrenList.Add(childX); childrenList.Add(childY); IntegrityHelper.checkGens(childrenList); } return(childrenList); }
public override List <Candidate> CrossoverPopulation(List <Candidate> population, int populationSize) { int parentX; int parentY; List <Candidate> newPopulation = new List <Candidate>(); int size = populationSize / 2; for (int i = 0; i < size; i++) { parentX = RandomCrossover.Next(0, population.Count()); parentY = RandomCrossover.Next(0, population.Count()); newPopulation.AddRange(Crossover(population[parentX], population[parentY])); } return(newPopulation); }
public override List <Candidate> Crossover(Candidate parentX, Candidate parentY) { List <Candidate> childrenList = new List <Candidate>(); float chance = (float)RandomCrossover.NextDouble(); if (chance < CrossoverChance) { int startIndex = RandomCrossover.Next(0, parentX.chromoson.Count() - 1); int endIndex = RandomCrossover.Next(0, parentX.chromoson.Count() - 1); if (startIndex > endIndex) { int temp = startIndex; startIndex = endIndex; endIndex = temp; } int[] childXChromosome = new int[parentX.chromoson.Count()]; int[] childYChromosome = new int[parentX.chromoson.Count()]; int[] parentYSection = new int[endIndex - startIndex]; int[] parentXSection = new int[endIndex - startIndex]; fillParentSection(ref parentXSection, parentX.chromoson, startIndex, endIndex); fillParentSection(ref parentYSection, parentY.chromoson, startIndex, endIndex); FillChromosoneWithParenScetion(ref childXChromosome, parentYSection, startIndex); FillChromosoneWithParenScetion(ref childYChromosome, parentXSection, startIndex); FillChromosoneWithMissingGens(ref childXChromosome, parentX.chromoson, endIndex); FillChromosoneWithMissingGens(ref childYChromosome, parentY.chromoson, endIndex); Candidate childX = new Candidate(parentX.generation + 1, childXChromosome.ToList(), parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString()); Candidate childY = new Candidate(parentY.generation + 1, childYChromosome.ToList(), parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString()); childrenList.Add(childX); childrenList.Add(childY); IntegrityHelper.checkGens(childrenList); } else { Candidate childX = new Candidate(parentX.generation, parentX.chromoson, parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString()); Candidate childY = new Candidate(parentY.generation, parentY.chromoson, parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString()); childX.generation = (parentX.generation + 1); childY.generation = (parentY.generation + 1); childrenList.Add(childX); childrenList.Add(childY); IntegrityHelper.checkGens(childrenList); } return(childrenList); }
public override List <Candidate> CrossoverPopulation(List <Candidate> population, int populationSize) { int parentX; int parentY; ConcurrentQueue <Candidate> newPopulation = new ConcurrentQueue <Candidate>(); int size = populationSize / 2; var res = Parallel.For(0, size, MultiTaskOptions.parallelOptCrossover, i => { parentX = RandomCrossover.Next(0, population.Count()); parentY = RandomCrossover.Next(0, population.Count()); var children = Crossover(population[parentX], population[parentY]); newPopulation.Enqueue(children[0]); newPopulation.Enqueue(children[1]); }); while (!res.IsCompleted) { } return(newPopulation.ToList()); }