00001 //----------------------------------------------------------------------------- 00002 /** @file example.cpp 00003 Example program using the pathfinding library. 00004 00005 $Id: example_8cpp-source.html,v 1.1.1.1 2003/08/07 19:41:38 emarkus Exp $ 00006 $Source: /usr/cvsroot/www_pathfind/libpathfind/0.1.0/doc/example_8cpp-source.html,v $ 00007 */ 00008 //----------------------------------------------------------------------------- 00009 00010 #include <memory> 00011 #include "pathfind.h" 00012 00013 using namespace std; 00014 using namespace PathFind; 00015 00016 //----------------------------------------------------------------------------- 00017 00018 static const int s_numberRuns = 100; 00019 static const int s_columns = 50; 00020 static const int s_rows = 50; 00021 static const float s_obstaclePercentage = 0.2; 00022 static const long long int s_nodesLimit = 100000000L; 00023 00024 //----------------------------------------------------------------------------- 00025 00026 typedef enum {A_STAR, IDA_STAR, FRINGE} SearchAlgorithm; 00027 00028 //----------------------------------------------------------------------------- 00029 00030 void printHeader(SearchAlgorithm searchAlgorithm, Tiling::Type type) 00031 { 00032 cout << "==============================================================\n"; 00033 switch (searchAlgorithm) 00034 { 00035 case A_STAR: 00036 cout << "A_STAR"; 00037 break; 00038 case IDA_STAR: 00039 cout << "IDA_STAR"; 00040 break; 00041 case FRINGE: 00042 cout << "FRINGE"; 00043 break; 00044 } 00045 cout << " "; 00046 switch (type) 00047 { 00048 case Tiling::TILE: 00049 cout << "TILE"; 00050 break; 00051 case Tiling::OCTILE: 00052 cout << "OCTILE"; 00053 break; 00054 case Tiling::OCTILE_UNICOST: 00055 cout << "OCTILE_UNICOST"; 00056 break; 00057 case Tiling::HEX: 00058 cout << "HEX"; 00059 break; 00060 } 00061 cout << '\n'; 00062 cout << "==============================================================\n"; 00063 } 00064 00065 void runExperiment(SearchAlgorithm searchAlgorithm, Tiling::Type type) 00066 { 00067 printHeader(searchAlgorithm, type); 00068 auto_ptr<Search> search; 00069 switch (searchAlgorithm) 00070 { 00071 case A_STAR: 00072 search.reset(new AStar<>()); 00073 break; 00074 case IDA_STAR: 00075 search.reset(new IDAStar(IDAStar::f_CACHING)); 00076 break; 00077 case FRINGE: 00078 search.reset(new FringeSearch<>()); 00079 break; 00080 } 00081 search->setNodesLimit(s_nodesLimit); 00082 SearchUtils searchUtils; 00083 StatisticsCollection statistics = search->createStatistics(); 00084 for (int runIndex = 0; runIndex < s_numberRuns; ++runIndex) 00085 { 00086 // Create a tiling search environment 00087 Tiling tiling(type, s_rows, s_columns); 00088 tiling.setObstacles(s_obstaclePercentage, false); 00089 00090 // Choose random start and target 00091 int start, target; 00092 searchUtils.findRandomStartTarget(tiling, start, target); 00093 //cout << "Run " << runIndex << ":\n"; 00094 //tiling.printFormatted(cout, start, target); 00095 //cout << '\n'; 00096 00097 // Do the search 00098 search->findPath(tiling, start, target); 00099 //cout << '\n'; 00100 //const vector<int>& path = search->getPath(); 00101 //tiling.printFormatted(cout, path); 00102 //cout << '\n'; 00103 //tiling.printPathAndLabels(cout, path, search->getVisitedNodes()); 00104 //cout << '\n'; 00105 00106 // Accumulate statistics 00107 const StatisticsCollection& searchStatistics = search->getStatistics(); 00108 //searchStatistics.print(cout); 00109 //cout << '\n'; 00110 statistics.add(searchStatistics); 00111 //statistics.print(cout); 00112 //cout << '\n'; 00113 } 00114 statistics.print(cout); 00115 } 00116 00117 int main() 00118 { 00119 try 00120 { 00121 //srand(time(0)); 00122 runExperiment(A_STAR, Tiling::TILE); 00123 //runExperiment(A_STAR, Tiling::OCTILE); 00124 //runExperiment(A_STAR, Tiling::OCTILE_UNICOST); 00125 //runExperiment(A_STAR, Tiling::HEX); 00126 runExperiment(IDA_STAR, Tiling::TILE); 00127 //runExperiment(IDA_STAR, Tiling::OCTILE); 00128 //runExperiment(IDA_STAR, Tiling::OCTILE_UNICOST); 00129 //runExperiment(IDA_STAR, Tiling::HEX); 00130 runExperiment(FRINGE, Tiling::TILE); 00131 } 00132 catch (const exception& e) 00133 { 00134 cerr << "Error: " << e.what() << '\n'; 00135 return -1; 00136 } 00137 return 0; 00138 } 00139 00140 //-----------------------------------------------------------------------------