当前位置: 首页>>代码示例>>C++>>正文


C++ Population::IncrementNextGenomeID方法代码示例

本文整理汇总了C++中Population::IncrementNextGenomeID方法的典型用法代码示例。如果您正苦于以下问题:C++ Population::IncrementNextGenomeID方法的具体用法?C++ Population::IncrementNextGenomeID怎么用?C++ Population::IncrementNextGenomeID使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Population的用法示例。


在下文中一共展示了Population::IncrementNextGenomeID方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ReproduceOne


//.........这里部分代码省略.........
            // There is a probability that the father may come from another species
            if ((a_RNG.RandFloat() < a_Parameters.InterspeciesCrossoverRate) && (a_Pop.m_Species.size()>1))
            {
                // Find different species (random one) // !!!!!!!!!!!!!!!!!
                // But the different species must have at least one evaluated individual
                int t_diffspec = 0;
                int t_giveup = 64;
                do
                {
                    t_diffspec = a_RNG.RandInt(0, static_cast<int>(a_Pop.m_Species.size()-1));
                }
                while ((a_Pop.m_Species[t_diffspec].m_AverageFitness == 0) && (t_giveup--));

                if (a_Pop.m_Species[t_diffspec].m_AverageFitness == 0)
                    t_dad = GetIndividual(a_Parameters, a_RNG);
                else
                    t_dad = a_Pop.m_Species[t_diffspec].GetIndividual(a_Parameters, a_RNG);

                t_interspecies = true;
            }
            else
            {
                // Mate within species
                t_dad = GetIndividual(a_Parameters, a_RNG);

                // The other parent should be a different one
                // number of tries to find different parent
                int t_tries = 32;
                while(((t_mom.GetID() == t_dad.GetID()) || ((!a_Parameters.AllowClones) && (t_mom.CompatibilityDistance(t_dad, a_Parameters) <= 0.00001)) ) && (t_tries--))
                {
                    t_dad = GetIndividual(a_Parameters, a_RNG);
                }
                t_interspecies = false;
            }

            // OK we have both mom and dad so mate them
            // Choose randomly one of two types of crossover
            if (a_RNG.RandFloat() < a_Parameters.MultipointCrossoverRate)
            {
                t_baby = t_mom.Mate( t_dad, false, t_interspecies, a_RNG);
            }
            else
            {
                t_baby = t_mom.Mate( t_dad, true, t_interspecies, a_RNG);
            }
            t_mated = true;
        }
        // don't mate - reproduce the mother asexually
        else
        {
            t_baby = t_mom;
            t_mated = false;
        }
    }

/*    if (t_baby.HasDeadEnds())
    {
        std::cout << "Dead ends in baby after crossover" << std::endl;
//        int p;
//        std::cin >> p;
    }*/

    // OK we have the baby, so let's mutate it.
    bool t_baby_is_clone = false;

    if ((!t_mated) || (a_RNG.RandFloat() < a_Parameters.OverallMutationRate))
        MutateGenome(t_baby_is_clone, a_Pop, t_baby, a_Parameters, a_RNG);

    // We have a new offspring now
    // give the offspring a new ID
    t_baby.SetID(a_Pop.GetNextGenomeID());
    a_Pop.IncrementNextGenomeID();

    // sort the baby's genes
    t_baby.SortGenes();

    // clear the baby's fitness
    t_baby.SetFitness(0);
    t_baby.SetAdjFitness(0);
    t_baby.SetOffspringAmount(0);

    t_baby.ResetEvaluated();

    // debug trap
/*    if (t_baby.NumLinks() == 0)
    {
        std::cout << "No links in baby after reproduction" << std::endl;
//        int p;
//        std::cin >> p;
    }

    if (t_baby.HasDeadEnds())
    {
        std::cout << "Dead ends in baby after reproduction" << std::endl;
//        int p;
//        std::cin >> p;
    }
*/
    return t_baby;
}
开发者ID:Arpic,项目名称:robogen,代码行数:101,代码来源:Species.cpp

示例2: Reproduce


//.........这里部分代码省略.........
                if (!a_Parameters.AllowClones)
                {
                    for(unsigned int i=0; i<a_Pop.m_TempSpecies.size(); i++)
                    {
                        for(unsigned int j=0; j<a_Pop.m_TempSpecies[i].m_Individuals.size(); j++)
                        {
                            if (
                                (t_baby.CompatibilityDistance(a_Pop.m_TempSpecies[i].m_Individuals[j], a_Parameters) < 0.00001) // identical genome?
                               )

                            {
                                t_baby_exists_in_pop = true;
                                break;
                            }
                        }
                    }
                }
            }
            while (t_baby_exists_in_pop); // end do
        }

        // Final place to test for problems
        // If there is anything wrong here, we will just
        // pick a random individual and leave him unchanged
        if ((t_baby.NumLinks() == 0) || t_baby.HasDeadEnds())
        {
            t_baby = GetIndividual(a_Parameters, a_RNG);
            t_new_individual = false;
        }

        if (t_new_individual) {
			// We have a new offspring now
			// give the offspring a new ID
			t_baby.SetID(a_Pop.GetNextGenomeID());
			a_Pop.IncrementNextGenomeID();

			// sort the baby's genes
			t_baby.SortGenes();

			// clear the baby's fitness
			t_baby.SetFitness(0);
			t_baby.SetAdjFitness(0);
			t_baby.SetOffspringAmount(0);

			t_baby.ResetEvaluated();
        }

        //////////////////////////////////
        // put the baby to its species  //
        //////////////////////////////////

        // before Reproduce() is invoked, it is assumed that a
        // clone of the population exists with the name of m_TempSpecies
        // we will store results there.
        // after all reproduction completes, the original species will be replaced back

        bool t_found = false;
        std::vector<Species>::iterator t_cur_species = a_Pop.m_TempSpecies.begin();

        // No species yet?
        if (t_cur_species == a_Pop.m_TempSpecies.end())
        {
            // create the first species and place the baby there
            a_Pop.m_TempSpecies.push_back( Species(t_baby, a_Pop.GetNextSpeciesID()));
            a_Pop.IncrementNextSpeciesID();
        }
        else
        {
            // try to find a compatible species
            Genome t_to_compare = t_cur_species->GetRepresentative();

            t_found = false;
            while((t_cur_species != a_Pop.m_TempSpecies.end()) && (!t_found))
            {
                if (t_baby.IsCompatibleWith( t_to_compare, a_Parameters))
                {
                    // found a compatible species
                    t_cur_species->AddIndividual(t_baby);
                    t_found = true; // the search is over
                }
                else
                {
                    // keep searching for a matching species
                    t_cur_species++;
                    if (t_cur_species != a_Pop.m_TempSpecies.end())
                    {
                        t_to_compare = t_cur_species->GetRepresentative();
                    }
                }
            }

            // if couldn't find a match, make a new species
            if (!t_found)
            {
                a_Pop.m_TempSpecies.push_back( Species(t_baby, a_Pop.GetNextSpeciesID()));
                a_Pop.IncrementNextSpeciesID();
            }
        }
    }
}
开发者ID:Arpic,项目名称:robogen,代码行数:101,代码来源:Species.cpp


注:本文中的Population::IncrementNextGenomeID方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。