Exemplo n.º 1
0
        /// <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.
                //List<TPhenome> parasites = _parasiteGenomes.Select(_genomeDecoder.Decode).Where(parasite => parasite != null).ToList();
                for (int j = 0; j < MusicEnvironment.SETS_OF_PARASITES; j++)
                {
                    var parasites = new List <TPhenome>();

                    foreach (var l in _parasiteGenomes)
                    {
                        if (l.Count > 0)
                        {
                            var genome = l[j];
                            if (genome != null)
                            {
                                parasites.Add(_genomeDecoder.Decode(genome));
                            }
                        }
                    }

                    if (parasites.Count == 0)
                    {
                        return;
                    }

                    parasites.Insert(0, host);

                    lock (parasites)
                    {
                        FitnessInfo parasiteFitness;
                        _phenomeListEvaluator.Evaluate(parasites, out parasiteFitness);
                        results[i]._fitness += parasiteFitness._fitness;
                    }
                }

                // Evaluate the host against the champions.
                FitnessInfo championFitness;
                List <TPhenome> finalChampions = champions.Select(_genomeDecoder.Decode).Where(champion => champion != null).ToList();

                if (finalChampions.Count == 0)
                {
                    return;
                }

                finalChampions.Insert(0, host);
                Monitor.Enter(finalChampions);

                _phenomeListEvaluator.Evaluate(finalChampions, out championFitness);

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

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


            // Update every genome in the population with its new fitness score.
            Monitor.Enter(hostGenomeList);
            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);
            Monitor.Exit(hostGenomeList);

            // Clean up hall of fame. Don't know this is good, just trying it out.
            if (_hallOfFame.Count == 100)
            {
                _hallOfFame.Sort((g1, g2) =>
                                 g2.EvaluationInfo.Fitness.CompareTo(g1.EvaluationInfo.Fitness));

                // Keep only the top genomes
                _hallOfFame.RemoveRange(10, 90);
            }
        }
Exemplo n.º 2
0
        /// <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 newFitness;
                    _phenomeListEvaluator.Evaluate(new List <TPhenome>()
                    {
                        host, parasite
                    }, out newFitness);

                    // Add the results to each genome's overall fitness.
                    results[i]._fitness += newFitness._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 newFitness;
                    _phenomeListEvaluator.Evaluate(new List <TPhenome>()
                    {
                        host, champion
                    }, out newFitness);

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

            int tmp = 0;

            // 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);
        }