Ejemplo n.º 1
0
        /// <summary>
        /// Main genome evaluation loop with no phenome caching (decode on each evaluation).
        /// Individuals are competed pairwise against every champion in the hall of fame.
        /// The final fitness score is the weighted sum of the fitness versus the champions
        /// and the fitness score by the inner evaluator.
        /// </summary>
        public void Evaluate(IList <TGenome> genomeList)
        {
            _innerEvaluator.Evaluate(genomeList);

            //Create a temporary list of fitness values with the scores of the inner evaluator.
            FitnessInfo[] results = new FitnessInfo[genomeList.Count];
            for (int i = 0; i < results.Length; i++)
            {
                results[i] = new FitnessInfo(genomeList[i].EvaluationInfo.Fitness * (1.0 - _hallOfFameWeight),
                                             genomeList[i].EvaluationInfo.AlternativeFitness * (1.0 - _hallOfFameWeight));
            }

            // Calculate how much each champion game is worth
            double championGameWeight = _hallOfFameWeight / (double)_hallOfFame.Count;

            // Exhaustively compete individuals against each other.
            Parallel.For(0, genomeList.Count, delegate(int i)
            {
                // Decode the first genome.
                TPhenome phenome1 = _genomeDecoder.Decode(genomeList[i]);

                // Check that the first genome is valid.
                if (phenome1 == null)
                {
                    return;
                }

                for (int j = 0; j < _hallOfFame.Count; j++)
                {
                    // Decode the second genome.
                    TPhenome phenome2 = _genomeDecoder.Decode(_hallOfFame[j]);

                    // Check that the second genome is valid.
                    if (phenome2 == null)
                    {
                        continue;
                    }

                    // Compete the two individuals against each other and get the results.
                    FitnessInfo fitness1, fitness2;
                    _phenomeEvaluator.Evaluate(phenome1, phenome2, out fitness1, out fitness2);

                    // Add the results to each genome's overall fitness.
                    // Note that we need to use a lock here because
                    // the += operation is not atomic.
                    // ENHANCEMENT: I don't think this lock is necessary here since the hall of fame
                    //              is our inner loop.
                    lock (results)
                    {
                        results[i]._fitness            += fitness1._fitness * championGameWeight;
                        results[i]._alternativeFitness += fitness1._alternativeFitness * championGameWeight;
                    }
                }
            });

            // Update every genome in the population with its new fitness score.
            for (int i = 0; i < results.Length; i++)
            {
                genomeList[i].EvaluationInfo.SetFitness(results[i]._fitness);
                genomeList[i].EvaluationInfo.AlternativeFitness = results[i]._alternativeFitness;
            }
        }
        //readonly ParallelOptions _parallelOptions;
        /// <summary>
        /// Main genome evaluation loop with no phenome caching (decode
        /// on each evaluation). Individuals are competed pairwise against
        /// every other in the population.
        /// Evaluations are summed to get the final genome fitness.
        /// </summary>
        public void Evaluate(IList <TGenome> genomeList)
        {
            //Create a temporary list of fitness values
            FitnessInfo[] results = new FitnessInfo[genomeList.Count];
            for (int i = 0; i < results.Length; i++)
            {
                results[i] = FitnessInfo.Zero;
            }

            // Exhaustively compete individuals against each other.
            for (int i = 0; i < genomeList.Count; i++)
            {
                for (int j = 0; j < genomeList.Count; j++)
                {
                    // Don't bother evaluating inviduals against themselves.
                    if (i == j)
                    {
                        continue;
                    }

                    // Decode the first genome.
                    TPhenome phenome1 = _genomeDecoder.Decode(genomeList[i]);

                    // Check that the first genome is valid.
                    if (phenome1 == null)
                    {
                        continue;
                    }

                    // Decode the second genome.
                    TPhenome phenome2 = _genomeDecoder.Decode(genomeList[j]);

                    // Check that the second genome is valid.
                    if (phenome2 == null)
                    {
                        continue;
                    }

                    // Compete the two individuals against each other and get
                    // the results.
                    FitnessInfo fitness1, fitness2;
                    _phenomeEvaluator.Evaluate(phenome1, phenome2,
                                               out fitness1, out fitness2);

                    // Add the results to each genome's overall fitness.
                    // Note that we need to use a lock here because
                    // the += operation is not atomic.
                    lock (results)
                    {
                        results[i]._fitness += fitness1._fitness;
                        //results[i]._alternativeFitness +=
                        //                              fitness1._alternativeFitness;
                        results[j]._fitness += fitness2._fitness;
                        //results[j]._alternativeFitness +=
                        //                              fitness2._alternativeFitness;
                    }
                }
            }

            // Update every genome in the population with its new fitness score.
            for (int i = 0; i < results.Length; i++)
            {
                genomeList[i].EvaluationInfo.SetFitness(results[i]._fitness);
                //genomeList[i].EvaluationInfo.AlternativeFitness =
                //                                      results[i]._alternativeFitness;
            }
        }
        /// <summary>
        /// Main genome evaluation loop with no phenome caching (decode on each evaluation).
        /// Individuals are competed pairwise against every parasite in the parasite list
        /// and against a randomly selected subset of the hall of fame.
        /// </summary>
        public void Evaluate(IList <TGenome> hostGenomeList)
        {
            //Create a temporary list of fitness values with the scores of the inner evaluator.
            FitnessInfo[] results = new FitnessInfo[hostGenomeList.Count];
            for (int i = 0; i < results.Length; i++)
            {
                results[i] = FitnessInfo.Zero;
            }

            // Randomly select champions from the hall of fame.
            TGenome[] champions = _hallOfFame.Count > 0
                                    ? new TGenome[_hallOfFameGenomesPerEvaluation]
                                    : new TGenome[0];
            for (int i = 0; i < champions.Length; i++)
            {
                // Pick a random champion's index
                int hallOfFameIdx = _random.Next(_hallOfFame.Count);

                // Add the champion to the list of competitors.
                champions[i] = _hallOfFame[hallOfFameIdx];
            }

            // Acquire a lock on the parasite genome list.
            Monitor.Enter(_parasiteGenomes);

            // Exhaustively compete individuals against each other.
            Parallel.For(0, hostGenomeList.Count, delegate(int i)
            {
                // Decode the host genome.
                TPhenome host = _genomeDecoder.Decode(hostGenomeList[i]);

                // Check that the host genome is valid.
                if (host == null)
                {
                    return;
                }

                // Evaluate the host against the parasites.
                for (int j = 0; j < _parasiteGenomes.Count; j++)
                {
                    // Decode the champion genome.
                    TPhenome parasite = _genomeDecoder.Decode(_parasiteGenomes[j]);

                    // Check that the champion genome is valid.
                    if (parasite == null)
                    {
                        continue;
                    }

                    // Compete the two individuals against each other and get the results.
                    FitnessInfo hostFitness, parasiteFitness;
                    _phenomeEvaluator.Evaluate(host, parasite, out hostFitness, out parasiteFitness);

                    // Add the results to each genome's overall fitness.
                    results[i]._fitness            += hostFitness._fitness;
                    results[i]._alternativeFitness += hostFitness._alternativeFitness;
                }

                // Evaluate the host against the champions.
                for (int j = 0; j < champions.Length; j++)
                {
                    // Decode the champion genome.
                    TPhenome champion = _genomeDecoder.Decode(champions[j]);

                    // Check that the champion genome is valid.
                    if (champion == null)
                    {
                        continue;
                    }

                    // Compete the two individuals against each other and get the results.
                    FitnessInfo hostFitness, championFitness;
                    _phenomeEvaluator.Evaluate(host, champion, out hostFitness, out championFitness);

                    // Add the results to each genome's overall fitness.
                    results[i]._fitness            += hostFitness._fitness;
                    results[i]._alternativeFitness += hostFitness._alternativeFitness;
                }
            });

            // Release the lock on the parasite genome list.
            Monitor.Exit(_parasiteGenomes);

            // Update every genome in the population with its new fitness score.
            TGenome champ = hostGenomeList[0];

            for (int i = 0; i < results.Length; i++)
            {
                TGenome hostGenome = hostGenomeList[i];
                hostGenome.EvaluationInfo.SetFitness(results[i]._fitness);
                hostGenome.EvaluationInfo.AlternativeFitness = results[i]._alternativeFitness;

                // Check if this genome is the generational champion
                if (hostGenome.EvaluationInfo.Fitness > champ.EvaluationInfo.Fitness)
                {
                    champ = hostGenome;
                }
            }

            // Add the new champion to the hall of fame.
            _hallOfFame.Add(champ);
        }