コード例 #1
0
        public virtual void EvaluatePopulation(Population pop, EvolutionAlgorithm ea)
        {
            // Evaluate in single-file each genome within the population.
            // Only evaluate new genomes (those with EvaluationCount==0).
            int count = pop.GenomeList.Count;

            for (int i = 0; i < count; i++)
            {
                IGenome g = pop.GenomeList[i];
                if (g.EvaluationCount != 0)
                {
                    continue;
                }

                INetwork network = g.Decode(activationFn);
                if (network == null)
                {                       // Future genomes may not decode - handle the possibility.
                    g.Fitness = EvolutionAlgorithm.MIN_GENOME_FITNESS;
                }
                else
                {
                    g.Fitness = Math.Max(networkEvaluator.EvaluateNetwork(network), EvolutionAlgorithm.MIN_GENOME_FITNESS);
                }

                // Reset these genome level statistics.
                g.TotalFitness    = g.Fitness;
                g.EvaluationCount = 1;

                // Update master evaluation counter.
                evaluationCount++;
            }
        }
コード例 #2
0
        /// <summary>
        /// Mate two genomes. Will loop over all chromosomes.
        /// </summary>
        /// <param name="father">The father.</param>
        /// <param name="child1">The first child.</param>
        /// <param name="child2">The second child.</param>
        public void Mate(IGenome father, IGenome child1, IGenome child2, GATracker.MatedElement matedElement)
        {
            int motherChromosomes = Chromosomes.Count;
            int fatherChromosomes = father.Chromosomes.Count;

            if (motherChromosomes != fatherChromosomes)
            {
                throw new GeneticError(
                          "Mother and father must have same chromosome count, Mother:"
                          + motherChromosomes + ",Father:"
                          + fatherChromosomes);
            }

            for (int i = 0; i < fatherChromosomes; i++)
            {
                Chromosome motherChromosome     = chromosomes[i];
                Chromosome fatherChromosome     = father.Chromosomes[i];
                Chromosome offspring1Chromosome = child1.Chromosomes[i];
                Chromosome offspring2Chromosome = child2.Chromosomes[i];

                //This splices the chromosomes together, using a random number to decide splice points
                GA.Crossover.Mate(motherChromosome, fatherChromosome, offspring1Chromosome, offspring2Chromosome);

                //Set the gene number
                if (matedElement != null)
                {
                    matedElement.SetNumChildGenes(offspring1Chromosome.Genes.Count, 1);
                    matedElement.SetNumChildGenes(offspring2Chromosome.Genes.Count, 2);
                }

                //Now we potentially mutate the children
                if (ThreadSafeRandom.NextDouble() < GA.MutationPercent)
                {
                    if (matedElement != null)
                    {
                        matedElement.Child1Mutated = true;
                    }
                    GA.Mutate.PerformMutation(offspring1Chromosome, matedElement, 1);
                }

                //Now we potentially mutate the children
                if (ThreadSafeRandom.NextDouble() < GA.MutationPercent)
                {
                    if (matedElement != null)
                    {
                        matedElement.Child2Mutated = true;
                    }
                    GA.Mutate.PerformMutation(offspring2Chromosome, matedElement, 2);
                }
            }

            child1.Decode();
            child2.Decode();
            GA.PerformScoreCalculation(child1);
            GA.PerformScoreCalculation(child2);
        }
コード例 #3
0
        public override void EvaluatePopulation(Population pop, EvolutionAlgorithm ea)
        {
            // Evaluate in single-file each genome within the population.
            // Only evaluate new genomes (those with EvaluationCount==0).
            int count = pop.GenomeList.Count;

            for (int i = 0; i < count; i++)
            {
                // Grab the next individual out of the population
                IGenome g = pop.GenomeList[i];
                if (g.EvaluationCount != 0)
                {
                    continue;
                }

                // The current genome is a CPPN genome, not a network genome
                // So, decode the CPPN genome into a CPPN, use the CPPN to generate an ANN,
                // then run the networkEvaluator on the ANN
                INetwork cppn = g.Decode(activationFn);
                if (cppn == null)
                {       // Future genomes may not decode - handle the possibility.
                    g.Fitness = EvolutionAlgorithm.MIN_GENOME_FITNESS;
                }
                else
                {
                    //BehaviorType behavior;
                    //INetwork network = Chromaria.Simulator.controllerSubstrate.generateGenome(cppn).Decode(activationFn);
                    g.Fitness = Math.Max(0.0f, EvolutionAlgorithm.MIN_GENOME_FITNESS);
                    //if (Chromaria.Simulator.plantingWasValid)
                    //Chromaria.Simulator.successfulPlanterGenome = g;
                    g.RealFitness = g.Fitness;
                }

                // Reset these genome level statistics.
                g.TotalFitness    = g.Fitness;
                g.EvaluationCount = 1;

                // Update master evaluation counter.
                evaluationCount++;

                // Close the XML tag for this individual
                //Chromaria.Simulator.xmlWriter.WriteEndElement();
            }
            if (ea.NeatParameters.noveltySearch)
            {
                if (ea.NeatParameters.noveltySearch && ea.noveltyInitialized)
                {
                    ea.CalculateNovelty();
                }
            }
        }
コード例 #4
0
        public override void EvaluatePopulation(Population pop, EvolutionAlgorithm ea)
        {
            // Evaluate in single-file each genome within the population.
            // Only evaluate new genomes (those with EvaluationCount==0).
            FoodGatherParams.fillLookups();
            FoodGatherParams.fillFood();
            int count = pop.GenomeList.Count;

            for (int i = 0; i < count; i++)
            {
                IGenome g = pop.GenomeList[i];
                if (g.EvaluationCount != 0)
                {
                    continue;
                }

                INetwork network = g.Decode(activationFn);
                if (network == null)
                {       // Future genomes may not decode - handle the possibility.
                    g.Fitness = EvolutionAlgorithm.MIN_GENOME_FITNESS;
                }
                else
                {
                    g.Fitness          = Math.Max(networkEvaluator.EvaluateNetwork(network), EvolutionAlgorithm.MIN_GENOME_FITNESS);
                    g.ObjectiveFitness = g.Fitness;
                }

                // Reset these genome level statistics.
                g.TotalFitness    = g.Fitness;
                g.EvaluationCount = 1;

                // Update master evaluation counter.
                evaluationCount++;
            }
            if (requestResolutionUp == true)
            {
                requestResolutionUp          = false;
                requestResolutionDown        = false;
                FoodGatherParams.resolution *= 2;
            }
            else if (requestResolutionDown == true)
            {
                requestResolutionUp   = false;
                requestResolutionDown = false;
                if (FoodGatherParams.resolution > 4)
                {
                    FoodGatherParams.resolution /= 2;
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Mate two genomes. Will loop over all chromosomes.
        /// </summary>
        ///
        /// <param name="father">The father.</param>
        /// <param name="child1">The first child.</param>
        /// <param name="child2">The second child.</param>
        public void Mate(IGenome father, IGenome child1,
                         IGenome child2)
        {
            int motherChromosomes = Chromosomes.Count;
            int fatherChromosomes = father.Chromosomes.Count;

            if (motherChromosomes != fatherChromosomes)
            {
                throw new GeneticError(
                          "Mother and father must have same chromosome count, Mother:"
                          + motherChromosomes + ",Father:"
                          + fatherChromosomes);
            }

            for (int i = 0; i < fatherChromosomes; i++)
            {
                Chromosome motherChromosome     = _chromosomes[i];
                Chromosome fatherChromosome     = father.Chromosomes[i];
                Chromosome offspring1Chromosome = child1.Chromosomes[i];
                Chromosome offspring2Chromosome = child2.Chromosomes[i];

                _ga.Crossover.Mate(motherChromosome,
                                   fatherChromosome, offspring1Chromosome,
                                   offspring2Chromosome);

                if (ThreadSafeRandom.NextDouble() < _ga.MutationPercent)
                {
                    _ga.Mutate.PerformMutation(
                        offspring1Chromosome);
                }

                if (ThreadSafeRandom.NextDouble() < _ga.MutationPercent)
                {
                    _ga.Mutate.PerformMutation(
                        offspring2Chromosome);
                }
            }

            child1.Decode();
            child2.Decode();
            _ga.PerformCalculateScore(child1);
            _ga.PerformCalculateScore(child2);
        }
コード例 #6
0
        public virtual void EvaluatePopulation(Population pop, EvolutionAlgorithm ea)
        {
            // Evaluate in single-file each genome within the population.
            // Only evaluate new genomes (those with EvaluationCount==0).
            int count = pop.GenomeList.Count;

            for (int i = 0; i < count; i++)
            {
                IGenome g = pop.GenomeList[i];
                if (g.EvaluationCount != 0)
                {
                    continue;
                }

                INetwork network = g.Decode(activationFn);
                if (network == null)
                {                       // Future genomes may not decode - handle the possibility.
                    g.Fitness = EvolutionAlgorithm.MIN_GENOME_FITNESS;
                }
                else
                {
                    BehaviorType behavior;
                    g.Fitness     = Math.Max(networkEvaluator.EvaluateNetwork(network, out behavior), EvolutionAlgorithm.MIN_GENOME_FITNESS);
                    g.RealFitness = g.Fitness;
                    g.Behavior    = behavior;
                }

                // Reset these genome level statistics.
                g.TotalFitness    = g.Fitness;
                g.EvaluationCount = 1;

                // Update master evaluation counter.
                evaluationCount++;
            }

            if (ea.NeatParameters.noveltySearch)
            {
                if (ea.NeatParameters.noveltySearch && ea.noveltyInitialized)
                {
                    ea.CalculateNovelty();
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Mate two genomes. Will loop over all chromosomes.
        /// </summary>
        ///
        /// <param name="father">The father.</param>
        /// <param name="child1">The first child.</param>
        /// <param name="child2">The second child.</param>
        public void Mate(IGenome father, IGenome child1,
                         IGenome child2)
        {
            int motherChromosomes = Chromosomes.Count;
            int fatherChromosomes = father.Chromosomes.Count;

            if (motherChromosomes != fatherChromosomes)
            {
                throw new GeneticError(
                    "Mother and father must have same chromosome count, Mother:"
                    + motherChromosomes + ",Father:"
                    + fatherChromosomes);
            }

            for (int i = 0; i < fatherChromosomes; i++)
            {
                Chromosome motherChromosome = _chromosomes[i];
                Chromosome fatherChromosome = father.Chromosomes[i];
                Chromosome offspring1Chromosome = child1.Chromosomes[i];
                Chromosome offspring2Chromosome = child2.Chromosomes[i];

                _ga.Crossover.Mate(motherChromosome,
                                                fatherChromosome, offspring1Chromosome,
                                                offspring2Chromosome);

                if (ThreadSafeRandom.NextDouble() < _ga.MutationPercent)
                {
                    _ga.Mutate.PerformMutation(
                        offspring1Chromosome);
                }

                if (ThreadSafeRandom.NextDouble() < _ga.MutationPercent)
                {
                    _ga.Mutate.PerformMutation(
                        offspring2Chromosome);
                }
            }

            child1.Decode();
            child2.Decode();
            _ga.PerformCalculateScore(child1);
            _ga.PerformCalculateScore(child2);
        }