Parasol Planning Library (PPL)
GoalTracker.h
Go to the documentation of this file.
1 #ifndef PMPL_GOAL_TRACKER_H_
2 #define PMPL_GOAL_TRACKER_H_
3 
6 #include "MPProblem/GroupTask.h"
7 #include "MPProblem/MPTask.h"
9 
10 #include <atomic>
11 #include <map>
12 #include <unordered_set>
13 #include <vector>
14 
15 
25 template <typename Roadmap, typename Task>
26 class GoalMap {
27 
28  public:
29 
32 
33  typedef typename Roadmap::VI VI;
34  typedef typename Roadmap::VID VID;
35 
39 
40  typedef std::unordered_set<VID> VIDSet;
41 
45 
49  GoalMap(Roadmap* const _r, const Task* const _t);
50 
51  ~GoalMap();
52 
56 
59  const VIDSet& GetStartVIDs() const noexcept;
60 
64  const VIDSet& GetGoalVIDs(const size_t _index) const noexcept;
65 
67  size_t UnreachedGoalCount() const noexcept;
68 
71  std::vector<size_t> UnreachedGoalIndexes() const noexcept;
72 
79 
81  void InstallHooks() noexcept;
82 
84  void RemoveHooks() noexcept;
85 
87 
88  private:
89 
92 
95  void TrackCfg(const VI _vertex);
96 
100  void UntrackCfg(const VI _vertex);
101 
103  std::string GetHookLabel() const noexcept;
104 
108 
109  static std::atomic<size_t> s_index;
110 
111  const size_t m_index;
112  Roadmap* const m_roadmap;
113  const Task* const m_task;
114 
116  VIDSet m_startVIDs;
117 
119  std::vector<VIDSet> m_goalVIDs;
120 
122 };
123 
124 /*------------------------------- Construction -------------------------------*/
125 
126 // Initalize the global index.
127 template <typename Roadmap, typename Task>
128 std::atomic<size_t> GoalMap<Roadmap, Task>::s_index(0);
129 
130 
131 template <typename Roadmap, typename Task>
132 GoalMap<Roadmap, Task>::
133 GoalMap(Roadmap* const _roadmap, const Task* const _task)
134  : m_index(++s_index), m_roadmap(_roadmap), m_task(_task) {
135  // If either roadmap or task is null, we cannot build this object.
136  if(!_roadmap)
137  throw RunTimeException(WHERE) << "A non-null roadmap pointer is required.";
138  if(!_task)
139  throw RunTimeException(WHERE) << "A non-null task pointer is required.";
140 
141  // If we have no start or goal constraints, then this object is not needed and
142  // will do nothing.
143  if(m_task->Empty())
144  return;
145 
146  // Initialize the goalVIDs structure with the correct number of constraints.
147  m_goalVIDs.resize(m_task->GetNumGoals());
148 
149  // Evaluate any existing configurations.
150  for(auto vi = m_roadmap->begin(); vi != m_roadmap->end(); ++vi)
151  TrackCfg(vi);
152 }
153 
154 
155 template <typename Roadmap, typename Task>
157 ~GoalMap() {
158 }
159 
160 /*-------------------------------- Interface ---------------------------------*/
161 
162 template <typename Roadmap, typename Task>
163 const typename GoalMap<Roadmap, Task>::VIDSet&
165 GetStartVIDs() const noexcept {
166  return m_startVIDs;
167 }
168 
169 
170 template <typename Roadmap, typename Task>
171 const typename GoalMap<Roadmap, Task>::VIDSet&
173 GetGoalVIDs(const size_t _index) const noexcept {
174  if(_index > m_goalVIDs.size())
175  throw RunTimeException(WHERE) << "Requested goal VIDs for constraint "
176  << _index << ", but only "
177  << m_goalVIDs.size() << " constraints are "
178  << "present in task '" << m_task->GetLabel()
179  << "'.";
180  return m_goalVIDs[_index];
181 }
182 
183 
184 template <typename Roadmap, typename Task>
185 size_t
187 UnreachedGoalCount() const noexcept {
188  size_t count = 0;
189 
190  // Add up the number of goal constraints that have no satisfying vids.
191  for(const auto& vids : m_goalVIDs)
192  count += vids.empty();
193 
194  return count;
195 }
196 
197 
198 template <typename Roadmap, typename Task>
199 std::vector<size_t>
201 UnreachedGoalIndexes() const noexcept {
202  std::vector<size_t> indexes;
203 
204  // Add up the number of goal constraints that have no satisfying vids.
205  for(size_t i = 0; i < m_goalVIDs.size(); ++i)
206  if(m_goalVIDs[i].empty())
207  indexes.push_back(i);
208 
209  return indexes;
210 }
211 
212 /*---------------------------------- Hooks -----------------------------------*/
213 
214 template <typename Roadmap, typename Task>
215 void
217 InstallHooks() noexcept {
218  using HT = typename Roadmap::HookType;
219 
220  const std::string& label = this->GetHookLabel();
221 
222  m_roadmap->InstallHook(HT::AddVertex, label,
223  [this](const VI _vi){this->TrackCfg(_vi);});
224 
225  m_roadmap->InstallHook(HT::DeleteVertex, label,
226  [this](const VI _vi){this->UntrackCfg(_vi);});
227 }
228 
229 
230 template <typename Roadmap, typename Task>
231 void
233 RemoveHooks() noexcept {
234  using HT = typename Roadmap::HookType;
235 
236  const std::string& label = this->GetHookLabel();
237 
238  if(m_roadmap->IsHook(HT::AddVertex, label))
239  m_roadmap->RemoveHook(HT::AddVertex, label);
240 
241  if(m_roadmap->IsHook(HT::DeleteVertex, label))
242  m_roadmap->RemoveHook(HT::DeleteVertex, label);
243 }
244 
245 /*--------------------------------- Helpers ----------------------------------*/
246 
247 template <typename Roadmap, typename Task>
248 void
250 TrackCfg(const VI _vertex) {
251  // Get the VID and cfg info.
252  const VID vid = _vertex->descriptor();
253  const auto& cfg = _vertex->property();
254 
255  // Check for satisfying the start constraint.
256  if(m_task->EvaluateStartConstraints(cfg))
257  m_startVIDs.insert(vid);
258 
259  // Check for satisfying each goal constraint.
260  const size_t numGoals = m_task->GetNumGoals();
261  for(size_t i = 0; i < numGoals; ++i)
262  if(m_task->EvaluateGoalConstraints(cfg, i))
263  m_goalVIDs[i].insert(vid);
264 }
265 
266 
267 template <typename Roadmap, typename Task>
268 void
270 UntrackCfg(const VI _vertex) {
271  // Get the VID info.
272  const VID vid = _vertex->descriptor();
273 
274  // Untrack satisfaction of the start constraint.
275  if(m_startVIDs.count(vid))
276  m_startVIDs.erase(vid);
277 
278  // Untrack satisfaction of the goal constraints.
279  for(auto& vids : m_goalVIDs)
280  if(vids.count(vid))
281  vids.erase(vid);
282 }
283 
284 
285 template <typename Roadmap, typename Task>
286 std::string
288 GetHookLabel() const noexcept {
289  return "GoalMap" + std::to_string(m_index);
290 }
291 
292 /*----------------------------------------------------------------------------*/
293 
294 
299 
300  public:
301 
304 
307  typedef typename RoadmapType::VID VID;
308 
312 
313  typedef std::unordered_set<VID> VIDSet;
314 
318 
321  GoalTrackerType(MPLibrary* const _library);
322 
323  ~GoalTrackerType();
324 
328 
334  template <typename Roadmap, typename Task>
335  const VIDSet& GetStartVIDs(Roadmap* const _roadmap, const Task* const _task)
336  const;
337 
341  const VIDSet& GetStartVIDs() const;
342 
350  template <typename Roadmap, typename Task>
351  const VIDSet& GetGoalVIDs(Roadmap* const _roadmap, const Task* const _task,
352  const size_t _index) const;
353 
357  const VIDSet& GetGoalVIDs(const size_t _index) const;
358 
363  template <typename Roadmap, typename Task>
364  size_t UnreachedGoalCount(Roadmap* const _roadmap, const Task* const _task)
365  const;
366 
370  size_t UnreachedGoalCount() const;
371 
377  template <typename Roadmap, typename Task>
378  std::vector<size_t> UnreachedGoalIndexes(Roadmap* const _roadmap,
379  const Task* const _task) const;
380 
384  std::vector<size_t> UnreachedGoalIndexes() const;
385 
389  void AddMap(RoadmapType* const _roadmap, const MPTask* const _task);
390 
394  void AddMap(GroupRoadmapType* const _roadmap, const GroupTask* const _task);
395 
399  bool IsMap(RoadmapType* const _roadmap, const MPTask* const _task) const;
400 
404  bool IsMap(GroupRoadmapType* const _roadmap, const GroupTask* const _task)
405  const;
406 
408  void Clear();
409 
411 
412  private:
413 
416 
417  typedef std::pair<RoadmapType*, const MPTask*> IndividualKey;
418  typedef std::pair<GroupRoadmapType*, const GroupTask*> GroupKey;
419 
422 
426 
427  const IndividualMap& GetMap(RoadmapType* const _roadmap,
428  const MPTask* const _task) const;
429 
430  const GroupMap& GetMap(GroupRoadmapType* const _roadmap,
431  const GroupTask* const _task) const;
432 
438 
439  std::map<IndividualKey, IndividualMap> m_individualMaps;
440  std::map<GroupKey, GroupMap> m_groupMaps;
441 
443 
444 };
445 
446 #endif
#define WHERE
Macro for retrieving info about file, function, and line number.
Definition: RuntimeUtils.h:32
Definition: GenericStateGraph.h:67
STAPLGraph::vertex_descriptor VID
Definition: GenericStateGraph.h:83
Definition: GoalTracker.h:26
const VIDSet & GetGoalVIDs(const size_t _index) const noexcept
Definition: GoalTracker.h:173
Roadmap::VI VI
Definition: GoalTracker.h:33
void InstallHooks() noexcept
Install hooks in the roadmap for mapping new configurations.
Definition: GoalTracker.h:217
void RemoveHooks() noexcept
Remove hooks from the roadmap.
Definition: GoalTracker.h:233
const VIDSet & GetStartVIDs() const noexcept
Definition: GoalTracker.h:165
std::vector< size_t > UnreachedGoalIndexes() const noexcept
Definition: GoalTracker.h:201
std::unordered_set< VID > VIDSet
Definition: GoalTracker.h:40
size_t UnreachedGoalCount() const noexcept
Check the number of unreached goals.
Definition: GoalTracker.h:187
~GoalMap()
Definition: GoalTracker.h:157
GoalMap(Roadmap *const _r, const Task *const _t)
Definition: GoalTracker.h:133
Roadmap::VID VID
Definition: GoalTracker.h:34
Maintains a set of GoalMap for multiple roadmap/task pairs.
Definition: GoalTracker.h:298
RoadmapType::VID VID
Definition: GoalTracker.h:307
MPBaseObject::RoadmapType RoadmapType
Definition: GoalTracker.h:305
std::unordered_set< VID > VIDSet
Definition: GoalTracker.h:313
MPBaseObject::GroupRoadmapType GroupRoadmapType
Definition: GoalTracker.h:306
Definition: GroupRoadmap.h:25
Definition: GroupTask.h:44
Definition: MPBaseObject.h:46
Definition: MPLibrary.h:47
Definition: MPTask.h:46
Definition: PMPLExceptions.h:62