本文整理汇总了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;
//.........这里部分代码省略.........