本文整理汇总了C++中Organism::nextGen方法的典型用法代码示例。如果您正苦于以下问题:C++ Organism::nextGen方法的具体用法?C++ Organism::nextGen怎么用?C++ Organism::nextGen使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Organism
的用法示例。
在下文中一共展示了Organism::nextGen方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: evolve
void Organism::evolve()
{ // user-mediated evolution procedure
char key = 0;
drawTree(leftChild != NULL && !leftChild->terminal,
midChild != NULL && !midChild->terminal,
rightChild != NULL && !rightChild->terminal);
// grow the parent and two children until a key is pressed
// try a new child while this is happening
Organism *newChild = spawnChild();
assert(newChild != NULL);
do
{ // repeat this loop until the space key is pressed
plotAt(vc.numxpixels/2, vc.numypixels*3/4);
nextGen();
if (leftChild == NULL)
leftChild = spawnChild(); // make new left child
else
leftChild->plotAt(vc.numxpixels/6, vc.numypixels/3);
assert(leftChild != NULL);
leftChild->nextGen();
if (midChild == NULL)
midChild = spawnChild(); // make new mid child
else
midChild->plotAt(vc.numxpixels/2, vc.numypixels/4);
assert(leftChild != NULL);
midChild->nextGen();
if (rightChild == NULL)
rightChild = spawnChild(); // make new left child
else
rightChild->plotAt(vc.numxpixels*5/6, vc.numypixels/3);
assert(leftChild != NULL);
rightChild->nextGen();
terminal = 0; // if the node was terminal before, its not now
if (newChild->age < 25)
newChild->nextGen();
else if (leftChild->terminal == 1 &&
newChild->fitness()*0.7 > leftChild->fitness())
{ // replace the left child with the new child
delete leftChild;
leftChild = newChild;
newChild = spawnChild();
assert(newChild != NULL);
} else if (midChild->terminal == 1 &&
newChild->fitness()*0.7 > midChild->fitness())
{ // replace the left child with the new child
delete midChild;
midChild = newChild;
newChild = spawnChild();
assert(newChild != NULL);
} else if (rightChild->terminal == 1 &&
newChild->fitness()*0.7 > rightChild->fitness())
{ // replace the left child with the new child
delete rightChild;
rightChild = newChild;
newChild = spawnChild();
assert(newChild != NULL);
} else if (newChild->age > 50)
{ // new child is 50 generations old and still has not promoted
delete newChild;
newChild = spawnChild();
assert(newChild != NULL);
} else
newChild->nextGen();
if (kbhit())
{ // read the key and see what needs to be done
key = getch();
if (_stackavail() > 0x0400)
{ // only process child branch if there is enough stack space
if (key == 'A' || key == 'a')
leftChild->evolve();
else if (key == 'G' || key == 'g')
midChild->evolve();
else if (key == 'L' || key == 'l')
rightChild->evolve();
drawTree(leftChild != NULL && !leftChild->terminal,
midChild != NULL && !midChild->terminal,
rightChild != NULL && !rightChild->terminal);
}; // if
} else
key = 0;
} while (key != ' ');
delete newChild;
}; // Organism::evolve()