Beispiel #1
0
void CTextNode::RebuildChildrenList()
{
    // If possible init the children with XmlNodeRef's
    m_children.clear();

    if (m_xmlNode)
    {
        XmlNodeIt childrenIter(m_xmlNode);

        for (XmlNodeRef child = childrenIter.first(); child; child = childrenIter.next())
        {
            m_children.push_back(CTextNode());
            m_children.back() = child;
        }

    }
    else
    {
        XmlConstNodeIt childrenIter(m_constXmlNode);

        for (XmlConstNodeRef child = childrenIter.first(); child; child = childrenIter.next())
        {
            m_children.push_back(CTextNode());
            m_children.back() = child;
        }
    }
}
Beispiel #2
0
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
				// list of pnode.
				//
				nodesToRemove.insertAtFront(pchild);
				nodesToInsert.insertAtFront(popen);
			}
			else if (( ! closedset.isEmpty()) && 
				 (inclosed = closedset.retrieveByValue(pclosed)) == OK)
			{
				// check which path is better, the new
				// path thru pnode, or the old one
				// thru pclosed.
				//
				int childGvalue = pchild->getGvalue();
				int oldGvalue = pclosed->getGvalue();
				if (childGvalue < oldGvalue)
				{
					// reset parent pointer
					pclosed->setParent(pchild->getParent());
					pclosed->heuristic(*pstart);

					// check parent pointers for
					// children of pclosed.
					//
					ListIterator<NodePtr<NodeType> > closedChildrenIter(*pclosed->getChildren());
					for ( ; ! closedChildrenIter.done(); closedChildrenIter++)
					{
						// first we need to check if this child has 
						// the closed node as a parent. if it does,
						// then do nothing. if it does not, then we
						// have to compare g-values. if the existing
						// g-value is smaller, then leave child node
						// alone. if the the existing g-value is greater,
						// then we have to reset the closed child parent
						// pointer to point to the closed node since this
						// path is now better.
						//
						NodePtr<NodeType> pclosedChild(closedChildrenIter());
						NodePtr<NodeType> pclosedChildParent = 
							pclosedChild->getParent();
						if ((NodeType *)pclosedChildParent == (NodeType *)pclosed)
						{
							// parent of closed child is the closed
							// node, so just skip it.
							continue;
						}
TRACE();

						// compare g-values to determine if the new path
						// is better (cheaper) than the old path to 
						// the closed child node.
						//
						int oldClosedChildGvalue = pclosedChild->getGvalue();
						pclosedChild->setParent(pclosed);
						pclosedChild->heuristic(*pstart);
						int newClosedChildGvalue = pclosedChild->getGvalue();
						if (newClosedChildGvalue > oldClosedChildGvalue)
						{
							// set parent to old value
							pclosedChild->setParent(pclosedChildParent);
							pclosedChild->heuristic(*pstart);
						}
					}
				}

				// delete copy of node, and insert
				// old node, pclosed, into children
				// list of pnode.
				//
				nodesToRemove.insertAtFront(pchild);
				nodesToInsert.insertAtFront(pclosed);
			}
			else if (inopen == NOMATCH && inclosed == NOMATCH)
			{
				// new node, place in open queue
				openpq.insertByValue(pchild);
				if (((++uniquenodes%1000) == 0) && (uniquenodes > 0))
				{
					cout << uniquenodes << " unique nodes." << endl;
				}
			}
			else
			{
				// an error of some type
				return(NOTOK);
			}
		}

		// get pointer to nodes children list
		List<NodePtr<NodeType> > 
			*pchildren = pnode->getChildren();

		// remove children nodes
		ListIterator<NodePtr<NodeType> > 
			nodesToRemoveIter(nodesToRemove);
		for ( ; !nodesToRemoveIter.done(); nodesToRemoveIter++)
		{
			pchildren->removeByValue(nodesToRemoveIter());
		}

		// add children nodes
		ListIterator<NodePtr<NodeType> > 
			nodesToInsertIter(nodesToInsert);
		for ( ; !nodesToInsertIter.done(); nodesToInsertIter++)
		{
			pchildren->insertAtFront(nodesToInsertIter());
		}

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

	// finished with error
	return(NOTOK);
}
Beispiel #3
0
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);
}
Beispiel #4
0
int
dfs(Proxy<NodeType> &pnode, int &threshold, int &next_threshold, 
	BinaryTree_AVL<DataType> &startlist, 
	BinaryTree_AVL<DataType> &otherlist)
{
	int status;

	// expand the current node's children
	if ((status = pnode->expand(startlist, otherlist)) != OK)
	{
		// some type of error
		ERRORD("expand() failed.", status, errno);
		return(status);
	}

	// scan children for goal nodes 
	ListIterator<Proxy<NodeType> > childrenIter(*pnode->getChildren());
	for ( ; !childrenIter.done(); childrenIter++)
	{
		// get pointer to child
		Proxy<NodeType> pchild(childrenIter());

		// set up link to parent
		if (reporttype == ReportParent ||
		    reporttype == ReportBoth)
			pchild->setParent(pnode);

		// calculate the heuristic value
		if ((status = pchild->heuristic(startlist, otherlist)) != OK)
		{
			ERRORD("heuristic() failed.", status, errno);
			return(status);
		}

		// check if we have a goal node
		status = pchild->isGoal(startlist, otherlist);
		switch (status)
		{
		case OK:
			break;

		case NOMATCH:
		case MAXDEPTHEXCEEDED:
			continue;

		case MAXCLAUSEEXCEEDED:
			return(status);

		case VALID:
			// goal node, print solution
			PrintSolution(pnode);
			PrintRenameVariables(pnode);
			return(VALID);

		default:
			// some type of error
			ERRORD("isGoal() failed.", status, errno);
			return(status);
		}

		// check if we continue, is threshold exceeded?
		int childcost = pchild->getFvalue();
		if (childcost <= threshold)
		{
			// continue depth-first search
			status = dfs(pchild, threshold, next_threshold,
					startlist, otherlist);
			switch (status)
			{
			case OK:
			case NOMATCH:
			case MAXDEPTHEXCEEDED:
			case NOTPROVEN:
				break;
	
			case MAXCLAUSEEXCEEDED:
				return(MAXCLAUSEEXCEEDED);

			case VALID:
				// goal node, print solution
				return(VALID);

			default:
				// some type of error
				ERRORD("dfs() failed.", status, errno);
				return(status);
			}
		}
		else if (childcost < next_threshold)
		{
			next_threshold = childcost;
		}
	}

	// goal was not not proven
	return(NOTPROVEN);
}
Beispiel #5
0
int
BreadthFirstSearch(List<NodeType> &startstatelist, 
		BinaryTree_AVL<DataType> &startlist, 
		BinaryTree_AVL<DataType> &otherlist)
{
	int status;

	// track all states created
	BinaryTree_AVL<String> allstates;

	// create open priority queue
	List<Proxy<NodeType> > openpq;

	// copy list of start states
	int nodedepth = 1;
	ListIterator<NodeType> startstateIter(startstatelist);
	for ( ; !startstateIter.done(); startstateIter++)
	{
		// copy start state
		Proxy<NodeType> pstart(startstateIter());

		// set start node depth
		pstart->setDepth(nodedepth);

		// insert start state into open queue
		if ((status = openpq.insertAtEnd(pstart)) != OK)
		{
			ERRORD("insertAtFront() failed.", status, errno);
			return(status);
		}
	}

	// start search loop
	for (Proxy<NodeType> pnode; ! openpq.isEmpty(); )
	{
		// remove next node from priority open queue
		if ((status = openpq.removeAtFront(pnode)) != OK)
		{
			ERRORD("removeAtFront() failed.", status, errno);
			return(status);
		}

		// set current node depth
		nodedepth = pnode->getDepth();

		// check if we have a goal node or not
		status = pnode->isGoal(startlist, otherlist);
		switch (status)
		{
		case OK:
			break;

		case NOMATCH:
		case MAXDEPTHEXCEEDED:
			// no clauses were generated. skip further
			// processing of this node.
			continue;

		case VALID:
			// goal node, print solution
			PrintSolution(pnode);
			PrintRenameVariables(pnode);

			// check if more than one solutions is required
			solutionsfound += 1;
			statistics[SolutionsFound] += 1;
			totalstatistics[TotalSolutionsFound] += 1;
			if (solutionsfound >= solutionsrequired)
				return(VALID);
			continue;

		case MAXCLAUSEEXCEEDED:
			// check if any solutions were found
			if (solutionsfound > 0)
				return(VALID);
			else
				return(NOTPROVEN);

		default:
			// some type of error
			ERRORD("isGoal() failed.", status, errno);
			return(status);
		}

		// generate the children of the current node
		if ((status = pnode->expand(startlist, otherlist)) != OK)
		{
			ERRORD("expand() failed.", status, errno);
			return(status);
		}

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

			// set up link to parent
			if (reporttype == ReportParent ||
			    reporttype == ReportBoth)
				pchild->setParent(pnode);

			// insert into queue
			if (!bfswithchecks)
			{
				pchild->setDepth(nodedepth+1);
				if ((status = openpq.insertAtEnd(
					pchild)) != OK)
				{
					ERRORD("insertAtEnd() failed.", 
						status, errno);
					return(status);
				}
			}
			else
			{
				statistics[RedundantClauseTestsAttempted] += 1;
				totalstatistics[TotalRedundantClauseTestsAttempted] += 1;
				String newnode(pchild->getNormalizedClauses());
				if (allstates.retrieve(newnode) == NOMATCH)
				{
					pchild->setDepth(nodedepth+1);
					if ((status = openpq.insertAtEnd(
							pchild)) != OK)
					{
						ERRORD("insertAtEnd() failed.", 
							status, errno);
						return(status);
					}
				}
				else
				{
					statistics[RedundantClausesRejected] += 1;
					totalstatistics[TotalRedundantClausesRejected] += 1;
				}
			}
		}

		if (bfswithchecks)
		{
			if (allstates.insert(pnode->getNormalizedClauses()) != OK)
			{
				ERRORD("insert() failed.", status, errno);
				return(status);
			}
		}
	}

	// check if any solutions were found
	if (solutionsfound > 0)
		return(VALID);
	else
		return(NOTPROVEN);
}
Beispiel #6
0
int
DepthFirstHillClimbSearch(List<NodeType> &startstatelist, 
		BinaryTree_AVL<DataType> &startlist, 
		BinaryTree_AVL<DataType> &otherlist)
{
	int status;

	// track all states created
	BinaryTree_AVL<String> allstates;

	// create open stack for traversal
	List<Proxy<NodeType> > openstack;

	// keep track of current path here instead of 
	// using parent pointers. parent pointers cause
	// cycles and reference-counted pointers cannot
	// deal with cyclic data structures. they cause
	// major memory leaks.
	//
	int nodedepth = 1;
	List<Proxy<NodeType> > currentpath;

	// copy list of start states
	ListIterator<NodeType> startstateIter(startstatelist);
	for ( ; !startstateIter.done(); startstateIter++)
	{
		// copy start state
		Proxy<NodeType> pstart(startstateIter());

		// calculate the heuristic value for this node
		if ((status = pstart->heuristic(startlist, otherlist)) != OK)
		{
			ERRORD("heuristic() failed.", status, errno);
			return(status);
		}

		// set start node depth
		pstart->setDepth(nodedepth);

		// insert start state into open queue
		if ((status = openstack.insertOrdered(pstart)) != OK)
		{
			ERRORD("insertOrdered() failed.", status, errno);
			return(status);
		}
	}

	// children priority queue
	List<Proxy<NodeType> > childpq;
	
	// start search loop
	int olddepth = nodedepth;
	Proxy<NodeType> pchild;
	for (Proxy<NodeType> pnode; ! openstack.isEmpty(); )
	{
		// remove next node from priority open queue
		if ((status = openstack.removeAtFront(pnode)) != OK)
		{
			ERRORD("removeAtFront() failed.", status, errno);
			return(status);
		}

		// check if we have a goal node or not
		status = pnode->isGoal(startlist, otherlist);
		switch (status)
		{
		case OK:
			// update current path
			if ((status = updatepath(nodedepth, 
				olddepth, pnode, currentpath)) != OK)
				return(status);
			break;

		case NOMATCH:
		case MAXDEPTHEXCEEDED:
			// no clauses were generated. skip further
			// processing of this node.
			continue;

		case VALID:
			// update current path
			if ((status = updatepath(nodedepth, 
				olddepth, pnode, currentpath)) != OK)
				return(status);

			// goal node, print solution
			PrintSolution(pnode);
			PrintSolution(currentpath);
			PrintRenameVariables(pnode);

			// check if more than one solutions is required
			solutionsfound += 1;
			statistics[SolutionsFound] += 1;
			totalstatistics[TotalSolutionsFound] += 1;
			if (solutionsfound >= solutionsrequired)
				return(VALID);
			continue;

		case MAXCLAUSEEXCEEDED:
			// check if any solutions were found
			if (solutionsfound > 0)
				return(VALID);
			else
				return(NOTPROVEN);

		default:
			// some type of error
			ERRORD("isGoal() failed.", status, errno);
			return(status);
		}

		// generate the children of the current node
		if ((status = pnode->expand(startlist, otherlist)) != OK)
		{
			ERRORD("expand() failed.", status, errno);
			return(status);
		}

		// clear children priority queue
		childpq.clear();

		// set up links to parent and calculate the heuristic value
		ListIterator<Proxy<NodeType> > 
			childrenIter(*pnode->getChildren());
		for ( ; !childrenIter.done(); childrenIter++)
		{
			// pointer to child
			pchild = childrenIter();

			// set up link to parent
			if (reporttype == ReportParent ||
			    reporttype == ReportBoth)
				pchild->setParent(pnode);

			// calculate the heuristic value
			if ((status = pchild->heuristic(
				startlist, otherlist)) != OK)
			{
				ERRORD("heuristic() failed.", status, errno);
				return(status);
			}

			// insert child into a priority queue to order
			pchild->setDepth(nodedepth+1);
			if ((status = childpq.insertOrdered(pchild)) != OK)
			{
				ERRORD("insertOrdered() failed.", 
					status, errno);
				return(status);
			}
		}

		// insert nodes into stack ordered by heuristic value
		while (!childpq.isEmpty())
		{
			// remove next node from priority open queue
			if ((status = childpq.removeAtEnd(pchild)) != OK)
			{
				ERRORD("removeAtEnd() failed.", 
					status, errno);
				return(status);
			}

			// insert node into a stack
			if (!bfswithchecks)
			{
				if ((status = openstack.insertAtFront(
					pchild)) != OK)
				{
					ERRORD("insertAtFront() failed.", 
						status, errno);
					return(status);
				}
			}
			else
			{
				statistics[RedundantClauseTestsAttempted] += 1;
				totalstatistics[TotalRedundantClauseTestsAttempted] += 1;
				String newnode(pchild->getNormalizedClauses());
				if (allstates.retrieve(newnode) == NOMATCH)
				{
					if ((status = openstack.insertAtFront(
							pchild)) != OK)
					{
						ERRORD("insertAtFront() failed.", 
							status, errno);
						return(status);
					}
				}
				else
				{
					statistics[RedundantClausesRejected] += 1;
					totalstatistics[TotalRedundantClausesRejected] += 1;
				}
			}
		}

		// children are now in the queue. release pointers to
		// children stored in node.
		//
		pnode->getChildren()->clear();
		if (bfswithchecks)
		{
			if (allstates.insert(pnode->getNormalizedClauses()) != OK)
			{
				ERRORD("insert() failed.", status, errno);
				return(status);
			}
		}
	}

	// check if any solutions were found
	if (solutionsfound > 0)
		return(VALID);
	else
		return(NOTPROVEN);
}