libmove3d-planners
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Groups Pages
LazyWAstar.h
1 #ifndef LAZYWASTAR_H
2 #define LAZYWASTAR_H
3 
4 #include "Edge.h"
5 #include "GraphInterface.h"
6 #include <deque>
7 #include <queue>
8 #include "Logging/Logger.h"
9 
10 namespace mho{
11 
12 /*
13 / **
14  * @brief LWAEdgeInterface implements an interface for a Lazy Weighted A* search algorithm Edge
15  *
16 class LWAEdgeInterface : public EdgeInterface
17 {
18 public:
19  virtual bool isTrueCost() =0;
20  virtual void setTrueCost(double cost) =0;
21  virtual void setTmpCost(double cost) =0;
22 };
23 */
24 
26 {
27 public:
28  virtual ~LWANodeInterface(){}
29  virtual LWANodeInterface *clone() =0;
30  //virtual std::vector<LWANodeInterface*> getClones() =0;
31  virtual bool haveLowerTrueCostClones() =0;
32  virtual double edgeCostEstimationFrom(LWANodeInterface *other) =0;
33  virtual double computeHeuristic(LWANodeInterface* goal) =0;
34  virtual double computeTrueCost(LWANodeInterface* other) = 0;
35  virtual double f() =0;
36  virtual double g() =0;
37  virtual void g(double g) =0;
38  virtual double h() =0;
39  virtual void h(double h) =0;
40  virtual void parent(LWANodeInterface *e) =0;
41  virtual LWANodeInterface* parent() =0;
42 
43  virtual bool isTrueCost() =0;
44  virtual void setTrueCost(bool t) =0;
45 
46  virtual bool isOpen() =0;
47  virtual bool isClosed() =0;
48  virtual void open() =0;
49  virtual void close() =0;
50  virtual void resetStatus() =0;
51  virtual bool isSameConf(LWANodeInterface *other) =0;
52 
53  virtual std::vector<LWANodeInterface*> sons() =0;
54 };
55 
57 public:
59  virtual ~DisplaySearchAstarInterface() {}
60  virtual void onOpen(LWANodeInterface* open){}
61  virtual void onClose(LWANodeInterface* closed){}
62  virtual void onFound(LWANodeInterface* goal){}
63  virtual void onFail(){}
64  virtual void onAbort(){}
65  virtual void onEnd(std::vector<std::pair<unsigned int,double> > times){}
66 
67 };
68 
70 {
71 public:
72  LWANodeBase();
73  LWANodeBase(LWANodeBase *other);
74  virtual ~LWANodeBase();
75  virtual LWANodeInterface *clone();
76  void addClone(LWANodeBase* clone);
77 private:
79  void updateOldest(LWANodeBase *new_oldest);
81  void removeClone(LWANodeBase* removed);
82 public:
83  bool haveLowerTrueCostClones();
84 
85  virtual std::vector<LWANodeInterface*> sons(){return std::vector<LWANodeInterface*>(0);}
86 
87  virtual double edgeCostEstimationFrom(LWANodeInterface *other){return -1;}
88  virtual double computeHeuristic(LWANodeInterface *goal){return -1;}
89  virtual double computeTrueCost(LWANodeInterface *other) {return -1;}
90 
91  virtual double f(){return g()+h();}
92  virtual double g(){return _g;}
93  virtual void g(double g){_g=g;}
94  virtual double h(){return _h;}
95  virtual void h(double h){_h=h;}
96  virtual void parent(LWANodeInterface *e){_parent=e;}
97  virtual LWANodeInterface* parent(){return _parent;}
98 
99  bool isTrueCost(){return _is_true_cost;}
100  void setTrueCost(bool t){_is_true_cost=t;}
101 
102  bool isOpen(){return _open;}
103  bool isClosed(){return _closed;}
104  void open(){_open=true;}
105  void close(){_closed=true;}
106  void resetStatus(){_open=_closed=false;}
107 
108  virtual bool isSameConf(LWANodeInterface *other){return false;}
109 
110 private:
111  bool _is_true_cost;
112  double _g,_h;
113  bool _open,_closed;
114  LWANodeInterface *_parent;
115  LWANodeBase *_oldest_clone;
116  std::vector<LWANodeInterface*> _clones;
117 };
118 
119 /*
120 class LWAEdge :public LWAEdgeInterface, public Edge
121 {
122 public:
123  LWAEdge();
124  LWAEdge(NodeInterface *n1, NodeInterface *n2, double c, bool true_cost);
125  virtual ~LWAEdge();
126 
127  bool isTrueCost();
128  void setTrueCost(double cost);
129  void setTmpCost(double cost);
130 
131 private:
132  bool _is_true_cost;
133 };
134 */
135 
137 {
138  MOVE3D_STATIC_LOGGER;
139 public:
140  LazyWeightedAstar(LWANodeInterface *start,LWANodeInterface *goal,double epsilon,DisplaySearchAstarInterface *interface=0,double time_limit_ms=-1,bool no_lazy=false):
141  _epsilon(epsilon),_start(start),_goal(goal),_interface(0),//we set the interface later in the body
142  _time_limit(time_limit_ms),_no_lazy(no_lazy)
143  {
144  setInterface(interface);
145  }
146  LazyWeightedAstar(double epsilon,DisplaySearchAstarInterface *interface=0):
147  _epsilon(epsilon), _start(0),_goal(0), _interface(0), _time_limit(-1),_no_lazy(0)
148  {
149  setInterface(interface);
150  }
151  virtual ~LazyWeightedAstar();
152  void reset();
153 
154  bool compute();
155  bool compute(LWANodeInterface* start, LWANodeInterface *goal, double time_limit_ms=-1, bool no_lazy=false);
156  std::deque<LWANodeInterface *> getPath();
157  void setInterface(DisplaySearchAstarInterface *interface);
158  void resetInterface(){setInterface(0);}
159 
160  double getTimeLimit(){return _time_limit;}
161  void setTimeLimit(double ms){_time_limit=ms;}
162 
163 private:
165  struct f_value_comp
166  {
167  bool operator() (LWANodeInterface* n1,LWANodeInterface* n2){
168  return n1->f() > n2->f();
169  }
170  };
171  double _epsilon;
172  LWANodeInterface *_start,*_goal,
173  *_goal_found;
174  DisplaySearchAstarInterface *_interface;
176  bool _owns_interface;
177  std::priority_queue<LWANodeInterface*,std::vector<LWANodeInterface*>, f_value_comp > open;
178  std::vector<LWANodeInterface*> closed;
179  //GraphInterface *_graph;
180  double _time_limit;
181  bool _no_lazy;
182 
183 };
184 
185 }
186 
187 #endif // LAZYWASTAR_H
Definition: LazyWAstar.h:136
Definition: LazyWAstar.h:69
This file implements macros to help with the logging, in a way similar to ROS, using log4cxx...
Definition: LazyWAstar.h:56
Definition: LazyWAstar.h:25