KrisLibrary  1.0.0
DisplacementPlanner.h
1 #ifndef DISPLACEMENT_PLANNER_H
2 #define DISPLACEMENT_PLANNER_H
3 
4 #include <KrisLibrary/Logger.h>
5 #include "MotionPlanner.h"
6 #include <KrisLibrary/utils/Subset.h>
7 #include <KrisLibrary/structs/IndexedPriorityQueue.h>
8 
36 {
37 public:
39  virtual ~ObstacleDisplacementCSpace() {}
40 
41  //these should be implemented by the subclass
42  virtual std::shared_ptr<CSpace> DisplacementSpace(int obstacle) const { return NULL; }
43  virtual bool IsFeasible(const Config& q,int obstacle,const Vector& d)=0;
44  virtual bool IsFeasibleAll(const Config& q,int obstacle) { return false; }
45  virtual bool IsVisibleAll(const Config& a,const Config& b,int obstacle) { return false; }
46 
47  virtual void SetDisplacement(int obstacle,const Vector& d);
48  virtual void GetDisplacement(int obstacle,Vector& d) const;
49  virtual void SetDisplacementRef(int obstacle,const Vector& d);
50  virtual void GetDisplacementRef(int obstacle,Vector& d) const;
51  bool IsDisplaceable(int obstacle) const;
52 
53  //overriding ExplicitCSpace's method
54  virtual bool IsFeasible(const Config& q,int obstacle);
55 
56  //re-initializes all displacements to 0, gets the displacementSpaeces
57  void InitZeroDisplacements();
58 
59  std::vector<Vector> obstacleDisplacements;
60  std::vector<std::shared_ptr<CSpace> > displacementSpaces;
61 };
62 
94 {
95  public:
97  struct TestResult
98  {
99  int allFeasible; //0 = false, 1 = true, -1 = unknown
100  Subset feasible;
101  Subset infeasible;
102  };
103  typedef std::vector<TestResult> TestResults;
104 
105  struct Milestone {
106  Config q;
107  TestResults tests;
108  };
109  struct Edge {
110  EdgePlannerPtr e;
111  TestResults tests;
112  };
114 
116  {
117  int vertex;
118  std::vector<int> assignment;
119  double pathLength,totalCost;
120  PathSearchNode* parent;
121  };
122 
124  void Init(const Config& start,const Config& goal);
127  bool Plan(int numIters,int numExpandsPerDisp,int numLocalOptimize,Real expandLimitStep,std::vector<int>& bestPath,std::vector<int>& bestDisplacements);
129  void Expand(Real maxTotalCost,std::vector<int>& newNodes);
132  int AddNewDisplacement(Real maxTotalCost = Inf);
136  virtual std::pair<int,Real> PickObstacleToSample(Real maxTotalCost);
139  virtual std::pair<int,Real> PickExploreObstacle(Real maxTotalCost);
142  virtual std::pair<int,Real> PickGoalExploreObstacle(Real maxTotalCost);
145  virtual std::pair<int,Real> PickRefineObstacle(Real maxTotalCost);
148  virtual std::pair<int,Real> PickGoalRefineObstacle(Real maxTotalCost);
150  bool IsCandidateForExploration(int i) const;
152  void BuildRoadmap(Real maxExplanationCost,RoadmapPlanner& prm);
153 
154  //helpers
155  double Cost(const std::vector<int>& assignment) const;
156 
157  int AddNode(const Config& q,int parent=-1);
158  void AddEdge(int i,int j);
159  void AddEdge(int i,int j,const TestResults& tests);
160  int CheckImmovableAndAddNode(const Config& q,int parent=-1);
161  int ExtendEdge(int i,const Config& q); //returns index of q
162  void KNN(const Config& q,int k,std::vector<int>& neighbors,std::vector<Real>& distances);
163  void KNN(const Config& q,Real maxTotalCost,int k,std::vector<int>& neighbors,std::vector<Real>& distances);
164  bool UpdateCoversIn(int nstart,Real maxTotalCost);
165  void UpdateCoversOut(int nstart,Real maxTotalCost);
166  std::shared_ptr<PathSearchNode> UpdateEdge(int i,int j,PathSearchNode* ni,Real maxTotalCost);
167  bool CheckUpstreamConstraints(PathSearchNode* n,int constraint,int sample);
168  void PropagateDownstream(PathSearchNode* n,std::vector<std::shared_ptr<PathSearchNode> >& nodes,Real maxTotalCost);
169  bool FindMinimumAssignment(PathSearchNode* n,Real maxTotalCost);
170  bool CheckNodeConstraint(Milestone& v,int constraint,int sample);
171  bool CheckEdgeConstraint(Edge& e,int constraint,int sample);
172  Real OptimalCost(int i) const;
173  PathSearchNode* OptimalPathTo(int i);
174  void PruneSearchNode(PathSearchNode* n,IndexedPriorityQueue<PathSearchNode*,Real>* q=NULL);
175  bool Revisited(PathSearchNode* n);
178  bool Dominates(PathSearchNode* a,PathSearchNode* b);
179  //returns true if all of the displacements of setting b are at least as
180  //costly as the corresponding displacements of assignment a
181  bool ParetoDominates(const std::vector<int>& a,const std::vector<int>& b) const;
182 
185  void LocalImprovementCandidates(int i,std::vector<int>& obstacles);
189  bool GenerateDisplacementSample(int obstacle, Real maxDispCost, Real maxTotalCost, int numTries);
192  bool AddDisplacementSample(int obstacle,const Vector& disp);
194  void AddDisplacementSampleRaw(int obstacle,const Vector& disp);
200  bool RefineGoalDisplacements(int numIters,Real perturbRadiusFrac=1.0);
209  bool RefineGoalPathAndDisplacements(int numIters,Real perturbRadiusFrac=1.0,Real lipschitzDisp=1.0);
212  bool ShortcutGoalPath(int skip,int numIters);
213 
215  void GetPath(PathSearchNode* n,std::vector<int>& path) const;
217  void GetMilestonePath(const std::vector<int>& path,MilestonePath& mpath) const;
221  void GetMilestonePath(PathSearchNode* n,MilestonePath& mpath) const;
222 
224  bool SanityCheck(bool checkOneCoverGreedy=false);
225 
226  Config start,goal;
228 
229  //minimize pathCostWeight*length(path) + sum of displacement costs
230  //if initialDisplacementCosts is nonempty and displacement i is nonzero,
231  //it incurs an extra cost of initialDisplacementCosts[i].
232  std::vector<Real> initialDisplacementCosts;
233  Real pathCostWeight;
234 
235  //settings for RRT* like expansion
236  bool dynamicDomainExpansion;
237  int numConnections;
238  Real connectThreshold,expandDistance,goalConnectThreshold;
239  Real goalBiasProbability; //probability of expanding toward the goal
240  bool bidirectional; //not functional yet
241 
242  //settings for path update
248 
249  //settings for obstacle sampling
250  int obstacleSampleCount,obstacleDescendIters;
251 
252  Roadmap roadmap;
253  std::vector<std::vector<Vector> > displacementSamples;
254  std::vector<std::vector<Real> > displacementSampleCosts;
255  std::vector<std::vector<int> > displacementSampleOrders;
256 
258  {
259  //store either a single cover (in case of greedy search)
260  //or multiple pareto-optimal covers (in case of optimal search)
261  std::vector<std::shared_ptr<PathSearchNode> > covers;
262  };
263  std::vector<DynamicShortestPathNode> pathCovers;
264 
265  //planning statistics
266  int numExpands,numRefinementAttempts,numRefinementSuccesses,numExplorationAttempts,
267  numEdgeChecks,numConfigChecks,
268  numUpdateCovers,numUpdateCoversIterations;
269  double timeNearestNeighbors,timeRefine,timeExplore,timeUpdateCoversIn,timeUpdateCoversOut,timeOverhead;
270 };
271 
272 #endif
Definition: DisplacementPlanner.h:109
Definition: DisplacementPlanner.h:257
Contains a priority queue with an associated index, allowing updates of the priority value...
Definition: IndexedPriorityQueue.h:36
Definition: DisplacementPlanner.h:115
Vector Config
an alias for Vector
Definition: RobotKinematics3D.h:14
Motion planning configuration space base class. The configuration space implements an interpolation s...
Definition: CSpace.h:34
A class that needs to be subclassed in order to implement a minimum constraint displacement problem...
Definition: DisplacementPlanner.h:35
A sequence of locally planned paths between milestones.
Definition: planning/Path.h:15
A planner that minimizes the the displacement cost of violated constraints using a RRT-like strategy...
Definition: DisplacementPlanner.h:93
Definition: DisplacementPlanner.h:105
A base roadmap planner class.
Definition: MotionPlanner.h:23
int updatePathsMax
For complete planning, keep at most this number of covers.
Definition: DisplacementPlanner.h:247
stores all values for a single obstacle displacement test
Definition: DisplacementPlanner.h:97
The logging system used in KrisLibrary.
A finite subset of numbered items with convenient operators for union, intersection, difference, etc.
Definition: Subset.h:13
bool updatePathsComplete
Definition: DisplacementPlanner.h:245