Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
        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);
        }