// Constructor FloodFillFinder(bool shouldPause = false) : pause(shouldPause) { coords.clearAll(); horizontal.clearAll(); vertical.clearAll(); heading = NORTH; for(int i = MazeDefinitions::MAZE_LEN/2; i < MazeDefinitions::MAZE_LEN; i++){ for(int j = 0; j < MazeDefinitions::MAZE_LEN/2; j++) Distance[i][j] = Manhattan(i, j, MazeDefinitions::MAZE_LEN/2, MazeDefinitions::MAZE_LEN/2 - 1); } for(int i = MazeDefinitions::MAZE_LEN/2; i < MazeDefinitions::MAZE_LEN; i++){ for(int j = MazeDefinitions::MAZE_LEN/2; j < MazeDefinitions::MAZE_LEN; j++) Distance[i][j] = Manhattan(i, j, MazeDefinitions::MAZE_LEN/2, MazeDefinitions::MAZE_LEN/2); } for(int i = 0; i < MazeDefinitions::MAZE_LEN/2; i++){ for(int j = 0; j < MazeDefinitions::MAZE_LEN/2; j++) Distance[i][j] = Manhattan(i, j, MazeDefinitions::MAZE_LEN/2 - 1, MazeDefinitions::MAZE_LEN/2 - 1); } for(int i = 0; i < MazeDefinitions::MAZE_LEN/2; i++){ for(int j = MazeDefinitions::MAZE_LEN/2; j < MazeDefinitions::MAZE_LEN; j++) Distance[i][j] = Manhattan(i, j, MazeDefinitions::MAZE_LEN/2 - 1, MazeDefinitions::MAZE_LEN/2); } for(int i = 0; i < MazeDefinitions::MAZE_LEN; i++) horizontal.set(0, i); for(int i = 0; i < MazeDefinitions::MAZE_LEN; i++) vertical.set(i, 0); }
// (Basic heuristic) Returns the Manhattan distance between two points in 1D coordinates int Manhattan(const int width, const int i, const int j) { int xStart; int yStart; int xTarget; int yTarget; OneDToTwoD(width, i, xStart, yStart); OneDToTwoD(width, j, xTarget, yTarget); return Manhattan(xStart, yStart, xTarget, yTarget); }
// A* implementation int FindPath( const int nStartX, const int nStartY, const int nTargetX, const int nTargetY, const unsigned char* pMap, const int nMapWidth, const int nMapHeight, int* pOutBuffer, const int nOutBufferSize) { // Lock critical section (verrrry basic way) simple_mutex.lock(); // If Start equals target path size is 0 if (nStartX == nTargetX && nStartY == nTargetY) { return 0; } // Define the open and the close list std::vector<int> openList; std::vector<int> closedList; // Cost and heuristic buffers const int mapSize = nMapWidth * nMapHeight; int* costSoFar = new int[mapSize]; int* hEstimate = new int[mapSize]; int* camefrom = new int[mapSize]; // Initialize path history for (int i = 0; i < mapSize; ++i) { camefrom[i] = INDEX_NULL; } int indexStart; int indexEnd; TwoDToOneD(nMapWidth, nStartX, nStartY, indexStart); TwoDToOneD(nMapWidth, nTargetX, nTargetY, indexEnd); costSoFar[indexStart] = 0; hEstimate[indexStart] = Manhattan(nStartX, nStartY, nTargetX, nTargetY); // Add the start point to the openList openList.push_back(indexStart); int current = openList[0]; bool success = false; // Release the Kraken ! while (openList.size() > 0) { // Look for the smallest cost in the openList and make it the current point int best = INTEGER_MAX; const int openListSize = openList.size(); int i = 0; for (; i < openListSize; ++i) { int cost = hEstimate[openList[i]]; if (cost < best) { current = openList[i]; best = cost; } } // Remove the current point from the openList openList.erase(openList.begin() + (i - 1)); // Add the current point to the closedList closedList.push_back(current); // Stop if we reached the end if (current == indexEnd) { success = true; break; } // Go over valid neighbors and get the most interesting std::vector<int> neighbors; NeighborsOneD(current, pMap, nMapWidth, nMapHeight, neighbors); const int neighborsSize = neighbors.size(); i = 0; for (; i < neighborsSize; ++i) { const int currentNeighbor = neighbors[i]; // If accessible and not already kicked out (could be done in Neighbors research function) if ((int)pMap[currentNeighbor] == 1 && std::find(closedList.begin(), closedList.end(), currentNeighbor) == closedList.end()) { // Evaluate cost and add to the open list const int cost = costSoFar[current] + Manhattan(nMapWidth, current, currentNeighbor); if (std::find(openList.begin(), openList.end(), currentNeighbor) == openList.end()) { openList.push_back(currentNeighbor); } // Else if already in open and is more expensive than before, forget it. else if (cost >= costSoFar[currentNeighbor]) { continue; } costSoFar[currentNeighbor] = cost; hEstimate[currentNeighbor] = cost + Manhattan(nMapWidth, currentNeighbor, indexEnd); camefrom[current] = currentNeighbor; } } } // If we reached target, fill out output buffer int pathIndex = 0; if (success) { for (int idx = 0; camefrom[idx] != INDEX_NULL; ++idx) { pOutBuffer[pathIndex] = camefrom[idx]; ++pathIndex; } pOutBuffer[pathIndex] = current; } //Clean up dynamic alloc delete hEstimate; delete camefrom; delete costSoFar; //Unlock critical section simple_mutex.unlock(); return success ? pathIndex + 1 : INDEX_NULL; }
void run_test(Cost_Map &map, Point &start, Point &goal, vector<int> ×) { struct timeval pre; struct timeval post; /// printf("UCS\n"); UCS ucs(&map, start, goal); gettimeofday(&pre, NULL); Path path = ucs.search(); gettimeofday(&post, NULL); double reference_cost = path.length; check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("A*\n"); Manhattan Manhattan(&map, start, goal); gettimeofday(&pre, NULL); path = Manhattan.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("A*\n"); Euclidean euclidean(&map, start, goal); gettimeofday(&pre, NULL); path = euclidean.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("A*\n"); Octile octile(&map, start, goal); gettimeofday(&pre, NULL); path = octile.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("Coarse single\n"); CUCS_Heuristic cucs(&map, start, goal); gettimeofday(&pre, NULL); path = cucs.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("BB\n"); Boundaries_Blocking bb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = bb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Boundaries_Blocking bb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = bb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("BN\n"); Boundaries_NonBlocking bnb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = bnb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Boundaries_NonBlocking bnb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = bnb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("CB\n"); Corners_Blocking cb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = cb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Corners_Blocking cb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = cb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("CN\n"); Corners_NonBlocking cnb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = cnb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Corners_NonBlocking cnb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = cnb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); }
bool UnitTests::WeightedDataMatrixMothur() { std::vector< std::vector<double> > dissMatrix; // weighted Bray-Curtis DiversityCalculator BC("../unit-tests/DataMatrixMothur.env", "", "Bray-Curtis", 1000, true, false, false, false, false); BC.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.8)) return false; if(!Compare(dissMatrix[2][0], 0.6)) return false; if(!Compare(dissMatrix[2][1], 0.8)) return false; // weighted Canberra DiversityCalculator Canberra("../unit-tests/DataMatrixMothur.env", "", "Canberra", 1000, true, false, false, false, false); Canberra.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 6.35152)) return false; if(!Compare(dissMatrix[2][0], 8.11111)) return false; if(!Compare(dissMatrix[2][1], 5.92063)) return false; // weighted Gower DiversityCalculator Gower("../unit-tests/DataMatrixMothur.env", "", "Gower", 1000, true, false, false, false, false); Gower.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 6.58333)) return false; if(!Compare(dissMatrix[2][0], 7.88889)) return false; if(!Compare(dissMatrix[2][1], 5.52778)) return false; // weighted Hellinger DiversityCalculator Hellinger("../unit-tests/DataMatrixMothur.env", "", "Hellinger", 1000, true, false, false, false, false); Hellinger.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.13904)) return false; if(!Compare(dissMatrix[2][0], 1.05146)) return false; if(!Compare(dissMatrix[2][1], 1.17079)) return false; // weighted Manhattan DiversityCalculator Manhattan("../unit-tests/DataMatrixMothur.env", "", "Manhattan", 1000, true, false, false, false, false); Manhattan.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.6)) return false; if(!Compare(dissMatrix[2][0], 1.2)) return false; if(!Compare(dissMatrix[2][1], 1.6)) return false; // weighted Morisita-Horn DiversityCalculator MH("../unit-tests/DataMatrixMothur.env", "", "Morisita-Horn", 1000, true, false, false, false, false); MH.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.873239)) return false; if(!Compare(dissMatrix[2][0], 0.333333)) return false; if(!Compare(dissMatrix[2][1], 0.859155)) return false; // weighted Soergel DiversityCalculator Soergel("../unit-tests/DataMatrixMothur.env", "", "Soergel", 1000, true, false, false, false, false); Soergel.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.88888901)) return false; if(!Compare(dissMatrix[2][0], 0.75)) return false; if(!Compare(dissMatrix[2][1], 0.88888901)) return false; // weighted species profile /* DiversityCalculator SP("../unit-tests/DataMatrixMothur.env", "", "Species profile", 1000, true, false, false, false, false); SP.Dissimilarity("../unit-tests/temp.diss"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.78740102)) return false; if(!Compare(dissMatrix[2][0], 0.44721401)) return false; if(!Compare(dissMatrix[2][1], 0.78102499)) return false; */ // weighted Chi-squared // Note: EBD uses a slightly different form of the Chi-squared measure as suggested in Numerical Ecology by Legendre adn Legendre // Nonetheless, it is easy to verify this using mothur. Simply divide by sqrt(N), N is the total number of sequences. DiversityCalculator ChiSquared("../unit-tests/DataMatrixMothur.env", "", "Chi-squared", 1000, true, false, false, false, false); ChiSquared.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.0973200)) return false; if(!Compare(dissMatrix[2][0], 0.96513098)) return false; if(!Compare(dissMatrix[2][1], 1.1147900)) return false; // weighted Euclidean DiversityCalculator Euclidean("../unit-tests/DataMatrixMothur.env", "", "Euclidean", 1000, true, false, false, false, false); Euclidean.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.78740102)) return false; if(!Compare(dissMatrix[2][0], 0.44721401)) return false; if(!Compare(dissMatrix[2][1], 0.78102499)) return false; // weighted Kulczynski DiversityCalculator Kulczynski("../unit-tests/DataMatrixMothur.env", "", "Kulczynski", 1000, true, false, false, false, false); Kulczynski.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.8)) return false; if(!Compare(dissMatrix[2][0], 0.6)) return false; if(!Compare(dissMatrix[2][1], 0.8)) return false; // weighted Pearson DiversityCalculator uPearson("../unit-tests/DataMatrixMothur.env", "", "Pearson", 1000, true, false, false, false, false); uPearson.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.22089)) return false; if(!Compare(dissMatrix[2][0], 0.5)) return false; if(!Compare(dissMatrix[2][1], 1.2008)) return false; // weighted Yue-Clayton DiversityCalculator YC("../unit-tests/DataMatrixMothur.env", "", "YueClayton", 1000, true, false, false, false, false); YC.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.93233103)) return false; if(!Compare(dissMatrix[2][0], 0.5)) return false; if(!Compare(dissMatrix[2][1], 0.92424202)) return false; return true; }
void IdentifySuccessors_QP(PathJob* pj, PathNode* node) { Vec2i npos = PathNodePos(node); int thisdistance = Magnitude2(Vec2i(npos.x - pj->ngoalx, npos.y - pj->ngoalz)); if( !pj->closestnode || thisdistance < pj->closest ) { pj->closestnode = node; pj->closest = thisdistance; } int runningD = 0; if(node->previous) runningD = node->previous->totalD; bool standable[DIRS]; for(int i=0; i<DIRS; i++) standable[i] = Standable(pj, npos.x + offsets[i].x, npos.y + offsets[i].y); bool passable[DIRS]; passable[DIR_NW] = standable[DIR_NW] && standable[DIR_N] && standable[DIR_W]; passable[DIR_N] = standable[DIR_N]; passable[DIR_NE] = standable[DIR_NE] && standable[DIR_N] && standable[DIR_E]; passable[DIR_E] = standable[DIR_E]; passable[DIR_SE] = standable[DIR_SE] && standable[DIR_S] && standable[DIR_E]; passable[DIR_S] = standable[DIR_S]; passable[DIR_SW] = standable[DIR_SW] && standable[DIR_S] && standable[DIR_W]; passable[DIR_W] = standable[DIR_W]; for(int i=0; i<DIRS; i++) { if(!passable[i]) continue; int newD = runningD + stepdist[i]; Vec2i nextnpos(npos.x + offsets[i].x, npos.y + offsets[i].y); PathNode* nextn = PathNodeAt(nextnpos.x, nextnpos.y); if(!nextn->closed && (!nextn->opened || newD < nextn->totalD)) { g_toclear.push_back(nextn); // Records this node to reset its properties later. nextn->totalD = newD; int H = Manhattan( nextnpos - Vec2i(pj->ngoalx, pj->ngoalz) ); nextn->F = nextn->totalD + H; nextn->previous = node; if( !nextn->opened ) { g_openlist.insert(nextn); nextn->opened = true; } else { g_openlist.heapify(nextn); } } } }