Loading...
Searching...
No Matches
HySST.h
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2025, University of Santa Cruz Hybrid Systems Laboratory
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the University of Santa Cruz nor the names of
18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Authors: Beverly Xu */
36/* Adapted from: ompl/geometric/planners/src/SST.cpp by Zakary Littlefield of Rutgers the State University of New
37 * Jersey, New Brunswick */
38
39#ifndef OMPL_CONTROL_PLANNERS_SST_HySST_
40#define OMPL_CONTROL_PLANNERS_SST_HySST_
41
42#include "ompl/control/planners/PlannerIncludes.h"
43#include "ompl/datastructures/NearestNeighbors.h"
44#include "ompl/control/Control.h"
45#include "ompl/control/spaces/RealVectorControlSpace.h"
46#include "ompl/base/spaces/HybridStateSpace.h"
47
48namespace ompl
49{
50 namespace control
51 {
63 class HySST : public base::Planner
64 {
65 public:
67 HySST(const control::SpaceInformationPtr &si);
68
70 ~HySST() override;
71
73 void setup() override;
74
79 class Motion
80 {
81 public:
83 Motion() = default;
84
86 Motion(const control::SpaceInformation *si) : state(si->allocState()), control(si->allocControl())
87 {
88 }
89
91 virtual ~Motion() = default;
92
94 virtual base::State *getState() const
95 {
96 return state;
97 }
98
100 virtual Motion *getParent() const
101 {
102 return parent;
103 }
104
107
110
112 Motion *parent{nullptr};
113
115 unsigned numChildren_{0};
116
118 bool inactive_{false};
119
121 std::vector<base::State *> *solutionPair{nullptr};
122
125 };
126
133
138 void getPlannerData(base::PlannerData &data) const override;
139
141 void clear() override;
142
153 void setSelectionRadius(double selectionRadius)
154 {
155 if (selectionRadius < 0)
156 throw Exception("Selection radius must be positive");
157 selectionRadius_ = selectionRadius;
158 }
159
164 double getSelectionRadius() const
165 {
166 return selectionRadius_;
167 }
168
180 void setPruningRadius(double pruningRadius)
181 {
182 if (pruningRadius < 0)
183 throw ompl::Exception("Pruning radius must be non-negative");
184 pruningRadius_ = pruningRadius;
185 }
186
191 double getPruningRadius() const
192 {
193 return pruningRadius_;
194 }
195
197 template <template <typename T> class NN>
199 {
200 if (nn_ && nn_->size() != 0)
201 OMPL_WARN("Calling setNearestNeighbors will clear all states.");
202 clear();
203 nn_ = std::make_shared<NN<Motion *>>();
204 witnesses_ = std::make_shared<NN<Motion *>>();
205 setup();
206 }
207
213 void setTm(double tM)
214 {
215 if (tM <= 0)
216 throw Exception("Maximum flow time per propagation step must be greater than 0");
218 {
219 if (tM < flowStepDuration_)
220 throw Exception(
221 "Maximum flow time per propagation step must be greater than or equal to the length of time for each flow "
222 "integration step (flowStepDuration_)");
223 }
224 tM_ = tM;
225 }
226
234 void setFlowStepDuration(double duration)
235 {
236 if (duration <= 0)
237 throw Exception("Flow step length must be greater than 0");
238 if (!tM_)
239 {
240 if (tM_ < duration)
241 throw Exception(
242 "Flow step length must be less than or equal to the maximum flow time per propagation step (Tm)");
243 }
244 flowStepDuration_ = duration;
245 }
246
251 void setJumpSet(std::function<bool(Motion *)> jumpSet)
252 {
253 jumpSet_ = jumpSet;
254 }
255
260 void setFlowSet(std::function<bool(Motion *)> flowSet)
261 {
262 flowSet_ = flowSet;
263 }
264
269 void setUnsafeSet(std::function<bool(Motion *)> unsafeSet)
270 {
271 unsafeSet_ = unsafeSet;
272 }
273
278 void setDistanceFunction(std::function<double(base::State *, base::State *)> function)
279 {
280 distanceFunc_ = function;
281 }
282
288 std::function<base::State *(base::State *curState, const control::Control *u, base::State *newState)>
289 function)
290 {
291 discreteSimulator_ = function;
292 }
293
298 void setContinuousSimulator(std::function<base::State *(const control::Control *u, base::State *curState,
299 double tFlowMax, base::State *newState)>
300 function)
301 {
302 continuousSimulator_ = function;
303 }
304
306 std::function<ompl::base::State *(const control::Control *control, ompl::base::State *x_cur, double tFlow,
307 ompl::base::State *new_state)>
309 [this](const control::Control *control, base::State *x_cur, double tFlow, base::State *new_state)
310 {
311 siC_->getStatePropagator()->propagate(x_cur, control, tFlow, new_state);
312 return new_state;
313 };
314
320 void setCollisionChecker(std::function<bool(Motion *motion, std::function<bool(Motion *motion)> obstacleSet,
321 base::State *newState, double *collisionTime)>
322 function)
323 {
324 collisionChecker_ = function;
325 }
326
332 void setBatchSize(int batchSize)
333 {
334 if (batchSize < 1)
335 throw Exception("Batch size must be greater than 0");
336 batchSize_ = batchSize;
337 }
338
340 {
342 throw Exception("Jump map not set");
344 throw Exception("Flow map not set");
345 if (!flowSet_)
346 throw Exception("Flow set not set");
347 if (!jumpSet_)
348 throw Exception("Jump set not set");
349 if (!unsafeSet_)
350 throw Exception("Unsafe set not set");
351 if (tM_ < 0.0)
352 throw Exception("Max flow propagation time (Tm) not set");
353 if (pruningRadius_ < 0)
354 throw Exception("Pruning radius (pruningRadius_) not set");
355 if (selectionRadius_ < 0)
356 throw Exception("Selection radius (selectionRadius_) not set");
357 }
358
359 protected:
360 const static ompl::control::Control *getFlowControl(const ompl::control::Control *control)
361 {
363 }
364
365 const static ompl::control::Control *getJumpControl(const ompl::control::Control *control)
366 {
367 return control->as<CompoundControl>()->as<ompl::control::Control>(1);
368 }
369
371 class Witness : public Motion
372 {
373 public:
375 Witness() = default;
376
379 {
380 }
381
386 base::State *getState() const override
387 {
388 return rep_->state;
389 }
390
395 Motion *getParent() const override
396 {
397 return rep_->parent;
398 }
399
404 void linkRep(Motion *lRep)
405 {
406 rep_ = lRep;
407 }
408
410 Motion *rep_{nullptr};
411 };
412
415
417 void initTree(void);
418
423 void randomSample(Motion *randomMotion);
424
426 std::shared_ptr<NearestNeighbors<Motion *>> nn_;
427
432
445 std::function<bool(Motion *motion, std::function<bool(Motion *motion)> obstacleSet, base::State *newState,
446 double *collisionTime)>
447 collisionChecker_ = [this](Motion *motion, std::function<bool(Motion *motion)> obstacleSet,
448 base::State *newState, double *collisionTime) -> bool
449 {
450 if (obstacleSet(motion))
451 {
452 si_->copyState(newState, motion->solutionPair->back());
453 *collisionTime =
455 motion->solutionPair->resize(motion->solutionPair->size() - 1);
456 return true;
457 }
458 return false;
459 };
460
462 control::DirectedControlSamplerPtr controlSampler_;
463
466
473 std::function<double(base::State *state1, base::State *state2)> distanceFunc_ =
474 [this](base::State *state1, base::State *state2) -> double { return si_->distance(state1, state2); };
475
477 double tM_{-1.};
478
480 double minStepLength = 1e-06;
481
484
492 std::function<base::State *(base::State *curState, const control::Control *u, base::State *newState)>
494
500 std::function<bool(Motion *motion)> jumpSet_;
501
507 std::function<bool(Motion *motion)> flowSet_;
508
514 std::function<bool(Motion *motion)> unsafeSet_;
515
517 base::OptimizationObjectivePtr opt_;
518
520 std::function<base::Cost(Motion *motion)> costFunc_;
521
530 std::function<base::State *(const control::Control *u, base::State *curState, double tFlowMax,
531 base::State *newState)>
533
540
545 Motion *selectNode(Motion *sample);
546
552
557 std::vector<Motion *> extend(Motion *m);
558
560 void freeMemory();
561
563 base::StateSamplerPtr sampler_;
564
566 std::shared_ptr<NearestNeighbors<Motion *>> witnesses_;
567
569 double selectionRadius_{-1.};
570
572 double pruningRadius_{-1.};
573
576
578 double dist_;
579
581 std::vector<Motion *> prevSolution_;
582
584 base::Cost prevSolutionCost_{std::numeric_limits<double>::quiet_NaN()};
585
588 };
589 } // namespace control
590} // namespace ompl
591
592#endif
The exception type for ompl.
Definition Exception.h:47
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition Cost.h:48
static double getStateTime(const ompl::base::State *state)
The time value of the given state.
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique,...
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
Base class for a planner.
Definition Planner.h:216
T * as()
Cast this instance to a desired type.
Definition Planner.h:230
SpaceInformationPtr si_
The space information for which planning is done.
Definition Planner.h:401
Definition of an abstract state.
Definition State.h:50
Definition of a compound control.
Definition Control.h:85
Definition of an abstract control.
Definition Control.h:48
Representation of a motion.
Definition HySST.h:80
virtual ~Motion()=default
Destructor.
base::Cost accCost_
The total cost accumulated from the root to this vertex.
Definition HySST.h:106
virtual base::State * getState() const
Get the state contained by the motion.
Definition HySST.h:94
unsigned numChildren_
Number of children. Starting with 0.
Definition HySST.h:115
std::vector< base::State * > * solutionPair
The integration steps defining the edge of the motion, between the parent and child vertices.
Definition HySST.h:121
virtual Motion * getParent() const
Get the parent motion in the tree.
Definition HySST.h:100
Motion * parent
The parent motion in the exploration tree.
Definition HySST.h:112
bool inactive_
If inactive, this node is not considered for selection.
Definition HySST.h:118
Motion()=default
Default constructor.
base::State * state
The state contained by the motion.
Definition HySST.h:109
Motion(const control::SpaceInformation *si)
Constructor that allocates memory for the state.
Definition HySST.h:86
Representation of a witness vertex in the search tree.
Definition HySST.h:372
Motion * getParent() const override
Get the state contained by the parent motion of the representative motion.
Definition HySST.h:395
Witness(const control::SpaceInformation *si)
Constructor that allocates memory for the state.
Definition HySST.h:378
Witness()=default
Default Constructor.
Motion * rep_
The node in the tree that is within the pruning radius.
Definition HySST.h:410
base::State * getState() const override
Get the state contained by the representative motion.
Definition HySST.h:386
void linkRep(Motion *lRep)
Set the representative of the witness.
Definition HySST.h:404
void setUnsafeSet(std::function< bool(Motion *)> unsafeSet)
Define the unsafe set.
Definition HySST.h:269
int batchSize_
The number of solutions allowed until the most optimal solution is returned.
Definition HySST.h:587
double flowStepDuration_
The flow time for a given integration step, within a flow propagation step. Must be set by user.
Definition HySST.h:483
std::shared_ptr< NearestNeighbors< Motion * > > nn_
A nearest-neighbors datastructure containing the tree of motions.
Definition HySST.h:426
HySST(const control::SpaceInformationPtr &si)
Constructor.
Definition HySST.cpp:52
void setFlowStepDuration(double duration)
Set the time allocated to a single continuous simulator call, within the full period of a continuous ...
Definition HySST.h:234
control::SpaceInformation * siC_
The base::SpaceInformation cast as control::SpaceInformation, for convenience.
Definition HySST.h:465
void initTree(void)
Runs the initial setup tasks for the tree.
void clear() override
Clear all allocated memory.
Definition HySST.cpp:104
std::function< base::State *(base::State *curState, const control::Control *u, base::State *newState)> discreteSimulator_
Simulator for propagation under jump regime.
Definition HySST.h:493
std::function< ompl::base::State *(const control::Control *control, ompl::base::State *x_cur, double tFlow, ompl::base::State *new_state)> continuousSimulator
Simulates the dynamics of the system.
Definition HySST.h:308
std::function< bool(Motion *motion, std::function< bool(Motion *motion)> obstacleSet, base::State *newState, double *collisionTime)> collisionChecker_
Collision checker. Default is point-by-point collision checking using the jump set.
Definition HySST.h:447
Motion * selectNode(Motion *sample)
Finds the best node in the tree withing the selection radius around a random sample.
Definition HySST.cpp:144
void setBatchSize(int batchSize)
Set solution batch size.
Definition HySST.h:332
double pruningRadius_
The radius for determining the size of the pruning region. Delta_bn.
Definition HySST.h:572
void checkMandatoryParametersSet(void) const
Check if all required parameters have been set.
Definition HySST.h:339
void setJumpSet(std::function< bool(Motion *)> jumpSet)
Define the jump set.
Definition HySST.h:251
std::function< double(base::State *state1, base::State *state2)> distanceFunc_
Compute distance between states, default is Euclidean distance.
Definition HySST.h:473
double getSelectionRadius() const
Get the selection radius the planner is using.
Definition HySST.h:164
void freeMemory()
Free the memory allocated by this planner.
Definition HySST.cpp:117
void setup() override
Set the problem instance to solve.
Definition HySST.cpp:65
double getPruningRadius() const
Get the pruning radius the planner is using.
Definition HySST.h:191
base::OptimizationObjectivePtr opt_
The optimization objective. Default is a shortest path objective.
Definition HySST.h:517
std::function< base::State *(const control::Control *u, base::State *curState, double tFlowMax, base::State *newState)> continuousSimulator_
Simulator for propagation under flow regime.
Definition HySST.h:532
void setFlowSet(std::function< bool(Motion *)> flowSet)
Define the flow set.
Definition HySST.h:260
void setTm(double tM)
Set the maximum time allocated to a full continuous simulator step.
Definition HySST.h:213
std::shared_ptr< NearestNeighbors< Motion * > > witnesses_
A nearest-neighbors datastructure containing the tree of witness motions.
Definition HySST.h:566
void setCollisionChecker(std::function< bool(Motion *motion, std::function< bool(Motion *motion)> obstacleSet, base::State *newState, double *collisionTime)> function)
Define the collision checker.
Definition HySST.h:320
double tM_
The maximum flow time for a given flow propagation step. Must be set by the user.
Definition HySST.h:477
~HySST() override
Destructor.
Definition HySST.cpp:61
void setContinuousSimulator(std::function< base::State *(const control::Control *u, base::State *curState, double tFlowMax, base::State *newState)> function)
Define the continuous dynamics simulator.
Definition HySST.h:298
void setSelectionRadius(double selectionRadius)
Set the radius for selecting nodes relative to random sample.
Definition HySST.h:153
std::function< bool(Motion *motion)> flowSet_
Function that returns true if a motion intersects with the flow set, and false if not.
Definition HySST.h:507
void getPlannerData(base::PlannerData &data) const override
Get the PlannerData object associated with this planner.
Definition HySST.cpp:562
void setPruningRadius(double pruningRadius)
Set the radius for pruning nodes.
Definition HySST.h:180
std::function< base::Cost(Motion *motion)> costFunc_
Calculate the cost of a motion. Default is using optimization objective.
Definition HySST.h:520
base::Cost prevSolutionCost_
The best solution cost we have found so far.
Definition HySST.h:584
Witness * findClosestWitness(Motion *node)
Find the closest witness node to a newly generated potential node.
Definition HySST.cpp:177
std::function< bool(Motion *motion)> unsafeSet_
Function that returns true if a motion intersects with the unsafe set, and false if not.
Definition HySST.h:514
std::function< bool(Motion *motion)> jumpSet_
Function that returns true if a motion intersects with the jump set, and false if not.
Definition HySST.h:500
double dist_
Minimum distance from goal to final vertex of generated trajectories.
Definition HySST.h:578
base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc) override
Main solve function.
Definition HySST.cpp:323
std::vector< Motion * > prevSolution_
The best solution (with best cost) we have found so far.
Definition HySST.h:581
double minStepLength
The minimum step length for a given flow propagation step. Default value is 1e-6.
Definition HySST.h:480
void setDistanceFunction(std::function< double(base::State *, base::State *)> function)
Define the distance measurement function.
Definition HySST.h:278
double selectionRadius_
The radius for determining the node selected for extension. Delta_s.
Definition HySST.h:569
std::vector< Motion * > extend(Motion *m)
Randomly propagate a new edge.
Definition HySST.cpp:195
base::StateSamplerPtr sampler_
State sampler.
Definition HySST.h:563
void setDiscreteSimulator(std::function< base::State *(base::State *curState, const control::Control *u, base::State *newState)> function)
Define the discrete dynamics simulator.
Definition HySST.h:287
control::DirectedControlSamplerPtr controlSampler_
Control Sampler.
Definition HySST.h:462
void setNearestNeighbors()
Set a different nearest neighbors datastructure.
Definition HySST.h:198
Motion * lastGoalMotion_
The most recent goal motion. Used for PlannerData computation.
Definition HySST.h:414
RNG rng_
The random number generator.
Definition HySST.h:575
base::PlannerStatus constructSolution(Motion *lastMotion)
Construct the path, starting at the last edge.
Definition HySST.cpp:495
void randomSample(Motion *randomMotion)
Sample the random motion.
Definition HySST.cpp:318
Space information containing necessary information for planning with controls. setup() needs to be ca...
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition Console.h:66
This namespace contains sampling based planning routines used by planning under differential constrai...
Definition Control.h:45
Main namespace. Contains everything in this library.
A class to store the exit status of Planner::solve().