コード例 #1
0
ファイル: Cga.cs プロジェクト: itggot-linus-styren/NEATrobot
        ResetAndKill()
        {
            m_dTotFitAdj = 0;
            m_dAvFitAdj  = 0;

            // Purge the species that doesn't improve
            for (int i = m_vecSpecies.Count - 1; i >= 0; i--)
            {
                CSpecies species = m_vecSpecies[i];
                species.Purge();

                if (species.GensNoImprovement() >
                    _params.NumGensAllowedNoImprovement &&
                    species.BestFitness() < m_dBestEverFitness)
                {
                    m_vecSpecies.RemoveAt(i);
                    Debug.Log("killed species " + species.ID());
                }
            }

            for (int i = 0; i < m_Population.Count; i++)
            {
                CGenome genome = m_Population[i];
                genome.DeletePhenotype();
            }
        }
コード例 #2
0
ファイル: Cga.cs プロジェクト: itggot-linus-styren/NEATrobot
        SpawnOffspring(CSpecies species, int numSpawned,
                       List <CGenome> newPopulation)
        {
            CGenome baby = null;

            /*
             * Prevent overflowing the total number of genomes spawned per population.
             */
            if (numSpawned < _params.NumGenomesToSpawn)
            {
                // Exclude the leader from numToSpawn.
                int numToSpawn = Mathf.RoundToInt(species.NumToSpawn()) - 1;

                numToSpawn = Mathf.Min(numToSpawn, _params.NumGenomesToSpawn - numSpawned);

                Debug.Log("spawning " + numToSpawn + " num indivudals for species " + species.ID() + ", best fitness: " + species.BestFitness());

                while (numToSpawn-- > 0)
                {
                    /*
                     * Unless we have >2 members in the species crossover
                     * can't be performed.
                     */
                    if (species.NumMembers() == 1)
                    {
                        baby = new CGenome(species.Spawn());
                    }
                    else
                    {
                        CGenome mum = species.Spawn();

                        if (Random.value < _params.CrossoverRate)
                        {
                            CGenome dad = species.Spawn();

                            int numAttempts = 5;

                            // try to select a genome which is not the same as mum
                            while (mum.ID() == dad.ID() && numAttempts-- > 0)
                            {
                                dad = species.Spawn();
                            }

                            if (mum.ID() != dad.ID())
                            {
                                baby = Crossover(mum, dad);
                            }
                            else
                            {
                                if (Random.value < 0.5f)
                                {
                                    baby = new CGenome(dad);
                                }
                                else
                                {
                                    baby = new CGenome(mum);
                                }
                            }
                        }
                        else
                        {
                            baby = new CGenome(mum);
                        }
                    }

                    if (baby == null)
                    {
                        continue;
                    }

                    baby.SetID(++nextGenomeID);

                    MutateBaby(baby);  //EDIT me

                    baby.SortGenes();

                    newPopulation.Add(baby);

                    if (numSpawned++ >= _params.NumGenomesToSpawn)
                    {
                        numToSpawn = 0;
                        break;
                    }
                }
            }

            return(numSpawned);
        }
コード例 #3
0
ファイル: Cga.cs プロジェクト: itggot-linus-styren/NEATrobot
        SpawnLeaders(List <CGenome> newPopulation)
        {
            for (int i = 0; i < m_vecSpecies.Count; i++)
            {
                CSpecies species = m_vecSpecies[i];
                CGenome  baby    = species.Leader();

                Debug.Log("spawning leader (" + baby.Fitness() + "): " + baby.ID() + " for " + species.ID());
                newPopulation.Add(baby);
            }

            return(newPopulation.Count);
        }
コード例 #4
0
ファイル: Cga.cs プロジェクト: itggot-linus-styren/NEATrobot
        SpeciateGenomes()
        {
            bool addedToSpecies = false;

            for (int i = 0; i < m_Population.Count; i++)
            {
                CGenome genome = m_Population[i];

                float    bestCompatability = 1000;
                CSpecies bestSpecies       = null;

                for (int j = 0; j < m_vecSpecies.Count; j++)
                {
                    CSpecies species       = m_vecSpecies[j];
                    float    compatibility = genome.GetCompatibilityScore(species.Leader());

                    // if this individual is similar to this species leader add to species
                    if (compatibility < bestCompatability)
                    {
                        bestCompatability = compatibility;
                        bestSpecies       = species;
                    }
                }

                if (bestCompatability <= _params.CompatibilityThreshold)
                {
                    bestSpecies.AddMember(genome);

                    genome.SetSpecies(bestSpecies.ID());

                    addedToSpecies = true;
                }

                if (!addedToSpecies)
                {
                    // we have not found a compatible species so a new one will be created
                    m_vecSpecies.Add(new CSpecies(genome, nextSpeciesID++));
                }

                addedToSpecies = false;
            }

            /*
             * Adjust the fitness for all members of the every species to take
             * into account fitness sharing and age of species.
             */
            for (int i = 0; i < m_vecSpecies.Count; i++)
            {
                CSpecies species = m_vecSpecies[i];
                species.AdjustFitnesses();
            }

            /*
             * Calculate new adjusted total and average fitness for the population.
             */
            for (int i = 0; i < m_Population.Count; i++)
            {
                CGenome genome = m_Population[i];
                m_dTotFitAdj += genome.GetAdjustedFitness();
            }

            m_dAvFitAdj = m_dTotFitAdj / m_Population.Count;
        }