public int CompareTo(Organism <T> other)
 {
     //sorts by fitness in decending order
     return(-fit.CompareTo(other.fit));
 }
Exemple #2
0
        private void CreateOffspring()
        {
            //obtains a working genome to be used as scratch
            T best    = pop[champ].Genome;
            T scratch = best.Clone();

            //now we can start creating offspring
            foreach (var spec in species)
            {
                //there are no more dead to be reborn
                if (deadpool.Count == 0)
                {
                    break;
                }
                int nchild = spec.NumChildren;

                for (int i = 0; i < nchild; i++)
                {
                    //obtains a dead creature to be reborn
                    if (deadpool.Count == 0)
                    {
                        break;
                    }
                    Organism <T> rep = deadpool.Pop();

                    //handels the generation of offspring
                    Procreate(spec, rep, scratch);

                    //calculates fitness and updates the organism
                    double fitness = fitf(scratch);
                    rep.Update(scratch, fitness);

                    //updates the champ if this new creature is better
                    if (fitness > pop[champ].TrueFit)
                    {
                        champ = rep.Index;
                    }

                    //the organisim is born again
                    babies.Add(fitness, rep);
                    rep.Marked = false;
                }
            }

            //we need to take care of the rest of the dead
            while (deadpool.Count > 0)
            {
                //obtains a dead creature to be reborn
                Organism <T> rep = deadpool.Pop();

                //creates a new random creature
                scratch.Overwrite(best);
                scratch.Randomize(rng);

                //calculates fitness and updates the organism
                double fitness = fitf(scratch);
                rep.Update(scratch, fitness);

                //updates the champ if this new creature is better
                if (fitness > pop[champ].TrueFit)
                {
                    champ = rep.Index;
                }

                //the organisim is born again
                babies.Add(fitness, rep);
                rep.Marked = false;
            }
        }