void Routing::generateRouteSetup(Point start, Point goal, Direction currDir) { Point pointBehindStart = pointBehind(start, currDir); if( ! maze->contains(pointBehindStart)) pointBehindStart = start; nodeList.resetList(goal, start, pointBehindStart, calcHeuristic(start,goal)); }
/************************************************************************************************** * Construcot * ***********************************************************************************************/ Solver::DeepSearch::DeepSearch(const Game::Game& field): SolverBaseClass(field) { positions.resize(field.getTileCount()); currentValue.resize(field.getField().getSizeX()); for(uint i=0; i< field.getField().getSizeX(); ++i){ currentValue[i].resize(field.getField().getSizeY(), 0); } for(uint x=0; x<field.getField().getSizeX(); ++x){ for(uint y=0; y<field.getField().getSizeY(); ++y){ currentValue[x][y] = field.getField().getField(x,y); } } for(uint i=0;i<gameField.getTileCount(); ++i){ positionsMax.push_back(Pos<uint>(field.getField().getSizeX() - field.getTile(i).getSizeX()+1,1+ field.getField().getSizeY() - field.getTile(i).getSizeY())); std::cout << "Max Pos: " <<positionsMax[i] << std::endl; } heuristicValue = calcHeuristic(); std::vector<bool> used; used.resize(field.getTileCount(), false); tileObserveList.resize(field.getTileCount()); for(uint i=0; i<used.size(); ++i){ uint filledValue(9999999); uint filledNumber(0); for(uint tile=0; tile<field.getTileCount(); ++tile){ if(used[tile]) continue; if(gameField.getTile(tile).getCountSet() < filledValue){ filledValue = gameField.getTile(tile).getCountSet(); filledNumber = tile; } } used[i] = true; tileObserveList[i] = filledNumber; } maxFlips.resize(field.getTileCount(), false); maxFlips[field.getTileCount()-1] = field.getTile(tileObserveList[field.getTileCount()-1]).getCountSet(); for(int i=field.getTileCount()-2; i>=0;--i){ maxFlips[i] = maxFlips[i+1] + field.getTile(tileObserveList[i]).getCountSet(); } }
bool Routing::classifyPoint(Point adjacentPt, Point current, Point parentOfCurrent, Point goal) { unsigned int adjacentPtCost = calcCost(parentOfCurrent, current, adjacentPt) + nodeList.calcCost(current); //Make it OPEN if it is NONE; if it is OPEN check if we can get there faster via the current node, if so we change the parent switch (nodeList.getListType(adjacentPt)) { case NodeList::OPEN: nodeList.update(adjacentPt, current, adjacentPtCost); break; case NodeList::NONE: if( ! maze->isPassable(adjacentPt)) nodeList.close(adjacentPt); else nodeList.update(adjacentPt, current, adjacentPtCost, calcHeuristic(adjacentPt, goal)); break; case NodeList::GOAL: nodeList.update(adjacentPt, current, adjacentPtCost); return true; } return false; }
double AI::DLMM(boardState* nodeOrig, int depth, int QS_Depth, bool maximizingPlayer, double alpha, double beta){ bool terminal = false; double newAlpha = alpha; double newBeta = beta; double newVal = alpha; int moverid = playerID(); map<string, int>::iterator it1; if (!maximizingPlayer){ if (moverid == 0){ moverid = 1; } else{ moverid = 0; } } vector<fakePiece> blank; if (kingInCheckmate(nodeOrig, moverid)){ double retVal = calcHeuristic(moverid, nodeOrig, blank); return (retVal); } else if ((depth == 0 && nodeOrig->QS_State == false) || (depth == 0 && QS_Depth == 0)){ double retVal = calcHeuristic(moverid, nodeOrig, blank); return (retVal); } else if (maximizingPlayer){ newBeta = beta; newAlpha = alpha; newVal = alpha; possibleMovesStruct* possibleMovesMax = new possibleMovesStruct; allPossibleMoves(nodeOrig, possibleMovesMax, moverid); if (possibleMovesMax->availiableMoves.size() > 0){ int size = possibleMovesMax->availiableMoves.size() - 1; for (int i = size; i >= 0; i--){ boardState* nodeNew = new boardState; *(nodeNew) = *(nodeOrig); int fileFrom = possibleMovesMax->availiableMoves.top().fileFrom; int rankFrom = possibleMovesMax->availiableMoves.top().rankFrom; int fileTo = possibleMovesMax->availiableMoves.top().fileTo; int rankTo = possibleMovesMax->availiableMoves.top().rankTo; nodeNew->unofficialMove(fileFrom, rankFrom, fileTo, rankTo, moverid, 0, 2); if (depth != 0){ newVal = DLMM(nodeNew, depth, QS_Depth - 1, false, newAlpha, newBeta); } else{ newVal = DLMM(nodeNew, depth - 1, QS_Depth, false, newAlpha, newBeta); } if (newVal <= newAlpha){ // Fail low possibleMovesMax->availiableMoves.pop(); } else if (newVal >= newBeta){ //Fail high -> prune because fail high on max size = -1; it1 = myHistoryTable.find(possibleMovesMax->availiableMoves.top().historyTableVal); while (possibleMovesMax->availiableMoves.size() > 0){ possibleMovesMax->availiableMoves.pop(); } } else{ // New best value/alpha newAlpha = newVal; it1 = myHistoryTable.find(possibleMovesMax->availiableMoves.top().historyTableVal); possibleMovesMax->availiableMoves.pop(); } delete nodeNew; } delete possibleMovesMax; //History table updates and inserts int currentCount = it1->second; string currentString = it1->first; myHistoryTable.erase(currentString); myHistoryTable[currentString] = currentCount + 1; return newAlpha; } else{ return alpha; } } else{ //Min player newAlpha = alpha; newBeta = beta; if (alpha != 0){ newBeta = alpha; } else{ newBeta = 1000; } newVal = 0; possibleMovesStruct* possibleMovesMax = new possibleMovesStruct; allPossibleMoves(nodeOrig, possibleMovesMax, moverid); if (possibleMovesMax->availiableMoves.size() > 0){ int size = possibleMovesMax->availiableMoves.size() - 1; for (int i = size; i >= 0; i--){ boardState* nodeNew = new boardState; *(nodeNew) = *(nodeOrig); int fileFrom = possibleMovesMax->availiableMoves.top().fileFrom; int rankFrom = possibleMovesMax->availiableMoves.top().rankFrom; int fileTo = possibleMovesMax->availiableMoves.top().fileTo; int rankTo = possibleMovesMax->availiableMoves.top().rankTo; nodeNew->unofficialMove(fileFrom, rankFrom, fileTo, rankTo, moverid, 0, 2); if (depth == 0){ newVal = DLMM(nodeNew, depth, QS_Depth - 1, true, newAlpha, newBeta); } else{ newVal = DLMM(nodeNew, depth - 1, QS_Depth, true, newAlpha, newBeta); } if (newVal <= newAlpha){ // Fail low on min -> prune size = -1; it1 = myHistoryTable.find(possibleMovesMax->availiableMoves.top().historyTableVal); while (possibleMovesMax->availiableMoves.size() > 0){ possibleMovesMax->availiableMoves.pop(); } } else if (newVal >= newBeta){ //Fail high on min -> dont use this one possibleMovesMax->availiableMoves.pop(); } else{ //We found a new value which is lower than the highest beta newBeta = newVal; it1 = myHistoryTable.find(possibleMovesMax->availiableMoves.top().historyTableVal); possibleMovesMax->availiableMoves.pop(); } delete nodeNew; } delete possibleMovesMax; //History table updates and inserts int currentCount = it1->second; string currentString = it1->first; myHistoryTable.erase(currentString); myHistoryTable[currentString] = currentCount + 1; return (newBeta); } else{ return (beta); } } }
bool AStarPlanner::computePath(std::vector<Util::Point>& agent_path, Util::Point start, Util::Point goal, SteerLib::GridDatabase2D * _gSpatialDatabase, bool append_to_path) { std::cout << goal << std::endl; gSpatialDatabase = _gSpatialDatabase; //Build node trees/sets priorityQueue openNodes = priorityQueue(); std::vector<SteerLib::AStarPlannerNode> closedNodes; double max = 10000; //Build start node SteerLib::AStarPlannerNode startNode = SteerLib::AStarPlannerNode(start, 0, max-(0+HEURISTICWEIGHT*calcHeuristic(start, goal, false)), -1); startNode.gridIndex = gSpatialDatabase->getCellIndexFromLocation(start); openNodes.insert(startNode); int expandedCount = 0; while(!openNodes.heap.empty()) { SteerLib::AStarPlannerNode currNode = openNodes.pop(); expandedCount++; closedNodes.push_back(currNode); //Found goal, trace back for path if(currNode.gridIndex == gSpatialDatabase->getCellIndexFromLocation(goal)) { std::cout << std::endl << "PATH LENGTH: " << currNode.g << std::endl << "NODES EXPANDED: " << expandedCount << std::endl; currNode.point = goal; int currIndex = currNode.gridIndex; while(currIndex != gSpatialDatabase->getCellIndexFromLocation(start)) { Util::Point currPoint; gSpatialDatabase->getLocationFromIndex(currIndex, currPoint); agent_path.insert(agent_path.begin(), currPoint); currIndex = closedNodes[searchClosed(closedNodes, currIndex, 0, 0, gSpatialDatabase)].parentIndex; } agent_path.insert(agent_path.begin(), start); return true; } //TODO //Update neighbors unsigned int xIndex, zIndex; gSpatialDatabase->getGridCoordinatesFromIndex(currNode.gridIndex, xIndex, zIndex); //aligned neighbors //check if neighbor is valid (out of bounds of grid) //left if(xIndex-1 >= 0 && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex-1, zIndex))) { //Is in closed set? if(searchClosed(closedNodes, currNode.gridIndex, -1, 0, gSpatialDatabase)==-1) { //Is in open set? openNodes.update(currNode, -1, 0, gSpatialDatabase, goal); } } //right if(xIndex+1 < gSpatialDatabase->getNumCellsX() && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex+1, zIndex))) { if(searchClosed(closedNodes, currNode.gridIndex, 1, 0, gSpatialDatabase)==-1) { openNodes.update(currNode, 1, 0, gSpatialDatabase, goal); } } //top if(zIndex-1 >= 0 && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex, zIndex-1))) { if(searchClosed(closedNodes, currNode.gridIndex, 0, -1, gSpatialDatabase)==-1) { openNodes.update(currNode, 0, -1, gSpatialDatabase, goal); } } //bottom if(zIndex+1 < gSpatialDatabase->getNumCellsZ() && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex, zIndex+1))) { if(searchClosed(closedNodes, currNode.gridIndex, 0, 1, gSpatialDatabase)==-1) { openNodes.update(currNode, 0, 1, gSpatialDatabase, goal); } } //diagonal neighbors //top-left if(zIndex-1>=0 && xIndex-1>=0 && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex-1, zIndex-1))) { if(searchClosed(closedNodes, currNode.gridIndex, -1, -1, gSpatialDatabase)==-1) { openNodes.update(currNode, -1, -1, gSpatialDatabase, goal); } } //top-right if(zIndex+-1>=0 && xIndex+1<gSpatialDatabase->getNumCellsX() && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex+1, zIndex-1))) { if(searchClosed(closedNodes, currNode.gridIndex, 1, -1, gSpatialDatabase)==-1) { openNodes.update(currNode, 1, -1, gSpatialDatabase, goal); } } //bottom-left if(zIndex+1<gSpatialDatabase->getNumCellsZ() && xIndex-1>=0 && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex-1, zIndex+1))) { if(searchClosed(closedNodes, currNode.gridIndex, -1, 1, gSpatialDatabase)==-1) { openNodes.update(currNode, -1, 1, gSpatialDatabase, goal); } } //bottom-right if(zIndex+1<gSpatialDatabase->getNumCellsZ() && xIndex+1<gSpatialDatabase->getNumCellsX() && canBeTraversed(gSpatialDatabase->getCellIndexFromGridCoords(xIndex+1, zIndex+1))) { if(searchClosed(closedNodes, currNode.gridIndex, 1, 1, gSpatialDatabase)==-1) { openNodes.update(currNode, 1, 1, gSpatialDatabase, goal); } } } //TODO std::cout<<"\nIn A*"; return false; }