00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <iostream>
00030 using namespace std;
00031
00032 #include <sbpl/headers.h>
00033 #include <sbpl_dynamic_planner/weightedAStar.h>
00034
00035
00036
00037
00038
00039 WeightedAStar::WeightedAStar(DiscreteSpaceTimeInformation* environment, bool bSearchForward)
00040 {
00041 WeightedAStar_FINAL_EPS = 1.0;
00042 bforwardsearch = bSearchForward;
00043
00044 environment_ = environment;
00045
00046 bsearchuntilfirstsolution = false;
00047 finitial_eps = WeightedAStar_DEFAULT_INITIAL_EPS;
00048 searchexpands = 0;
00049 MaxMemoryCounter = 0;
00050
00051 fDeb = SBPL_FOPEN("debug.txt", "w");
00052
00053 pSearchStateSpace_ = new WeightedAStarSearchStateSpace_t;
00054
00055
00056
00057 if(CreateSearchStateSpace(pSearchStateSpace_) != 1)
00058 {
00059 SBPL_ERROR("ERROR: failed to create statespace\n");
00060 return;
00061 }
00062
00063
00064 if(InitializeSearchStateSpace(pSearchStateSpace_) != 1)
00065 {
00066 SBPL_ERROR("ERROR: failed to create statespace\n");
00067 return;
00068 }
00069 }
00070
00071 WeightedAStar::~WeightedAStar()
00072 {
00073 if(pSearchStateSpace_ != NULL){
00074
00075 DeleteSearchStateSpace(pSearchStateSpace_);
00076 delete pSearchStateSpace_;
00077 }
00078 SBPL_FCLOSE(fDeb);
00079 }
00080
00081
00082 void WeightedAStar::Initialize_searchinfo(CMDPSTATE* state, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00083 {
00084
00085 WeightedAStarState* searchstateinfo = (WeightedAStarState*)state->PlannerSpecificData;
00086
00087 searchstateinfo->MDPstate = state;
00088 InitializeSearchStateInfo(searchstateinfo, pSearchStateSpace);
00089 }
00090
00091
00092 CMDPSTATE* WeightedAStar::CreateState(int stateID, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00093 {
00094 CMDPSTATE* state = NULL;
00095
00096 #if DEBUG
00097 if(environment_->StateID2IndexMapping[stateID][ARAMDP_STATEID2IND] != -1)
00098 {
00099 SBPL_ERROR("ERROR in CreateState: state already created\n");
00100 throw new SBPL_Exception();
00101 }
00102 #endif
00103
00104
00105 state = pSearchStateSpace->searchMDP.AddState(stateID);
00106
00107
00108 environment_->StateID2IndexMapping[stateID][ARAMDP_STATEID2IND] = pSearchStateSpace->searchMDP.StateArray.size()-1;
00109
00110 #if DEBUG
00111 if(state != pSearchStateSpace->searchMDP.StateArray[environment_->StateID2IndexMapping[stateID][ARAMDP_STATEID2IND]])
00112 {
00113 SBPL_ERROR("ERROR in CreateState: invalid state index\n");
00114 throw new SBPL_Exception();
00115 }
00116 #endif
00117
00118
00119
00120 state->PlannerSpecificData = (WeightedAStarState*)malloc(sizeof(WeightedAStarState));
00121 Initialize_searchinfo(state, pSearchStateSpace);
00122 MaxMemoryCounter += sizeof(WeightedAStarState);
00123
00124 return state;
00125
00126 }
00127
00128 CMDPSTATE* WeightedAStar::GetState(int stateID, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00129 {
00130
00131 if(stateID >= (int)environment_->StateID2IndexMapping.size())
00132 {
00133 SBPL_ERROR("ERROR int GetState: stateID %d is invalid\n", stateID);
00134 throw new SBPL_Exception();
00135 }
00136
00137 if(environment_->StateID2IndexMapping[stateID][ARAMDP_STATEID2IND] == -1)
00138 return CreateState(stateID, pSearchStateSpace);
00139 else
00140 return pSearchStateSpace->searchMDP.StateArray[environment_->StateID2IndexMapping[stateID][ARAMDP_STATEID2IND]];
00141
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151 int WeightedAStar::ComputeHeuristic(CMDPSTATE* MDPstate, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00152 {
00153
00154
00155 if(bforwardsearch)
00156 {
00157
00158 #if MEM_CHECK == 1
00159
00160 #endif
00161
00162
00163 int retv = environment_->GetGoalHeuristic(MDPstate->StateID);
00164
00165 #if MEM_CHECK == 1
00166
00167
00168 #endif
00169
00170 return retv;
00171
00172 }
00173 else
00174 {
00175
00176 return environment_->GetStartHeuristic(MDPstate->StateID);
00177 }
00178 }
00179
00180
00181
00182 void WeightedAStar::InitializeSearchStateInfo(WeightedAStarState* state, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00183 {
00184 state->g = INFINITECOST;
00185 state->v = INFINITECOST;
00186 state->iterationclosed = 0;
00187 state->callnumberaccessed = pSearchStateSpace->callnumber;
00188 state->bestnextstate = NULL;
00189 state->costtobestnextstate = INFINITECOST;
00190 state->heapindex = 0;
00191 state->listelem[WeightedAStar_INCONS_LIST_ID] = 0;
00192 state->numofexpands = 0;
00193
00194 state->bestpredstate = NULL;
00195
00196
00197 #if USE_HEUR
00198 if(pSearchStateSpace->searchgoalstate != NULL)
00199 state->h = ComputeHeuristic(state->MDPstate, pSearchStateSpace);
00200 else
00201 state->h = 0;
00202 #else
00203 state->h = 0;
00204 #endif
00205
00206
00207 }
00208
00209
00210
00211
00212 void WeightedAStar::ReInitializeSearchStateInfo(WeightedAStarState* state, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00213 {
00214 state->g = INFINITECOST;
00215 state->v = INFINITECOST;
00216 state->iterationclosed = 0;
00217 state->callnumberaccessed = pSearchStateSpace->callnumber;
00218 state->bestnextstate = NULL;
00219 state->costtobestnextstate = INFINITECOST;
00220 state->heapindex = 0;
00221 state->listelem[WeightedAStar_INCONS_LIST_ID] = 0;
00222 state->numofexpands = 0;
00223
00224 state->bestpredstate = NULL;
00225
00226
00227 #if USE_HEUR
00228
00229 if(pSearchStateSpace->searchgoalstate != NULL)
00230 {
00231 state->h = ComputeHeuristic(state->MDPstate, pSearchStateSpace);
00232 }
00233 else
00234 state->h = 0;
00235
00236 #else
00237
00238 state->h = 0;
00239
00240 #endif
00241
00242
00243 }
00244
00245
00246
00247 void WeightedAStar::DeleteSearchStateData(WeightedAStarState* state)
00248 {
00249
00250 MaxMemoryCounter = 0;
00251 return;
00252 }
00253
00254
00255
00256
00257 void WeightedAStar::UpdatePreds(WeightedAStarState* state, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00258 {
00259 vector<int> PredIDV;
00260 vector<int> CostV;
00261 CKey key;
00262 WeightedAStarState *predstate;
00263
00264 environment_->GetPreds(state->MDPstate->StateID, &PredIDV, &CostV);
00265
00266
00267 for(int pind = 0; pind < (int)PredIDV.size(); pind++)
00268 {
00269 CMDPSTATE* PredMDPState = GetState(PredIDV[pind], pSearchStateSpace);
00270 predstate = (WeightedAStarState*)(PredMDPState->PlannerSpecificData);
00271 if(predstate->callnumberaccessed != pSearchStateSpace->callnumber)
00272 ReInitializeSearchStateInfo(predstate, pSearchStateSpace);
00273
00274
00275 if(predstate->g > state->v + CostV[pind])
00276 {
00277 predstate->g = state->v + CostV[pind];
00278 predstate->bestnextstate = state->MDPstate;
00279 predstate->costtobestnextstate = CostV[pind];
00280
00281
00282 if(predstate->iterationclosed != pSearchStateSpace->searchiteration)
00283 {
00284 key.key[0] = predstate->g + (int)(pSearchStateSpace->eps*predstate->h);
00285
00286 if(predstate->heapindex != 0)
00287 pSearchStateSpace->heap->updateheap(predstate,key);
00288 else
00289 pSearchStateSpace->heap->insertheap(predstate,key);
00290 }
00291
00292 else if(predstate->listelem[WeightedAStar_INCONS_LIST_ID] == NULL)
00293 {
00294 pSearchStateSpace->inconslist->insert(predstate, WeightedAStar_INCONS_LIST_ID);
00295 }
00296 }
00297 }
00298
00299 }
00300
00301
00302 void WeightedAStar::UpdateSuccs(WeightedAStarState* state, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00303 {
00304 vector<int> SuccIDV;
00305 vector<int> CostV;
00306 CKey key;
00307 WeightedAStarState *succstate;
00308
00309 vector<int> holdList;
00310 vector<int> activeBubbles;
00311
00312 int tempX,tempY,tempTH,tempT;
00313 environment_->GetCoordFromState(state->MDPstate->StateID,tempX,tempY,tempTH,tempT);
00314
00315
00316
00317 environment_->GetSuccs(state->MDPstate->StateID, &SuccIDV, &CostV, &holdList, &activeBubbles);
00318
00319
00320 for(unsigned int i=0; i<activeBubbles.size(); i++){
00321
00322 bubbles[activeBubbles[i]].active = true;
00323
00324 for(unsigned int j=0; j<bubbles[activeBubbles[i]].hold.size(); j++){
00325 if(!bubbles[activeBubbles[i]].hold[j]->done){
00326
00327
00328
00329 pSearchStateSpace->heap->insertheap(bubbles[activeBubbles[i]].hold[j]->state, bubbles[activeBubbles[i]].hold[j]->key);
00330
00331 bubbles[activeBubbles[i]].hold[j]->done = true;
00332 }
00333 }
00334 }
00335
00336
00337 for(int sind = 0; sind < (int)SuccIDV.size(); sind++)
00338 {
00339 CMDPSTATE* SuccMDPState = GetState(SuccIDV[sind], pSearchStateSpace);
00340 int cost = CostV[sind];
00341
00342 succstate = (WeightedAStarState*)(SuccMDPState->PlannerSpecificData);
00343 if(succstate->callnumberaccessed != pSearchStateSpace->callnumber)
00344 ReInitializeSearchStateInfo(succstate, pSearchStateSpace);
00345
00346
00347
00348 if(succstate->g > state->v + cost)
00349 {
00350 succstate->g = state->v + cost;
00351 succstate->bestpredstate = state->MDPstate;
00352
00353
00354 if(succstate->iterationclosed != pSearchStateSpace->searchiteration)
00355 {
00356
00357 environment_->Relax(state->MDPstate->StateID, SuccIDV[sind]);
00358
00359 key.key[0] = succstate->g + (int)(pSearchStateSpace->eps*succstate->h);
00360
00361
00362
00363 if(succstate->heapindex != 0)
00364 pSearchStateSpace->heap->updateheap(succstate,key);
00365 else{
00366 if(sind == (int)SuccIDV.size()-1 && holdList.size() > 0){
00367 bool onHold = true;
00368 for(unsigned int i=0; i<holdList.size(); i++)
00369 onHold = onHold && !bubbles[holdList[i]].active;
00370 if(onHold){
00371
00372
00373
00374
00375 HoldState_t* temp = new HoldState_t;
00376 temp->state = succstate;
00377 temp->key = key;
00378 temp->done = false;
00379 for(unsigned int i=0; i<holdList.size(); i++)
00380 bubbles[holdList[i]].hold.push_back(temp);
00381 }
00382 else{
00383
00384
00385 pSearchStateSpace->heap->insertheap(succstate,key);
00386 }
00387 }
00388 else{
00389
00390
00391 pSearchStateSpace->heap->insertheap(succstate,key);
00392 }
00393 }
00394 }
00395
00396 else if(succstate->listelem[WeightedAStar_INCONS_LIST_ID] == NULL)
00397 {
00398 pSearchStateSpace->inconslist->insert(succstate, WeightedAStar_INCONS_LIST_ID);
00399 }
00400 }
00401
00402 }
00403 }
00404
00405
00406 int WeightedAStar::GetGVal(int StateID, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00407 {
00408 CMDPSTATE* cmdp_state = GetState(StateID, pSearchStateSpace);
00409 WeightedAStarState* state = (WeightedAStarState*)cmdp_state->PlannerSpecificData;
00410 return state->g;
00411 }
00412
00413
00414 int WeightedAStar::ImprovePath(WeightedAStarSearchStateSpace_t* pSearchStateSpace, double MaxNumofSecs)
00415 {
00416 int expands;
00417 WeightedAStarState *state, *searchgoalstate;
00418 CKey key, minkey;
00419 CKey goalkey;
00420
00421 expands = 0;
00422
00423
00424 if(pSearchStateSpace->searchgoalstate == NULL)
00425 {
00426 SBPL_ERROR("ERROR searching: no goal state is set\n");
00427 throw new SBPL_Exception();
00428 }
00429
00430
00431 searchgoalstate = (WeightedAStarState*)(pSearchStateSpace->searchgoalstate->PlannerSpecificData);
00432 if(searchgoalstate->callnumberaccessed != pSearchStateSpace->callnumber)
00433 ReInitializeSearchStateInfo(searchgoalstate, pSearchStateSpace);
00434
00435
00436 goalkey.key[0] = searchgoalstate->g;
00437
00438
00439
00440 minkey = pSearchStateSpace->heap->getminkeyheap();
00441 CKey oldkey = minkey;
00442 while(!pSearchStateSpace->heap->emptyheap() && minkey.key[0] < INFINITECOST && goalkey > minkey &&
00443 (clock()-TimeStarted) < MaxNumofSecs*(double)CLOCKS_PER_SEC)
00444 {
00445
00446
00447 state = (WeightedAStarState*)pSearchStateSpace->heap->deleteminheap();
00448
00449
00450 #if DEBUG
00451
00452
00453
00454 SBPL_FPRINTF(fDeb, "expanding: ");
00455 PrintSearchState(state, fDeb);
00456 if(state->listelem[WeightedAStar_INCONS_LIST_ID] != NULL)
00457 {
00458 SBPL_FPRINTF(fDeb, "ERROR: expanding a state from inconslist\n");
00459 SBPL_ERROR("ERROR: expanding a state from inconslist\n");
00460 throw new SBPL_Exception();
00461 }
00462 fflush(fDeb);
00463 #endif
00464
00465 #if DEBUG
00466 if(minkey.key[0] < oldkey.key[0] && fabs(this->finitial_eps - 1.0) < ERR_EPS)
00467 {
00468
00469
00470 }
00471 oldkey = minkey;
00472 #endif
00473
00474 if(state->v == state->g)
00475 {
00476 SBPL_ERROR("ERROR: consistent state is being expanded\n");
00477 #if DEBUG
00478 SBPL_FPRINTF(fDeb, "ERROR: consistent state is being expanded\n");
00479 throw new SBPL_Exception();
00480 #endif
00481 }
00482
00483
00484 state->v = state->g;
00485 state->iterationclosed = pSearchStateSpace->searchiteration;
00486
00487
00488 expands++;
00489 state->numofexpands++;
00490
00491
00492 if(bforwardsearch == false)
00493 UpdatePreds(state, pSearchStateSpace);
00494 else
00495 UpdateSuccs(state, pSearchStateSpace);
00496
00497
00498 minkey = pSearchStateSpace->heap->getminkeyheap();
00499
00500
00501 if(goalkey.key[0] != (int)searchgoalstate->g)
00502 {
00503
00504
00505 goalkey.key[0] = searchgoalstate->g;
00506
00507 }
00508
00509 if(expands%100000 == 0 && expands > 0)
00510 {
00511 SBPL_PRINTF("expands so far=%u\n", expands);
00512 }
00513
00514 }
00515
00516 int retv = 1;
00517 if(searchgoalstate->g == INFINITECOST && pSearchStateSpace->heap->emptyheap())
00518 {
00519 SBPL_PRINTF("solution does not exist: search exited because heap is empty\n");
00520 retv = 0;
00521 }
00522 else if(!pSearchStateSpace->heap->emptyheap() && goalkey > minkey)
00523 {
00524 SBPL_PRINTF("search exited because it ran out of time\n");
00525 retv = 2;
00526 }
00527 else if(searchgoalstate->g == INFINITECOST && !pSearchStateSpace->heap->emptyheap())
00528 {
00529 SBPL_PRINTF("solution does not exist: search exited because all candidates for expansion have infinite heuristics\n");
00530 retv = 0;
00531 }
00532 else
00533 {
00534 SBPL_PRINTF("search exited with a solution for eps=%.3f\n", pSearchStateSpace->eps);
00535 retv = 1;
00536 }
00537
00538
00539
00540 searchexpands += expands;
00541
00542 return retv;
00543 }
00544
00545
00546 void WeightedAStar::BuildNewOPENList(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00547 {
00548 WeightedAStarState *state;
00549 CKey key;
00550 CHeap* pheap = pSearchStateSpace->heap;
00551 CList* pinconslist = pSearchStateSpace->inconslist;
00552
00553
00554 while(pinconslist->firstelement != NULL)
00555 {
00556 state = (WeightedAStarState*)pinconslist->firstelement->liststate;
00557
00558
00559 key.key[0] = state->g + (int)(pSearchStateSpace->eps*state->h);
00560
00561
00562
00563 pheap->insertheap(state, key);
00564
00565 pinconslist->remove(state, WeightedAStar_INCONS_LIST_ID);
00566 }
00567 }
00568
00569
00570 void WeightedAStar::Reevaluatefvals(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00571 {
00572 CKey key;
00573 int i;
00574 CHeap* pheap = pSearchStateSpace->heap;
00575
00576
00577 for (i = 1; i <= pheap->currentsize; ++i)
00578 {
00579 WeightedAStarState* state = (WeightedAStarState*)pheap->heap[i].heapstate;
00580 pheap->heap[i].key.key[0] = state->g +
00581 (int)(pSearchStateSpace->eps*state->h);
00582
00583 }
00584 pheap->makeheap();
00585
00586 pSearchStateSpace->bReevaluatefvals = false;
00587 }
00588
00589
00590
00591
00592
00593
00594 int WeightedAStar::CreateSearchStateSpace(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00595 {
00596
00597
00598 pSearchStateSpace->heap = new CHeap;
00599 pSearchStateSpace->inconslist = new CList;
00600 MaxMemoryCounter += sizeof(CHeap);
00601 MaxMemoryCounter += sizeof(CList);
00602
00603 pSearchStateSpace->searchgoalstate = NULL;
00604 pSearchStateSpace->searchstartstate = NULL;
00605
00606 searchexpands = 0;
00607
00608
00609 pSearchStateSpace->bReinitializeSearchStateSpace = false;
00610
00611 return 1;
00612 }
00613
00614
00615 void WeightedAStar::DeleteSearchStateSpace(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00616 {
00617 if(pSearchStateSpace->heap != NULL)
00618 {
00619 pSearchStateSpace->heap->makeemptyheap();
00620 delete pSearchStateSpace->heap;
00621 pSearchStateSpace->heap = NULL;
00622 }
00623
00624 if(pSearchStateSpace->inconslist != NULL)
00625 {
00626 pSearchStateSpace->inconslist->makeemptylist(WeightedAStar_INCONS_LIST_ID);
00627 delete pSearchStateSpace->inconslist;
00628 pSearchStateSpace->inconslist = NULL;
00629 }
00630
00631
00632 int iend = (int)pSearchStateSpace->searchMDP.StateArray.size();
00633 for(int i=0; i < iend; i++)
00634 {
00635 CMDPSTATE* state = pSearchStateSpace->searchMDP.StateArray[i];
00636 if(state != NULL && state->PlannerSpecificData != NULL){
00637 DeleteSearchStateData((WeightedAStarState*)state->PlannerSpecificData);
00638 free((WeightedAStarState*)state->PlannerSpecificData);
00639 state->PlannerSpecificData = NULL;
00640 }
00641 }
00642 pSearchStateSpace->searchMDP.Delete();
00643 }
00644
00645
00646
00647
00648
00649 int WeightedAStar::ResetSearchStateSpace(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00650 {
00651 pSearchStateSpace->heap->makeemptyheap();
00652 pSearchStateSpace->inconslist->makeemptylist(WeightedAStar_INCONS_LIST_ID);
00653
00654 return 1;
00655 }
00656
00657
00658 void WeightedAStar::ReInitializeSearchStateSpace(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00659 {
00660 CKey key;
00661
00662
00663 pSearchStateSpace->callnumber++;
00664
00665
00666 pSearchStateSpace->searchiteration = 0;
00667 pSearchStateSpace->bNewSearchIteration = true;
00668
00669 #if DEBUG
00670 SBPL_FPRINTF(fDeb, "reinitializing search state-space (new call number=%d search iter=%d)\n",
00671 pSearchStateSpace->callnumber,pSearchStateSpace->searchiteration );
00672 #endif
00673
00674
00675
00676 pSearchStateSpace->heap->makeemptyheap();
00677 pSearchStateSpace->inconslist->makeemptylist(WeightedAStar_INCONS_LIST_ID);
00678
00679
00680 pSearchStateSpace->eps = this->finitial_eps;
00681 pSearchStateSpace->eps_satisfied = INFINITECOST;
00682
00683
00684 WeightedAStarState* startstateinfo = (WeightedAStarState*)(pSearchStateSpace->searchstartstate->PlannerSpecificData);
00685 if(startstateinfo->callnumberaccessed != pSearchStateSpace->callnumber)
00686 ReInitializeSearchStateInfo(startstateinfo, pSearchStateSpace);
00687
00688 startstateinfo->g = 0;
00689
00690
00691 key.key[0] = (long int)(pSearchStateSpace->eps*startstateinfo->h);
00692
00693 pSearchStateSpace->heap->insertheap(startstateinfo, key);
00694
00695 pSearchStateSpace->bReinitializeSearchStateSpace = false;
00696 pSearchStateSpace->bReevaluatefvals = false;
00697 }
00698
00699
00700 int WeightedAStar::InitializeSearchStateSpace(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00701 {
00702
00703 if(pSearchStateSpace->heap->currentsize != 0 ||
00704 pSearchStateSpace->inconslist->currentsize != 0)
00705 {
00706 SBPL_ERROR("ERROR in InitializeSearchStateSpace: heap or list is not empty\n");
00707 throw new SBPL_Exception();
00708 }
00709
00710 pSearchStateSpace->eps = this->finitial_eps;
00711 pSearchStateSpace->eps_satisfied = INFINITECOST;
00712 pSearchStateSpace->searchiteration = 0;
00713 pSearchStateSpace->bNewSearchIteration = true;
00714 pSearchStateSpace->callnumber = 0;
00715 pSearchStateSpace->bReevaluatefvals = false;
00716
00717
00718
00719 pSearchStateSpace->searchgoalstate = NULL;
00720
00721 pSearchStateSpace->searchstartstate = NULL;
00722
00723
00724 pSearchStateSpace->bReinitializeSearchStateSpace = true;
00725
00726 return 1;
00727
00728 }
00729
00730
00731 int WeightedAStar::SetSearchGoalState(int SearchGoalStateID, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00732 {
00733 if(pSearchStateSpace->searchgoalstate == NULL ||
00734 pSearchStateSpace->searchgoalstate->StateID != SearchGoalStateID)
00735 {
00736 pSearchStateSpace->searchgoalstate = GetState(SearchGoalStateID, pSearchStateSpace);
00737
00738
00739 pSearchStateSpace->eps_satisfied = INFINITECOST;
00740 pSearchStateSpace->bNewSearchIteration = true;
00741 pSearchStateSpace_->eps = this->finitial_eps;
00742
00743
00744
00745 #if USE_HEUR
00746 for(int i = 0; i < (int)pSearchStateSpace->searchMDP.StateArray.size(); i++)
00747 {
00748 CMDPSTATE* MDPstate = pSearchStateSpace->searchMDP.StateArray[i];
00749 WeightedAStarState* state = (WeightedAStarState*)MDPstate->PlannerSpecificData;
00750 state->h = ComputeHeuristic(MDPstate, pSearchStateSpace);
00751 }
00752
00753 pSearchStateSpace->bReevaluatefvals = true;
00754 #endif
00755 }
00756
00757
00758 return 1;
00759
00760 }
00761
00762
00763 int WeightedAStar::SetSearchStartState(int SearchStartStateID, WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00764 {
00765
00766 CMDPSTATE* MDPstate = GetState(SearchStartStateID, pSearchStateSpace);
00767
00768 if(MDPstate != pSearchStateSpace->searchstartstate)
00769 {
00770 pSearchStateSpace->searchstartstate = MDPstate;
00771 pSearchStateSpace->bReinitializeSearchStateSpace = true;
00772 }
00773
00774 return 1;
00775
00776 }
00777
00778
00779
00780 int WeightedAStar::ReconstructPath(WeightedAStarSearchStateSpace_t* pSearchStateSpace)
00781 {
00782
00783
00784 if(bforwardsearch)
00785 {
00786 CMDPSTATE* MDPstate = pSearchStateSpace->searchgoalstate;
00787 CMDPSTATE* PredMDPstate;
00788 WeightedAStarState *predstateinfo, *stateinfo;
00789
00790
00791
00792 #if DEBUG
00793 SBPL_FPRINTF(fDeb, "reconstructing a path:\n");
00794 #endif
00795
00796 while(MDPstate != pSearchStateSpace->searchstartstate)
00797 {
00798 stateinfo = (WeightedAStarState*)MDPstate->PlannerSpecificData;
00799
00800 #if DEBUG
00801 PrintSearchState(stateinfo, fDeb);
00802 #endif
00803 if(stateinfo->g == INFINITECOST)
00804 {
00805
00806
00807 return -1;
00808 }
00809
00810 if(stateinfo->bestpredstate == NULL)
00811 {
00812 SBPL_ERROR("ERROR in ReconstructPath: bestpred is NULL\n");
00813 throw new SBPL_Exception();
00814 }
00815
00816
00817 PredMDPstate = stateinfo->bestpredstate;
00818 predstateinfo = (WeightedAStarState*)PredMDPstate->PlannerSpecificData;
00819
00820
00821 predstateinfo->bestnextstate = MDPstate;
00822
00823
00824 if(predstateinfo->v >= stateinfo->g)
00825 {
00826 SBPL_ERROR("ERROR in ReconstructPath: g-values are non-decreasing\n");
00827 PrintSearchState(predstateinfo, fDeb);
00828 throw new SBPL_Exception();
00829 }
00830
00831
00832 MDPstate = PredMDPstate;
00833 }
00834 }
00835
00836 return 1;
00837 }
00838
00839
00840
00841 void WeightedAStar::PrintSearchPath(WeightedAStarSearchStateSpace_t* pSearchStateSpace, FILE* fOut)
00842 {
00843 WeightedAStarState* searchstateinfo;
00844 CMDPSTATE* state;
00845 int goalID;
00846 int PathCost;
00847
00848 if(bforwardsearch)
00849 {
00850 state = pSearchStateSpace->searchstartstate;
00851 goalID = pSearchStateSpace->searchgoalstate->StateID;
00852 }
00853 else
00854 {
00855 state = pSearchStateSpace->searchgoalstate;
00856 goalID = pSearchStateSpace->searchstartstate->StateID;
00857 }
00858 if(fOut == NULL)
00859 fOut = stdout;
00860
00861 PathCost = ((WeightedAStarState*)pSearchStateSpace->searchgoalstate->PlannerSpecificData)->g;
00862
00863 SBPL_FPRINTF(fOut, "Printing a path from state %d to the goal state %d\n",
00864 state->StateID, pSearchStateSpace->searchgoalstate->StateID);
00865 SBPL_FPRINTF(fOut, "Path cost = %d:\n", PathCost);
00866
00867
00868 environment_->PrintState(state->StateID, false, fOut);
00869
00870 int costFromStart = 0;
00871 while(state->StateID != goalID)
00872 {
00873 SBPL_FPRINTF(fOut, "state %d ", state->StateID);
00874
00875 if(state->PlannerSpecificData == NULL)
00876 {
00877 SBPL_FPRINTF(fOut, "path does not exist since search data does not exist\n");
00878 break;
00879 }
00880
00881 searchstateinfo = (WeightedAStarState*)state->PlannerSpecificData;
00882
00883 if(searchstateinfo->bestnextstate == NULL)
00884 {
00885 SBPL_FPRINTF(fOut, "path does not exist since bestnextstate == NULL\n");
00886 break;
00887 }
00888 if(searchstateinfo->g == INFINITECOST)
00889 {
00890 SBPL_FPRINTF(fOut, "path does not exist since bestnextstate == NULL\n");
00891 break;
00892 }
00893
00894 int costToGoal = PathCost - costFromStart;
00895 int transcost = searchstateinfo->g - ((WeightedAStarState*)(searchstateinfo->bestnextstate->PlannerSpecificData))->v;
00896 if(bforwardsearch)
00897 transcost = -transcost;
00898
00899 costFromStart += transcost;
00900
00901 SBPL_FPRINTF(fOut, "g=%d-->state %d, h = %d ctg = %d ", searchstateinfo->g,
00902 searchstateinfo->bestnextstate->StateID, searchstateinfo->h, costToGoal);
00903
00904 state = searchstateinfo->bestnextstate;
00905
00906 environment_->PrintState(state->StateID, false, fOut);
00907
00908
00909
00910 }
00911 }
00912
00913 void WeightedAStar::PrintSearchState(WeightedAStarState* state, FILE* fOut)
00914 {
00915 SBPL_FPRINTF(fOut, "state %d: h=%d g=%u v=%u iterc=%d callnuma=%d expands=%d heapind=%d inconslist=%d\n",
00916 state->MDPstate->StateID, state->h, state->g, state->v,
00917 state->iterationclosed, state->callnumberaccessed, state->numofexpands, state->heapindex, state->listelem[WeightedAStar_INCONS_LIST_ID]?1:0);
00918 environment_->PrintState(state->MDPstate->StateID, true, fOut);
00919
00920 }
00921
00922
00923
00924 int WeightedAStar::getHeurValue(WeightedAStarSearchStateSpace_t* pSearchStateSpace, int StateID)
00925 {
00926 CMDPSTATE* MDPstate = GetState(StateID, pSearchStateSpace);
00927 WeightedAStarState* searchstateinfo = (WeightedAStarState*)MDPstate->PlannerSpecificData;
00928 return searchstateinfo->h;
00929 }
00930
00931
00932 vector<int> WeightedAStar::GetSearchPath(WeightedAStarSearchStateSpace_t* pSearchStateSpace, int& solcost)
00933 {
00934 vector<int> SuccIDV;
00935 vector<int> CostV;
00936 vector<int> wholePathIds;
00937 WeightedAStarState* searchstateinfo;
00938 CMDPSTATE* state = NULL;
00939 CMDPSTATE* goalstate = NULL;
00940 CMDPSTATE* startstate=NULL;
00941
00942 if(bforwardsearch){
00943 startstate = pSearchStateSpace->searchstartstate;
00944 goalstate = pSearchStateSpace->searchgoalstate;
00945
00946
00947 ReconstructPath(pSearchStateSpace);
00948 }
00949 else{
00950 startstate = pSearchStateSpace->searchgoalstate;
00951 goalstate = pSearchStateSpace->searchstartstate;
00952 }
00953
00954
00955 state = startstate;
00956
00957 wholePathIds.push_back(state->StateID);
00958 solcost = 0;
00959
00960 FILE* fOut = stdout;
00961 while(state->StateID != goalstate->StateID){
00962 if(state->PlannerSpecificData == NULL){
00963 SBPL_FPRINTF(fOut, "path does not exist since search data does not exist\n");
00964 break;
00965 }
00966
00967 searchstateinfo = (WeightedAStarState*)state->PlannerSpecificData;
00968
00969 if(searchstateinfo->bestnextstate == NULL){
00970 SBPL_FPRINTF(fOut, "path does not exist since bestnextstate == NULL\n");
00971 break;
00972 }
00973 if(searchstateinfo->g == INFINITECOST){
00974 SBPL_FPRINTF(fOut, "path does not exist since bestnextstate == NULL\n");
00975 break;
00976 }
00977
00978 environment_->GetSuccs(state->StateID, &SuccIDV, &CostV);
00979 int actioncost = INFINITECOST;
00980 for(int i = 0; i < (int)SuccIDV.size(); i++){
00981 if(SuccIDV.at(i) == searchstateinfo->bestnextstate->StateID && CostV.at(i)<actioncost)
00982 actioncost = CostV.at(i);
00983 }
00984 if(actioncost == INFINITECOST)
00985 SBPL_PRINTF("WARNING: actioncost = %d (%d)\n", actioncost,state->StateID);
00986
00987 solcost += actioncost;
00988
00989
00990
00991
00992
00993
00994
00995 #if DEBUG
00996 WeightedAStarState* nextstateinfo = (WeightedAStarState*)(searchstateinfo->bestnextstate->PlannerSpecificData);
00997 if(actioncost != abs((int)(searchstateinfo->g - nextstateinfo->g)) && pSearchStateSpace->eps_satisfied <= 1.001){
00998 SBPL_FPRINTF(fDeb, "ERROR: actioncost=%d is not matching the difference in g-values of %d\n",
00999 actioncost, abs((int)(searchstateinfo->g - nextstateinfo->g)));
01000 SBPL_ERROR("ERROR: actioncost=%d is not matching the difference in g-values of %d\n",
01001 actioncost,abs((int)(searchstateinfo->g - nextstateinfo->g)));
01002 PrintSearchState(searchstateinfo, fDeb);
01003 PrintSearchState(nextstateinfo, fDeb);
01004 }
01005 #endif
01006
01007
01008 state = searchstateinfo->bestnextstate;
01009
01010 wholePathIds.push_back(state->StateID);
01011 }
01012
01013
01014 return wholePathIds;
01015 }
01016
01017
01018
01019 bool WeightedAStar::Search(WeightedAStarSearchStateSpace_t* pSearchStateSpace, vector<int>& pathIds, int & PathCost, bool bFirstSolution, bool bOptimalSolution, double MaxNumofSecs)
01020 {
01021 CKey key;
01022 TimeStarted = clock();
01023 searchexpands = 0;
01024
01025 #if DEBUG
01026 SBPL_FPRINTF(fDeb, "new search call (call number=%d)\n", pSearchStateSpace->callnumber);
01027 #endif
01028
01029 if(pSearchStateSpace->bReinitializeSearchStateSpace == true){
01030
01031 ReInitializeSearchStateSpace(pSearchStateSpace);
01032 }
01033
01034
01035 if(bOptimalSolution)
01036 {
01037 pSearchStateSpace->eps = 1;
01038 MaxNumofSecs = INFINITECOST;
01039 }
01040 else if(bFirstSolution)
01041 {
01042 MaxNumofSecs = INFINITECOST;
01043 }
01044
01045
01046 int prevexpands = 0;
01047 clock_t loop_time;
01048 while(pSearchStateSpace->eps_satisfied > WeightedAStar_FINAL_EPS &&
01049 (clock()- TimeStarted) < MaxNumofSecs*(double)CLOCKS_PER_SEC)
01050 {
01051 loop_time = clock();
01052
01053 if(fabs(pSearchStateSpace->eps_satisfied - pSearchStateSpace->eps) < ERR_EPS && !bFirstSolution)
01054 {
01055 pSearchStateSpace->eps = pSearchStateSpace->eps - WeightedAStar_DECREASE_EPS;
01056 if(pSearchStateSpace->eps < WeightedAStar_FINAL_EPS)
01057 pSearchStateSpace->eps = WeightedAStar_FINAL_EPS;
01058
01059
01060 pSearchStateSpace->bReevaluatefvals = true;
01061
01062
01063 pSearchStateSpace->bNewSearchIteration = true;
01064
01065
01066 BuildNewOPENList(pSearchStateSpace);
01067
01068 }
01069
01070 if(pSearchStateSpace->bNewSearchIteration)
01071 {
01072 pSearchStateSpace->searchiteration++;
01073 pSearchStateSpace->bNewSearchIteration = false;
01074 }
01075
01076
01077 if(pSearchStateSpace->bReevaluatefvals)
01078 Reevaluatefvals(pSearchStateSpace);
01079
01080
01081 if(ImprovePath(pSearchStateSpace, MaxNumofSecs) == 1){
01082 pSearchStateSpace->eps_satisfied = pSearchStateSpace->eps;
01083 }
01084
01085
01086 SBPL_PRINTF("eps=%f expands=%d g(searchgoal)=%d time=%.3f\n", pSearchStateSpace->eps_satisfied, searchexpands - prevexpands,
01087 ((WeightedAStarState*)pSearchStateSpace->searchgoalstate->PlannerSpecificData)->g,double(clock()-loop_time)/CLOCKS_PER_SEC);
01088
01089 #if DEBUG
01090 SBPL_FPRINTF(fDeb, "eps=%f expands=%d g(searchgoal)=%d time=%.3f\n", pSearchStateSpace->eps_satisfied, searchexpands - prevexpands,
01091 ((WeightedAStarState*)pSearchStateSpace->searchgoalstate->PlannerSpecificData)->g,double(clock()-loop_time)/CLOCKS_PER_SEC);
01092 PrintSearchState((WeightedAStarState*)pSearchStateSpace->searchgoalstate->PlannerSpecificData, fDeb);
01093 #endif
01094 prevexpands = searchexpands;
01095
01096
01097
01098 if(bFirstSolution)
01099 break;
01100
01101
01102 if(((WeightedAStarState*)pSearchStateSpace->searchgoalstate->PlannerSpecificData)->g == INFINITECOST)
01103 break;
01104
01105 }
01106
01107
01108 #if DEBUG
01109 fflush(fDeb);
01110 #endif
01111
01112 PathCost = ((WeightedAStarState*)pSearchStateSpace->searchgoalstate->PlannerSpecificData)->g;
01113 MaxMemoryCounter += environment_->StateID2IndexMapping.size()*sizeof(int);
01114
01115 SBPL_PRINTF("MaxMemoryCounter = %d\n", MaxMemoryCounter);
01116
01117 int solcost = INFINITECOST;
01118 bool ret = false;
01119 if(PathCost == INFINITECOST)
01120 {
01121 SBPL_PRINTF("could not find a solution\n");
01122 ret = false;
01123 }
01124 else
01125 {
01126 SBPL_PRINTF("solution is found\n");
01127 pathIds = GetSearchPath(pSearchStateSpace, solcost);
01128 ret = true;
01129 }
01130
01131 solfound = ret;
01132 searchtime = (clock()-TimeStarted)/((double)CLOCKS_PER_SEC);
01133 finalsolcost = solcost;
01134 SBPL_PRINTF("total expands this call = %d, planning time = %.3f secs, solution cost=%d\n",
01135 searchexpands, searchtime, solcost);
01136
01137
01138
01139
01140 return ret;
01141
01142 }
01143
01144
01145
01146
01147 int WeightedAStar::replan(double allocated_time_secs, vector<int>* solution_stateIDs_V)
01148 {
01149 int solcost;
01150
01151 return replan(allocated_time_secs, solution_stateIDs_V, &solcost);
01152
01153 }
01154
01155
01156 int WeightedAStar::replan(double allocated_time_secs, vector<int>* solution_stateIDs_V, int* psolcost)
01157 {
01158 bubbles.resize(environment_->getNumBubbles());
01159 for(unsigned int i=0; i<bubbles.size(); i++)
01160 bubbles[i].active = false;
01161
01162 vector<int> pathIds;
01163 bool bFound = false;
01164 int PathCost;
01165 bool bFirstSolution = this->bsearchuntilfirstsolution;
01166 bool bOptimalSolution = false;
01167 *psolcost = 0;
01168
01169 SBPL_PRINTF("planner: replan called (bFirstSol=%d, bOptSol=%d)\n", bFirstSolution, bOptimalSolution);
01170
01171
01172 if((bFound = Search(pSearchStateSpace_, pathIds, PathCost, bFirstSolution, bOptimalSolution, allocated_time_secs)) == false)
01173 {
01174 SBPL_PRINTF("failed to find a solution\n");
01175 }
01176
01177
01178 *solution_stateIDs_V = pathIds;
01179 *psolcost = PathCost;
01180
01181
01182
01183 return (int)bFound;
01184
01185 }
01186
01187
01188 int WeightedAStar::set_goal(int goal_stateID)
01189 {
01190
01191 SBPL_PRINTF("planner: setting goal to %d\n", goal_stateID);
01192 environment_->PrintState(goal_stateID, true, stdout);
01193
01194 if(bforwardsearch)
01195 {
01196 if(SetSearchGoalState(goal_stateID, pSearchStateSpace_) != 1)
01197 {
01198 SBPL_ERROR("ERROR: failed to set search goal state\n");
01199 return 0;
01200 }
01201 }
01202 else
01203 {
01204 if(SetSearchStartState(goal_stateID, pSearchStateSpace_) != 1)
01205 {
01206 SBPL_ERROR("ERROR: failed to set search start state\n");
01207 return 0;
01208 }
01209 }
01210
01211 return 1;
01212 }
01213
01214
01215 int WeightedAStar::set_start(int start_stateID)
01216 {
01217
01218 SBPL_PRINTF("planner: setting start to %d\n", start_stateID);
01219 environment_->PrintState(start_stateID, true, stdout);
01220
01221 if(bforwardsearch)
01222 {
01223
01224 if(SetSearchStartState(start_stateID, pSearchStateSpace_) != 1)
01225 {
01226 SBPL_ERROR("ERROR: failed to set search start state\n");
01227 return 0;
01228 }
01229 }
01230 else
01231 {
01232 if(SetSearchGoalState(start_stateID, pSearchStateSpace_) != 1)
01233 {
01234 SBPL_ERROR("ERROR: failed to set search goal state\n");
01235 return 0;
01236 }
01237 }
01238
01239 return 1;
01240
01241 }
01242
01243
01244
01245 void WeightedAStar::costs_changed(StateChangeQuery const & stateChange)
01246 {
01247
01248
01249 pSearchStateSpace_->bReinitializeSearchStateSpace = true;
01250
01251
01252 }
01253
01254 void WeightedAStar::costs_changed()
01255 {
01256
01257 pSearchStateSpace_->bReinitializeSearchStateSpace = true;
01258
01259 }
01260
01261
01262
01263 int WeightedAStar::force_planning_from_scratch()
01264 {
01265 SBPL_PRINTF("planner: forceplanfromscratch set\n");
01266
01267 pSearchStateSpace_->bReinitializeSearchStateSpace = true;
01268
01269 return 1;
01270 }
01271
01272
01273 int WeightedAStar::set_search_mode(bool bSearchUntilFirstSolution)
01274 {
01275
01276 SBPL_PRINTF("planner: search mode set to %d\n", bSearchUntilFirstSolution);
01277
01278 bsearchuntilfirstsolution = bSearchUntilFirstSolution;
01279
01280 return 1;
01281 }
01282
01283
01284 void WeightedAStar::print_searchpath(FILE* fOut)
01285 {
01286 PrintSearchPath(pSearchStateSpace_, fOut);
01287 }
01288
01289
01290
01291
01292
01293 void WeightedAStar::getSearchStats(bool* solutionFound, int* numExpands, int* solutionCost, double* searchTime){
01294 *solutionFound = solfound;
01295 *numExpands = searchexpands;
01296 *solutionCost = finalsolcost;
01297 *searchTime = searchtime;
01298 }
01299