Ejemplo n.º 1
0
 private static void gasolver_CrossoverSelected(CrossoverEventArgs args)
 {
     if (_createDiagLogs)
     {
         using (var sw = new StreamWriter(_crossoverlogFilename, true)) {
             sw.WriteLine(args.MovesParent1);
             sw.WriteLine(args.MovesParent2);
             sw.WriteLine("----------------------------------------------------------------------------------------------------");
             sw.Flush();
             sw.Close();
         }
     }
 }
Ejemplo n.º 2
0
        private void PerformCrossover(IRandomizer randomizer)
        {
            var x       = new ChromosomeSelector(_currentGeneration);
            var parent1 = x.Get(randomizer);
            var parent2 = x.Get(randomizer);

            MoveList child1;
            MoveList child2;

            if (parent1.CompareTo(parent2) == 0)
            {
                child1 = Perform2ROG(randomizer);
                child2 = Perform2ROG(randomizer);
                _currentGeneration.Add(child1);
                _currentGeneration.Add(child2);
            }
            else
            {
                if (CrossoverSelected != null)
                {
                    var coEventArgs = new CrossoverEventArgs {
                        MovesParent1 = parent1.ToString(),
                        MovesParent2 = parent2.ToString()
                    };
                    CrossoverSelected(coEventArgs);
                }

                //Get crossover strategy
                var strategy = CrossoverFactory.GetCrossoverStrategy(randomizer);

                //Do crossover
                var children = strategy.DoCrossover(parent1, parent2);

                child1 = children.Item1;
                child2 = children.Item2;

                CheckAndAddValidChild(child1, true, false);
                CheckAndAddValidChild(child2, true, false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Main process loop for performing a crossover on a population.
        /// </summary>
        protected void Process()
        {
            int maxLoop    = 100;
            int eliteCount = 0;

            //reset the number of evaluations
            _evaluations = 0;

            //now that we know how many children we are to be creating, in the case of 'Delete Last'
            //we copy the current generation across and add new children where they are greater than
            //the worst solution.
            if (_replacementMethodS == ReplacementMethod.DeleteLast)
            {
                //copy everything accross including the elites
                NewPopulation.Solutions.Clear();
                NewPopulation.Solutions.AddRange(CurrentPopulation.Solutions);
            }
            else
            {
                //just copy the elites, this will take all elites

                //TODO: Sort out what we do if we overfill the population with elites
                var elites = CurrentPopulation.GetElites();
                eliteCount = elites.Count();
                if (elites != null && eliteCount > 0)
                {
                    NewPopulation.Solutions.AddRange(elites);
                }
            }
            _currentPopulationSize = CurrentPopulation.Solutions.Count;

            _numberOfChildrenToGenerate =
                _currentPopulationSize - eliteCount;

            while (_numberOfChildrenToGenerate > 0)
            {
                //emergency exit
                if (maxLoop <= 0)
                {
                    throw new ChromosomeException("Unable to create a suitable child. If duplicates have been disallowed then consider increasing the chromosome length or increasing the number of elites.");
                }

                //these will hold the children
                Chromosome c1 = null;
                Chromosome c2 = null;

                //select some parents
                var parents = CurrentPopulation.SelectParents();
                var p1      = parents[0];
                var p2      = parents[1];

                //crossover
                var crossoverData   = CreateCrossoverData(p1.Genes.Count, CrossoverType);
                var crossoverResult = PerformCrossover(p1, p2, CrossoverProbability, CrossoverType, crossoverData, out c1, out c2);

                //pass the children out to derived classes
                //(e.g. CrossoverMutate class uses this to perform mutation)
                if (OnCrossoverComplete != null)
                {
                    var eventArgs = new CrossoverEventArgs(crossoverResult);
                    OnCrossoverComplete(this, eventArgs);
                }

                if (AddChild(c1))
                {
                    _numberOfChildrenToGenerate--;
                }
                else
                {
                    //unable to create child
                    maxLoop--;
                }

                //see if we can add the secomd
                if (_numberOfChildrenToGenerate > 0)
                {
                    if (AddChild(c2))
                    {
                        _numberOfChildrenToGenerate--;
                    }
                    else
                    {
                        //unable to create child
                        maxLoop--;
                    }
                }
            }
        }