Tree.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotComponents
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <deque>
27 #include <mutex>
28 #include <unordered_map>
29 
31 
32 #include <RobotComponents/interface/components/MotionPlanning/Tasks/AdaptiveDynamicDomainInformedRRTStar/DataStructures.h>
33 #include <RobotComponents/interface/components/MotionPlanning/Tasks/AdaptiveDynamicDomainInformedRRTStar/Task.h>
34 
35 #include "util.h"
36 
37 namespace armarx::addirrtstar
38 {
39  /**
40  * @brief A structure holding and managing all data connected to the tree used in the ADDIRRT* algorithm.
41  *
42  * Function overview:
43  * [public ] applyXYZUpdate(XYZUpdate u): applies an existing update u to the tree
44  * [protected] createNewXYZUpdate(...) : creates a new update and adds it to the tree's current update
45  * [public ] addNode/{in,de}creaseRadius/setNodeParent/addGoalReached: modifies the tree (through do functions)
46  * and adds the corresponding update to the local tree
47  * [protected] doXYZ: performs the actual modification of the tree
48  */
49  class Tree
50  {
51  //types
52  public:
53  /**
54  * @brief The type of configurations.
55  */
56  using ConfigType = VectorXf;
57  /**
58  * @brief Represents a node of thr rrt.
59  */
60  struct NodeType
61  {
62  /**
63  * @brief The node's configuration.
64  */
66  /**
67  * @brief The node's parent.
68  */
69  NodeId parent;
70  /**
71  * @brief The node's radius. (adaptive dynamic domain)
72  */
73  float radius;
74  /**
75  * @brief Cost of the edge (parent node, this node)
76  */
78  /**
79  * @brief Cost of the path (sttart node, this node)
80  */
82  /**
83  * @brief Link to all children nodes
84  */
85  std::set<NodeId> children;
86  };
87 
88  /**
89  * @brief Lookuptable for pending updates.
90  */
91  using PendingUpdateLookuptableType = std::unordered_map<std::pair<std::size_t, std::size_t>, std::size_t>;
92  /**
93  * @brief Iterator for the PendingUpdateLookuptable.
94  */
95  using PendingUpdateLookuptableIterator = typename PendingUpdateLookuptableType::iterator;
96  /**
97  * @brief Const iterator for the PendingUpdateLookuptable.
98  */
99  using PendingUpdateLookuptableConstIterator = typename PendingUpdateLookuptableType::const_iterator;
100 
101  /**
102  * @brief Default ctor.
103  */
104  Tree() = default;
105 
106  /**
107  * @brief Creates a tree that can be used to analyze and apply updates.
108  * Other operations are undefined.
109  * @param startCfg The start config.
110  */
111  Tree(const VectorXf& startCfg);
112 
113  //init
114  /**
115  * @brief Initailizes the tree from the given tree.
116  * @param iceTree The tree to use as initial state.
117  * @param newaddParams The new adaptive dynamic domain parameters.
118  * @param newWorkerId The owner's worker id.
119  */
120  void init(const FullIceTree& iceTree,
121  AdaptiveDynamicDomainParameters newaddParams,
122  std::size_t newWorkerId
123  );
124 
125  /**
126  * @brief Increases the storage of all data structures to be appropriate for count workers.
127  * @param count The number of workers.
128  */
129  void increaseWorkerCountTo(std::size_t count);
130 
131  //updating
132  public:
133  /**
134  * @param u The update.
135  * @return Whether the given update can be applied.
136  */
137  bool canApplyUpdate(const Update& u);
138  /**
139  * @brief Adds a new update to the list of pending updates.
140  * @param u The update.
141  */
142  void addPendingUpdate(const Update& u);
143  /**
144  * @brief Applies the given update.
145  * @param u The update.
146  */
147  void applyUpdate(const Update& u);
148  /**
149  * @brief Sends the current update to the given proxy. A new update is prepared afterwards.
150  * @param prx The proxy to send the update to.
151  */
152  void sendCurrentUpdate(TreeUpdateInterfacePrx& prx);
153  /**
154  * @return Returns all updates send by this tree.
155  */
156  const std::deque<Update>& getLocalUpdates() const
157  {
158  return localUpdates;
159  }
160 
161  /**
162  * @param workerId The update's worker id.
163  * @param updateId The update's sub id.
164  * @return Whether the given update was applied.
165  */
166  bool hasAppliedUpdate(std::size_t workerId, Ice::Long updateId)
167  {
169  return updateId <= appliedUpdateIds.at(workerId);
170  }
171  /**
172  * @param workerId The update's worker id.
173  * @param updateId The update's sub id.
174  * @return Whether the given update is in the list of pending updates.
175  */
176  bool hasPendingUpdate(std::size_t workerId, std::size_t updateId) const
177  {
178  return findPendingUpdate(workerId, updateId) != pendingUpdatesLookupTable.end();
179  }
180 
181  /**
182  * @param workerId The update's worker id.
183  * @param updateId The update's sub id.
184  * @return Returns the requested update from the list of pending updates.
185  */
186  const Update& getPendingUpdate(std::size_t workerId, std::size_t updateId) const
187  {
189  return pendingUpdates.at(findPendingUpdate(workerId, updateId)->second);
190  }
191 
192  /**
193  * @brief Applies all pending updates. Updates are not consumes.
194  * If a required update is missing this methode will fail. (no update getter is providet).
195  * This function is not thread save! Use it only in a serial/protected environment.
196  */
198  {
199  std::mutex m;
200  std::unique_lock<std::mutex> lock {m};
202  lock,
203  [](std::size_t w, Ice::Long u)
204  {
205  ARMARX_ERROR_S << "no remote update getter set but missing an update (w/s): " << w << "/" << u;
206  throw std::logic_error {"no remote update getter set but missing an update!"};
207  return Update();
208  }
209  );
210  }
211 
212  /**
213  * @brief The same as applyPendingUpdates(LockType, RemoteUpdateGetter, UpdateConsumer) but uses an noop as updateConsumer
214  * This overload is needed since the defaultparameter does not work.
215  * @param getRemoteUpdate Function used to request a missing update.
216  * @see Tree::applyPendingUpdates
217  */
218  template<class LockType, class RemoteUpdateGetter>
219  void applyPendingUpdates(LockType&& treeLock, RemoteUpdateGetter getRemoteUpdate)
220  {
221  applyPendingUpdates(treeLock, getRemoteUpdate, [](Update&&) {});
222  }
223 
224  /**
225  * @brief Applies all pending updates.
226  * @param treeLock The lock used to protect the update structures.
227  * It will be unlocked when getting a remote update.
228  * @param getRemoteUpdate Function used to request a missing update.
229  * @param updateConsumer Function used to consume updates after they were added. (will be moved to the consumer)
230  */
231  template<class LockType, class RemoteUpdateGetter, class UpdateConsumer>
233  LockType&& treeLock,
234  RemoteUpdateGetter getRemoteUpdate,
235  UpdateConsumer updateConsumer = [](Update&&) {} //no op
236  )
237  {
238  ARMARX_CHECK_EXPRESSION(treeLock);
240 
241  //we want i for an assert after the loop
242  std::size_t i = 0;
243 
244  for (; i < pendingUpdates.size(); ++i)
245  {
246  //apply this pending update
247  applyPendingUpdate(i, treeLock, getRemoteUpdate, updateConsumer);
248  ARMARX_CHECK_EXPRESSION(treeLock);
249  }
250 
251  ARMARX_CHECK_EXPRESSION(treeLock);
253  //clear updates
254  pendingUpdates.clear();
256  }
257 
258  /**
259  * @return The update sub id of the current update. (The one currently constructed, not the last send)
260  */
262  {
263  const auto id = static_cast<Ice::Long>(getLocalUpdates().size()) - 1;
265  return id;
266  }
267  /**
268  * @return The update sub id of the last send update.
269  */
271  {
272  return appliedUpdateIds.at(workerId);
273  }
274 
275  /**
276  * @return The currently constructed update.
277  */
278  const Update& getCurrentUpdate() const
279  {
280  return localUpdates.back();
281  }
282  protected:
283  /**
284  * @return The currently constructed update. (non const version for internal use)
285  */
287  {
288  return localUpdates.back();
289  }
290 
291  /**
292  * @brief Applies a radius update to the tree.
293  * @param u The update.
294  */
295  void applyRadiusUpdate(const RadiusUpdate& u);
296  /**
297  * @brief Applies a node creation update to the tree.
298  * @param u The update.
299  * @param workerId The updates source worker.
300  */
301  void applyNodeCreationUpdate(const NodeCreationUpdate& u, std::size_t workerId)
302  {
303  doAddNode(u.config, u.parent, u.fromParentCost, workerId);
304  }
305  /**
306  * @brief Applies a rewire update to the tree.
307  * @param rewireUpdate The update.
308  */
309  void applyRewireUpdate(const RewireUpdate& rewireUpdate);
310  /**
311  * @brief Applies multiple goal reached updates to the tree.
312  * @param newGoalNodes The new nodes that can reach the goal configuration.
313  */
314  void applyGoalReachedUpdates(const GoalInfoList newGoalNodes)
315  {
316  std::copy(newGoalNodes.begin(), newGoalNodes.end(), std::back_inserter(goalNodes));
317  }
318 
319  /**
320  * @brief Prepares the next update. (the update to be send)
321  */
322  void prepareNextUpdate();
323 
324  /**
325  * @brief Adds a new radius update to the current update.
326  * @param id The node's id.
327  * @param increaseRadius Whether the node's radius increases.
328  */
329  void createNewRadiusUpdate(const NodeId& id, bool increaseRadius)
330  {
331  getCurrentUpdateNonConst().radii.emplace_back(RadiusUpdate {id, increaseRadius});
332  }
333  /**
334  * @brief Adds a new node creation update to the current update.
335  * @param cfg The new node's configuration.
336  * @param parent The new node's parent node.
337  * @param fromParentCost The cost of the edge (node, parent)
338  */
339  void createNewNodeCreationUpdate(const ConfigType& cfg, const NodeId& parent, float fromParentCost)
340  {
341  getCurrentUpdateNonConst().nodes.emplace_back(NodeCreationUpdate {cfg, parent, fromParentCost});
342  }
343  /**
344  * @brief Adds a new node creation update to the current update.
345  * @param child The rewired child node.
346  * @param newParent The new parent node.
347  * @param fromParentCost The cost of the edge (child, new parent)
348  */
349  void createNewRewireUpdate(const NodeId& child, const NodeId& newParent, float fromParentCost)
350  {
351  getCurrentUpdateNonConst().rewires.emplace_back(RewireUpdate {child, newParent, fromParentCost});
352  }
353  /**
354  * @brief Adds a new goal reached update to the current update.
355  * @param goal The node that can reach the goal configuration.
356  * @param costToGoToGoal The cost to reach the goal configuration from the given node)
357  */
358  void createNewGoalReachedUpdate(const NodeId& goal, float costToGoToGoal)
359  {
360  getCurrentUpdateNonConst().newGoalNodes.emplace_back(GoalInfo {goal, costToGoToGoal});
361  }
362 
363  /**
364  * @brief Applies one pending update from the list of pending updates.
365  * @param updateIndex The pending update's index.
366  * @param treeLock The lock used to protect the update structures.
367  * It will be unlocked when getting a remote update.
368  * @param getRemoteUpdate Function used to request a missing update.
369  * @param updateConsumer Function used to consume updates after they were added. (will be moved to the consumer)
370  */
371  template<class LockType, class RemoteUpdateGetter, class UpdateConsumer>
372  void applyPendingUpdate(std::size_t updateIndex, LockType&& treeLock, RemoteUpdateGetter getRemoteUpdate, UpdateConsumer updateConsumer)
373  {
374  ARMARX_CHECK_EXPRESSION(treeLock);
375  ARMARX_CHECK_EXPRESSION(updateIndex < pendingUpdates.size());
376 
377  auto& u = pendingUpdates.at(updateIndex);
378  increaseWorkerCountTo(u.dependetOnUpdateIds.size());
379 
380  const auto& dependetOnUpdateIds = u.dependetOnUpdateIds;
381 
382  ARMARX_CHECK_EXPRESSION(u.workerId >= 0);
383  const auto updateWorkerId = static_cast<std::size_t>(u.workerId);
384 
385  if (dependetOnUpdateIds.empty())
386  {
387  //this update was moved from! => it was applied because it was queued out of order)
388  return;
389  }
390 
391  ARMARX_CHECK_EXPRESSION(updateWorkerId < dependetOnUpdateIds.size());
392  const auto updateId = dependetOnUpdateIds.at(updateWorkerId) + 1;
393 
394  //is it an update from this node or was it already applied?
395  if (
396  updateWorkerId == workerId || //self update
397  hasAppliedUpdate(updateWorkerId, updateId)//update was already applied
398  )
399  {
400  return;
401  }
402 
403  //this update is new => check if it requires prev updates (if we have then, apply them directly, else get them from remote)
404  prepareUpdate(dependetOnUpdateIds, treeLock, getRemoteUpdate, updateConsumer);
405  ARMARX_CHECK_EXPRESSION(treeLock);
406 
407  //now we have all required updates
408  applyUpdate(u);
409  //we dont need the update anymore. move it to the consumer
410  updateConsumer(std::move(u));
411  }
412 
413  public:
414  /**
415  * @brief Prepares an update by applying all dependet on updates.
416  * @param dependetOnUpdateIds The ids of dependet on updates.
417  * @param treeLock The lock used to protect the update structures.
418  * It will be unlocked when getting a remote update.
419  * @param getRemoteUpdate Function used to request a missing update.
420  * @param updateConsumer Function used to consume updates after they were added. (will be moved to the consumer)
421  */
422  template<class LockType, class RemoteUpdateGetter, class UpdateConsumer>
423  void prepareUpdate(Ice::LongSeq dependetOnUpdateIds, LockType&& treeLock, RemoteUpdateGetter getRemoteUpdate, UpdateConsumer updateConsumer)
424  {
425  ARMARX_CHECK_EXPRESSION(treeLock);
426 
427  for (std::size_t workerNodeId = 0; workerNodeId < dependetOnUpdateIds.size(); ++workerNodeId)
428  {
429  const auto updateSubId = dependetOnUpdateIds.at(workerNodeId);
430 
431  if (hasAppliedUpdate(workerNodeId, updateSubId))
432  {
433  //we have this update!
434  continue;
435  }
436 
437  //the update was not applied
438  //is this update local?
439  auto it = findPendingUpdate(workerNodeId, updateSubId);
440 
441  if (it != pendingUpdatesLookupTable.end())
442  {
443  auto missingIndex = (*it).second;
444  //recurse
445  applyPendingUpdate(missingIndex, treeLock, getRemoteUpdate, updateConsumer);
446  }
447  else
448  {
449  ARMARX_CHECK_EXPRESSION(treeLock);
450  //get the update from remote. this may take a while => unlock
451  treeLock.unlock();
452  Update update = getRemoteUpdate(workerNodeId, updateSubId);
453  treeLock.lock();
454  //prepare this update and apply it
455  prepareUpdate(update.dependetOnUpdateIds, treeLock, getRemoteUpdate, updateConsumer);
456  ARMARX_CHECK_EXPRESSION(treeLock);
458  //we dont need the update anymore. move it to the consumer
459  updateConsumer(std::move(update));
460  }
461  }
462  }
463  protected:
464  /**
465  * @param workerId The update's worker.
466  * @param updateId The update's sub id.
467  * @return An iterator tho the pending update. (If it does not exist to end.)
468  */
470  {
471  return pendingUpdatesLookupTable.find(std::make_pair(workerId, updateId));
472  }
473 
474  /**
475  * @param workerId The update's worker.
476  * @param updateId The update's sub id.
477  * @return An iterator tho the pending update. (If it does not exist to end.) (const version)
478  */
479  PendingUpdateLookuptableConstIterator findPendingUpdate(std::size_t workerId, std::size_t updateId) const
480  {
481  return pendingUpdatesLookupTable.find(std::make_pair(workerId, updateId));
482  }
483 
484  //changing the tree from the rrt algorithm
485  public:
486  /**
487  * @brief Adds a node to the tree. (creates an appropriate update)
488  * @param cfg The node's configuration.
489  * @param parent The node's parent.
490  * @param fromParentCost The cost of the edge (node, parent)
491  * @return The new node's id.
492  */
493  NodeId addNode(ConfigType cfg, const NodeId& parent, float fromParentCost);
494  /**
495  * @brief Sets the parent node of child to parent. (costs are updated) (creates an appropriate update)
496  * @param child The cild node.
497  * @param newParent The new parent node.
498  * @param fromParentCost The cost of the edge (child, new parent)
499  */
500  void setNodeParent(const NodeId& child, const NodeId& newParent, float fromParentCost)
501  {
502  doSetNodeParent(child, newParent, fromParentCost);
503  createNewRewireUpdate(child, newParent, fromParentCost);
504  }
505  /**
506  * @brief Decreases a node's radius.(creates an appropriate update)
507  * @param id The node.
508  */
509  void decreaseRadius(const NodeId& id)
510  {
511  createNewRadiusUpdate(id, false);
512  doDecreaseRadius(id);
513  }
514  /**
515  * @brief Increases a node's radius.(creates an appropriate update)
516  * @param id The node.
517  */
518  void increaseRadius(const NodeId& id)
519  {
520  createNewRadiusUpdate(id, true);
521  doIncreaseRadius(id);
522  }
523  /**
524  * @brief Adds a node to the list of nodes that can reach the goal configuration. (creates an appropriate update)
525  * @param goal The node to add.
526  * @param costToGoToGoal The cost to reach the gol configuration from the given node.
527  */
528  void addGoalReached(const NodeId& goal, float costToGoToGoal)
529  {
530  doAddGoalReached(goal, costToGoToGoal);
531  createNewGoalReachedUpdate(goal, costToGoToGoal);
532  }
533  protected:
534  //change the tree
535  /**
536  * @brief Updates fromStartCosts for all children from root.
537  * @param root The root node for the cost update.
538  */
539  void pushCosts(const NodeId& root);
540 
541  /**
542  * @brief Adds a node to the tree.
543  * @param cfg The node's configuration.
544  * @param parent The node's parent.
545  * @param fromParentCost The cost of the edge (node, parent)
546  * @param workerId The worker creating this node.
547  * @return The new node's id.
548  */
549  NodeId doAddNode(ConfigType cfg, const NodeId& parent, float fromParentCost, std::size_t workerId);
550  /**
551  * @brief Sets the parent node of child to parent. (costs are updated)
552  * @param child The cild node.
553  * @param newParent The new parent node.
554  * @param fromParentCost The cost of the edge (child, new parent)
555  * @param updateFromStartCost Whether to update the costs of the subtree beneath cild.
556  */
557  void doSetNodeParent(const NodeId& child, const NodeId& newParent, float fromParentCost, bool updateFromStartCost = true);
558  /**
559  * @brief Decreases a node's radius.
560  * @param id The node.
561  */
562  void doDecreaseRadius(const NodeId& id);
563  /**
564  * @brief Increases a node's radius.
565  * @param id The node.
566  */
567  void doIncreaseRadius(const NodeId& id)
568  {
569  getNode(id).radius *= (1.f + addParams.alpha); //inf will stay inf
570  }
571  /**
572  * @brief Adds a node to the list of nodes that can reach the goal configuration.
573  * @param goal The node to add.
574  * @param costToGoToGoal The cost to reach the gol configuration from the given node.
575  */
576  void doAddGoalReached(const NodeId& goal, float costToGoToGoal)
577  {
578  goalNodes.emplace_back(GoalInfo {goal, costToGoToGoal});
579  }
580 
581  //querry the tree
582  public:
583  /**
584  * @param cfg The configuration.
585  * @param k The number of neighbours.
586  * @return The k nearest neighbours of and their distance to the given configuration.
587  */
588  std::vector<std::pair<NodeId, float>> getKNearestNeighboursAndDistances(const ConfigType& cfg, std::size_t k);
589  /**
590  * @param cfg The configuration.
591  * @return The nearest neighbour of and its distance to the given configuration.
592  */
593  std::pair<NodeId, float> getNearestNeighbourAndDistance(const ConfigType& cfg)
594  {
595  return getKNearestNeighboursAndDistances(cfg, 1).front();
596  }
597  /**
598  * @param id The node's id.
599  * @return The requested node.
600  */
601  NodeType& getNode(const NodeId& id);
602  /**
603  * @param id The node's id.
604  * @return The requested node. (const)
605  */
606  const NodeType& getNode(const NodeId& id) const;
607  /**
608  * @return All nodes.
609  */
610  const std::deque<std::deque<NodeType>>& getNodes() const
611  {
612  return nodes;
613  }
614 
615  /**
616  * @param index The index.
617  * @return The id corresponding to the given node index.
618  */
619  NodeId getIdOfIndex(std::size_t index) const;
620  /**
621  * @return The number of nodes in the tree.
622  */
623  std::size_t size() const
624  {
625  return nodeCount;
626  }
627  /**
628  * @param id The node's id.
629  * @return The requested node.
630  */
631  NodeType& at(const NodeId& id)
632  {
633  return getNode(id);
634  }
635  /**
636  * @param id The node's id.
637  * @return The requested node. (const)
638  */
639  const NodeType& at(const NodeId& id) const
640  {
641  return getNode(id);
642  }
643  /**
644  * @param index The node's index.
645  * @return The requested node.
646  */
647  NodeType& at(std::size_t index)
648  {
649  return at(getIdOfIndex(index));
650  }
651  /**
652  * @param index The node's index.
653  * @return The requested node. (const)
654  */
655  const NodeType& at(std::size_t index) const
656  {
657  return at(getIdOfIndex(index));
658  }
659 
660  /**
661  * @return The current RRT with all updates applied. (used to transmitt it through ice.)
662  */
663  FullIceTree getIceTree() const;
664 
665  protected:
666  /**
667  * @return An iterator to the node that can reach the goal configuration and results in the lowest path cost. (end if no path was found)
668  */
669  typename std::deque<GoalInfo>::const_iterator getBestCostIt() const
670  {
671  return std::min_element(goalNodes.begin(), goalNodes.end(), [this](const GoalInfo & fst, const GoalInfo & snd)
672  {
673  return (getNode(fst.node).fromStartCost + fst.costToGoToGoal) < (getNode(snd.node).fromStartCost + snd.costToGoToGoal);
674  });
675  }
676 
677  //querry for paths
678  public:
679  /**
680  * @return The cost of the shortest solution.
681  */
682  float getBestCost() const;
683 
684  /**
685  * @return The path with the lowest cost.
686  */
687  PathWithCost getBestPath() const;
688 
689  /**
690  * @param nodeId The node to check.
691  * @return Whether the given node is a node in the list of nodes able to reach the goal configuration.
692  */
693  bool hasGoalNode(const NodeId& nodeId) const
694  {
695  return std::any_of(
696  goalNodes.begin(),
697  goalNodes.end(),
698  [&nodeId](const GoalInfo & info)
699  {
700  return info.node == nodeId;
701  }
702  );
703  }
704 
705  /**
706  * @return The number of paths found.
707  */
708  std::size_t getPathCount() const
709  {
710  return goalNodes.size();
711  }
712 
713  /**
714  * @param n The paths index.
715  * @return The cost of the given path.
716  */
717  float getNthPathCost(std::size_t n) const
718  {
720  return getNode(goalNodes.at(n).node).fromStartCost + goalNodes.at(n).costToGoToGoal;
721  }
722  /**
723  * @param n N
724  * @return The nth path.
725  */
726  PathWithCost getNthPathWithCost(std::size_t n) const;
727  /**
728  * @param n The path's index.
729  * @return The node ids of nodes contained by the given path.
730  */
731  NodeIdList getNthPathIds(std::size_t n) const;
732  protected:
733  /**
734  * @param id The node's id.
735  * @return Returns a path to the requested node.
736  */
737  VectorXfSeq getPathTo(NodeId id) const;
738  //utility
739  public:
740  /**
741  * @param workerId The worker's id.
742  * @return The next node id for the given worker.
743  */
744  NodeId getNextNodeIdFor(std::size_t workerId)
745  {
747  return {static_cast<Ice::Long>(workerId), static_cast<Ice::Long>(nodes.at(workerId).size())};
748  }
749 
750  /**
751  * @brief The root node's id
752  */
753  static const NodeId ROOT_ID;
754 
755  /**
756  * @return List of ids of applied updates.
757  */
758  const Ice::LongSeq& getAppliedUpdateIds() const
759  {
760  return appliedUpdateIds;
761  }
762 
763  protected:
764  /**
765  * @brief List of ids of applied updates.
766  */
767  Ice::LongSeq appliedUpdateIds;
768  //deques are used because they don't need to copy all already stored data
769  //when the capacity runs out
770 
771  /**
772  * @brief Updates created by this tree.
773  */
774  std::deque<Update> localUpdates;
775 
776  /**
777  * @brief Holds all nodes.
778  * The node nodes[i][j] is the jth node created by worker i.
779  */
780  std::deque<std::deque<NodeType>> nodes;
781  /**
782  * @brief List of nodes able to reach the goal configuration.
783  */
784  std::deque<GoalInfo> goalNodes;
785 
786  /**
787  * @brief The parameters of adaptive dynamic domain)
788  */
789  AdaptiveDynamicDomainParameters addParams;
790 
791  /**
792  * @brief The worker id of this trees owner. (only used for sending updates and skipping updates send by this tree)
793  */
794  std::size_t workerId;
795  /**
796  * @brief The number of nodes in the rrt.
797  */
798  std::size_t nodeCount;
799 
800  /**
801  * @brief Updates to apply to the tree.
802  */
803  std::deque<Update> pendingUpdates; //only default init required
804 
805  /**
806  * @brief Speeds up lookup for available updates. (O(log(n)) instead of O(n))
807  *
808  * maps from UpdateId to the corresponding index in the pendingUpdates vector
809  */
811  };
812 }
armarx::addirrtstar::Tree::createNewNodeCreationUpdate
void createNewNodeCreationUpdate(const ConfigType &cfg, const NodeId &parent, float fromParentCost)
Adds a new node creation update to the current update.
Definition: Tree.h:339
armarx::addirrtstar::Tree::pendingUpdatesLookupTable
PendingUpdateLookuptableType pendingUpdatesLookupTable
Speeds up lookup for available updates.
Definition: Tree.h:810
armarx::addirrtstar::Tree::at
NodeType & at(const NodeId &id)
Definition: Tree.h:631
armarx::addirrtstar::Tree::canApplyUpdate
bool canApplyUpdate(const Update &u)
Definition: Tree.cpp:136
armarx::addirrtstar::Tree::at
NodeType & at(std::size_t index)
Definition: Tree.h:647
armarx::addirrtstar::Tree::getPendingUpdate
const Update & getPendingUpdate(std::size_t workerId, std::size_t updateId) const
Definition: Tree.h:186
armarx::addirrtstar::Tree::size
std::size_t size() const
Definition: Tree.h:623
armarx::addirrtstar::Tree::getNthPathIds
NodeIdList getNthPathIds(std::size_t n) const
Definition: Tree.cpp:420
armarx::addirrtstar::Tree::doDecreaseRadius
void doDecreaseRadius(const NodeId &id)
Decreases a node's radius.
Definition: Tree.cpp:298
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::addirrtstar::Tree::setNodeParent
void setNodeParent(const NodeId &child, const NodeId &newParent, float fromParentCost)
Sets the parent node of child to parent.
Definition: Tree.h:500
armarx::addirrtstar::Tree::getBestPath
PathWithCost getBestPath() const
Definition: Tree.cpp:406
armarx::addirrtstar::Tree::hasAppliedUpdate
bool hasAppliedUpdate(std::size_t workerId, Ice::Long updateId)
Definition: Tree.h:166
armarx::addirrtstar::Tree::nodeCount
std::size_t nodeCount
The number of nodes in the rrt.
Definition: Tree.h:798
armarx::addirrtstar::Tree::addParams
AdaptiveDynamicDomainParameters addParams
The parameters of adaptive dynamic domain)
Definition: Tree.h:789
util.h
armarx::addirrtstar::Tree::decreaseRadius
void decreaseRadius(const NodeId &id)
Decreases a node's radius.
Definition: Tree.h:509
armarx::addirrtstar::Tree::PendingUpdateLookuptableIterator
typename PendingUpdateLookuptableType::iterator PendingUpdateLookuptableIterator
Iterator for the PendingUpdateLookuptable.
Definition: Tree.h:95
armarx::addirrtstar::Tree::init
void init(const FullIceTree &iceTree, AdaptiveDynamicDomainParameters newaddParams, std::size_t newWorkerId)
Initailizes the tree from the given tree.
Definition: Tree.cpp:65
armarx::addirrtstar::Tree::getNextNodeIdFor
NodeId getNextNodeIdFor(std::size_t workerId)
Definition: Tree.h:744
armarx::addirrtstar::Tree::applyPendingUpdates
void applyPendingUpdates()
Applies all pending updates.
Definition: Tree.h:197
armarx::addirrtstar::Tree::NodeType::config
ConfigType config
The node's configuration.
Definition: Tree.h:65
armarx::addirrtstar::Tree::findPendingUpdate
PendingUpdateLookuptableIterator findPendingUpdate(std::size_t workerId, std::size_t updateId)
Definition: Tree.h:469
armarx::addirrtstar::Tree::getNthPathCost
float getNthPathCost(std::size_t n) const
Definition: Tree.h:717
armarx::addirrtstar::Tree::appliedUpdateIds
Ice::LongSeq appliedUpdateIds
List of ids of applied updates.
Definition: Tree.h:767
armarx::addirrtstar::Tree::NodeType
Represents a node of thr rrt.
Definition: Tree.h:60
armarx::addirrtstar::Tree::addNode
NodeId addNode(ConfigType cfg, const NodeId &parent, float fromParentCost)
Adds a node to the tree.
Definition: Tree.cpp:241
armarx::addirrtstar::Tree::findPendingUpdate
PendingUpdateLookuptableConstIterator findPendingUpdate(std::size_t workerId, std::size_t updateId) const
Definition: Tree.h:479
armarx::addirrtstar::Tree::doAddNode
NodeId doAddNode(ConfigType cfg, const NodeId &parent, float fromParentCost, std::size_t workerId)
Adds a node to the tree.
Definition: Tree.cpp:261
armarx::addirrtstar::Tree::ConfigType
VectorXf ConfigType
The type of configurations.
Definition: Tree.h:56
armarx::addirrtstar::Tree::getNearestNeighbourAndDistance
std::pair< NodeId, float > getNearestNeighbourAndDistance(const ConfigType &cfg)
Definition: Tree.h:593
armarx::addirrtstar::Tree::applyNodeCreationUpdate
void applyNodeCreationUpdate(const NodeCreationUpdate &u, std::size_t workerId)
Applies a node creation update to the tree.
Definition: Tree.h:301
armarx::addirrtstar::Tree::NodeType::radius
float radius
The node's radius.
Definition: Tree.h:73
armarx::addirrtstar::Tree::createNewGoalReachedUpdate
void createNewGoalReachedUpdate(const NodeId &goal, float costToGoToGoal)
Adds a new goal reached update to the current update.
Definition: Tree.h:358
armarx::addirrtstar::Tree::applyGoalReachedUpdates
void applyGoalReachedUpdates(const GoalInfoList newGoalNodes)
Applies multiple goal reached updates to the tree.
Definition: Tree.h:314
armarx::addirrtstar::Tree::NodeType::parent
NodeId parent
The node's parent.
Definition: Tree.h:69
armarx::addirrtstar::Tree::applyUpdate
void applyUpdate(const Update &u)
Applies the given update.
Definition: Tree.cpp:151
armarx::addirrtstar::Tree::createNewRadiusUpdate
void createNewRadiusUpdate(const NodeId &id, bool increaseRadius)
Adds a new radius update to the current update.
Definition: Tree.h:329
armarx::addirrtstar::Tree::applyRewireUpdate
void applyRewireUpdate(const RewireUpdate &rewireUpdate)
Applies a rewire update to the tree.
Definition: Tree.cpp:212
armarx::addirrtstar::Tree::getPathCount
std::size_t getPathCount() const
Definition: Tree.h:708
armarx::addirrtstar::Tree::sendCurrentUpdate
void sendCurrentUpdate(TreeUpdateInterfacePrx &prx)
Sends the current update to the given proxy.
Definition: Tree.cpp:189
armarx::addirrtstar::Tree::getPathTo
VectorXfSeq getPathTo(NodeId id) const
Definition: Tree.cpp:440
armarx::addirrtstar::Tree::workerId
std::size_t workerId
The worker id of this trees owner.
Definition: Tree.h:794
armarx::addirrtstar::Tree::increaseRadius
void increaseRadius(const NodeId &id)
Increases a node's radius.
Definition: Tree.h:518
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
armarx::addirrtstar::Tree::applyRadiusUpdate
void applyRadiusUpdate(const RadiusUpdate &u)
Applies a radius update to the tree.
Definition: Tree.cpp:200
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::addirrtstar::Tree::PendingUpdateLookuptableType
std::unordered_map< std::pair< std::size_t, std::size_t >, std::size_t > PendingUpdateLookuptableType
Lookuptable for pending updates.
Definition: Tree.h:91
armarx::addirrtstar::Tree::Tree
Tree()=default
Default ctor.
armarx::addirrtstar::Tree::getLocalUpdates
const std::deque< Update > & getLocalUpdates() const
Definition: Tree.h:156
armarx::addirrtstar::Tree::at
const NodeType & at(const NodeId &id) const
Definition: Tree.h:639
armarx::addirrtstar::Tree::getAppliedUpdateIds
const Ice::LongSeq & getAppliedUpdateIds() const
Definition: Tree.h:758
armarx::addirrtstar::Tree::doIncreaseRadius
void doIncreaseRadius(const NodeId &id)
Increases a node's radius.
Definition: Tree.h:567
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::addirrtstar::Tree::applyPendingUpdate
void applyPendingUpdate(std::size_t updateIndex, LockType &&treeLock, RemoteUpdateGetter getRemoteUpdate, UpdateConsumer updateConsumer)
Applies one pending update from the list of pending updates.
Definition: Tree.h:372
armarx::addirrtstar::Tree::hasPendingUpdate
bool hasPendingUpdate(std::size_t workerId, std::size_t updateId) const
Definition: Tree.h:176
armarx::addirrtstar::Tree::hasGoalNode
bool hasGoalNode(const NodeId &nodeId) const
Definition: Tree.h:693
armarx::addirrtstar::Tree::getIceTree
FullIceTree getIceTree() const
Definition: Tree.cpp:458
armarx::addirrtstar::Tree::getNode
NodeType & getNode(const NodeId &id)
Definition: Tree.cpp:354
armarx::addirrtstar::Tree::getIdOfIndex
NodeId getIdOfIndex(std::size_t index) const
Definition: Tree.cpp:372
armarx::addirrtstar::Tree::localUpdates
std::deque< Update > localUpdates
Updates created by this tree.
Definition: Tree.h:774
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::addirrtstar::Tree::getPreviousUpdateId
Ice::Long getPreviousUpdateId() const
Definition: Tree.h:270
armarx::addirrtstar::Tree::ROOT_ID
static const NodeId ROOT_ID
The root node's id.
Definition: Tree.h:753
armarx::addirrtstar::Tree::nodes
std::deque< std::deque< NodeType > > nodes
Holds all nodes.
Definition: Tree.h:780
armarx::addirrtstar::Tree::NodeType::fromStartCost
float fromStartCost
Cost of the path (sttart node, this node)
Definition: Tree.h:81
armarx::addirrtstar::Tree::getKNearestNeighboursAndDistances
std::vector< std::pair< NodeId, float > > getKNearestNeighboursAndDistances(const ConfigType &cfg, std::size_t k)
Definition: Tree.cpp:311
armarx::addirrtstar::Tree::goalNodes
std::deque< GoalInfo > goalNodes
List of nodes able to reach the goal configuration.
Definition: Tree.h:784
armarx::addirrtstar::Tree::NodeType::fromParentCost
float fromParentCost
Cost of the edge (parent node, this node)
Definition: Tree.h:77
armarx::addirrtstar::Tree::getCurrentUpdate
const Update & getCurrentUpdate() const
Definition: Tree.h:278
armarx::addirrtstar::Tree::applyPendingUpdates
void applyPendingUpdates(LockType &&treeLock, RemoteUpdateGetter getRemoteUpdate, UpdateConsumer updateConsumer=[](Update &&) {})
Applies all pending updates.
Definition: Tree.h:232
armarx::addirrtstar::Tree::getNthPathWithCost
PathWithCost getNthPathWithCost(std::size_t n) const
Definition: Tree.cpp:413
armarx::addirrtstar::Tree::doSetNodeParent
void doSetNodeParent(const NodeId &child, const NodeId &newParent, float fromParentCost, bool updateFromStartCost=true)
Sets the parent node of child to parent.
Definition: Tree.cpp:281
armarx::addirrtstar::Tree::getCurrentUpdateId
Ice::Long getCurrentUpdateId() const
Definition: Tree.h:261
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::addirrtstar::Tree
A structure holding and managing all data connected to the tree used in the ADDIRRT* algorithm.
Definition: Tree.h:49
armarx::addirrtstar::Tree::NodeType::children
std::set< NodeId > children
Link to all children nodes.
Definition: Tree.h:85
armarx::addirrtstar::Tree::addPendingUpdate
void addPendingUpdate(const Update &u)
Adds a new update to the list of pending updates.
Definition: Tree.cpp:483
armarx::addirrtstar::Tree::increaseWorkerCountTo
void increaseWorkerCountTo(std::size_t count)
Increases the storage of all data structures to be appropriate for count workers.
Definition: Tree.cpp:122
armarx::addirrtstar::Tree::getBestCost
float getBestCost() const
Definition: Tree.cpp:399
Logging.h
armarx::addirrtstar::Tree::getBestCostIt
std::deque< GoalInfo >::const_iterator getBestCostIt() const
Definition: Tree.h:669
armarx::addirrtstar::Tree::addGoalReached
void addGoalReached(const NodeId &goal, float costToGoToGoal)
Adds a node to the list of nodes that can reach the goal configuration.
Definition: Tree.h:528
armarx::addirrtstar::Tree::at
const NodeType & at(std::size_t index) const
Definition: Tree.h:655
armarx::addirrtstar::Tree::pendingUpdates
std::deque< Update > pendingUpdates
Updates to apply to the tree.
Definition: Tree.h:803
armarx::addirrtstar::Tree::getCurrentUpdateNonConst
Update & getCurrentUpdateNonConst()
Definition: Tree.h:286
armarx::addirrtstar::Tree::doAddGoalReached
void doAddGoalReached(const NodeId &goal, float costToGoToGoal)
Adds a node to the list of nodes that can reach the goal configuration.
Definition: Tree.h:576
armarx::addirrtstar::Tree::pushCosts
void pushCosts(const NodeId &root)
Updates fromStartCosts for all children from root.
Definition: Tree.cpp:252
armarx::addirrtstar::Tree::prepareUpdate
void prepareUpdate(Ice::LongSeq dependetOnUpdateIds, LockType &&treeLock, RemoteUpdateGetter getRemoteUpdate, UpdateConsumer updateConsumer)
Prepares an update by applying all dependet on updates.
Definition: Tree.h:423
armarx::addirrtstar::Tree::getNodes
const std::deque< std::deque< NodeType > > & getNodes() const
Definition: Tree.h:610
armarx::addirrtstar::Tree::createNewRewireUpdate
void createNewRewireUpdate(const NodeId &child, const NodeId &newParent, float fromParentCost)
Adds a new node creation update to the current update.
Definition: Tree.h:349
armarx::addirrtstar::Tree::prepareNextUpdate
void prepareNextUpdate()
Prepares the next update.
Definition: Tree.cpp:233
armarx::addirrtstar
Definition: ManagerNode.cpp:35
armarx::addirrtstar::Tree::applyPendingUpdates
void applyPendingUpdates(LockType &&treeLock, RemoteUpdateGetter getRemoteUpdate)
The same as applyPendingUpdates(LockType, RemoteUpdateGetter, UpdateConsumer) but uses an noop as upd...
Definition: Tree.h:219
armarx::addirrtstar::Tree::PendingUpdateLookuptableConstIterator
typename PendingUpdateLookuptableType::const_iterator PendingUpdateLookuptableConstIterator
Const iterator for the PendingUpdateLookuptable.
Definition: Tree.h:99