Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

randomBiasState #70

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/rrt/2dplane/PlaneStateSpace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <Eigen/Dense>
#include <rrt/StateSpace.hpp>

#include <cmath>
#include <algorithm>
#include <iostream>

using namespace std;

namespace RRT {

/**
Expand All @@ -18,6 +24,31 @@ class PlaneStateSpace : public StateSpace<POINT_CLASS> {
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this 0.67 value guess and check? Where did it come from?

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 {
Expand All @@ -41,6 +72,17 @@ class PlaneStateSpace : public StateSpace<POINT_CLASS> {
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; }

Expand Down
8 changes: 8 additions & 0 deletions src/rrt/StateSpace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/rrt/Tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ class Tree {
* This is called at each iteration of the run() method.
*/
Node<T>* 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
Expand Down Expand Up @@ -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
Expand Down