Beispiel #1
0
        Epoch(Dictionary <int, float> fitnessScores)
        {
            if (fitnessScores.Count != m_Population.Count)
            {
                Debug.LogError("scores and genomes mismatch error. (" + fitnessScores.Count + " / " + m_Population.Count + ")");
                return(null);
            }

            ResetAndKill();

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

            SortAndRecord();
            SpeciateGenomes();
            CalculateSpawnLevels();

            Debug.Log("best fitness last gen: " + m_Population[0].Fitness());

            List <CGenome> newPopulation = new List <CGenome> ();

            int numSpawned = SpawnLeaders(newPopulation);

            for (int i = 0; i < m_vecSpecies.Count; i++)
            {
                CSpecies species = m_vecSpecies[i];
                numSpawned = SpawnOffspring(species, numSpawned, newPopulation);
            }

            /*
             * Tournament selection is used over the entire population if due
             * to a underflow of the species amount to spawn the offspring
             * doesn't fill the entire population additional children needs to
             * created.
             */
            if (numSpawned < _params.NumGenomesToSpawn)
            {
                int numMoreToSpawn = _params.NumGenomesToSpawn - numSpawned;

                while (numMoreToSpawn-- > 0)
                {
                    CGenome baby = new CGenome(TournamentSelection(m_PopSize / 5));
                    baby.SetID(++nextGenomeID);
                    newPopulation.Add(baby);
                }
            }

            m_Population = newPopulation;

            m_iGeneration++;

            return(CreatePhenotypes());
        }
Beispiel #2
0
        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);
        }
Beispiel #3
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);
        }