IMP logo
IMP Reference Guide  2.5.0
The Integrative Modeling Platform
RRT.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kinematics/RRT.h
3  * \brief simple RRT implementation
4  *
5  * \authors Dina Schneidman, Barak Raveh
6  *
7  * Copyright 2007-2015 IMP Inventors. All rights reserved.
8  *
9  */
10 
11 #ifndef IMPKINEMATICS_RR_T_H
12 #define IMPKINEMATICS_RR_T_H
13 
14 #include <IMP/kinematics/kinematics_config.h>
15 #include <IMP/kinematics/DOF.h>
18 
19 #include <IMP/Sampler.h>
20 #include <IMP/ConfigurationSet.h>
21 
22 #include <boost/shared_ptr.hpp>
23 
24 IMPKINEMATICS_BEGIN_NAMESPACE
25 
26 // TODO: should move out of kinematics, or be in kinematic_algorithms
27 
28 /** A Simple implementation of the Rapidly-exploring Random Trees
29  algorithm
30 */
31 class IMPKINEMATICSEXPORT RRT : public IMP::Sampler {
32  public:
34 
35  // simple RRT node implementation
36  // we may replace it with something from boost so we can use boost graph
37  class RRTNode {
38  public:
39  RRTNode(const DOFValues& vec) : vec_(vec) {
40  id_ = node_counter_;
41  node_counter_++;
42  score_ = 0;
43  }
44 
45  // Accessors
46  const DOFValues& get_DOFValues() const { return vec_; }
47 
48  const std::vector<std::pair<const RRTNode*, float> >& get_edges() const {
49  return edges_;
50  }
51 
52  int get_id() const { return id_; }
53 
54  float get_score() const { return score_; }
55 
56  bool is_edge(const RRTNode* node) const {
57  for (unsigned int i = 0; i < edges_.size(); i++)
58  if (node == edges_[i].first) return true;
59  return false;
60  }
61 
62  // Modifiers
63  void add_edge(const RRTNode* node, float distance) {
64  if (id_ > node->id_) std::cerr << "wrong direction edge" << std::endl;
65  edges_.push_back(std::make_pair(node, distance));
66  }
67 
68  void set_score(float score) { score_ = score; }
69 
70  struct less_node {
71  bool operator()(const RRTNode* n1, const RRTNode* n2) {
72  return n1->get_score() < n2->get_score();
73  }
74  };
75 
76  friend std::ostream& operator<<(std::ostream& s, const RRTNode& n);
77 
78  private:
79  const DOFValues vec_;
80  // a node and a distance between two dofs
81  std::vector<std::pair<const RRTNode*, float> > edges_;
82  int id_;
83  float score_;
84  static int node_counter_;
85  };
86 
87  private:
88  // counters for stop condition...
89  class Parameters {
90  public:
91  Parameters()
92  : number_of_iterations_(0),
93  actual_tree_size_(0),
94  tree_size_(0),
95  number_of_collisions_(0) {}
96 
97  Parameters(unsigned int number_of_iterations,
98  unsigned int actual_tree_size = 100,
99  unsigned int tree_size = 100,
100  unsigned int number_of_collisions = 10000)
101  : number_of_iterations_(number_of_iterations),
102  actual_tree_size_(actual_tree_size),
103  tree_size_(tree_size),
104  number_of_collisions_(number_of_collisions) {}
105 
106  unsigned int number_of_iterations_;
107  unsigned int actual_tree_size_; // not including path nodes
108  unsigned int tree_size_; // all nodes
109  unsigned int number_of_collisions_;
110  };
111 
112  friend std::ostream& operator<<(std::ostream& s, const Parameters& p);
113 
114  public:
115  // Constructor
116  RRT(Model* m, DOFsSampler* sampler, LocalPlanner* planner,
117  const DOFs& cspace_dofs, unsigned int iteration_number = 1000,
118  unsigned int tree_size = 100,
119  unsigned int number_of_sampled_dofs = 0); // 0 for all dofs
120 
121  // function required by Sampler
122  // TODO: think how to save configurations in internal coords
123  // to be more memory efficient
124  IMP::ConfigurationSet* do_sample() const {
125  const_cast<RRT*>(this)->run();
126  return nullptr;
127  }
128 
129  void run();
130 
131  std::vector<DOFValues> get_DOFValues();
132 
133  /* Parameters for controlling RRT stop condition */
134 
135  // number of RRT iterations
136  void set_number_of_iterations(unsigned int num) {
137  default_parameters_.number_of_iterations_ = num;
138  }
139 
140  // tree size
141  void set_tree_size(unsigned int num) { default_parameters_.tree_size_ = num; }
142 
143  // actual tree size - not including path nodes
144  void set_actual_tree_size(unsigned int num) {
145  default_parameters_.actual_tree_size_ = num;
146  }
147 
148  // number of collisions
149  void set_number_of_collisions(unsigned int num) {
150  default_parameters_.number_of_collisions_ = num;
151  }
152 
153  private:
154  RRTNode* get_q_near(const DOFValues& q_rand) const;
155 
156  void check_initial_configuration(ScoringFunction *sf) const;
157 
158  void add_nodes(RRTNode* q_near, const std::vector<DOFValues>& new_nodes);
159 
160  // TODO: make it more general
161  bool is_stop_condition(const Parameters& parameters,
162  const Parameters& counters) {
163  return (counters.number_of_iterations_ > parameters.number_of_iterations_ ||
164  counters.actual_tree_size_ > parameters.actual_tree_size_ ||
165  counters.tree_size_ > parameters.tree_size_ ||
166  counters.number_of_collisions_ > parameters.number_of_collisions_);
167  }
168 
169  private:
170  DOFsSampler* dofs_sampler_;
171  LocalPlanner* local_planner_;
172  typedef boost::shared_ptr<RRTNode> RRTNodePtr;
173  typedef std::vector<RRTNodePtr> RRTTree;
174  RRTTree tree_;
175  DOFs cspace_dofs_; // configuration space dofs
176  Parameters default_parameters_; // limits for stop condition
177 
178  // for sampling subsets of dofs
179  unsigned int number_of_sampled_dofs_;
180  std::vector<bool> active_dofs_;
181 };
182 
183 IMPKINEMATICS_END_NAMESPACE
184 
185 #endif /* IMPKINEMATICS_RR_T_H */
a simple class for storage of DOF values.
single degree of freedom
IMP::Vector< IMP::Pointer< DOF > > DOFs
Definition: DOF.h:70
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
IntPairs get_edges(const BoundingBoxD< 3 > &)
Return the edges of the box as indices into the vertices list.
Definition: BoundingBoxD.h:307
Base class for all samplers.
Definition: Sampler.h:31
Base class for all samplers.
A class to store a set of configurations of a model.
Store a set of configurations of the model.