Example #1
0
 public void Reset(Random r)
 {
     Genome = Genomes[r.Next(Genomes.Count)];
     Genomes.Clear();
     FitnessPop.Clear();
     TotalAdjustedFitness = 0f;
 }
 public MasterMindPopulation(int numberOfGenomes)
 {
     Genomes.Clear();
     for (int i = 0; i < numberOfGenomes; i++)
     {
         MastermindGenome aGenome = new MastermindGenome(kLength, 1, 9);
         aGenome.SetCrossoverPoint(kCrossover);
         aGenome.CalculateFitness();
         Genomes.Add(aGenome);
     }
 }
Example #3
0
        private void CreateStartPopulation(int genomes, int numChromosoneBits, int numGeneBits)
        {
            //clear existing population
            Genomes.Clear();

            for (int i = 0; i < genomes; i++)
            {
                var genome = new TGenome();
                genome.Initialize(numChromosoneBits, numGeneBits);
                Genomes.Add(genome);
            }

            //reset all variables
            Generation        = 0;
            TotalFitnessScore = 0;
        }
Example #4
0
        private void CreateStartPopulation(int genomes, Action <TGenome> genomeCreationMethod)
        {
            //clear existing population
            Genomes.Clear();

            for (int i = 0; i < genomes; i++)
            {
                var genome = new TGenome();
                genomeCreationMethod(genome);
                Genomes.Add(genome);
            }

            //reset all variables
            Generation        = 0;
            TotalFitnessScore = 0;

            GeneLength       = -1;
            ChromosoneLength = Genomes.First().Genes.Sum(g => g.Length);
        }
Example #5
0
        public void CreateStartPopulation()
        {
            // clear existing population
            Genomes.Clear();

            //Parallel.For(0, _settings.PopulationSize, (i) =>
            for (int i = 0; i < _settings.PopulationSize; i++)
            {
                var genome = new Genome <TMainModel, TCombinationModel>(_settings);

                // Make sure all genomes are initially different
                int initPasses = 0;
                do
                {
                    genome.Initialize();
                    initPasses++;
                    lock (Genomes)
                    {
                        if (!Genomes.AsParallel().Any(g => g.GetUniqueGenesString().Equals(genome.GetUniqueGenesString())))
                        {
                            break; // we are all good
                        }
                        if (initPasses > 10)
                        {
                            throw new InvalidOperationException("No more combinations possible, reduce population size!");
                        }
                    }
                } while (true);


                lock (Genomes)
                {
                    Genomes.Add(genome);
                }
                //Debug.WriteLineIf(initPasses>1, $"# Re-initialized because of duplicate genome ({initPasses})");
            }
            ;

            //reset all variables
            Generation        = 0;
            TotalFitnessScore = 0;
        }
Example #6
0
        public void NextGen(List <Species> speciesList)
        {
            //var speciesList = _speciesList.Where(x => (x.Stagnant == false)).ToList();

            var popSize = Genomes.Count;

            Genomes.Clear();

            var totalFitness = speciesList.Select(x => (1.0 / x.MinFitness)).Sum();
            //var expSum = speciesList.Select(x => Math.Exp(1.0/ x.Fitness)).Sum();
            var remainder = popSize;
            var count     = speciesList.Count;

            foreach (var species in speciesList)
            {
                //var newProp = Math.Exp((1.0/species.Fitness)) / expSum * popSize; ;
                var proportion = (1.0 / species.MinFitness) / totalFitness * popSize;
                //var proportion = newProp;

                var decimalPart = proportion - Math.Truncate(proportion);

                //ensures the total adds up at the end
                int numChildren = decimalPart < 0.5 ? (int)Math.Floor(proportion) : (int)Math.Ceiling(proportion);


                if (count == 1)
                {
                    numChildren = remainder;
                }
                numChildren = numChildren < 0 ? 0 : numChildren;
                remainder  -= numChildren;
                NextGeneration(species, numChildren);

                count--;
            }
        }