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