git commit -m "first commit"
This commit is contained in:
1121
navigations/sbpl/src/planners/ANAplanner.cpp
Executable file
1121
navigations/sbpl/src/planners/ANAplanner.cpp
Executable file
File diff suppressed because it is too large
Load Diff
1470
navigations/sbpl/src/planners/adplanner.cpp
Executable file
1470
navigations/sbpl/src/planners/adplanner.cpp
Executable file
File diff suppressed because it is too large
Load Diff
1286
navigations/sbpl/src/planners/araplanner.cpp
Executable file
1286
navigations/sbpl/src/planners/araplanner.cpp
Executable file
File diff suppressed because it is too large
Load Diff
671
navigations/sbpl/src/planners/lazyARA.cpp
Executable file
671
navigations/sbpl/src/planners/lazyARA.cpp
Executable file
@@ -0,0 +1,671 @@
|
||||
/*
|
||||
* Copyright (c) 2013, Mike Phillips and Maxim Likhachev
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the Carnegie Mellon University nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <sbpl/planners/lazyARA.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
LazyARAPlanner::LazyARAPlanner(
|
||||
DiscreteSpaceInformation* environment,
|
||||
bool bSearchForward)
|
||||
:
|
||||
params(0.0)
|
||||
{
|
||||
bforwardsearch = bSearchForward;
|
||||
environment_ = environment;
|
||||
replan_number = 0;
|
||||
|
||||
goal_state_id = -1;
|
||||
start_state_id = -1;
|
||||
}
|
||||
|
||||
LazyARAPlanner::~LazyARAPlanner()
|
||||
{
|
||||
freeMemory();
|
||||
}
|
||||
|
||||
void LazyARAPlanner::freeMemory()
|
||||
{
|
||||
heap.makeemptyheap();
|
||||
incons.clear();
|
||||
stats.clear();
|
||||
for (unsigned int i = 0; i < states.size(); i++) {
|
||||
if (states[i]) {
|
||||
delete states[i];
|
||||
}
|
||||
}
|
||||
states.clear();
|
||||
}
|
||||
|
||||
LazyARAState* LazyARAPlanner::GetState(int id)
|
||||
{
|
||||
// if this stateID is out of bounds of our state vector then grow the list
|
||||
if (id >= int(states.size())) {
|
||||
for (int i = states.size(); i <= id; i++) {
|
||||
states.push_back(NULL);
|
||||
}
|
||||
}
|
||||
// if we have never seen this state then create one
|
||||
if (states[id] == NULL) {
|
||||
states[id] = new LazyARAState();
|
||||
states[id]->id = id;
|
||||
states[id]->replan_number = -1;
|
||||
}
|
||||
// initialize the state if it hasn't been for this call to replan
|
||||
LazyARAState* s = states[id];
|
||||
if (s->replan_number != replan_number) {
|
||||
s->g = INFINITECOST;
|
||||
s->v = INFINITECOST;
|
||||
s->iteration_closed = -1;
|
||||
s->replan_number = replan_number;
|
||||
s->best_parent = NULL;
|
||||
s->expanded_best_parent = NULL;
|
||||
s->heapindex = 0;
|
||||
s->in_incons = false;
|
||||
s->isTrueCost = true;
|
||||
// clear the lazy list
|
||||
while (!s->lazyList.empty()) {
|
||||
s->lazyList.pop();
|
||||
}
|
||||
|
||||
//compute heuristics
|
||||
if (bforwardsearch) {
|
||||
s->h = environment_->GetGoalHeuristic(s->id);
|
||||
}
|
||||
else {
|
||||
s->h = environment_->GetStartHeuristic(s->id);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void LazyARAPlanner::ExpandState(LazyARAState* parent)
|
||||
{
|
||||
SBPL_DEBUG("expand %d", parent->id);
|
||||
vector<int> children;
|
||||
vector<int> costs;
|
||||
vector<bool> isTrueCost;
|
||||
|
||||
if (bforwardsearch) {
|
||||
environment_->GetLazySuccs(parent->id, &children, &costs, &isTrueCost);
|
||||
}
|
||||
else
|
||||
environment_->GetLazyPreds(parent->id, &children, &costs, &isTrueCost);
|
||||
|
||||
// iterate through children of the parent
|
||||
for (int i = 0; i < (int)children.size(); i++) {
|
||||
LazyARAState* child = GetState(children[i]);
|
||||
insertLazyList(child, parent, costs[i], isTrueCost[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// assumptions:
|
||||
// state is at the front of the open list
|
||||
// it's minimum f-value is an underestimate (the edge cost from the parent is a guess and needs to be evaluated properly)
|
||||
// it hasn't been expanded yet this iteration
|
||||
void LazyARAPlanner::EvaluateState(LazyARAState* state)
|
||||
{
|
||||
SBPL_DEBUG("evaluate %d (from %d)", state->id, state->best_parent->id);
|
||||
LazyARAState* parent = state->best_parent;
|
||||
|
||||
getNextLazyElement(state);
|
||||
int trueCost;
|
||||
if (bforwardsearch) {
|
||||
trueCost = environment_->GetTrueCost(parent->id, state->id);
|
||||
}
|
||||
else {
|
||||
trueCost = environment_->GetTrueCost(state->id, parent->id);
|
||||
}
|
||||
// if the evaluated true cost is valid (positive), insert it into the lazy list
|
||||
if (trueCost > 0) {
|
||||
SBPL_DEBUG(" edge is valid with cost %d added to parent->v=%d", trueCost, parent->v);
|
||||
insertLazyList(state, parent, trueCost, true);
|
||||
}
|
||||
else{
|
||||
SBPL_DEBUG(" edge not valid");
|
||||
}
|
||||
}
|
||||
|
||||
// this should only be used with EvaluateState since it is assuming state hasn't
|
||||
// been expanded yet (only evaluated)
|
||||
void LazyARAPlanner::getNextLazyElement(LazyARAState* state)
|
||||
{
|
||||
if (state->lazyList.empty()) {
|
||||
state->g = INFINITECOST;
|
||||
state->best_parent = NULL;
|
||||
state->isTrueCost = true;
|
||||
return;
|
||||
}
|
||||
LazyListElement elem = state->lazyList.top();
|
||||
state->lazyList.pop();
|
||||
state->g = elem.parent->v + elem.edgeCost;
|
||||
state->best_parent = elem.parent;
|
||||
state->isTrueCost = elem.isTrueCost;
|
||||
// the new value is cheapest and if the value is also true then we want to
|
||||
// throw out all the other options
|
||||
if (state->isTrueCost) {
|
||||
while (!state->lazyList.empty()) {
|
||||
state->lazyList.pop();
|
||||
}
|
||||
}
|
||||
putStateInHeap(state);
|
||||
}
|
||||
|
||||
void LazyARAPlanner::insertLazyList(
|
||||
LazyARAState* state,
|
||||
LazyARAState* parent,
|
||||
int edgeCost,
|
||||
bool isTrueCost)
|
||||
{
|
||||
SBPL_DEBUG("insertLazyList state->id=%d parent->id=%d state->g=%d parent->v=%d edgeCost=%d isTrueCost=%d\n", state->id, parent->id, state->g, parent->v, edgeCost, isTrueCost);
|
||||
if (state->v <= parent->v + edgeCost) {
|
||||
return;
|
||||
}
|
||||
else if (state->g <= parent->v + edgeCost){
|
||||
//if the best g-value we have is true and better, then the value we had dominates this one and we don't need it
|
||||
if (state->isTrueCost) {
|
||||
return;
|
||||
}
|
||||
//insert this guy into the lazy list
|
||||
LazyListElement elem(parent, edgeCost, isTrueCost);
|
||||
state->lazyList.push(elem);
|
||||
}
|
||||
else {
|
||||
// the new guy is the cheapest so far
|
||||
// should we save what was the previous best?
|
||||
if (!isTrueCost && // the better guy's cost is not for sure
|
||||
// state->g < INFINITECOST && // we actually have a previous best (we actually don't need this line because of the next one)
|
||||
state->g < state->v) // we're not saving something we already expanded (and is stored in v and expanded_best_parent)
|
||||
{
|
||||
//we save it by putting it in the lazy list
|
||||
LazyListElement elem(state->best_parent, state->g - state->best_parent->v, state->isTrueCost);
|
||||
state->lazyList.push(elem);
|
||||
}
|
||||
|
||||
// the new guy is the cheapest
|
||||
state->g = parent->v + edgeCost;
|
||||
state->best_parent = parent;
|
||||
state->isTrueCost = isTrueCost;
|
||||
|
||||
// the new value is cheapest and if the value is also true then we want
|
||||
// to throw out all the other options
|
||||
if (isTrueCost) {
|
||||
while (!state->lazyList.empty()) {
|
||||
state->lazyList.pop();
|
||||
}
|
||||
}
|
||||
|
||||
// this function puts the state into the heap (or updates the position)
|
||||
// if we haven't expanded. if we have expanded, it will put the state in
|
||||
// the incons list (if we haven't already)
|
||||
putStateInHeap(state);
|
||||
}
|
||||
}
|
||||
|
||||
void LazyARAPlanner::putStateInHeap(LazyARAState* state)
|
||||
{
|
||||
// we only allow one expansion per search iteration
|
||||
// so insert into heap if not closed yet
|
||||
if (state->iteration_closed != search_iteration) {
|
||||
SBPL_DEBUG("put state %d in open with state->g=%d and state->isTrueCost=%d\n", state->id, state->g, state->isTrueCost);
|
||||
CKey key;
|
||||
key.key[0] = state->g + int(eps * state->h);
|
||||
// if the state is already in the heap, just update its priority
|
||||
if (state->heapindex != 0) {
|
||||
heap.updateheap(state, key);
|
||||
}
|
||||
else { // otherwise add it to the heap
|
||||
heap.insertheap(state, key);
|
||||
}
|
||||
}
|
||||
// if the state has already been expanded once for this iteration then add
|
||||
// it to the incons list so we can keep track of states that we know we have
|
||||
// better costs for
|
||||
else if (!state->in_incons) {
|
||||
SBPL_DEBUG("put state %d in incons with state->g=%d and state->isTrueCost=%d\n", state->id, state->g, state->isTrueCost);
|
||||
incons.push_back(state);
|
||||
state->in_incons = true;
|
||||
}
|
||||
}
|
||||
|
||||
// returns 1 if the solution is found, 0 if the solution does not exist and 2 if
|
||||
// it ran out of time
|
||||
int LazyARAPlanner::ImprovePath()
|
||||
{
|
||||
// expand states until done
|
||||
int expands = 0;
|
||||
CKey min_key = heap.getminkeyheap();
|
||||
while (!heap.emptyheap() &&
|
||||
min_key.key[0] < INFINITECOST &&
|
||||
(goal_state->g > min_key.key[0] || !goal_state->isTrueCost) &&
|
||||
(goal_state->v > min_key.key[0]) &&
|
||||
!outOfTime())
|
||||
{
|
||||
// get the state
|
||||
LazyARAState* state = (LazyARAState*)heap.deleteminheap();
|
||||
|
||||
if (state->v == state->g) {
|
||||
std::stringstream ss("ERROR: consistent state is being esxpanded ");
|
||||
ss << "id=" << state->id;
|
||||
ss << "v=" << state->v;
|
||||
ss << "g=" << state->g;
|
||||
ss << "isTrueCost=" << state->isTrueCost;
|
||||
ss << "lazyListSize=" << state->lazyList.size();
|
||||
throw SBPL_Exception(ss.str());
|
||||
}
|
||||
|
||||
if (state->isTrueCost) {
|
||||
// mark the state as expanded
|
||||
state->v = state->g;
|
||||
state->expanded_best_parent = state->best_parent;
|
||||
state->iteration_closed = search_iteration;
|
||||
// expand the state
|
||||
expands++;
|
||||
ExpandState(state);
|
||||
if (expands % 100000 == 0) {
|
||||
SBPL_DEBUG("expands so far=%u", expands);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// otherwise the state needs to be evaluated for its true cost
|
||||
EvaluateState(state);
|
||||
}
|
||||
|
||||
// get the min key for the next iteration
|
||||
min_key = heap.getminkeyheap();
|
||||
}
|
||||
|
||||
search_expands += expands;
|
||||
|
||||
if (goal_state->v < goal_state->g) {
|
||||
goal_state->g = goal_state->v;
|
||||
goal_state->best_parent = goal_state->expanded_best_parent;
|
||||
}
|
||||
|
||||
if (goal_state->g == INFINITECOST && (heap.emptyheap() || min_key.key[0] >= INFINITECOST)) {
|
||||
return 0; // solution does not exists
|
||||
}
|
||||
if (!heap.emptyheap() && goal_state->g > min_key.key[0]) {
|
||||
return 2; // search exited because it ran out of time
|
||||
}
|
||||
SBPL_DEBUG("search exited with a solution for eps=%.3f", eps);
|
||||
if (goal_state->g < goal_state->v) {
|
||||
goal_state->expanded_best_parent = goal_state->best_parent;
|
||||
goal_state->v = goal_state->g;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
vector<int> LazyARAPlanner::GetSearchPath(int& solcost)
|
||||
{
|
||||
vector<int> SuccIDV;
|
||||
vector<int> CostV;
|
||||
vector<bool> isTrueCost;
|
||||
vector<int> wholePathIds;
|
||||
|
||||
LazyARAState* state = goal_state;
|
||||
LazyARAState* final_state = start_state;
|
||||
|
||||
// if the goal was not expanded but was generated (we don't have a bound on
|
||||
// the solution) pretend that it was expanded for path reconstruction and
|
||||
// revert the state afterward
|
||||
bool goal_expanded = true;
|
||||
if (goal_state->expanded_best_parent == NULL) {
|
||||
goal_expanded = false;
|
||||
goal_state->expanded_best_parent = goal_state->best_parent;
|
||||
goal_state->v = goal_state->g;
|
||||
}
|
||||
|
||||
wholePathIds.push_back(state->id);
|
||||
solcost = 0;
|
||||
|
||||
while (state->id != final_state->id) {
|
||||
if (state->expanded_best_parent == NULL) {
|
||||
SBPL_DEBUG("a state along the path has no parent!");
|
||||
break;
|
||||
}
|
||||
if (state->v == INFINITECOST) {
|
||||
SBPL_DEBUG("a state along the path has an infinite g-value!");
|
||||
SBPL_DEBUG("inf state = %d", state->id);
|
||||
break;
|
||||
}
|
||||
|
||||
if (bforwardsearch) {
|
||||
environment_->GetLazySuccs(state->expanded_best_parent->id, &SuccIDV, &CostV, &isTrueCost);
|
||||
}
|
||||
else {
|
||||
environment_->GetLazyPreds(state->expanded_best_parent->id, &SuccIDV, &CostV, &isTrueCost);
|
||||
}
|
||||
int actioncost = INFINITECOST;
|
||||
for (unsigned int i = 0; i < SuccIDV.size(); i++) {
|
||||
if (SuccIDV[i] == state->id && CostV[i] < actioncost) {
|
||||
actioncost = CostV[i];
|
||||
}
|
||||
}
|
||||
if (actioncost == INFINITECOST) {
|
||||
SBPL_WARN("WARNING: actioncost = %d", actioncost);
|
||||
}
|
||||
solcost += actioncost;
|
||||
|
||||
state = state->expanded_best_parent;
|
||||
wholePathIds.push_back(state->id);
|
||||
}
|
||||
|
||||
// if we pretended that the goal was expanded for path reconstruction then
|
||||
// revert the state now
|
||||
if (!goal_expanded) {
|
||||
goal_state->expanded_best_parent = NULL;
|
||||
goal_state->v = INFINITECOST;
|
||||
}
|
||||
|
||||
// if we searched forward then the path reconstruction worked backward from
|
||||
// the goal, so we have to reverse the path
|
||||
if (bforwardsearch) {
|
||||
// in place reverse
|
||||
for (unsigned int i = 0; i < wholePathIds.size() / 2; i++) {
|
||||
int other_idx = wholePathIds.size() - i - 1;
|
||||
int temp = wholePathIds[i];
|
||||
wholePathIds[i] = wholePathIds[other_idx];
|
||||
wholePathIds[other_idx] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return wholePathIds;
|
||||
}
|
||||
|
||||
bool LazyARAPlanner::outOfTime()
|
||||
{
|
||||
// if we are supposed to run until the first solution, then we are never out
|
||||
// of time
|
||||
if (params.return_first_solution) {
|
||||
return false;
|
||||
}
|
||||
double time_used = double(clock() - TimeStarted) / CLOCKS_PER_SEC;
|
||||
if (time_used >= params.max_time) {
|
||||
SBPL_DEBUG("out of max time");
|
||||
}
|
||||
if (use_repair_time && eps_satisfied != INFINITECOST && time_used >= params.repair_time) {
|
||||
SBPL_DEBUG("used all repair time...");
|
||||
}
|
||||
// we are out of time if:
|
||||
// we used up the max time limit OR
|
||||
// we found some solution and used up the minimum time limit
|
||||
return time_used >= params.max_time ||
|
||||
(use_repair_time && eps_satisfied != INFINITECOST && time_used >= params.repair_time);
|
||||
}
|
||||
|
||||
void LazyARAPlanner::initializeSearch()
|
||||
{
|
||||
// it's a new search, so increment replan_number and reset the
|
||||
// search_iteration
|
||||
replan_number++;
|
||||
search_iteration = 0;
|
||||
search_expands = 0;
|
||||
totalPlanTime = 0;
|
||||
totalExpands = 0;
|
||||
reconstructTime = 0;
|
||||
totalTime = 0;
|
||||
|
||||
// clear open list, incons list, and stats list
|
||||
heap.makeemptyheap();
|
||||
incons.clear();
|
||||
stats.clear();
|
||||
|
||||
// initialize epsilon variable
|
||||
eps = params.initial_eps;
|
||||
eps_satisfied = INFINITECOST;
|
||||
|
||||
// call get state to initialize the start and goal states
|
||||
if (bforwardsearch) {
|
||||
goal_state = GetState(goal_state_id);
|
||||
start_state = GetState(start_state_id);
|
||||
}
|
||||
else {
|
||||
start_state = GetState(goal_state_id);
|
||||
goal_state = GetState(start_state_id);
|
||||
}
|
||||
|
||||
// put start state in the heap
|
||||
start_state->g = 0;
|
||||
CKey key;
|
||||
key.key[0] = eps * start_state->h;
|
||||
heap.insertheap(start_state, key);
|
||||
|
||||
// ensure heuristics are up-to-date
|
||||
environment_->EnsureHeuristicsUpdated((bforwardsearch == true));
|
||||
}
|
||||
|
||||
bool LazyARAPlanner::Search(vector<int>& pathIds, int& PathCost)
|
||||
{
|
||||
CKey key;
|
||||
TimeStarted = clock();
|
||||
|
||||
initializeSearch();
|
||||
|
||||
// the main loop of ARA*
|
||||
while (eps_satisfied > params.final_eps && !outOfTime()) {
|
||||
|
||||
// run weighted A*
|
||||
clock_t before_time = clock();
|
||||
int before_expands = search_expands;
|
||||
// ImprovePath returns:
|
||||
// 1 if the solution is found
|
||||
// 0 if the solution does not exist
|
||||
// 2 if it ran out of time
|
||||
int ret = ImprovePath();
|
||||
if (ret == 1) {
|
||||
// solution found for this iteration
|
||||
eps_satisfied = eps;
|
||||
}
|
||||
int delta_expands = search_expands - before_expands;
|
||||
double delta_time = double(clock() - before_time) / CLOCKS_PER_SEC;
|
||||
|
||||
// print the bound, expands, and time for that iteration
|
||||
SBPL_DEBUG("bound=%f expands=%d cost=%d time=%.3f", eps_satisfied, delta_expands, goal_state->g, delta_time);
|
||||
|
||||
// update stats
|
||||
totalPlanTime += delta_time;
|
||||
totalExpands += delta_expands;
|
||||
PlannerStats tempStat;
|
||||
tempStat.eps = eps_satisfied;
|
||||
tempStat.expands = delta_expands;
|
||||
tempStat.time = delta_time;
|
||||
tempStat.cost = goal_state->g;
|
||||
stats.push_back(tempStat);
|
||||
|
||||
// no solution exists
|
||||
if (ret == 0) {
|
||||
SBPL_DEBUG("Solution does not exist");
|
||||
return false;
|
||||
}
|
||||
|
||||
// if we're just supposed to find the first solution or if we ran out of
|
||||
// time, we're done
|
||||
if (params.return_first_solution || ret == 2) {
|
||||
break;
|
||||
}
|
||||
|
||||
prepareNextSearchIteration();
|
||||
}
|
||||
|
||||
if (goal_state->g == INFINITECOST) {
|
||||
SBPL_DEBUG("could not find a solution (ran out of time)");
|
||||
return false;
|
||||
}
|
||||
if (eps_satisfied == INFINITECOST) {
|
||||
SBPL_DEBUG("WARNING: a solution was found but we don't have quality bound for it!");
|
||||
}
|
||||
|
||||
SBPL_DEBUG("solution found");
|
||||
clock_t before_reconstruct = clock();
|
||||
pathIds = GetSearchPath(PathCost);
|
||||
reconstructTime = double(clock() - before_reconstruct) / CLOCKS_PER_SEC;
|
||||
totalTime = totalPlanTime + reconstructTime;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void LazyARAPlanner::prepareNextSearchIteration()
|
||||
{
|
||||
//decrease epsilon
|
||||
eps -= params.dec_eps;
|
||||
if (eps < params.final_eps) {
|
||||
eps = params.final_eps;
|
||||
}
|
||||
|
||||
// dump the inconsistent states into the open list
|
||||
CKey key;
|
||||
while (!incons.empty()) {
|
||||
LazyARAState* s = incons.back();
|
||||
incons.pop_back();
|
||||
s->in_incons = false;
|
||||
key.key[0] = s->g + int(eps * s->h);
|
||||
heap.insertheap(s,key);
|
||||
}
|
||||
|
||||
// recompute priorities for states in OPEN and reorder it
|
||||
for (int i = 1; i <= heap.currentsize; ++i){
|
||||
LazyARAState* state = (LazyARAState*)heap.heap[i].heapstate;
|
||||
heap.heap[i].key.key[0] = state->g + int(eps * state->h);
|
||||
}
|
||||
heap.makeheap();
|
||||
|
||||
search_iteration++;
|
||||
}
|
||||
|
||||
//-----------------------------Interface function-------------------------------
|
||||
|
||||
int LazyARAPlanner::replan(
|
||||
double allocated_time_secs,
|
||||
std::vector<int>* solution_stateIDs_V)
|
||||
{
|
||||
int solcost = 0;
|
||||
return replan(allocated_time_secs, solution_stateIDs_V, &solcost);
|
||||
}
|
||||
|
||||
int LazyARAPlanner::replan(
|
||||
double allocated_time_sec,
|
||||
std::vector<int>* solution_stateIDs_V,
|
||||
int* solcost)
|
||||
{
|
||||
params.max_time = allocated_time_sec;
|
||||
return replan(solution_stateIDs_V, params, solcost);
|
||||
}
|
||||
|
||||
int LazyARAPlanner::replan(
|
||||
vector<int>* solution_stateIDs_V,
|
||||
ReplanParams p)
|
||||
{
|
||||
int solcost = 0;
|
||||
return replan(solution_stateIDs_V, p, &solcost);
|
||||
}
|
||||
|
||||
int LazyARAPlanner::replan(
|
||||
int start,
|
||||
int goal,
|
||||
vector<int>* solution_stateIDs_V,
|
||||
ReplanParams p,
|
||||
int* solcost)
|
||||
{
|
||||
set_start(start);
|
||||
set_goal(goal);
|
||||
return replan(solution_stateIDs_V, p, solcost);
|
||||
}
|
||||
|
||||
int LazyARAPlanner::replan(
|
||||
vector<int>* solution_stateIDs_V,
|
||||
ReplanParams p,
|
||||
int* solcost)
|
||||
{
|
||||
SBPL_DEBUG("planner: replan called");
|
||||
params = p;
|
||||
use_repair_time = params.repair_time >= 0;
|
||||
|
||||
if (goal_state_id < 0) {
|
||||
SBPL_ERROR("ERROR searching: no goal state set");
|
||||
return 0;
|
||||
}
|
||||
if (start_state_id < 0) {
|
||||
SBPL_ERROR("ERROR searching: no start state set");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// plan
|
||||
vector<int> pathIds;
|
||||
int PathCost = 0;
|
||||
bool solnFound = Search(pathIds, PathCost);
|
||||
SBPL_DEBUG("total expands=%d planning time=%.3f reconstruct path time=%.3f total time=%.3f solution cost=%d", totalExpands, totalPlanTime, reconstructTime, totalTime, goal_state->g);
|
||||
|
||||
// copy the solution
|
||||
*solution_stateIDs_V = pathIds;
|
||||
*solcost = PathCost;
|
||||
|
||||
start_state_id = -1;
|
||||
goal_state_id = -1;
|
||||
|
||||
return (int)solnFound;
|
||||
}
|
||||
|
||||
int LazyARAPlanner::set_goal(int id)
|
||||
{
|
||||
SBPL_DEBUG("planner: setting goal to %d", id);
|
||||
if (bforwardsearch) {
|
||||
goal_state_id = id;
|
||||
}
|
||||
else {
|
||||
start_state_id = id;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LazyARAPlanner::set_start(int id)
|
||||
{
|
||||
SBPL_DEBUG("planner: setting start to %d", id);
|
||||
if (bforwardsearch) {
|
||||
start_state_id = id;
|
||||
}
|
||||
else {
|
||||
goal_state_id = id;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void LazyARAPlanner::get_search_stats(vector<PlannerStats>* s)
|
||||
{
|
||||
s->clear();
|
||||
s->reserve(stats.size());
|
||||
for (unsigned int i = 0; i < stats.size(); i++) {
|
||||
s->push_back(stats[i]);
|
||||
}
|
||||
}
|
||||
|
||||
654
navigations/sbpl/src/planners/mhaplanner.cpp
Executable file
654
navigations/sbpl/src/planners/mhaplanner.cpp
Executable file
@@ -0,0 +1,654 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Maxim Likhachev
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the Carnegie Mellon University nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sbpl/planners/mhaplanner.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include <sbpl/utils/key.h>
|
||||
|
||||
static double GetTime()
|
||||
{
|
||||
return (double)clock() / (double)CLOCKS_PER_SEC;
|
||||
}
|
||||
|
||||
MHAPlanner::MHAPlanner(
|
||||
DiscreteSpaceInformation* environment,
|
||||
Heuristic* hanchor,
|
||||
Heuristic** heurs,
|
||||
int hcount)
|
||||
:
|
||||
SBPLPlanner(),
|
||||
// environment_(environment),
|
||||
m_hanchor(hanchor),
|
||||
m_heurs(heurs),
|
||||
m_hcount(hcount),
|
||||
m_params(0.0),
|
||||
m_initial_eps_mha(1.0),
|
||||
m_max_expansions(0),
|
||||
m_eps(1.0),
|
||||
m_eps_mha(1.0),
|
||||
m_eps_satisfied((double)INFINITECOST),
|
||||
m_num_expansions(0),
|
||||
m_elapsed(0.0),
|
||||
m_call_number(0), // uninitialized
|
||||
m_start_state(NULL),
|
||||
m_goal_state(NULL),
|
||||
m_search_states(),
|
||||
m_open(NULL)
|
||||
{
|
||||
environment_ = environment;
|
||||
|
||||
m_open = new CHeap[hcount + 1];
|
||||
|
||||
// Overwrite default members for ReplanParams to represent a single optimal
|
||||
// search
|
||||
m_params.initial_eps = 1.0;
|
||||
m_params.final_eps = 1.0;
|
||||
m_params.dec_eps = 0.2; // NOTE: same initial epsilon delta as ARA*
|
||||
m_params.return_first_solution = false;
|
||||
m_params.max_time = 0.0;
|
||||
m_params.repair_time = 0.0;
|
||||
|
||||
/// Four Modes:
|
||||
/// Search Until Solution Bounded
|
||||
/// Search Until Solution Unbounded
|
||||
/// Improve Solution Bounded
|
||||
/// Improve Solution Unbounded
|
||||
}
|
||||
|
||||
MHAPlanner::~MHAPlanner()
|
||||
{
|
||||
clear();
|
||||
|
||||
delete[] m_open;
|
||||
}
|
||||
|
||||
int MHAPlanner::set_start(int start_stateID)
|
||||
{
|
||||
m_start_state = get_state(start_stateID);
|
||||
if (!m_start_state) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int MHAPlanner::set_goal(int goal_stateID)
|
||||
{
|
||||
m_goal_state = get_state(goal_stateID);
|
||||
if (!m_goal_state) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int MHAPlanner::replan(
|
||||
double allocated_time_sec,
|
||||
std::vector<int>* solution_stateIDs_V)
|
||||
{
|
||||
int solcost;
|
||||
return replan(allocated_time_sec, solution_stateIDs_V, &solcost);
|
||||
}
|
||||
|
||||
int MHAPlanner::replan(
|
||||
double allocated_time_sec,
|
||||
std::vector<int>* solution_stateIDs_V,
|
||||
int* solcost)
|
||||
{
|
||||
ReplanParams params = m_params;
|
||||
params.max_time = allocated_time_sec;
|
||||
return replan(solution_stateIDs_V, params, solcost);
|
||||
}
|
||||
|
||||
int MHAPlanner::replan(
|
||||
std::vector<int>* solution_stateIDs_V,
|
||||
ReplanParams params)
|
||||
{
|
||||
int solcost;
|
||||
return replan(solution_stateIDs_V, params, &solcost);
|
||||
}
|
||||
|
||||
int MHAPlanner::replan(
|
||||
std::vector<int>* solution_stateIDs_V,
|
||||
ReplanParams params,
|
||||
int* solcost)
|
||||
{
|
||||
if (!check_params(params)) { // errors printed within
|
||||
return 0;
|
||||
}
|
||||
|
||||
m_params = params;
|
||||
|
||||
SBPL_INFO("Generic Search parameters:");
|
||||
SBPL_INFO(" Initial Epsilon: %0.3f", m_params.initial_eps);
|
||||
SBPL_INFO(" Final Epsilon: %0.3f", m_params.final_eps);
|
||||
SBPL_INFO(" Delta Epsilon: %0.3f", m_params.dec_eps);
|
||||
SBPL_INFO(" Return First Solution: %s", m_params.return_first_solution ? "true" : "false");
|
||||
SBPL_INFO(" Max Time: %0.3f", m_params.max_time);
|
||||
SBPL_INFO(" Repair Time: %0.3f", m_params.repair_time);
|
||||
SBPL_INFO("MHA Search parameters:");
|
||||
SBPL_INFO(" MHA Epsilon: %0.3f", m_initial_eps_mha);
|
||||
SBPL_INFO(" Max Expansions: %d", m_max_expansions);
|
||||
|
||||
environment_->EnsureHeuristicsUpdated(true); // TODO: support backwards search
|
||||
|
||||
// TODO: pick up from where last search left off and detect lazy
|
||||
// reinitializations
|
||||
reinit_search();
|
||||
|
||||
m_eps = m_params.initial_eps;
|
||||
m_eps_mha = m_initial_eps_mha;
|
||||
m_eps_satisfied = (double)INFINITECOST;
|
||||
|
||||
// reset time limits
|
||||
m_num_expansions = 0;
|
||||
m_elapsed = 0.0;
|
||||
|
||||
double start_time, end_time;
|
||||
|
||||
start_time = GetTime();
|
||||
|
||||
++m_call_number;
|
||||
reinit_state(m_goal_state);
|
||||
reinit_state(m_start_state);
|
||||
m_start_state->g = 0;
|
||||
|
||||
// insert start state into all heaps with key(start, i) as priority
|
||||
for (int hidx = 0; hidx < num_heuristics(); ++hidx) {
|
||||
CKey key;
|
||||
key.key[0] = compute_key(m_start_state, hidx);
|
||||
m_open[hidx].insertheap(&m_start_state->od[hidx].open_state, key);
|
||||
SBPL_DEBUG("Inserted start state %d into search %d with f = %d", m_start_state->state_id, hidx, key.key[0]);
|
||||
}
|
||||
|
||||
end_time = GetTime();
|
||||
m_elapsed += (end_time - start_time);
|
||||
|
||||
while (!m_open[0].emptyheap() && !time_limit_reached()) {
|
||||
start_time = GetTime();
|
||||
|
||||
// special case for mha* without additional heuristics
|
||||
if (num_heuristics() == 1) {
|
||||
if (m_goal_state->g <= get_minf(m_open[0])) {
|
||||
m_eps_satisfied = m_eps * m_eps_mha;
|
||||
extract_path(solution_stateIDs_V, solcost);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
MHASearchState* s = state_from_open_state(m_open[0].getminheap());
|
||||
expand(s, 0);
|
||||
}
|
||||
}
|
||||
|
||||
for (int hidx = 1; hidx < num_heuristics(); ++hidx) {
|
||||
if (m_open[0].emptyheap()) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!m_open[hidx].emptyheap() && get_minf(m_open[hidx]) <=
|
||||
m_eps_mha * get_minf(m_open[0]))
|
||||
{
|
||||
if (m_goal_state->g <= get_minf(m_open[hidx])) {
|
||||
m_eps_satisfied = m_eps * m_eps_mha;
|
||||
extract_path(solution_stateIDs_V, solcost);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
MHASearchState* s =
|
||||
state_from_open_state(m_open[hidx].getminheap());
|
||||
expand(s, hidx);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (m_goal_state->g <= get_minf(m_open[0])) {
|
||||
m_eps_satisfied = m_eps * m_eps_mha;
|
||||
extract_path(solution_stateIDs_V, solcost);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
MHASearchState* s =
|
||||
state_from_open_state(m_open[0].getminheap());
|
||||
expand(s, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
end_time = GetTime();
|
||||
m_elapsed += (end_time - start_time);
|
||||
}
|
||||
|
||||
if (m_open[0].emptyheap()) {
|
||||
SBPL_DEBUG("Anchor search exhausted");
|
||||
}
|
||||
if (time_limit_reached()) {
|
||||
SBPL_DEBUG("Time limit reached");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MHAPlanner::force_planning_from_scratch()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MHAPlanner::force_planning_from_scratch_and_free_memory()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MHAPlanner::costs_changed(StateChangeQuery const & stateChange)
|
||||
{
|
||||
}
|
||||
|
||||
void MHAPlanner::costs_changed()
|
||||
{
|
||||
}
|
||||
|
||||
int MHAPlanner::set_search_mode(bool bSearchUntilFirstSolution)
|
||||
{
|
||||
return m_params.return_first_solution = bSearchUntilFirstSolution;
|
||||
}
|
||||
|
||||
void MHAPlanner::set_initialsolution_eps(double eps)
|
||||
{
|
||||
m_params.initial_eps = eps;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_initial_eps()
|
||||
{
|
||||
return m_params.initial_eps;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_solution_eps() const
|
||||
{
|
||||
return m_eps_satisfied;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_final_epsilon()
|
||||
{
|
||||
return m_eps_satisfied;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_final_eps_planning_time()
|
||||
{
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_initial_eps_planning_time()
|
||||
{
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
int MHAPlanner::get_n_expands() const
|
||||
{
|
||||
return m_num_expansions;
|
||||
}
|
||||
|
||||
int MHAPlanner::get_n_expands_init_solution()
|
||||
{
|
||||
return m_num_expansions;
|
||||
}
|
||||
|
||||
void MHAPlanner::get_search_stats(std::vector<PlannerStats>* s)
|
||||
{
|
||||
}
|
||||
|
||||
void MHAPlanner::set_initial_mha_eps(double eps)
|
||||
{
|
||||
m_initial_eps_mha = eps;
|
||||
}
|
||||
|
||||
void MHAPlanner::set_final_eps(double eps)
|
||||
{
|
||||
m_params.final_eps = eps;
|
||||
}
|
||||
|
||||
void MHAPlanner::set_dec_eps(double eps)
|
||||
{
|
||||
m_params.dec_eps = eps;
|
||||
}
|
||||
|
||||
void MHAPlanner::set_max_expansions(int expansion_count)
|
||||
{
|
||||
m_max_expansions = expansion_count;
|
||||
}
|
||||
|
||||
void MHAPlanner::set_max_time(double max_time)
|
||||
{
|
||||
m_params.max_time = max_time;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_initial_mha_eps() const
|
||||
{
|
||||
return m_initial_eps_mha;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_final_eps() const
|
||||
{
|
||||
return m_params.final_eps;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_dec_eps() const
|
||||
{
|
||||
return m_params.dec_eps;
|
||||
}
|
||||
|
||||
int MHAPlanner::get_max_expansions() const
|
||||
{
|
||||
return m_max_expansions;
|
||||
}
|
||||
|
||||
double MHAPlanner::get_max_time() const
|
||||
{
|
||||
return m_params.max_time;
|
||||
}
|
||||
|
||||
bool MHAPlanner::check_params(const ReplanParams& params)
|
||||
{
|
||||
if (params.initial_eps < 1.0) {
|
||||
SBPL_ERROR("Initial Epsilon must be greater than or equal to 1");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (params.final_eps > params.initial_eps) {
|
||||
SBPL_ERROR("Final Epsilon must be less than or equal to initial epsilon");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (params.dec_eps <= 0.0) {
|
||||
SBPL_ERROR("Delta epsilon must be strictly positive");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_initial_eps_mha < 1.0) {
|
||||
SBPL_ERROR("MHA Epsilon must be greater than or equal to 1");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (params.return_first_solution &&
|
||||
params.max_time <= 0.0 &&
|
||||
m_max_expansions <= 0)
|
||||
{
|
||||
SBPL_ERROR("Max Time or Max Expansions must be positive");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MHAPlanner::time_limit_reached() const
|
||||
{
|
||||
if (m_params.return_first_solution) {
|
||||
return false;
|
||||
}
|
||||
else if (m_params.max_time > 0.0 && m_elapsed >= m_params.max_time) {
|
||||
return true;
|
||||
}
|
||||
else if (m_max_expansions > 0 && m_num_expansions >= m_max_expansions) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
MHASearchState* MHAPlanner::get_state(int state_id)
|
||||
{
|
||||
assert(state_id >= 0 && state_id < environment_->StateID2IndexMapping.size());
|
||||
int* idxs = environment_->StateID2IndexMapping[state_id];
|
||||
if (idxs[MHAMDP_STATEID2IND] == -1) {
|
||||
// overallocate search state for appropriate heuristic information
|
||||
const size_t state_size =
|
||||
sizeof(MHASearchState) +
|
||||
sizeof(MHASearchState::HeapData) * (m_hcount);
|
||||
MHASearchState* s = (MHASearchState*)malloc(state_size);
|
||||
|
||||
const size_t mha_state_idx = m_search_states.size();
|
||||
init_state(s, mha_state_idx, state_id);
|
||||
|
||||
// map graph state to search state
|
||||
idxs[MHAMDP_STATEID2IND] = mha_state_idx;
|
||||
m_search_states.push_back(s);
|
||||
|
||||
return s;
|
||||
}
|
||||
else {
|
||||
int ssidx = idxs[MHAMDP_STATEID2IND];
|
||||
return m_search_states[ssidx];
|
||||
}
|
||||
}
|
||||
|
||||
void MHAPlanner::clear()
|
||||
{
|
||||
clear_open_lists();
|
||||
|
||||
// free states
|
||||
for (size_t i = 0; i < m_search_states.size(); ++i) {
|
||||
// unmap graph to search state
|
||||
const int state_id = m_search_states[i]->state_id;
|
||||
int* idxs = environment_->StateID2IndexMapping[state_id];
|
||||
idxs[MHAMDP_STATEID2IND] = -1;
|
||||
|
||||
// free search state
|
||||
free(m_search_states[i]);
|
||||
}
|
||||
|
||||
// empty state table
|
||||
m_search_states.clear();
|
||||
|
||||
m_start_state = NULL;
|
||||
m_goal_state = NULL;
|
||||
}
|
||||
|
||||
void MHAPlanner::init_state(
|
||||
MHASearchState* state,
|
||||
size_t mha_state_idx,
|
||||
int state_id)
|
||||
{
|
||||
state->call_number = 0; // not initialized for any iteration
|
||||
state->state_id = state_id;
|
||||
state->closed_in_anc = false;
|
||||
state->closed_in_add = false;
|
||||
for (int i = 0; i < num_heuristics(); ++i) {
|
||||
state->od[i].open_state.heapindex = 0;
|
||||
state->od[i].h = compute_heuristic(state->state_id, i);
|
||||
// hijack list element pointers to map back to mha search state
|
||||
assert(sizeof(state->od[i].open_state.listelem) >= sizeof(struct listelement*));
|
||||
reinterpret_cast<size_t&>(state->od[i].open_state.listelem[0]) = mha_state_idx;
|
||||
}
|
||||
}
|
||||
|
||||
void MHAPlanner::reinit_state(MHASearchState* state)
|
||||
{
|
||||
if (state->call_number != m_call_number) {
|
||||
state->call_number = m_call_number;
|
||||
state->g = INFINITECOST;
|
||||
state->bp = NULL;
|
||||
|
||||
state->closed_in_anc = false;
|
||||
state->closed_in_add = false;
|
||||
|
||||
for (int i = 0; i < num_heuristics(); ++i) {
|
||||
state->od[i].open_state.heapindex = 0;
|
||||
state->od[i].h = compute_heuristic(state->state_id, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MHAPlanner::reinit_search()
|
||||
{
|
||||
clear_open_lists();
|
||||
}
|
||||
|
||||
void MHAPlanner::clear_open_lists()
|
||||
{
|
||||
for (int i = 0; i < num_heuristics(); ++i) {
|
||||
m_open[i].makeemptyheap();
|
||||
}
|
||||
}
|
||||
|
||||
int MHAPlanner::compute_key(MHASearchState* state, int hidx)
|
||||
{
|
||||
return state->g + m_eps * state->od[hidx].h;
|
||||
}
|
||||
|
||||
void MHAPlanner::expand(MHASearchState* state, int hidx)
|
||||
{
|
||||
SBPL_DEBUG("Expanding state %d in search %d", state->state_id, hidx);
|
||||
|
||||
assert(!closed_in_add_search(state) || !closed_in_anc_search(state));
|
||||
|
||||
if (hidx == 0) {
|
||||
state->closed_in_anc = true;
|
||||
}
|
||||
else {
|
||||
state->closed_in_add = true;
|
||||
}
|
||||
++m_num_expansions;
|
||||
|
||||
// remove s from all open lists
|
||||
for (int temp_hidx = 0; temp_hidx < num_heuristics(); ++temp_hidx) {
|
||||
if (m_open[temp_hidx].inheap(&state->od[temp_hidx].open_state)) {
|
||||
m_open[temp_hidx].deleteheap(&state->od[temp_hidx].open_state);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> succ_ids;
|
||||
std::vector<int> costs;
|
||||
environment_->GetSuccs(state->state_id, &succ_ids, &costs);
|
||||
assert(succ_ids.size() == costs.size());
|
||||
|
||||
for (size_t sidx = 0; sidx < succ_ids.size(); ++sidx) {
|
||||
const int cost = costs[sidx];
|
||||
MHASearchState* succ_state = get_state(succ_ids[sidx]);
|
||||
reinit_state(succ_state);
|
||||
|
||||
SBPL_DEBUG(" Successor %d", succ_state->state_id);
|
||||
|
||||
int new_g = state->g + costs[sidx];
|
||||
if (new_g < succ_state->g) {
|
||||
succ_state->g = new_g;
|
||||
succ_state->bp = state;
|
||||
if (!closed_in_anc_search(succ_state)) {
|
||||
const int fanchor = compute_key(succ_state, 0);
|
||||
insert_or_update(succ_state, 0, fanchor);
|
||||
SBPL_DEBUG(" Update in search %d with f = %d", 0, fanchor);
|
||||
|
||||
if (!closed_in_add_search(succ_state)) {
|
||||
for (int temp_hidx = 1; temp_hidx < num_heuristics(); ++temp_hidx) {
|
||||
int fn = compute_key(succ_state, temp_hidx);
|
||||
if (fn <= m_eps_mha * fanchor) {
|
||||
insert_or_update(succ_state, temp_hidx, fn);
|
||||
SBPL_DEBUG(" Update in search %d with f = %d", temp_hidx, fn);
|
||||
}
|
||||
else {
|
||||
SBPL_DEBUG(" Skipping update of in search %d (%0.3f > %0.3f)", temp_hidx, (double)fn, m_eps_mha * fanchor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(closed_in_any_search(state));
|
||||
}
|
||||
|
||||
MHASearchState* MHAPlanner::state_from_open_state(
|
||||
AbstractSearchState* open_state)
|
||||
{
|
||||
const size_t ssidx = reinterpret_cast<size_t>(open_state->listelem[0]);
|
||||
return m_search_states[ssidx];
|
||||
}
|
||||
|
||||
int MHAPlanner::compute_heuristic(int state_id, int hidx)
|
||||
{
|
||||
if (hidx == 0) {
|
||||
return m_hanchor->GetGoalHeuristic(state_id);
|
||||
}
|
||||
else {
|
||||
return m_heurs[hidx - 1]->GetGoalHeuristic(state_id);
|
||||
}
|
||||
}
|
||||
|
||||
int MHAPlanner::get_minf(CHeap& pq) const
|
||||
{
|
||||
return pq.getminkeyheap().key[0];
|
||||
}
|
||||
|
||||
void MHAPlanner::insert_or_update(MHASearchState* state, int hidx, int f)
|
||||
{
|
||||
CKey new_key;
|
||||
new_key.key[0] = f;
|
||||
|
||||
if (state->od[hidx].open_state.heapindex != 0) {
|
||||
m_open[hidx].updateheap(&state->od[hidx].open_state, new_key);
|
||||
}
|
||||
else {
|
||||
m_open[hidx].insertheap(&state->od[hidx].open_state, new_key);
|
||||
}
|
||||
}
|
||||
|
||||
void MHAPlanner::extract_path(std::vector<int>* solution_path, int* solcost)
|
||||
{
|
||||
SBPL_DEBUG("Extracting path");
|
||||
solution_path->clear();
|
||||
*solcost = 0;
|
||||
for (MHASearchState* state = m_goal_state; state; state = state->bp)
|
||||
{
|
||||
solution_path->push_back(state->state_id);
|
||||
if (state->bp) {
|
||||
*solcost += (state->g - state->bp->g);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: special cases for backward search
|
||||
std::reverse(solution_path->begin(), solution_path->end());
|
||||
}
|
||||
|
||||
bool MHAPlanner::closed_in_anc_search(MHASearchState* state) const
|
||||
{
|
||||
return state->closed_in_anc;
|
||||
}
|
||||
|
||||
bool MHAPlanner::closed_in_add_search(MHASearchState* state) const
|
||||
{
|
||||
return state->closed_in_add;
|
||||
}
|
||||
|
||||
bool MHAPlanner::closed_in_any_search(MHASearchState* state) const
|
||||
{
|
||||
return state->closed_in_anc || state->closed_in_add;
|
||||
}
|
||||
135
navigations/sbpl/src/planners/ppcpplanner.cpp
Executable file
135
navigations/sbpl/src/planners/ppcpplanner.cpp
Executable file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 2009, Maxim Likhachev
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the Carnegie Mellon University nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sbpl/discrete_space_information/environment.h>
|
||||
#include <sbpl/planners/ppcpplanner.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
PPCPPlanner::PPCPPlanner(DiscreteSpaceInformation* environment, int sizeofS, int sizeofH)
|
||||
{
|
||||
environment_ = environment;
|
||||
|
||||
#ifndef ROS
|
||||
const char* debug = "debug.txt";
|
||||
#endif
|
||||
fDeb = SBPL_FOPEN(debug, "w");
|
||||
if (fDeb == NULL) {
|
||||
throw SBPL_Exception("ERROR: could not open planner debug file");
|
||||
}
|
||||
|
||||
pStateSpace = new PPCPStateSpace_t;
|
||||
|
||||
//TODO - create and initialize the state-space
|
||||
}
|
||||
|
||||
PPCPPlanner::~PPCPPlanner()
|
||||
{
|
||||
if (pStateSpace != NULL) {
|
||||
//delete the statespace
|
||||
DeleteStateSpace( pStateSpace);
|
||||
delete pStateSpace;
|
||||
pStateSpace = NULL;
|
||||
}
|
||||
SBPL_FCLOSE( fDeb);
|
||||
}
|
||||
|
||||
//deallocates memory used by StateSpace
|
||||
void PPCPPlanner::DeleteStateSpace(PPCPStateSpace_t* pStateSpace)
|
||||
{
|
||||
//TODO - fill in
|
||||
}
|
||||
|
||||
//creates (allocates memory) search state space
|
||||
//does not initialize search statespace
|
||||
int PPCPPlanner::CreateSearchStateSpace(PPCPStateSpace_t* pStateSpace)
|
||||
{
|
||||
//create a heap
|
||||
pStateSpace->bReinitializeSearchStateSpace = true;
|
||||
pStateSpace->currentpolicyconfidence = 0;
|
||||
pStateSpace->GoalState = NULL;
|
||||
pStateSpace->StartState = NULL;
|
||||
pStateSpace->iteration = 0;
|
||||
pStateSpace->searchiteration = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
|
||||
//setting start state in S
|
||||
int PPCPPlanner::set_goal(int goal_stateID)
|
||||
{
|
||||
//TODO
|
||||
return 1;
|
||||
}
|
||||
|
||||
//setting goal state in S
|
||||
int PPCPPlanner::set_start(int start_stateID)
|
||||
{
|
||||
//TODO
|
||||
return 1;
|
||||
}
|
||||
|
||||
void PPCPPlanner::costs_changed(StateChangeQuery const & stateChange)
|
||||
{
|
||||
SBPL_PRINTF("planner: costs_changed, state-space reset\n");
|
||||
|
||||
pStateSpace->bReinitializeSearchStateSpace = true;
|
||||
}
|
||||
|
||||
void PPCPPlanner::costs_changed()
|
||||
{
|
||||
SBPL_PRINTF("planner: costs_changed, state-space reset\n");
|
||||
|
||||
pStateSpace->bReinitializeSearchStateSpace = true;
|
||||
}
|
||||
|
||||
int PPCPPlanner::force_planning_from_scratch()
|
||||
{
|
||||
SBPL_PRINTF("planner: forceplanfromscratch set, state-space reset\n");
|
||||
|
||||
pStateSpace->bReinitializeSearchStateSpace = true;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int PPCPPlanner::replan(double allocated_time_secs, vector<sbpl_PolicyStatewithBinaryh_t>* SolutionPolicy,
|
||||
float* ExpectedCost, float* ProbofReachGoal)
|
||||
{
|
||||
//TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
|
||||
1463
navigations/sbpl/src/planners/rstarplanner.cpp
Executable file
1463
navigations/sbpl/src/planners/rstarplanner.cpp
Executable file
File diff suppressed because it is too large
Load Diff
410
navigations/sbpl/src/planners/viplanner.cpp
Executable file
410
navigations/sbpl/src/planners/viplanner.cpp
Executable file
@@ -0,0 +1,410 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Maxim Likhachev
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the Carnegie Mellon University nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cmath>
|
||||
#include <sbpl/discrete_space_information/environment.h>
|
||||
#include <sbpl/planners/viplanner.h>
|
||||
#include <sbpl/utils/key.h>
|
||||
#include <sbpl/utils/mdpconfig.h>
|
||||
#include <sbpl/utils/utils.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
static unsigned int g_backups;
|
||||
static clock_t g_runtime = 0;
|
||||
static double g_belldelta = INFINITECOST;
|
||||
|
||||
VIPlanner::~VIPlanner()
|
||||
{
|
||||
//delete the statespace
|
||||
}
|
||||
|
||||
void VIPlanner::Initialize_vidata(CMDPSTATE* state)
|
||||
{
|
||||
VIState* vi_data = (VIState*)state->PlannerSpecificData;
|
||||
|
||||
vi_data->bestnextaction = NULL;
|
||||
vi_data->iteration = 0;
|
||||
vi_data->v = (float)environment_->GetGoalHeuristic(state->StateID);
|
||||
}
|
||||
|
||||
CMDPSTATE* VIPlanner::CreateState(int stateID)
|
||||
{
|
||||
CMDPSTATE* state = NULL;
|
||||
|
||||
#if DEBUG
|
||||
if (environment_->StateID2IndexMapping[stateID][VIMDP_STATEID2IND] != -1) {
|
||||
throw SBPL_Exception("ERROR in CreateState: state already created");
|
||||
}
|
||||
#endif
|
||||
|
||||
//adds to the tail a state
|
||||
state = viPlanner.MDP.AddState(stateID);
|
||||
|
||||
//remember the index of the state
|
||||
environment_->StateID2IndexMapping[stateID][VIMDP_STATEID2IND] = viPlanner.MDP.StateArray.size() - 1;
|
||||
|
||||
#if DEBUG
|
||||
if (state != viPlanner.MDP.StateArray[environment_->StateID2IndexMapping[stateID][VIMDP_STATEID2IND]]) {
|
||||
throw SBPL_Exception("ERROR in CreateState: invalid state index");
|
||||
}
|
||||
#endif
|
||||
|
||||
//create and initialize vi_data
|
||||
state->PlannerSpecificData = new VIState;
|
||||
Initialize_vidata(state);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
CMDPSTATE* VIPlanner::GetState(int stateID)
|
||||
{
|
||||
if (stateID >= (int)environment_->StateID2IndexMapping.size()) {
|
||||
throw SBPL_Exception("ERROR in GetState: stateID is invalid");
|
||||
}
|
||||
|
||||
if (environment_->StateID2IndexMapping[stateID][VIMDP_STATEID2IND] == -1)
|
||||
return CreateState(stateID);
|
||||
else
|
||||
return viPlanner.MDP.StateArray[environment_->StateID2IndexMapping[stateID][VIMDP_STATEID2IND]];
|
||||
}
|
||||
|
||||
void VIPlanner::PrintVIData()
|
||||
{
|
||||
SBPL_PRINTF("iteration %d: v(start) = %f\n", viPlanner.iteration,
|
||||
((VIState*)(viPlanner.StartState->PlannerSpecificData))->v);
|
||||
}
|
||||
|
||||
void VIPlanner::PrintStatHeader(FILE* fOut)
|
||||
{
|
||||
SBPL_FPRINTF(fOut, "iteration backups v(start)\n");
|
||||
}
|
||||
|
||||
void VIPlanner::PrintStat(FILE* fOut, clock_t starttime)
|
||||
{
|
||||
SBPL_FPRINTF(fOut, "%d %d %f %f %d\n", viPlanner.iteration, g_backups,
|
||||
((double)(clock() - starttime)) / CLOCKS_PER_SEC,
|
||||
((VIState*)(viPlanner.StartState->PlannerSpecificData))->v,
|
||||
(unsigned int)viPlanner.MDP.StateArray.size());
|
||||
}
|
||||
|
||||
void VIPlanner::PrintPolicy(FILE* fPolicy)
|
||||
{
|
||||
bool bPrintStatOnly = true;
|
||||
vector<CMDPSTATE*> WorkList;
|
||||
CMDP PolicyforEvaluation;
|
||||
|
||||
viPlanner.iteration++;
|
||||
WorkList.push_back(viPlanner.StartState);
|
||||
((VIState*)viPlanner.StartState->PlannerSpecificData)->iteration = viPlanner.iteration;
|
||||
double PolVal = 0.0;
|
||||
double Conf = 0;
|
||||
bool bCycles = false;
|
||||
SBPL_PRINTF("Printing policy...\n");
|
||||
while ((int)WorkList.size() > 0) {
|
||||
//pop the last state
|
||||
CMDPSTATE* state = WorkList.at(WorkList.size() - 1);
|
||||
WorkList.pop_back();
|
||||
VIState* statedata = (VIState*)state->PlannerSpecificData;
|
||||
|
||||
CMDPSTATE* polstate = PolicyforEvaluation.AddState(state->StateID);
|
||||
|
||||
//print state ID
|
||||
if (!bPrintStatOnly) {
|
||||
SBPL_FPRINTF(fPolicy, "%d\n", state->StateID);
|
||||
environment_->PrintState(state->StateID, false, fPolicy);
|
||||
|
||||
int h = environment_->GetGoalHeuristic(state->StateID);
|
||||
SBPL_FPRINTF(fPolicy, "h=%d\n", h);
|
||||
if (h > statedata->v) {
|
||||
SBPL_FPRINTF(fPolicy, "WARNING h overestimates exp.cost\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (state->StateID == viPlanner.GoalState->StateID) {
|
||||
//goal state
|
||||
if (!bPrintStatOnly) SBPL_FPRINTF(fPolicy, "0\n");
|
||||
Conf += ((VIState*)state->PlannerSpecificData)->Pc;
|
||||
}
|
||||
else if (statedata->bestnextaction == NULL) {
|
||||
//unexplored
|
||||
if (!bPrintStatOnly) {
|
||||
//no outcome explored - stay in the same place
|
||||
SBPL_FPRINTF(fPolicy, "%d %d %d\n", 1, 0, state->StateID);
|
||||
}
|
||||
}
|
||||
else {
|
||||
//get best action
|
||||
CMDPACTION* action = statedata->bestnextaction;
|
||||
|
||||
//add action to evaluation MDP
|
||||
CMDPACTION* polaction = polstate->AddAction(action->ActionID);
|
||||
|
||||
if (!bPrintStatOnly) SBPL_FPRINTF(fPolicy, "%d ", (unsigned int)action->SuccsID.size());
|
||||
|
||||
//print successors and insert them into the list
|
||||
for (int i = 0; i < (int)action->SuccsID.size(); i++) {
|
||||
if (!bPrintStatOnly) SBPL_FPRINTF(fPolicy, "%d %d ", action->Costs[i], action->SuccsID[i]);
|
||||
polaction->AddOutcome(action->SuccsID[i], action->Costs[i], action->SuccsProb[i]);
|
||||
|
||||
CMDPSTATE* succstate = GetState(action->SuccsID[i]);
|
||||
if ((int)((VIState*)succstate->PlannerSpecificData)->iteration != viPlanner.iteration) {
|
||||
((VIState*)succstate->PlannerSpecificData)->iteration = viPlanner.iteration;
|
||||
WorkList.push_back(succstate);
|
||||
|
||||
((VIState*)succstate->PlannerSpecificData)->Pc = action->SuccsProb[i] *
|
||||
((VIState*)state->PlannerSpecificData)->Pc;
|
||||
PolVal += ((VIState*)succstate->PlannerSpecificData)->Pc * action->Costs[i];
|
||||
}
|
||||
}
|
||||
if (!bPrintStatOnly) SBPL_FPRINTF(fPolicy, "\n");
|
||||
}
|
||||
}//while worklist not empty
|
||||
SBPL_PRINTF("done\n");
|
||||
|
||||
//now evaluate the policy
|
||||
double PolicyValue = -1;
|
||||
bool bFullPolicy = false;
|
||||
double Pcgoal = -1;
|
||||
int nMerges = 0;
|
||||
EvaluatePolicy(&PolicyforEvaluation, viPlanner.StartState->StateID, viPlanner.GoalState->StateID, &PolicyValue,
|
||||
&bFullPolicy, &Pcgoal, &nMerges, &bCycles);
|
||||
|
||||
SBPL_PRINTF("Policy value = %f FullPolicy=%d Merges=%d Cycles=%d\n", PolicyValue, bFullPolicy, nMerges, bCycles);
|
||||
|
||||
if (!bFullPolicy) SBPL_PRINTF("WARN: POLICY IS ONLY PARTIAL\n");
|
||||
if (fabs(PolicyValue - ((VIState*)(viPlanner.StartState->PlannerSpecificData))->v) > MDP_ERRDELTA) {
|
||||
SBPL_PRINTF("WARN: POLICY VALUE IS NOT CORRECT\n");
|
||||
}
|
||||
|
||||
if (!bPrintStatOnly)
|
||||
SBPL_FPRINTF(fPolicy,
|
||||
"backups=%d runtime=%f vstart=%f policyvalue=%f fullpolicy=%d Pc(goal)=%f nMerges=%d bCyc=%d\n",
|
||||
g_backups, (double)g_runtime / CLOCKS_PER_SEC,
|
||||
((VIState*)(viPlanner.StartState->PlannerSpecificData))->v, PolicyValue, bFullPolicy, Pcgoal,
|
||||
nMerges, bCycles);
|
||||
else
|
||||
SBPL_FPRINTF(fPolicy, "%d %f %f %f %d %f %d %d\n", g_backups, (double)g_runtime / CLOCKS_PER_SEC,
|
||||
((VIState*)(viPlanner.StartState->PlannerSpecificData))->v, PolicyValue, bFullPolicy, Pcgoal,
|
||||
nMerges, bCycles);
|
||||
}
|
||||
|
||||
void VIPlanner::backup(CMDPSTATE* state)
|
||||
{
|
||||
int aind, oind;
|
||||
CMDPSTATE* succstate;
|
||||
|
||||
g_backups++;
|
||||
|
||||
if (state == viPlanner.GoalState) {
|
||||
((VIState*)(state->PlannerSpecificData))->bestnextaction = NULL;
|
||||
((VIState*)(state->PlannerSpecificData))->v = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
//iterate through actions
|
||||
double minactionQ = INFINITECOST;
|
||||
CMDPACTION* minaction = NULL;
|
||||
for (aind = 0; aind < (int)state->Actions.size(); aind++) {
|
||||
double actionQ = 0;
|
||||
CMDPACTION* action = state->Actions[aind];
|
||||
for (oind = 0; oind < (int)action->SuccsID.size(); oind++) {
|
||||
succstate = GetState(action->SuccsID[oind]);
|
||||
actionQ += action->SuccsProb[oind] * (action->Costs[oind] +
|
||||
((VIState*)(succstate->PlannerSpecificData))->v);
|
||||
}
|
||||
|
||||
if (minaction == NULL || actionQ < minactionQ) {
|
||||
minactionQ = actionQ;
|
||||
minaction = action;
|
||||
}
|
||||
}
|
||||
|
||||
if (((VIState*)state->PlannerSpecificData)->bestnextaction == NULL)
|
||||
g_belldelta = INFINITECOST;
|
||||
else if (g_belldelta < fabs(((VIState*)state->PlannerSpecificData)->v - minactionQ))
|
||||
g_belldelta = fabs(((VIState*)state->PlannerSpecificData)->v - minactionQ);
|
||||
|
||||
//set state values
|
||||
((VIState*)state->PlannerSpecificData)->bestnextaction = minaction;
|
||||
((VIState*)state->PlannerSpecificData)->v = (float)minactionQ;
|
||||
}
|
||||
|
||||
void VIPlanner::perform_iteration_backward()
|
||||
{
|
||||
CMDPSTATE* state;
|
||||
vector<int> Worklist;
|
||||
int aind, oind;
|
||||
|
||||
//initialize the worklist
|
||||
Worklist.push_back(viPlanner.GoalState->StateID);
|
||||
|
||||
//backup all the states
|
||||
while ((int)Worklist.size() > 0) {
|
||||
//get the next state to process
|
||||
state = GetState(Worklist[Worklist.size() - 1]);
|
||||
Worklist.pop_back();
|
||||
|
||||
//add all actions to the state
|
||||
if ((int)state->Actions.size() == 0) environment_->SetAllActionsandAllOutcomes(state);
|
||||
|
||||
//backup the state
|
||||
backup(state);
|
||||
|
||||
//insert all the not yet processed successors into the worklist
|
||||
for (aind = 0; aind < (int)state->Actions.size(); aind++) {
|
||||
CMDPACTION* action = state->Actions[aind];
|
||||
for (oind = 0; oind < (int)action->SuccsID.size(); oind++) {
|
||||
CMDPSTATE* succstate = GetState(action->SuccsID[oind]);
|
||||
|
||||
//skip if already was in the queue
|
||||
if ((int)((VIState*)succstate->PlannerSpecificData)->iteration != viPlanner.iteration) {
|
||||
Worklist.push_back(succstate->StateID);
|
||||
|
||||
//mark it
|
||||
((VIState*)succstate->PlannerSpecificData)->iteration = viPlanner.iteration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//it is not necessary to process the predecessors of the start state
|
||||
if (state == viPlanner.StartState) continue;
|
||||
|
||||
//add all predecessor ids to the state
|
||||
if ((int)state->PredsID.size() == 0) environment_->SetAllPreds(state);
|
||||
//insert all the not yet processed predecessors into the worklist
|
||||
for (int pind = 0; pind < (int)state->PredsID.size(); pind++) {
|
||||
CMDPSTATE* PredState = GetState(state->PredsID[pind]);
|
||||
|
||||
//skip if already was in the queue
|
||||
if ((int)((VIState*)PredState->PlannerSpecificData)->iteration != viPlanner.iteration) {
|
||||
Worklist.push_back(PredState->StateID);
|
||||
|
||||
//mark it
|
||||
((VIState*)PredState->PlannerSpecificData)->iteration = viPlanner.iteration;
|
||||
}
|
||||
}
|
||||
} //until empty worklist
|
||||
}
|
||||
|
||||
void VIPlanner::perform_iteration_forward()
|
||||
{
|
||||
CMDPSTATE* state = NULL;
|
||||
vector<CMDPSTATE*> Worklist;
|
||||
int aind, oind;
|
||||
|
||||
//initialize the worklist
|
||||
Worklist.push_back(viPlanner.StartState);
|
||||
|
||||
//backup all the states
|
||||
while ((int)Worklist.size() > 0) {
|
||||
//get the next state to process from the front
|
||||
state = Worklist[Worklist.size() - 1];
|
||||
//Env_PrintState(state->StateID);
|
||||
Worklist.pop_back();
|
||||
|
||||
//add all actions to the state
|
||||
if ((int)state->Actions.size() == 0) environment_->SetAllActionsandAllOutcomes(state);
|
||||
|
||||
//backup the state
|
||||
backup(state);
|
||||
|
||||
//insert all the not yet processed successors into the worklist
|
||||
for (aind = 0; aind < (int)state->Actions.size(); aind++) {
|
||||
//CMDPACTION* action = state->Actions[aind];
|
||||
CMDPACTION* action = ((VIState*)state->PlannerSpecificData)->bestnextaction;
|
||||
for (oind = 0; action != NULL && oind < (int)action->SuccsID.size(); oind++) {
|
||||
CMDPSTATE* succstate = GetState(action->SuccsID[oind]);
|
||||
|
||||
//skip if already was in the queue
|
||||
if ((int)((VIState*)succstate->PlannerSpecificData)->iteration != viPlanner.iteration) {
|
||||
Worklist.push_back(succstate);
|
||||
|
||||
//mark it
|
||||
((VIState*)succstate->PlannerSpecificData)->iteration = viPlanner.iteration;
|
||||
}
|
||||
}
|
||||
}
|
||||
} //until empty worklist
|
||||
}
|
||||
|
||||
void VIPlanner::InitializePlanner()
|
||||
{
|
||||
viPlanner.iteration = 0;
|
||||
|
||||
//create and set up goal and start states
|
||||
viPlanner.StartState = GetState(MDPCfg_->startstateid);
|
||||
viPlanner.GoalState = GetState(MDPCfg_->goalstateid);
|
||||
}
|
||||
|
||||
//the planning entry point
|
||||
//returns 1 if path is found, 0 otherwise
|
||||
int VIPlanner::replan(double allocatedtime, vector<int>* solution_stateIDs_V)
|
||||
{
|
||||
#ifndef ROS
|
||||
const char* policy = "policy.txt";
|
||||
const char* stat = "stat.txt";
|
||||
#endif
|
||||
FILE* fPolicy = SBPL_FOPEN(policy, "w");
|
||||
FILE* fStat = SBPL_FOPEN(stat, "w");
|
||||
|
||||
//initialization
|
||||
InitializePlanner();
|
||||
|
||||
//start the timer
|
||||
clock_t starttime = clock();
|
||||
|
||||
//--------------iterate-------------------------------
|
||||
while (((clock() - starttime) / (double)CLOCKS_PER_SEC) < allocatedtime && g_belldelta > MDP_ERRDELTA) {
|
||||
viPlanner.iteration++;
|
||||
|
||||
g_belldelta = 0;
|
||||
perform_iteration_forward();
|
||||
|
||||
if (viPlanner.iteration % 100 == 0) {
|
||||
PrintStat(stdout, starttime);
|
||||
PrintStat(fStat, starttime);
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
|
||||
g_runtime = clock() - starttime;
|
||||
|
||||
PrintStat(stdout, starttime);
|
||||
PrintStat(fStat, starttime);
|
||||
SBPL_FFLUSH(fStat);
|
||||
|
||||
PrintPolicy(fPolicy);
|
||||
|
||||
SBPL_FCLOSE(fPolicy);
|
||||
SBPL_FCLOSE(fStat);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user