示例#1
0
        private GenomePair GetChromoRoulette()
        {
            //generate a random number between 0 & total fitness count
            double Slice = AIUtils.Random(_totalFitnessForChromoRoulette);
            //this will be set to the chosen chromosome
            GenomePair TheChosenOne = null;

            //go through the chromosones adding up the fitness so far
            double FitnessSoFar = 0;

            foreach (GenomePair genpair in _population)
            {
                FitnessSoFar += genpair.Genome.Fitness;
                if (FitnessSoFar >= Slice)
                {
                    TheChosenOne = genpair;
                    break;
                }
            }
            return(TheChosenOne);
        }
示例#2
0
        public List <GenomePair> Epoch(List <GenomePair> old_population)
        {
            _generation++;

            _population     = old_population;
            _totalFitness   = 0;
            _bestFitness    = 0;
            WorstFitness    = double.MaxValue;
            _averageFitness = 0;

            // this sort is requiered for further scale and fintess calculations!
            _population.Sort(GenomePair.GenomePairComparison);

            CalculateFitnesses();
            ScaleFitnesses();

            List <GenomePair> new_population = new List <GenomePair>();

            if (AIParams.NumCopiesElite * AIParams.NumElite % 2 == 0)
            {
                GrabNBest(AIParams.NumElite, AIParams.NumCopiesElite, ref new_population);
            }

            while (new_population.Count < _populationSize)
            {
                GenomePair mum = GetChromoRoulette();
                GenomePair dad = GetChromoRoulette();

                if (mum == null)
                {
                    mum = new_population[0];
                }
                if (dad == null)
                {
                    dad = new_population[2];
                }

                List <double> baby1 = new List <double>();
                List <double> baby2 = new List <double>();

                try
                {
                    if (AIParams.MultiPointCrossover)
                    {
                        CrossoverSplitPoints(mum.Genome.Weights, dad.Genome.Weights, ref baby1, ref baby2);
                    }
                    else
                    {
                        Crossover(mum.Genome.Weights, dad.Genome.Weights, ref baby1, ref baby2);
                    }

                    Mutate(baby1);
                    Mutate(baby2);

                    new_population.Add(new GenomePair("AIObjMut " + (++_populationCounter).ToString(), new Genome(baby1, 0)));
                    new_population.Add(new GenomePair("AIObjMut " + (++_populationCounter).ToString(), new Genome(baby2, 0)));
                }
                catch
                {
                    new_population.Add(new_population[0]);
                    new_population.Add(new_population[2]);
                }
            }
            _population = new_population;

            return(_population);
        }