diff --git a/src/rrt/2dplane/PlaneStateSpace.hpp b/src/rrt/2dplane/PlaneStateSpace.hpp index 27c07aa..13aebc8 100644 --- a/src/rrt/2dplane/PlaneStateSpace.hpp +++ b/src/rrt/2dplane/PlaneStateSpace.hpp @@ -3,6 +3,12 @@ #include #include +#include +#include +#include + +using namespace std; + namespace RRT { /** @@ -18,6 +24,31 @@ class PlaneStateSpace : public StateSpace { return POINT_CLASS(drand48() * width(), drand48() * height()); } + /** + * Generates a point based on a goal bias and a goal state + * + * @params goalState - Point that we want to extend towards + * @params goalBias - Changes likelihood of choosing a point near goal state + * @returns POINT_CLASS - random point based on goal bias and goal state + */ + POINT_CLASS randomBiasState(const POINT_CLASS& goalState, float goalBias) const { + // Generates random value based on goalBias + float logitX = logit(goalBias / 3 + .67, (float) rand() / RAND_MAX); + float logitY = logit(goalBias / 3 + .67, (float) rand() / RAND_MAX); + float offsetY = 0; + float offsetX = 0; + + // Scale X value based on distance from border + offsetX = std::max(goalState.x(), width() - goalState.x()) * logitX; + + // Scale Y value based on distance from border + offsetY = std::max(goalState.y(), height() - goalState.y()) * logitY; + + int randX = goalState.x() + offsetX; + int randY = goalState.y() + offsetY; + return POINT_CLASS(randX, randY); + } + POINT_CLASS intermediateState(const POINT_CLASS& source, const POINT_CLASS& target, float stepSize) const { @@ -41,6 +72,17 @@ class PlaneStateSpace : public StateSpace { pt.y() < height(); } + /** + * Uses the logit function to generate a random value based on a goal bias + * + * @params goalBias - increases / decreases distance from 0 + * @params num - value to be inputted into logit function + * @returns float - output of logit function scaled based on goal bias + */ + float logit(const float goalBias, const float num) const{ + return (1 - goalBias) * log(num/(1 - num)); + } + float width() const { return _width; } float height() const { return _height; } diff --git a/src/rrt/StateSpace.hpp b/src/rrt/StateSpace.hpp index 1c627d5..e78c5fe 100644 --- a/src/rrt/StateSpace.hpp +++ b/src/rrt/StateSpace.hpp @@ -22,6 +22,14 @@ class StateSpace { */ virtual T randomState() const = 0; + /** + * Generates a point within the bounds of the state space + * based on the goalBias + * + * @return A biased state + */ + virtual T randomBiasState(const T& goalState, float goalBias) const = 0; + /** * Finds a state in the direction of @target from @source.state(). * This new state will potentially be added to the tree. No need to do diff --git a/src/rrt/Tree.hpp b/src/rrt/Tree.hpp index f807d42..321e7a1 100644 --- a/src/rrt/Tree.hpp +++ b/src/rrt/Tree.hpp @@ -235,9 +235,9 @@ class Tree { * This is called at each iteration of the run() method. */ Node* grow() { - // extend towards goal, waypoint, or random state depending on the - // biases - // and a random number + // extend towards goal, waypoint, or random state depending on the + // biases + // and a random number float r = rand() / (float)RAND_MAX; // r is between 0 and one since we normalize it @@ -339,7 +339,6 @@ class Tree { nodes.push_back(node); node = node->parent(); } - // pass them one-by-one to the callback, reversing the order so // that the callback is called with the start point first and the // dest point last