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


C++ NodePtr::expand方法代码示例

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


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

示例1: if

int
BestFirst_Astar_Check(const NodeType &start, 
	int &uniquenodes, int &expandednodes)
{
	uniquenodes = expandednodes = 0;

	// copy start state
	NodePtr<NodeType> pstart(start);

	// calculate the heuristic value for this node
	pstart->heuristic(*pstart);

	// create open priority queue
	List<NodePtr<NodeType> > openpq;
	openpq.insertByValue(pstart);
	uniquenodes++;

	// create closed set
	List<NodePtr<NodeType> > closedset;

	// start search loop
	for (NodePtr<NodeType> pnode; ! openpq.isEmpty(); )
	{
		// remove next node from priority open queue
		openpq.removeAtFront(pnode);

		// check if we have a goal node or not
		if (pnode->isGoal())
		{
			// goal node, print solution
			PrintSolution((NodeType *)pnode);
			return(OK);
		}

		// generate the children of the current node
		if (pnode->expand() != OK)
		{
			// unable to expand current node
			return(NOTOK);
		}
		if (((++expandednodes%1000) == 0) && (expandednodes > 0))
		{
			cout << expandednodes << " nodes expanded." << endl;
			cout << "current node is ... " << *pnode << endl;
		}

		// the following two lists are REQUIRED since the
		// list iterator used below does NOT allow nodes
		// to be deleted or inserted into the list while
		// the iterator is traversing the list. the solution
		// is to save the nodes that must be deleted and
		// inserted in two separate lists, and after the
		// iterator is done, then remove and add nodes to
		// pnodes children list.
		//
		// list of nodes to delete from pnode children
		List<NodePtr<NodeType> > nodesToRemove;
		nodesToRemove.clear();

		// list of nodes to add to pnode children
		List<NodePtr<NodeType> > nodesToInsert;
		nodesToInsert.clear();

		// scan children and determine if they already exist.
		ListIterator<NodePtr<NodeType> > 
			childrenIter(*pnode->getChildren());
		for ( ; !childrenIter.done(); childrenIter++)
		{
			// set up link to parent
			childrenIter()->setParent((NodeType *)pnode);

			// calculate the heuristic value
			childrenIter()->heuristic(*pstart);

			// check if node already exists
			NodePtr<NodeType> pchild(childrenIter());
			NodePtr<NodeType> popen(childrenIter());
			NodePtr<NodeType> pclosed(childrenIter());

			// check if node was already generated
			int inopen = NOMATCH;
			int inclosed = NOMATCH;
			if (( ! openpq.isEmpty()) && 
			    (inopen = openpq.retrieveByValue(popen)) == OK)
			{
				// check which path is better, the new
				// path thru pnode, or the old one
				// thru popen.
				//
				int childGvalue = pchild->getGvalue();
				int oldGvalue = popen->getGvalue();
				if (childGvalue < oldGvalue)
				{
					// reset old node parent pointer
					popen->setParent(pchild->getParent());
					popen->heuristic(*pstart);
				}

				// delete copy of node, and insert
				// old node, popen, into children
//.........这里部分代码省略.........
开发者ID:ombt,项目名称:ombt,代码行数:101,代码来源:search.c

示例2: pstart

int
BestFirst_Astar_WOCheck(const NodeType &start, const List<NodeType> &goals, 
	int &uniquenodes, int &expandednodes)
{
	uniquenodes = expandednodes = 0;

	// copy start state
	NodePtr<NodeType> pstart(start);

	// calculate the heuristic value for this node
	pstart->heuristic(*pstart, goals);

	// create open priority queue
	List<NodePtr<NodeType> > openpq;
	openpq.insertByValue(pstart);
	uniquenodes++;

	// create closed set
	List<NodePtr<NodeType> > closedset;

	// start search loop
	for (NodePtr<NodeType> pnode; ! openpq.isEmpty(); )
	{
		// remove next node from priority open queue
		openpq.removeAtFront(pnode);

		// check if we have a goal node or not
		if (goals.isInList(*pnode))
		{
			// goal node, print solution
			PrintSolution((NodeType *)pnode);
			return(OK);
		}

		// generate the children of the current node
		if (pnode->expand() != OK)
		{
			// unable to expand current node
			return(NOTOK);
		}
		if (((++expandednodes%1000) == 0) && (expandednodes > 0))
		{
			cout << expandednodes << " nodes expanded." << endl;
			cout << "current node is ... " << *pnode << endl;
		}

		// set up links to parent and calculate the heuristic value
		ListIterator<NodePtr<NodeType> > 
			childrenIter(*pnode->getChildren());
		for ( ; !childrenIter.done(); childrenIter++)
		{
			// set up link to parent
			childrenIter()->setParent((NodeType *)pnode);

			// calculate the heuristic value
			childrenIter()->heuristic(*pstart, goals);

			// insert into open queue
			openpq.insertByValue(childrenIter());
			if (((++uniquenodes%1000) == 0) && (uniquenodes > 0))
			{
				cout << uniquenodes << " unique nodes." << endl;
			}
		}

		// store node in closed set
		closedset.insertAtFront(pnode);
	}

	// finished with error
	return(NOTOK);
}
开发者ID:ombt,项目名称:ombt,代码行数:72,代码来源:search.c


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