IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
RRT.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kinematics/RRT.h
3  * \brief Implementation of Randomly-Exploring Random Trees
4  *
5  * \authors Dina Schneidman, Barak Raveh
6  *
7  * Copyright 2007-2022 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 <memory>
23 
24 IMPKINEMATICS_BEGIN_NAMESPACE
25 
26 // TODO: should move out of kinematics, or be in kinematic_algorithms
27 
28 //! Simple implementation of the Rapidly-exploring Random Trees algorithm
29 class IMPKINEMATICSEXPORT RRT : public IMP::Sampler {
30  public:
32 
33  //! Simple RRT node implementation
34  /** we may replace it with something from boost so we can use boost graph */
35  class RRTNode {
36  public:
37  RRTNode(const DOFValues& vec) : vec_(vec) {
38  id_ = node_counter_;
39  node_counter_++;
40  score_ = 0;
41  }
42 
43  // Accessors
44  const DOFValues& get_DOFValues() const { return vec_; }
45 
46  const std::vector<std::pair<const RRTNode*, float> >& get_edges() const {
47  return edges_;
48  }
49 
50  int get_id() const { return id_; }
51 
52  float get_score() const { return score_; }
53 
54  bool is_edge(const RRTNode* node) const {
55  for (unsigned int i = 0; i < edges_.size(); i++)
56  if (node == edges_[i].first) return true;
57  return false;
58  }
59 
60  // Modifiers
61  void add_edge(const RRTNode* node, float distance) {
62  if (id_ > node->id_) std::cerr << "wrong direction edge" << std::endl;
63  edges_.push_back(std::make_pair(node, distance));
64  }
65 
66  void set_score(float score) { score_ = score; }
67 
68  struct less_node {
69  bool operator()(const RRTNode* n1, const RRTNode* n2) {
70  return n1->get_score() < n2->get_score();
71  }
72  };
73 
74  friend std::ostream& operator<<(std::ostream& s, const RRTNode& n);
75 
76  private:
77  const DOFValues vec_;
78  // a node and a distance between two dofs
79  std::vector<std::pair<const RRTNode*, float> > edges_;
80  int id_;
81  float score_;
82  static int node_counter_;
83  };
84 
85  private:
86  // counters for stop condition...
87  class Parameters {
88  public:
89  Parameters()
90  : number_of_iterations_(0),
91  actual_tree_size_(0),
92  tree_size_(0),
93  number_of_collisions_(0) {}
94 
95  Parameters(unsigned int number_of_iterations,
96  unsigned int actual_tree_size = 100,
97  unsigned int tree_size = 100,
98  unsigned int number_of_collisions = 10000)
99  : number_of_iterations_(number_of_iterations),
100  actual_tree_size_(actual_tree_size),
101  tree_size_(tree_size),
102  number_of_collisions_(number_of_collisions) {}
103 
104  unsigned int number_of_iterations_;
105  unsigned int actual_tree_size_; // not including path nodes
106  unsigned int tree_size_; // all nodes
107  unsigned int number_of_collisions_;
108  };
109 
110 #ifndef SWIG
111  friend std::ostream& operator<<(std::ostream& s, const Parameters& p);
112 #endif
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 override {
125  const_cast<RRT*>(this)->run();
126  return nullptr;
127  }
128 
129  //! Run the specified number of iterations
130  /** If zero, will run for default iteration number.DOFValuesVector
131  This option allows to output the already sampled
132  configurations during the sampling run. */
133  bool run(unsigned int number_of_iterations = 0);
134 
135  DOFValuesList get_DOFValuesList();
136 
137  /* Parameters for controlling RRT stop condition */
138 
139  //! Set number of RRT iterations
140  void set_number_of_iterations(unsigned int num) {
141  default_parameters_.number_of_iterations_ = num;
142  }
143 
144  //! Set tree size
145  void set_tree_size(unsigned int num) { default_parameters_.tree_size_ = num; }
146 
147  //! Set the actual tree size - not including path nodes
148  void set_actual_tree_size(unsigned int num) {
149  default_parameters_.actual_tree_size_ = num;
150  }
151 
152  //! Set the number of collisions
153  void set_number_of_collisions(unsigned int num) {
154  default_parameters_.number_of_collisions_ = num;
155  }
156 
157  void check_initial_configuration(ScoringFunction *sf) const;
158 
159  private:
160  RRTNode* get_q_near(const DOFValues& q_rand) const;
161 
162 
163 
164  void add_nodes(RRTNode* q_near, const std::vector<DOFValues>& new_nodes);
165 
166  // TODO: make it more general
167  bool is_stop_condition(const Parameters& parameters,
168  const Parameters& counters) {
169  return (counters.number_of_iterations_ > parameters.number_of_iterations_ ||
170  counters.actual_tree_size_ > parameters.actual_tree_size_ ||
171  counters.tree_size_ > parameters.tree_size_ ||
172  counters.number_of_collisions_ > parameters.number_of_collisions_);
173  }
174 
175  private:
176  PointerMember<DOFsSampler> dofs_sampler_;
177  PointerMember<LocalPlanner> local_planner_;
178  typedef std::shared_ptr<RRTNode> RRTNodePtr;
179  typedef std::vector<RRTNodePtr> RRTTree;
180  RRTTree tree_;
181  DOFs cspace_dofs_; // configuration space dofs
182  Parameters default_parameters_; // limits for stop condition
183  Parameters current_counters_;
184 
185  // for sampling subsets of dofs
186  unsigned int number_of_sampled_dofs_;
187  std::vector<bool> active_dofs_;
188 };
189 
190 IMPKINEMATICS_END_NAMESPACE
191 
192 #endif /* IMPKINEMATICS_RR_T_H */
void set_actual_tree_size(unsigned int num)
Set the actual tree size - not including path nodes.
Definition: RRT.h:148
Simple RRT node implementation.
Definition: RRT.h:35
Simple implementation of the Rapidly-exploring Random Trees algorithm.
Definition: RRT.h:29
a simple class for storage of DOF values.
single degree of freedom
IMP::Vector< IMP::Pointer< DOF > > DOFs
Definition: DOF.h:71
void set_number_of_iterations(unsigned int num)
Set number of RRT iterations.
Definition: RRT.h:140
#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:314
Base class for all samplers.
Definition: Sampler.h:31
IMP::Vector< DOFValues > DOFValuesList
Definition: DOFValues.h:95
Base class for all samplers.
A class that holds DOF values for DOFs.
Definition: DOFValues.h:23
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:143
A class to store a set of configurations of a model.
void set_number_of_collisions(unsigned int num)
Set the number of collisions.
Definition: RRT.h:153
Represents a scoring function on the model.
Store a set of configurations of the model.
void set_tree_size(unsigned int num)
Set tree size.
Definition: RRT.h:145