本文整理汇总了C++中Nodes::append方法的典型用法代码示例。如果您正苦于以下问题:C++ Nodes::append方法的具体用法?C++ Nodes::append怎么用?C++ Nodes::append使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nodes
的用法示例。
在下文中一共展示了Nodes::append方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: expand
int UltimateTicTacToeMontecarloAI::expand(int leafIndex, Nodes& nodes, int const player) const
{
Node& node = nodes[leafIndex];
node.children.reserve(maxChildren);
Moves options = movementOptions(node.board, node.previousMove);
int turn = node.previousMove > 0 ? otherPlayer(node.board.grids.at(node.previousMove)) : player;
int mostPromisingChildIndex = -1;
int mostPromisingChildScore = 0;
while(node.children.size() < maxChildren && !options.empty())
{
Move move = options.takeAt(qrand() % options.size());
int childIndex = nodes.size();
node.children.append(childIndex);
Board newBoard(node.board);
nodes.append( Node {0, 1, playMove(newBoard, move, turn), move, leafIndex, Node::Children()});
int score = scoreBoard(nodes.last().board, player);
if(score > mostPromisingChildScore || mostPromisingChildIndex < 0)
{
mostPromisingChildIndex = childIndex;
mostPromisingChildScore = score;
}
}
return mostPromisingChildIndex;
}
示例2: realThink
int UltimateTicTacToeMontecarloAI::realThink(const UltimateTicTacToeMontecarloAI::Board &board, const int previousMove, const int player) const
{
//printBoard(board);
if(maxIterations == 0) {
Moves options = movementOptions(board, previousMove);
return options.at(qrand() % options.size());
}
//qint64 now = QDateTime::currentMSecsSinceEpoch();
//qDebug() << "c: " << c << ", maxIterations: " << maxIterations << ", maxChildren: " <<maxChildren;
Nodes nodes;
nodes.reserve(maxIterations * maxChildren);
nodes.append(Node { 0, 1, board, previousMove, -1, Node::Children() });
int i;
for(i = 0; i < maxIterations; ++i)
{
int leafIndex = select(nodes);
Node const& leaf = nodes.at(leafIndex);
GameState leafState = gameState(leaf.board, player);
if(leafState == GameState::WIN)
{
/* qDebug() << "---";
printBoard(leaf.board);
*/
break;
}
else if(leafState == GameState::LOSE)
{
backpropagate(leafIndex, nodes, -10);
}
else if(leafState == GameState::TIE)
{
backpropagate(leafIndex, nodes, -5);
}
else if(leafState == GameState::UNRESOLVED)
{
int nodeIndex = expand(leafIndex, nodes, player);
Node const& node = nodes.at(nodeIndex);
int score = simulate(node.board, node.previousMove, player);
backpropagate(nodeIndex, nodes, score);
}
}
//qDebug() << "Found solution in " << i + 1 << " iterations";
Node const& root = nodes.at(0);
int bestChildIndex = pickBestChild(root, nodes, false);
Node const& bestChild = nodes.at(bestChildIndex);
//qDebug() << "AI took " << (QDateTime::currentMSecsSinceEpoch() - now) << " ms";
/*for(int childIndex : root.children)
{
Node const& child = nodes.at(childIndex);
qDebug() << child.previousMove << ":" << child.v << child.n;
}*/
//qDebug() << bestChild.previousMove / 9 << bestChild.previousMove %9;
return bestChild.previousMove;
}