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


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

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


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

示例1: main

int main()
{
	srand(time(NULL));
	Population population;

	for (int i = 0; i < POPULATION_COUNT; ++i) {
		Individual ind;
		Chromosome chromosome(targetString.size());
		chromosome.randomize();
		float fitness = evaluate(chromosome);
		ind.setChromosome(chromosome);
		ind.setFitness(fitness);

		population.addIndividual(ind);
	}

	population.sortPopulation();


	std::string solution = "";
	Individual bestInd;

	int generationCount = 0;

	int nth = 64;
	for (int i = 0; i < GENERATION_COUNT; ++i) {
		generationCount++;

		evolve(population);
		population.sortPopulation();
		if (bestInd < population[0]) {
			bestInd = population[0];
		}

		if (i % nth == 0) {
			std::cout << "Generation: " << generationCount << std::endl;
			std::cout << "Best individual: " << bestInd << std::endl;
		}

		if (bestInd.getFitness() >= 1){
			break;
		}
	}

	std::cout << std::endl;

	std::cout << "Solved on generation " << generationCount << '!' << std::endl;

	std::cout << bestInd << std::endl << std::endl;
	std::cout << "Press enter to exit";
	

	std::cin.get();
	return 0;
}
开发者ID:Backman,项目名称:Artificiell-Intelligence-Course,代码行数:55,代码来源:Main.cpp

示例2: evolve

void evolve(Population& population)
{
	Population newGeneration;
	while (newGeneration < population) {
		Chromosome chromosome1 = select(population).getChromosome();
		Chromosome chromosome2 = select(population).getChromosome();
		crossover(chromosome1, chromosome2);
		mutate(chromosome1, chromosome2);

		Individual newInd1;
		newInd1.setChromosome(chromosome1);
		newInd1.setFitness(evaluate(chromosome1));
		newGeneration.addIndividual(newInd1);
		if (newGeneration < population) {
			Individual newInd2;
			newInd2.setChromosome(chromosome2);
			newInd2.setFitness(evaluate(chromosome2));
			newGeneration.addIndividual(newInd2);
		}
	}
	population = newGeneration;
}
开发者ID:Backman,项目名称:Artificiell-Intelligence-Course,代码行数:22,代码来源:Main.cpp

示例3: evolve

//essentially an update
void Evolve::evolve(Population &pop)
{
    typedef std::pair<MEMBER*, SCORE>  data_pair;
    
    auto sort = findBest(pop);
    
    std::vector<data_pair>fin_sort(sort.begin(), sort.end() );
    std::sort(fin_sort.begin(), fin_sort.end(), [](const data_pair& that, const data_pair& thus)
              {
                  return that.second < thus.second;
              }
              );
    
    //find two best and copy them temporarily
    auto end_it = fin_sort.rbegin();
    MEMBER first = std::move(*end_it->first);
    end_it++;
    MEMBER second = std::move(*end_it->first);
    
    //checks best
    if(sort.rbegin()->second > best.first)
    {
        best = {sort.rbegin()->second, *first};
    }
    
    int START = 1;
    
    //adds to mode
    decltype(mode)::value_type data = {START, {first->getLemons(), first->getSugar(), first->getPrice()} };
    int val = DATA_FIND(mode, data);
    if(val != 0)
    {
        //adds one to mode if value is already placed
        decltype(data) newdata = {val, data.second};
        int pos = std::find(mode.begin(), mode.end(), decltype(newdata){newdata}) - mode.begin();
        mode.at(pos).first++;
    }
    else
    {
        mode.push_back(data);
    }
    
    //debug
    std::cout << first->getLemons() << std::endl;
    std::cout << first->getSugar() << std::endl;
    std::cout << first->getPrice() << std::endl;
    std::cout << "Profit assuming 100 chances: " << sort.rbegin()->second << std::endl;
    
    
    
    //clear the old vector
    clear(pop);
    
    //generate rest to fill up population size
    for(int i = 0; i < POP_SIZE - 2; i++)
    {
        pop.addIndividual(generate(*first, *second) );
    }
    
    //add in parents
    pop.addIndividual(std::move(first) );
    pop.addIndividual(std::move(second) );
}
开发者ID:wileyyugioh,项目名称:lemonade-genetics,代码行数:64,代码来源:Population.cpp


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