Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PursuitGraph.hpp
Go to the documentation of this file.
00001 
00012 #ifndef LGL_PURSUIT_GRAPH_HPP
00013 #define LGL_PURSUIT_GRAPH_HPP
00014 
00015 #include <lgl/VisibilityMap.hpp>
00016 #include <lgl/PursuitGNode.hpp>
00017 #include <lgl/PursuitNavigator.hpp>
00018 #include <lgl/CellID.hpp>
00019 
00020 #include <vector>
00021 
00022 #include <boost/shared_ptr.hpp>
00023 #include <boost/weak_ptr.hpp>
00024 
00025 #ifndef DEFAULT_PURSUIT_TEMPORAL_TOLERANCE
00026 #define DEFAULT_PURSUIT_TEMPORAL_TOLERANCE 15
00027 #endif
00028 
00029 #ifndef DEFAULT_RISK_TERM_MAX_VALUE 
00030 #define DEFAULT_RISK_TERM_MAX_VALUE 15
00031 #endif
00032 
00033 namespace jafar {
00034   namespace lgl {
00035 
00037     struct tr1Node 
00038     {
00039       RISK_LEVEL b ;    // risk or not
00040       Cell_ID r;      // robot position
00041       Cell_ID t;      // target position
00042       unsigned int rt;  // risk term
00043 
00044       bool operator< (const tr1Node &rhs) const
00045       {
00046         if ( b!=rhs.b ) return ( b  < rhs.b ) ;
00047         if ( r!=rhs.r ) return ( r  < rhs.r ) ;
00048         if ( t!=rhs.t ) return ( t  < rhs.t ) ;
00049         if ( rt!=rhs.rt ) return ( rt < rhs.rt) ;
00050         
00051         return (false); // equals
00052       }
00053 
00054       bool operator== (const tr1Node &rhs) const
00055       {
00056         if ( b==rhs.b && r==rhs.r && t==rhs.t && rt==rhs.rt ) return true ;
00057         return false;
00058       }
00059     };
00060 
00061     struct tr2Node 
00062     {
00063       RISK_LEVEL b ;                      // risk or not
00064       Cell_ID r;                        // robot position
00065       unsigned int rt;                    // risk term
00066       std::vector < boost::shared_ptr< PursuitGNode> > p;   // parent nodes 
00067 
00068       bool operator< (const tr2Node &rhs) const
00069       {
00070         if ( b!=rhs.b   ) return ( b  < rhs.b   ) ;
00071         if ( r!=rhs.r   ) return ( r  < rhs.r   ) ;
00072         if ( rt!=rhs.rt ) return ( rt < rhs.rt  ) ;
00073         if ( p!=rhs.p   ) return ( p  < rhs.p   ) ;
00074         
00075         return (false); // equals
00076       }
00077 
00078       bool operator== (const tr2Node &rhs) const
00079       {
00080         if ( b==rhs.b && r==rhs.r && rt==rhs.rt && p==rhs.p ) return true ;
00081         return false;
00082       }
00083     };
00084     
00085     
00092     class PursuitGraph {
00094     
00095       private :
00096 
00097         // Provide GeoData information and navigation services
00098         PursuitNavigator* navigator ;
00099 
00100         // VisibilityMap : use for visibility information
00101         VisibilityMap *visibilityMap; 
00102 
00103         // ysize is the Y-axis size of the map 
00104         // (use by Cell_ID)
00105         unsigned int ysize ;
00106         unsigned int xsize ;
00107 
00108         // Root of the graph (tree)
00109         boost::shared_ptr<PursuitGNode> root;
00110 
00111         // risk, danger, and front (leaves) nodes
00112         std::vector< boost::weak_ptr<PursuitGNode> > risk;
00113         std::vector< boost::weak_ptr<PursuitGNode> > danger;
00114         std::vector< boost::weak_ptr<PursuitGNode> > front;
00115 
00116         // set of (currently used) Nodes 
00117         std::set< boost::shared_ptr<PursuitGNode> > setOfNodes;
00118 
00119         /* all nodes (=list) organized as a multi-map container
00120          * using the ID of the robot position as key
00121          */
00122         std::multimap< Cell_ID, boost::weak_ptr<PursuitGNode> > nodeMap;
00123 
00124         // current number of stages (levels) in the graph (tree)
00125         // This stands for the temporal horizon of the pursuit graph.
00126         unsigned int horizon ;
00127         
00128         // temporal reference
00129         unsigned long int time ;
00130         
00131         // temporal tolerance when the robot cannot see the target
00132         // (ie how long he is authorized to stay in this situation)
00133         unsigned int  pursuitTemporalTolerance;
00134 
00135         // the maximal risk term value
00136         // (beyond this limit, the risk become a danger)
00137         unsigned int riskTermMaxValue ;
00138 
00139         // estimation of the time needed to compute a new stage (or to complete of stage)
00140         double d_est ;
00141 
00142       public : 
00143 
00145         PursuitGraph(   PursuitNavigator &_navigator, 
00146                 VisibilityMap & _visibilityMap, 
00147                 unsigned int _pursuitTemporalTolerance = DEFAULT_PURSUIT_TEMPORAL_TOLERANCE, 
00148                 unsigned int _riskTermMaxValue = DEFAULT_RISK_TERM_MAX_VALUE
00149         ) :
00150           navigator(&_navigator),
00151           visibilityMap(&_visibilityMap),
00152           horizon(0),
00153           time(0),
00154           pursuitTemporalTolerance(_pursuitTemporalTolerance),
00155           riskTermMaxValue (_riskTermMaxValue)
00156         {
00157           ysize = visibilityMap->getysize();
00158           xsize = visibilityMap->getxsize();
00159 
00160           //TODO check for navigator and visibilityMap compatibility (eg: same dimensions)
00161         }
00162       
00164         ~PursuitGraph() {}
00165 
00171         void computeAll ( Cell_ID cellR, Cell_ID cellT, const unsigned int _horizon = 10  ); 
00172         void computeAll ( int xR, int yR, int xT, int yT, const unsigned int _horizon = 10  ); 
00173 
00179         void computeAllBefore ( Cell_ID cellR, Cell_ID cellT, const double tMax = 5000.0  ); 
00180         void computeAllBefore ( int xR, int yR, int xT, int yT, const double tMax = 5000.0  ); 
00181 
00182         /* Update the PursuitGraph according to a new given position of the target
00183          * 
00184          * return true if a solution is found in the graph, false otherwise.
00185          *
00186          * @newtargetPos is the new given position of the target
00187          * @newrobotPos is the new position of the robot, according to the new position of the target and to the graph
00188          */
00189         bool updatePG( Cell_ID newtargetPos, Cell_ID &newrobotPos);
00190         bool updatePG( int xT, int yT, int &xR, int &yR );
00191 
00192         /* compute new stage until each member of the front has a time equal to root->time + horizon */
00193         void completeToHorizon();
00194         void completeToHorizonBefore( const double &tMax ); // tMax in ms
00195 
00196 
00197         /* get functions */
00198         unsigned int gethorizon() const ;
00199 
00200         const std::vector< boost::weak_ptr<PursuitGNode> > & getrisk() const ;
00201         const std::vector< boost::weak_ptr<PursuitGNode> > & getdanger() const ;
00202         const std::vector< boost::weak_ptr<PursuitGNode> > & getfront() const ;
00203 
00204         const std::set< boost::shared_ptr<PursuitGNode> > & getsetOfNodes() const ;
00205 
00206         const std::multimap< Cell_ID, boost::weak_ptr<PursuitGNode> > & getnodeMap() const ;
00207 
00208         const boost::shared_ptr<PursuitGNode> & getroot() const ;
00209 
00210         PursuitNavigator* getnavigator();
00211 
00212         unsigned int getysize() const;
00213         unsigned int getxsize() const;
00214 
00215         unsigned long int gettime() const;
00216 
00217         void disp() ;
00218         void disp_risk() ;
00219         void disp_danger() ;
00220 
00221       private :
00222 
00227         void computeAll ( const boost::shared_ptr <PursuitGNode> &_root, const unsigned int _horizon = 10 ); 
00228 
00233         void computeAllBefore ( const boost::shared_ptr <PursuitGNode> &_root, const double tMax = 5.0 ); 
00234         
00235 
00237         void setRoot() ;
00238 
00240         void computeNewStage(); // TRHOW EXCEPTION
00241 
00243         void clean() ;
00244 
00248         //void reorient() ;
00249         void reorient() ;
00250 
00251         /* Make new Nodes by linking robot positions and target positions
00252          */
00253         void makeNewNodes(  std::vector< boost::shared_ptr < PursuitGNode > > &newNodes, 
00254                   const unsigned int &riskTerm,
00255                   const Cell_ID &robotPos, 
00256                   const std::set< Cell_ID > &robotAccess, 
00257                   const std::set< Cell_ID > &targetAccess
00258                   ); // THROW EXCEPTION
00259 
00260         /* develop the node of the front with a time equal to the argument
00261          * similar to computeNewStage for the content
00262          */
00263         void completeStage( unsigned long int stage) ;
00264 
00265         /* risk management */
00266         void manageRisk(  const Cell_ID &robotPos, 
00267                   const boost::shared_ptr<PursuitGNode> &risk, 
00268                   std::vector< boost::shared_ptr< PursuitGNode > > &solution 
00269                   );  
00270 
00271         /* for a given set of positions, return another set of positions, if it exits, of positions from which ALL the first ones are visible
00272          */
00273         void findIdealPositions( const std::set<Cell_ID> &targetPos, std::set<Cell_ID> &positions);
00274 
00275         /* for a given set of positions, return another set of positions, if it exits, of positions from which at least ONE of the first ones are visible
00276          */
00277         void findAnyPositions( const std::set<Cell_ID> &targetPos, std::set<Cell_ID> &positions);
00278 
00279         /* for a given position of the robot and a given position of the target
00280          * compute a risk level 
00281          * (use for the root risk level computation)
00282          */
00283         RISK_LEVEL computeRiskLevel(const Cell_ID &robotPos, const Cell_ID &targetPos);
00284 
00285     };
00286 
00287   }
00288 }
00289 
00290 #endif /* LGL_PURSUIT_GRAPH_HPP */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS