Example #1
0
//------------------------------------------------------------------------------
//deleteData() -- delete member data
//------------------------------------------------------------------------------
void Puzzle::deleteData()
{
   setInitState(nullptr);
   setGoalState(nullptr);
   clearOpenList();
   clearHashTable();
}
Example #2
0
    const Node* explore(const State& startState, TerminationChecker& terminationChecker) {
        ++iterationCounter;
        clearOpenList();
        openList.reorder(fComparator);

        Planner::incrementGeneratedNodeCount();
        Node*& startNode = nodes[startState];

        if (startNode == nullptr) {
            startNode = nodePool->construct(Node{nullptr, startState, Action(), 0, domain.heuristic(startState), true});
        } else {
            startNode->g = 0;
            startNode->action = Action();
            startNode->predecessors.clear();
            startNode->parent = nullptr;
        }

        startNode->iteration = iterationCounter;
        addToOpenList(*startNode);

        while (!terminationChecker.reachedTermination() && openList.isNotEmpty()) {
            Node* const currentNode = popOpenList();

            if (domain.isGoal(currentNode->state)) {
                return currentNode;
            }

            terminationChecker.notifyExpansion();
            expandNode(currentNode);
        }

        return openList.top();
    }
Example #3
0
    Node* explore(const State& startState, TerminationChecker& terminationChecker) {
        ++iterationCounter;
        clearOpenList();
        openList.reorder(fComparator);

        Planner::incrementGeneratedNodeCount();
        Node*& startNode = nodes[startState];

        if (startNode == nullptr) {
            startNode = nodePool->construct(Node{nullptr, startState, Action(), 0, domain.heuristic(startState), true});
        } else {
            startNode->g = 0;
            startNode->action = Action();
            startNode->predecessors.clear();
            startNode->parent = nullptr;
        }

        startNode->iteration = iterationCounter;
        addToOpenList(*startNode);

        Node* currentNode = startNode;

        checkSafeNode(currentNode);

        while (!terminationChecker.reachedTermination() && !domain.isGoal(currentNode->state)) {
            //            if (domain.safetyPredicate(currentNode->state)) { // try to find nodes which lead to safety
            //                currentNode = popOpenList();
            //                terminationChecker.notifyExpansion();
            //                expandNode(currentNode);
            //            }
            //
            //            if (currentNode == startNode) { // if we can't just do LSS-LRTA*
            //                while (!terminationChecker.reachedTermination() && !domain.isGoal(currentNode->state)) {
            //                    currentNode = popOpenList();
            //                    terminationChecker.notifyExpansion();
            //                    expandNode(currentNode);
            //                }
            //            }
            Node* const currentNode = popOpenList();
            if (domain.isGoal(currentNode->state)) {
                return currentNode;
            }

            terminationChecker.notifyExpansion();
            expandNode(currentNode);
        }

        return openList.top();
    }
Example #4
0
MapNode* MapUtils::searchPath(int startRowIndex, int startColIndex, int endRowIndex, int endColIndex, Map* map)
{
	CCArray* openList = new CCArray;
	CCArray* closeList = new CCArray;
	MapNode* endNode = map->getMapNode(endRowIndex, endColIndex);
	MapNode* searchNode = map->getMapNode(startRowIndex, startColIndex);
	searchNode->g = 0;
	searchNode->h = MapUtils::calculateH(searchNode, endNode);
	searchNode->f = searchNode->g + searchNode->h;
	int g, f, h;
	const int NORMAL = 10;
	const int ANGLE = 14;
	while (searchNode->nodeData->matrixIndex->rowIndex != endRowIndex || searchNode->nodeData->matrixIndex->colIndex != endColIndex)
	{
		/***遍历点的四周****/
		int colIndex, rowIndex, col, row, i, j;
		row = std::min(searchNode->nodeData->matrixIndex->rowIndex + 2, map->nodeData->matrix->row);
		col = std::min(searchNode->nodeData->matrixIndex->colIndex + 2, map->nodeData->matrix->col);
		rowIndex = std::max(searchNode->nodeData->matrixIndex->rowIndex - 1, 0);
		colIndex = std::max(searchNode->nodeData->matrixIndex->colIndex - 1, 0);
		for (i = rowIndex; i < row; i ++)
		{

			for (j = colIndex; j < col; j ++)
			{
				MapNode* checkNode = map->getMapNode(i, j);
				/**是否和检测的节点相等**/
				bool b1 = MapUtils::checkNode(searchNode, checkNode);
				/**是否容许通过**/
				bool b2 = MapUtils::checkAllow(checkNode);
				/**斜角是否容许通过**/
				bool b3 = MapUtils::checkAllow(map->getMapNode(searchNode->nodeData->matrixIndex->rowIndex, checkNode->nodeData->matrixIndex->colIndex));
				/**斜角是否容许通过**/
				bool b4 = MapUtils::checkAllow(map->getMapNode(searchNode->nodeData->matrixIndex->colIndex, checkNode->nodeData->matrixIndex->rowIndex));
				if (b1 == true || b2 == false || b3 == false || b4 == false)
				{
					continue;
				}
				if (searchNode->nodeData->matrixIndex->rowIndex != checkNode->nodeData->matrixIndex->rowIndex
					&& searchNode->nodeData->matrixIndex->colIndex != checkNode->nodeData->matrixIndex->colIndex)
				{
					g = searchNode->g + ANGLE;
				}
				else
				{
					g = searchNode->g + NORMAL;
				}
				h = MapUtils::calculateH(checkNode, endNode);
				f = g + h;
				if (checkNode->isOpen == true || checkNode->isClose == true)
				{
					if (checkNode->f > f)
					{
						checkNode->f = f;
						checkNode->h = h;
						checkNode->g = g;
						checkNode->father = searchNode;
					}
				}
				else
				{
					checkNode->f = f;
					checkNode->h = h;
					checkNode->g = g;
					checkNode->father = searchNode;

					checkNode->isOpen = true;
					openList->addObject(checkNode);
				}
			}
		}
		searchNode->isClose = true;
		closeList->addObject(searchNode);
		if (openList->count() <= 0)
		{
			return NULL;
		}
		searchNode = MapUtils::getMinF(openList);/**获取f值最小的节点,并且从openList中删除**/
		searchNode->isOpen = false;
	}
	clearCloseList(closeList);
	clearOpenList(openList);
	delete openList;
	delete closeList;
	return searchNode;
}