LEMUR Packages: ompl_lemur or_lemur pr_bgl prpy_lemur
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Groups Pages
RoadmapRGG.h
Go to the documentation of this file.
1 
7 /* requires:
8 #include <ompl_lemur/SamplerGenMonkeyPatch.h>
9 */
10 
11 namespace ompl_lemur
12 {
13 
14 // for now this is an r-disk prm,
15 // uniform milestone sampling with given seed,
16 // uses the space's default sampler
17 template <class RoadmapArgs>
18 class RoadmapRGG : public Roadmap<RoadmapArgs>
19 {
20  typedef boost::graph_traits<typename RoadmapArgs::Graph> GraphTypes;
21  typedef typename GraphTypes::vertex_descriptor Vertex;
22  typedef typename GraphTypes::edge_descriptor Edge;
23 
24  // set on construction
25  unsigned int _dim;
27 
28  // params
29  unsigned int _num;
30  double _radius;
31  unsigned int _seed;
32  bool _seed_set;
33 
34  // set on initialization
36 
37 public:
38  RoadmapRGG(RoadmapArgs & args):
39  Roadmap<RoadmapArgs>(args, "RGG", 1),
40  _dim(0),
41  _bounds(0),
42  _num(0),
43  _radius(0.0),
44  _seed(0),
45  _seed_set(false),
46  _sampler(this->space->allocStateSampler())
47  {
48  // check that we're in a real vector state space
49  if (this->space->getType() != ompl::base::STATE_SPACE_REAL_VECTOR)
50  throw std::runtime_error("RoadmapRGG only supports rel vector state spaces!");
51  _dim = this->space->getDimension();
52  if (0 == ompl_lemur::util::get_prime(_dim-1))
53  throw std::runtime_error("not enough primes hardcoded!");
54  ompl::base::StateSpacePtr myspace(this->space);
55  _bounds = myspace->as<ompl::base::RealVectorStateSpace>()->getBounds();
56 
57  this->template declareParam<unsigned int>("num", this,
60  this->template declareParam<double>("radius", this,
61  &RoadmapRGG::setRadius,
62  &RoadmapRGG::getRadius);
63  this->template declareParam<unsigned int>("seed", this,
64  &RoadmapRGG::setSeed,
65  &RoadmapRGG::getSeed);
66  }
67 
68  void setNum(unsigned int num)
69  {
70  if (num == _num)
71  return;
72  if (this->initialized)
73  throw std::runtime_error("cannot set num, already initialized!");
74  _num = num;
75  }
76 
77  unsigned int getNum() const
78  {
79  return _num;
80  }
81 
82  void setRadius(double radius)
83  {
84  if (radius == _radius)
85  return;
86  if (this->initialized)
87  throw std::runtime_error("cannot set radius, already initialized!");
88  _radius = radius;
89  }
90 
91  double getRadius() const
92  {
93  return _radius;
94  }
95 
96  void setSeed(unsigned int seed)
97  {
98  if (_seed_set && seed == _seed)
99  return;
100  if (this->initialized)
101  throw std::runtime_error("cannot set seed, already initialized!");
102  _seed = seed;
103  _seed_set = true;
104  }
105 
106  unsigned int getSeed() const
107  {
108  return _seed;
109  }
110 
111  void initialize()
112  {
113  std::vector<std::string> missings;
114  if (_num == 0)
115  missings.push_back("num");
116  if (_radius == 0.0)
117  missings.push_back("radius");
118  if (!_seed_set)
119  missings.push_back("seed");
120  if (missings.size())
121  {
122  std::string str = "Cannot initialize, parameters not set:";
123  for (unsigned int ui=0; ui<missings.size(); ui++)
124  str += " " + missings[ui];
125  throw std::runtime_error(str);
126  }
127 
128  ompl_lemur::StateSamplerSetSeed(_sampler, _seed);
129 
130  this->initialized = true;
131  }
132 
133  void deserialize(const std::string & ser_data)
134  {
135  throw std::runtime_error("RoadmapRGG deserialize from ser_data not supported!");
136  }
137 
138  // should be stateless
139  double root_radius(std::size_t i_batch)
140  {
141  return _radius;
142  }
143 
144  // sets all of these maps
145  // generates one additional batch
146  void generate()
147  {
148  if (this->max_batches < this->num_batches_generated + 1)
149  throw std::runtime_error("this roadmap gen doesnt support that many batches!");
150  for (std::size_t v_index=num_vertices(this->g); v_index<_num; v_index++)
151  {
152  Vertex v_new = add_vertex(this->g);
153 
154  put(this->vertex_batch_map, v_new, this->num_batches_generated);
155  put(this->is_shadow_map, v_new, false);
156 
157  // allocate a new state for this vertex
158  put(this->state_map, v_new, this->space->allocState());
159  ompl::base::State * v_state = get(this->state_map, v_new);
160  _sampler->sampleUniform(v_state);
161  this->nn->add(v_new);
162 
163  // allocate new undirected edges
164  std::vector<Vertex> vs_near;
165  this->nn->nearestR(v_new, _radius, vs_near);
166  for (unsigned int ui=0; ui<vs_near.size(); ui++)
167  {
168  if (vs_near[ui] == v_new)
169  continue;
170  Edge e = add_edge(v_new, vs_near[ui], this->g).first;
171  ompl::base::State * vnear_state = get(this->state_map,vs_near[ui]);
172  put(this->distance_map, e, this->space->distance(v_state,vnear_state));
173  put(this->edge_batch_map, e, this->num_batches_generated);
174  }
175  }
176  this->num_batches_generated++;
177  }
178 
179  void serialize(std::string & ser_data)
180  {
181  throw std::runtime_error("RoadmapRGG serialize to ser_data not supported!");
182  }
183 };
184 
185 } // namespace ompl_lemur
void deserialize(const std::string &ser_data)
Re-constitute the internal generator state from serialized data.
Definition: RoadmapRGG.h:133
Definition: RoadmapRGG.h:18
void serialize(std::string &ser_data)
Serialize the internal generator state into the passed string.
Definition: RoadmapRGG.h:179
Interface for generating roadmaps over OMPL state spaces into Boost Graph objects.
Definition: Roadmap.h:76
double root_radius(std::size_t i_batch)
Calcuate the root radius to be used for connecting to potential root vertices.
Definition: RoadmapRGG.h:139
void generate()
Generates one additional batch.
Definition: RoadmapRGG.h:146
void initialize()
Initialize roadmap; must be called once after setting parameters.
Definition: RoadmapRGG.h:111
Definition: Roadmap.h:11