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


C++ DNA::getWeight方法代码示例

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


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

示例1: Box

main()
{
	//STEP 0
	//make my boxes
	//for i in 50, new Box()
	for (int i=0; i<BOXNUM; i++)
	{
		box[i]=new Box();
		cout<<"Box "<<i<<": w="<<box[i]->weight<<", v="<<box[i]->value<<endl;
	}

	//verify by print all the boxes and weights

	//while our population is less than the initial population 
	//	dna[i]=new DNA()
	// 	only add it if the weight below 1000

	while(population < INITIAL_POPULATION)
	{
		DNA* d = new DNA();
		if(d->getWeight()<=1000)
		{
			dna[population]=d;
			population++;
		}
	}



	//for loop # of generations
	//	figure out who the best is and print out its dna/weight/value
	//	kill

		//bubble sort by value

		for(int i=0; i<population; i++)
		{
			for (int j=0; j<population-1; j++)
			{
				if(dna[j+1]->getValue() > dna[j]->getValue())
				{
					DNA* temp = dna[j+1];
					dna[j+1]=dna[j];
					dna[j]=temp;
				}
			}
		}
		population=SURVIVORS;
		//set population = SURVIVORS
	cout << "DNA 0 has "<< dna[0]->getWeight() << " "<<dna[0]->getValue()<<endl;
	cout << "DNA 1 has "<< dna[1]->getWeight() << " "<<dna[1]->getValue()<<endl;

	//	breed
	int breedingpopulation=population; //go through every possible pair
	for(int i=0; i<breedingpopulation; i++)
	{
		for(int mom=0; mom<breedingpopualtion; mom++)
		{
			if(mom==dad) //can't breed with self
				continue;
	





			DNA *baby = new DNA(); //copy over from dad

			for(int k=0; k<crossoverpoint; k++) //copy over from mom
			{
				baby->boxes[k]=dna[dad]->boxes[k];
			}

	
			for(int k=crossoverpoint; k>BOXDNA; k++) //keep the baby
			{
				baby->boxes[k]=dna[mom]->boxes[k];
			}

			if (baby->getweight()>1000) //1000 garbage collect the baby
				continue;

			dna[population++]=baby;

			cout << "daddy is " << dna[dad]->getValue() << ", mommy is " << dna[mom]->getValue << "baby is " << dna[population] << endl;
		}
	}
	
	//	mutate
	int cloningpopulation = population;
	for(int i=0; i<cloningpopulation; i++)
	{
		if(rand()%100 < DOMUTATERATE)
		{
			DNA* mutant = new DNA();
			for(int j=0; j<BOXNUM; j++)
			{
				mutant->boxes[j] = dna[i]->boxes[j]
			}
			cout << "Making a clone: parent: " << dna[i]->getValue() << " spawn: " >> mutant->getValue() << endl;
//.........这里部分代码省略.........
开发者ID:ericjeffers,项目名称:Genetics-Simulator,代码行数:101,代码来源:knapsack.cpp


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