00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 00005 | | 00006 | Copyright (C) 2005-2011 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CASTARALGORITHM_H 00029 #define CASTARALGORITHM_H 00030 #include <map> 00031 #include <vector> 00032 #include <cmath> 00033 #include <mrpt/utils/CTicTac.h> 00034 00035 namespace mrpt 00036 { 00037 namespace graphs 00038 { 00039 /** This class is intended to efficiently solve graph-search problems using heuristics to determine the best path. To use it, a solution class must be defined 00040 * so that it contains all the information about any partial or complete solution. Then, a class inheriting from CAStarAlgorithm<Solution class> must also be 00041 * implemented, overriding five virtual methods which define the behaviour of the solutions. These methods are isSolutionEnded, isSolutionValid, 00042 * generateChildren, getHeuristic and getCost. 00043 * Once both classes are generated, each object of the class inheriting from CAStarAlgorithm represents a problem who can be solved by calling 00044 * getOptimalSolution. See http://en.wikipedia.org/wiki/A*_search_algorithm for details about how this algorithm works. 00045 * \sa CAStarAlgorithm::isSolutionEnded 00046 * \sa CAStarAlgorithm::isSolutionValid 00047 * \sa CAStarAlgorithm::generateChildren 00048 * \sa CAStarAlgorithm::getHeuristic 00049 * \sa CAStarAlgorithm::getCost 00050 * \ingroup mrpt_graphs_grp 00051 */ 00052 template<typename T> class CAStarAlgorithm { 00053 public: 00054 /** 00055 * Client code must implement this method. 00056 * Returns true if the given solution is complete. 00057 */ 00058 virtual bool isSolutionEnded(const T &sol)=0; 00059 /** 00060 * Client code must implement this method. 00061 * Returns true if the given solution is acceptable, that is, doesn't violate the problem logic. 00062 */ 00063 virtual bool isSolutionValid(const T &sol)=0; 00064 /** 00065 * Client code must implement this method. 00066 * Given a partial solution, returns all its children solution, regardless of their validity or completeness. 00067 */ 00068 virtual void generateChildren(const T &sol,std::vector<T> &sols)=0; 00069 /** 00070 * Client code must implement this method. 00071 * Given a partial solution, estimates the cost of the remaining (unknown) part. 00072 * This cost must always be greater or equal to zero, and not greater than the actual cost. Thus, must be 0 if the solution is complete. 00073 */ 00074 virtual double getHeuristic(const T &sol)=0; 00075 /** 00076 * Client code must implement this method. 00077 * Given a (possibly partial) solution, calculates its cost so far. 00078 * This cost must not decrease with each step. That is, a solution cannot have a smaller cost than the previous one from which it was generated. 00079 */ 00080 virtual double getCost(const T &sol)=0; 00081 private: 00082 /** 00083 * Calculates the total cost (known+estimated) of a solution. 00084 */ 00085 inline double getTotalCost(const T &sol) { 00086 return getHeuristic(sol)+getCost(sol); 00087 } 00088 public: 00089 /** 00090 * Finds the optimal solution for a problem, using the A* algorithm. Returns whether an optimal solution was actually found. 00091 * Returns 0 if no solution was found, 1 if an optimal solution was found and 2 if a (possibly suboptimal) solution was found but the time lapse ended. 00092 */ 00093 int getOptimalSolution(const T &initialSol,T &finalSol,double upperLevel=HUGE_VAL,double maxComputationTime=HUGE_VAL) { 00094 //Time measuring object is defined. 00095 mrpt::utils::CTicTac time; 00096 time.Tic(); 00097 //The partial solution set is initialized with a single element (the starting solution). 00098 std::multimap<double,T> partialSols; 00099 partialSols.insert(std::pair<double,T>(getTotalCost(initialSol),initialSol)); 00100 //The best known solution is set to the upper bound (positive infinite, if there is no given parameter). 00101 double currentOptimal=upperLevel; 00102 bool found=false; 00103 std::vector<T> children; 00104 //Main loop. Each iteration checks an element of the set, with minimum estimated cost. 00105 while (!partialSols.empty()) { 00106 //Return if elapsed time has been reached. 00107 if (time.Tac()>=maxComputationTime) return found?2:0; 00108 typename std::multimap<double,T>::iterator it=partialSols.begin(); 00109 double tempCost=it->first; 00110 //If the minimum estimated cost is higher than the upper bound, then also is every solution in the set. So the algorithm returns immediately. 00111 if (tempCost>=currentOptimal) return found?1:0; 00112 T tempSol=it->second; 00113 partialSols.erase(it); 00114 //At this point, the solution cost is lesser than the upper bound. So, if the solution is complete, the optimal solution and the upper bound are updated. 00115 if (isSolutionEnded(tempSol)) { 00116 currentOptimal=tempCost; 00117 finalSol=tempSol; 00118 found=true; 00119 continue; 00120 } 00121 //If the solution is not complete, check for its children. Each one is included in the set only if it's valid and it's not yet present in the set. 00122 generateChildren(tempSol,children); 00123 for (typename std::vector<T>::const_iterator it2=children.begin();it2!=children.end();it2++) if (isSolutionValid(*it2)) { 00124 bool alreadyPresent=false; 00125 double cost=getTotalCost(*it2); 00126 typename std::pair<typename std::multimap<double,T>::const_iterator,typename std::multimap<double,T>::const_iterator> range = partialSols.equal_range(cost); 00127 for (typename std::multimap<double,T>::const_iterator it3=range.first;it3!=range.second;it3++) if (it3->second==*it2) { 00128 alreadyPresent=true; 00129 break; 00130 } 00131 if (!alreadyPresent) partialSols.insert(std::pair<double,T>(getTotalCost(*it2),*it2)); 00132 } 00133 } 00134 //No more solutions to explore... 00135 return found?1:0; 00136 } 00137 }; 00138 } 00139 } //End of namespaces 00140 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |