00001 #include <cerrno>
00002 #include <cstring>
00003 #include <iomanip>
00004
00005 #include "SARSOP.h"
00006 #include "SARSOPPrune.h"
00007 #include "MOMDP.h"
00008 #include "BeliefValuePairPoolSet.h"
00009 #include "AlphaPlanePoolSet.h"
00010 #include "BeliefTreeNode.h"
00011 #include "CPTimer.h"
00012 #include "BlindLBInitializer.h"
00013 #include "FastInfUBInitializer.h"
00014 #include "BackupBeliefValuePairMOMDP.h"
00015 #include "BackupAlphaPlaneMOMDP.h"
00016
00017
00018 void printSampleBelief(list<cacherow_stval>& beliefNStates)
00019 {
00020 cout << "SampledBelief" << endl;
00021 for(list<cacherow_stval>::iterator iter =beliefNStates.begin(); iter != beliefNStates.end() ; iter ++)
00022 {
00023 cout << "[ " <<(*iter).row << " : " << (*iter).sval << " ] ";
00024 }
00025 cout << endl;
00026 }
00027
00028
00029 void SARSOP::progressiveIncreasePolicyInteval(int& numPolicies)
00030 {
00031 if (numPolicies == 0)
00032 {
00033 this->solverParams->interval *= 10;
00034 numPolicies++;
00035
00036 }
00037 else
00038 {
00039 if (numPolicies == 5)
00040 {
00041 this->solverParams->interval *= 5;
00042 }
00043 else if (numPolicies == 10)
00044 {
00045 this->solverParams->interval *= 2;
00046 }
00047 else if (numPolicies == 15)
00048 {
00049 this->solverParams->interval *= 4;
00050 }
00051
00052 numPolicies++;
00053 }
00054 }
00055
00056 SARSOP::SARSOP(SharedPointer<MOMDP> problem, SolverParams * solverParams)
00057 {
00058 this->problem = problem;
00059 this->solverParams = solverParams;
00060 beliefForest = new BeliefForest();
00061 sampleEngine = new SampleBP();
00062 ((SampleBP*)sampleEngine)->setup(problem, this);
00063 beliefForest->setup(problem, this->sampleEngine, &this->beliefCacheSet);
00064 numBackups = 0;
00065 }
00066
00067 SARSOP::~SARSOP(void)
00068 {
00069 }
00070
00071
00072 void SARSOP::solve(SharedPointer<MOMDP> problem)
00073 {
00074 try
00075 {
00076
00077 bool skipSample = false;
00078
00079
00080 int policyIndex, checkIndex;
00081 bool stop;
00082 std::vector<cacherow_stval> currentBeliefIndexArr;
00083
00084
00085 currentBeliefIndexArr.resize(problem->initialBeliefX->size());
00086
00087 FOR(r,currentBeliefIndexArr.size())
00088 {
00089 currentBeliefIndexArr[r].sval = -1;
00090 currentBeliefIndexArr[r].row = -1;
00091 }
00092
00093 list<cacherow_stval> sampledBeliefs;
00094 cacherow_stval lastRootBeliefIndex;
00095 lastRootBeliefIndex.row = -1;
00096 lastRootBeliefIndex.sval = -1;
00097
00098 int numPolicies = 0;
00099
00100
00101
00102 runtimeTimer.start();
00103 cout << "\nSARSOP initializing ..." << endl;
00104
00105 initialize(problem);
00106
00107 if(problem->XStates->size() != 1 && problem->hasPOMDPMatrices())
00108 {
00109
00110 problem->deletePOMDPMatrices();
00111 }
00112 if(problem->XStates->size() != 1 && problem->hasPOMDPMatrices())
00113 {
00114
00115 problem->deletePOMDPMatrices();
00116 }
00117 if(problem->XStates->size() != 1 && problem->hasPOMDPMatrices())
00118 {
00119
00120 problem->deletePOMDPMatrices();
00121 }
00122 GlobalResource::getInstance()->getInstance()->solving = true;
00123
00124
00125
00126
00127 stop = false;
00128
00129
00130 BeliefForest& globalroot = *(sampleEngine->getGlobalNode());
00131 beliefForest->globalRootPrepare();
00132
00133
00134 FOR(r, globalroot.sampleRootEdges.size()) {
00135
00136 if (NULL != globalroot.sampleRootEdges[r])
00137 {
00138 BeliefTreeNode& thisRoot = *(globalroot.sampleRootEdges[r]->sampleRoot);
00139 sampleEngine->samplePrepare(thisRoot.cacheIndex);
00140 }
00141 }
00142
00143
00144
00145 policyIndex = 0;
00146 checkIndex = 0;
00147
00148 lapTimer.start();
00149 elapsed = runtimeTimer.elapsed();
00150 printf(" initialization time : %.2fs\n", elapsed);
00151
00152
00153 DEBUG_LOG(logFilePrint(policyIndex-1););
00154
00155
00156 double currentElapsed = lapTimer.elapsed();
00157 lapTimer.pause();
00158 runtimeTimer.pause();
00159
00160
00161
00162 DEBUG_LOG(writeIntermediatePolicyTraceToFile(0, 0.0, this->solverParams->outPolicyFileName, this->solverParams->problemName ); );
00163 DEBUG_LOG(cout << "Initial policy written" << endl;);
00164
00165 printHeader();
00166
00167 lapTimer.resume();
00168 runtimeTimer.resume();
00169
00170 policyIndex++;
00171 elapsed += currentElapsed;
00172
00173
00174 lapTimer.restart();
00175
00176 alwaysPrint();
00177
00178 DEBUG_LOG( logFilePrint(policyIndex-1); );
00179
00180
00181
00182 int lastTrial = ((SampleBP*)sampleEngine)->numTrials;
00183
00184
00185 int activeRoot = -1;
00186
00187 while(!stop)
00188 {
00189 int numTrials = ((SampleBP*)sampleEngine)->numTrials;
00190 if( this->solverParams->targetTrials > 0 && numTrials > this->solverParams->targetTrials )
00191 {
00192
00193 break;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 if (activeRoot == -1)
00203 {
00204 FOR (r, globalroot.getGlobalRootNumSampleroots())
00205 {
00206 SampleRootEdge* eR = globalroot.sampleRootEdges[r];
00207
00208 if (NULL != eR)
00209 {
00210 BeliefTreeNode & sn = *eR->sampleRoot;
00211 sampledBeliefs.clear();
00212 sampledBeliefs.push_back(sn.cacheIndex);
00213
00214 DEBUG_TRACE( printSampleBelief(sampledBeliefs); );
00215
00216 currentBeliefIndexArr[r] = backup(sampledBeliefs);
00217 }
00218 }
00219 sampledBeliefs.clear();
00220
00221 }
00222 else
00223 {
00224
00225 if (((SampleBP *)sampleEngine)->newTrialFlagArr[activeRoot] == 1)
00226 {
00227
00228 DEBUG_TRACE( printSampleBelief(sampledBeliefs); );
00229 currentBeliefIndexArr[activeRoot] = backup(sampledBeliefs);
00230 lastRootBeliefIndex = currentBeliefIndexArr[activeRoot];
00231 sampledBeliefs.clear();
00232
00233 FOR (r, globalroot.getGlobalRootNumSampleroots())
00234 {
00235 SampleRootEdge* eR = globalroot.sampleRootEdges[r];
00236
00237 if (NULL != eR) {
00238 BeliefTreeNode & sn = *eR->sampleRoot;
00239
00240 if( !((sn.cacheIndex.row == lastRootBeliefIndex.row)&&(sn.cacheIndex.sval == lastRootBeliefIndex.sval)) ) {
00241
00242
00243
00244 double lbVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->LB;
00245 double ubVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->UB;
00246
00247 if (!((ubVal - lbVal) <= 0))
00248 {
00249
00250 sampledBeliefs.clear();
00251 sampledBeliefs.push_back(sn.cacheIndex);
00252
00253 DEBUG_TRACE( cout << "LB backup only " << endl; );
00254 DEBUG_TRACE( printSampleBelief(sampledBeliefs); );
00255
00256 backupLBonly(sampledBeliefs);
00257
00258 }
00259 }
00260 }
00261 }
00262 sampledBeliefs.clear();
00263
00264 ((SampleBP *)sampleEngine)->newTrialFlagArr[activeRoot] = 0;
00265
00266 }
00267 else
00268 {
00269 DEBUG_TRACE( printSampleBelief(sampledBeliefs); );
00270 currentBeliefIndexArr[activeRoot] = backup(sampledBeliefs);
00271
00272 }
00273
00274 }
00275
00276
00277 if (activeRoot == -1)
00278 {
00279
00280 FOR (r, globalroot.getGlobalRootNumSampleroots())
00281 {
00282 SampleRootEdge* eR = globalroot.sampleRootEdges[r];
00283 if (NULL != eR)
00284 {
00285 activeRoot = r;
00286 break;
00287 }
00288 }
00289 }
00290 else
00291 {
00292 int currActiveRoot = activeRoot;
00293 bool passedcurrActiveRoot = false;
00294 while(true){
00295
00296
00297 if ((activeRoot == currActiveRoot) && passedcurrActiveRoot)
00298 { skipSample = true;
00299 break;
00300 }
00301
00302 if (activeRoot == currActiveRoot) passedcurrActiveRoot = true;
00303
00304 if (activeRoot == (globalroot.getGlobalRootNumSampleroots()-1))
00305 activeRoot = 0;
00306 else activeRoot++;
00307
00308 if (globalroot.sampleRootEdges[activeRoot] != NULL){
00309
00310
00311
00312 cacherow_stval currCacheIndex = globalroot.sampleRootEdges[activeRoot]->sampleRoot->cacheIndex;
00313 double lbVal = beliefCacheSet[currCacheIndex.sval]->getRow(currCacheIndex.row)->LB;
00314 double ubVal = beliefCacheSet[currCacheIndex.sval]->getRow(currCacheIndex.row)->UB;
00315 if (!((((SampleBP *)sampleEngine)->trialTargetPrecisionArr[activeRoot] == -1)&&((ubVal - lbVal) <= 0) ))
00316 {
00317 break;
00318 }
00319 }
00320 }
00321 }
00322
00323
00324
00325
00326
00327 if (!skipSample)
00328 {
00329
00330 sampledBeliefs = sampleEngine->sample(currentBeliefIndexArr[activeRoot], activeRoot);
00331 }
00332
00333
00334
00335
00336
00337
00338 pruneEngine->prune();
00339
00340
00341
00342 if(this->solverParams->interval > 0 || this->solverParams->timeoutSeconds > 0)
00343 {
00344
00345 if((numBackups/CHECK_INTERVAL) >= checkIndex)
00346 {
00347
00348 checkIndex++;
00349
00350
00351 if (this->solverParams->interval > 0)
00352 {
00353 double currentElapsed = lapTimer.elapsed();
00354 if(currentElapsed > this->solverParams->interval)
00355 {
00356
00357
00358
00359 lapTimer.pause();
00360 runtimeTimer.pause();
00361
00362 writeIntermediatePolicyTraceToFile(numTrials, runtimeTimer.elapsed(), this->solverParams->outPolicyFileName, this->solverParams->problemName );
00363
00364 lapTimer.resume();
00365 runtimeTimer.resume();
00366
00367 policyIndex++;
00368 elapsed += currentElapsed;
00369 cout << "Intermediate policy written(interval: "<< this->solverParams->interval <<")" << endl;
00370
00371
00372 lapTimer.restart();
00373
00374
00375 DEBUG_LOG(logFilePrint(policyIndex-1););
00376 DEBUG_LOG( progressiveIncreasePolicyInteval(numPolicies); );
00377
00378
00379 }
00380 }
00381
00382 else if(this->solverParams->timeoutSeconds >0)
00383 {
00384 double currentElapsed = runtimeTimer.elapsed();
00385 elapsed = currentElapsed;
00386 }
00387 }
00388 }
00389
00390
00391 print();
00392
00393
00394 stop = stopNow();
00395
00396
00397 }
00398
00399 }
00400
00401 catch(bad_alloc &e)
00402 {
00403
00404
00405 cout << "Memory limit reached, trying to write out policy" << endl;
00406
00407 }
00408
00409
00410 FOR (stateidx, lowerBoundSet->set.size())
00411 {
00412 lowerBoundSet->set[stateidx]->pruneEngine->prunePlanes();
00413 }
00414
00415 printHeader();
00416 alwaysPrint();
00417 printDivider();
00418 DEBUG_LOG(logFilePrint(-1););
00419
00420
00421 cout << endl << "Writing out policy ..." << endl;
00422 cout << " output file : " << this->solverParams->outPolicyFileName << endl;
00423 writePolicy(this->solverParams->outPolicyFileName, this->solverParams->problemName);
00424 }
00425
00426
00427
00428
00429
00430
00431 void SARSOP::print()
00432 {
00433 if(numBackups/CHECK_INTERVAL>printIndex)
00434 {
00435 printIndex++;
00436
00437 alwaysPrint();
00438 }
00439 }
00440
00441
00442
00443
00444
00445 void SARSOP::alwaysPrint()
00446 {
00447
00448
00449
00450
00451
00452
00453 double currentTime =0;
00454 if(this->solverParams->interval >0)
00455 {
00456 currentTime = elapsed + lapTimer.elapsed();
00457 }
00458 else
00459 {
00460 currentTime = runtimeTimer.elapsed();
00461 }
00462
00463 cout.precision(6);
00464 cout <<" ";cout.width(8);cout << left << currentTime;
00465
00466
00467 int numTrials = ((SampleBP*)sampleEngine)->numTrials;
00468
00469 cout.width(7);cout << left <<numTrials << " ";
00470
00471 cout.width(8);cout << left << numBackups << " ";
00472
00473
00474
00475
00476
00477
00478 double lb = 0, ub = 0, width = 0;
00479
00480 BeliefForest& globalRoot = *(sampleEngine->getGlobalNode());
00481 FOR (r, globalRoot.getGlobalRootNumSampleroots())
00482 {
00483 SampleRootEdge* eR = globalRoot.sampleRootEdges[r];
00484 if (NULL != eR)
00485 {
00486 BeliefTreeNode & sn = *eR->sampleRoot;
00487 double lbVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->LB;
00488 double ubVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->UB;
00489 lb += eR->sampleRootProb * lbVal;
00490 ub += eR->sampleRootProb * ubVal;
00491 width += eR->sampleRootProb * (ubVal - lbVal);
00492 }
00493 }
00494
00495
00496
00497
00498
00499
00500
00501 cout.width(10); cout << left << lb<< " ";
00502 cout.width(10); cout << left << ub<< " ";
00503
00504
00505 double precision = width;
00506
00507 cout.width(11);
00508 cout << left << precision << " ";
00509 int numAlphas = 0;
00510 FOR (setIdx, beliefCacheSet.size())
00511 {
00512 numAlphas += (int)lowerBoundSet->set[setIdx]->planes.size();
00513 }
00514
00515
00516 cout.width(9);cout << left << numAlphas;
00517
00518
00519
00520 cout.width(9);cout << left << sampleEngine->numStatesExpanded;
00521
00522
00523 printf("\n");
00524
00525 }
00526
00527
00528
00529
00530
00531
00532
00533 void SARSOP::logFilePrint(int index)
00534 {
00535
00536
00537
00538
00539
00540
00541
00542 FILE *fp = fopen("solve.log", "a");
00543 if(fp==NULL){
00544 cerr << "can't open logfile\n";
00545 exit(1);
00546 }
00547
00548
00549 fprintf(fp,"%d ",index);
00550
00551
00552 int numTrials = ((SampleBP*)sampleEngine)->numTrials;
00553
00554 fprintf(fp,"%d ",numTrials);
00555
00556
00557
00558 int numAlphas = 0;
00559 FOR (setIdx, beliefCacheSet.size())
00560 {
00561
00562 numAlphas += (int)lowerBoundSet->set[setIdx]->planes.size();
00563 }
00564
00565 fprintf(fp, "%d ", numAlphas);
00566
00567 double currentTime =0;
00568 if(this->solverParams->interval >0)
00569 {
00570
00571
00572
00573 currentTime = elapsed + lapTimer.elapsed();
00574 fprintf(fp, "%.2f ", currentTime);
00575
00576 }
00577 else{
00578
00579
00580
00581 currentTime = runtimeTimer.elapsed();
00582 fprintf(fp, "%.2f ", currentTime);
00583
00584 }
00585
00586 fprintf(fp,"\n");
00587
00588 fclose(fp);
00589 }
00590
00591
00592 bool SARSOP::stopNow(){
00593 bool stop = false;
00594
00595 double width = 0;
00596 BeliefForest& globalRoot = *(sampleEngine->getGlobalNode());
00597
00598
00599
00600 FOR (r, globalRoot.getGlobalRootNumSampleroots())
00601 {
00602 SampleRootEdge* eR = globalRoot.sampleRootEdges[r];
00603 if (NULL != eR) {
00604 BeliefTreeNode & sn = *eR->sampleRoot;
00605 double lbVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->LB;
00606 double ubVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->UB;
00607 width += eR->sampleRootProb * (ubVal - lbVal);
00608 }
00609 }
00610
00611 if(GlobalResource::getInstance()->userTerminatedG)
00612 {
00613 stop = true;
00614 }
00615
00616 if ((width) < this->solverParams->targetPrecision)
00617 {
00618 alwaysPrint();
00619 printDivider();
00620 printf("\nSARSOP finishing ...\n");
00621 printf(" target precision reached\n");
00622 printf(" target precision : %f\n", this->solverParams->targetPrecision);
00623 printf(" precision reached : %f \n", width);
00624
00625 stop = true;
00626 }
00627 if (this->solverParams->timeoutSeconds > 0)
00628 {
00629 if (elapsed > this->solverParams->timeoutSeconds )
00630 {
00631 printDivider();
00632 printf("\nSARSOP finishing ...\n");
00633 printf(" Preset timeout reached\n");
00634 printf(" Timeout : %fs\n", this->solverParams->timeoutSeconds );
00635 printf(" Actual Time : %fs\n", elapsed);
00636 stop = true;
00637 }
00638 }
00639 return stop;
00640 }
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674 void SARSOP::writeIntermediatePolicyTraceToFile(int trial, double time, const string& outFileName, string problemName)
00675 {
00676 stringstream newFileNameStream;
00677 string outputBasename = GlobalResource::parseBaseNameWithPath(outFileName);
00678 newFileNameStream << outputBasename << "_" << trial << "_" << time << ".policy";
00679 string newFileName = newFileNameStream.str();
00680 cout << "Writing policy file: " << newFileName << endl;
00681 writePolicy(newFileName, problemName);
00682 }
00683
00684
00685 BeliefTreeNode& SARSOP::getMaxExcessUncRoot(BeliefForest& globalroot)
00686 {
00687
00688 double maxExcessUnc = -99e+20;
00689 int maxExcessUncRoot = -1;
00690 double width;
00691 double lbVal, ubVal;
00692
00693 FOR (r, globalroot.getGlobalRootNumSampleroots()) {
00694 SampleRootEdge* eR = globalroot.sampleRootEdges[r];
00695 if (NULL != eR) {
00696 BeliefTreeNode & sn = *eR->sampleRoot;
00697 lbVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->LB;
00698 ubVal = beliefCacheSet[sn.cacheIndex.sval]->getRow(sn.cacheIndex.row)->UB;
00699 width = eR->sampleRootProb * (ubVal - lbVal);
00700
00701 if (width > maxExcessUnc)
00702 {
00703 maxExcessUnc = width;
00704 maxExcessUncRoot = r;
00705 }
00706 }
00707
00708 }
00709
00710 return *(globalroot.sampleRootEdges[maxExcessUncRoot]->sampleRoot);
00711
00712 }
00713
00714 void SARSOP::backup(BeliefTreeNode* node)
00715 {
00716 upperBoundSet->backup(node);
00717 lowerBoundSet->backup(node);
00718 }
00719
00720 void SARSOP::initialize(SharedPointer<MOMDP> problem)
00721 {
00722 printIndex = 0;
00723
00724 int xStateNum = problem->XStates->size();
00725 beliefCacheSet.resize(xStateNum);
00726 lbDataTableSet.resize(xStateNum);
00727 ubDataTableSet.resize(xStateNum);
00728
00729 for(States::iterator iter = problem->XStates->begin(); iter != problem->XStates->end(); iter ++ )
00730 {
00731 beliefCacheSet[iter.index()] = new BeliefCache();
00732 lbDataTableSet[iter.index()] = new IndexedTuple<AlphaPlanePoolDataTuple>();
00733 ubDataTableSet[iter.index()] = new IndexedTuple<BeliefValuePairPoolDataTuple>();
00734 }
00735
00736 initializeUpperBound(problem);
00737
00738 upperBoundSet->setBeliefCache(beliefCacheSet);
00739 upperBoundSet->setDataTable(ubDataTableSet);
00740
00741 initializeLowerBound(problem);
00742 lowerBoundSet->setBeliefCache(beliefCacheSet);
00743 lowerBoundSet->setDataTable(lbDataTableSet);
00744
00745 initializeBounds(this->solverParams->targetPrecision);
00746 initSampleEngine(problem);
00747
00748 pruneEngine = new SARSOPPrune(this);
00749
00750 }
00751 void SARSOP::initSampleEngine(SharedPointer<MOMDP> problem)
00752 {
00753 sampleEngine->appendOnGetNodeHandler(&SARSOP::onGetNode);
00754 binManagerSet = new BinManagerSet(upperBoundSet);
00755 ((SampleBP*)sampleEngine)->setBinManager(binManagerSet);
00756 ((SampleBP*)sampleEngine)->setRandomization(solverParams->randomizationBP);
00757
00758 }
00759 void SARSOP::initializeUpperBound(SharedPointer<MOMDP> problem)
00760 {
00761 upperBoundSet = new BeliefValuePairPoolSet(upperBoundBackup);
00762 upperBoundSet->setProblem(problem);
00763 upperBoundSet->setSolver(this);
00764 upperBoundSet->initialize();
00765 upperBoundSet->appendOnBackupHandler(&SARSOP::onUpperBoundBackup);
00766 ((BackupBeliefValuePairMOMDP*)upperBoundBackup)->boundSet = upperBoundSet;
00767 }
00768 void SARSOP::initializeLowerBound(SharedPointer<MOMDP> problem)
00769 {
00770 lowerBoundSet = new AlphaPlanePoolSet(lowerBoundBackup);
00771 lowerBoundSet->setProblem(problem);
00772 lowerBoundSet->setSolver(this);
00773 lowerBoundSet->initialize();
00774 lowerBoundSet->appendOnBackupHandler(&SARSOP::onLowerBoundBackup);
00775 lowerBoundSet->appendOnBackupHandler(&SARSOPPrune::onLowerBoundBackup);
00776 ((BackupAlphaPlaneMOMDP* )lowerBoundBackup)->boundSet = lowerBoundSet;
00777 }
00778
00779 void SARSOP::initializeBounds(double _targetPrecision)
00780 {
00781 double targetPrecision = _targetPrecision * CB_INITIALIZATION_PRECISION_FACTOR;
00782
00783 CPTimer heurTimer;
00784 heurTimer.start();
00785 BlindLBInitializer blb(problem, lowerBoundSet);
00786 blb.initialize(targetPrecision);
00787 elapsed = heurTimer.elapsed();
00788
00789 DEBUG_LOG( cout << fixed << setprecision(2) << elapsed << "s blb.initialize(targetPrecision) done" << endl; );
00790
00791 heurTimer.restart();
00792 FastInfUBInitializer fib(problem, upperBoundSet);
00793 fib.initialize(targetPrecision);
00794 elapsed = heurTimer.elapsed();
00795 DEBUG_LOG(cout << fixed << setprecision(2) << elapsed << "s fib.initialize(targetPrecision) done" << endl;);
00796
00797 FOR (state_idx, problem->XStates->size())
00798 {
00799 upperBoundSet->set[state_idx]->cornerPointsVersion++;
00800 }
00801
00802
00803
00804 numBackups = 0;
00805 }
00806
00807 cacherow_stval SARSOP::backup(list<cacherow_stval> beliefNStates)
00808 {
00809
00810 cacherow_stval rowNState, nextRowNState;
00811 nextRowNState.row = -1;
00812
00813
00814 LISTFOREACH(cacherow_stval, beliefNState, beliefNStates)
00815 {
00816
00817 rowNState = *beliefNState;
00818 nextRowNState = backup(rowNState);
00819 }
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831 if(nextRowNState.row== -1)
00832 {
00833 printf("Error: backup list empty\n");
00834 cout << "In SARSOP::backup( )" << endl;
00835 }
00836 return nextRowNState;
00837 }
00838
00839
00840 cacherow_stval SARSOP::backupLBonly(list<cacherow_stval> beliefNStates){
00841
00842 cacherow_stval rowNState, nextRowNState;
00843 nextRowNState.row = -1;
00844
00845
00846 LISTFOREACH(cacherow_stval, beliefNState, beliefNStates) {
00847
00848 rowNState = *beliefNState;
00849 nextRowNState = backupLBonly(rowNState);
00850 }
00851
00852
00853 if(nextRowNState.row== -1){
00854 printf("Error: backup list empty\n");
00855 cout << "In SARSOP::backupLBonly( )" << endl;
00856 }
00857 return nextRowNState;
00858 }
00859
00860
00861
00862
00863
00864
00865
00866
00867 cacherow_stval SARSOP::backup(cacherow_stval beliefNState)
00868 {
00869
00870 unsigned int stateidx = beliefNState.sval;
00871 int row = beliefNState.row;
00872
00873
00874
00875
00876 BeliefTreeNode* cn = beliefCacheSet[stateidx]->getRow(row)->REACHABLE;
00877
00878
00879
00880
00881 numBackups++;
00882
00883 GlobalResource::getInstance()->setTimeStamp(numBackups);
00884 lowerBoundSet->backup(cn);
00885 upperBoundSet->backup(cn);
00886
00887 GlobalResource::getInstance()->setTimeStamp(numBackups);
00888 return beliefNState;
00889 }
00890
00891 cacherow_stval SARSOP::backupLBonly(cacherow_stval beliefNState){
00892
00893 unsigned int stateidx = beliefNState.sval;
00894 int row = beliefNState.row;
00895
00896
00897
00898
00899 BeliefTreeNode* cn = beliefCacheSet[stateidx]->getRow(row)->REACHABLE;
00900
00901
00902 GlobalResource::getInstance()->setTimeStamp(numBackups);
00903 lowerBoundSet->backup(cn);
00904
00905 numBackups++;
00906 GlobalResource::getInstance()->setTimeStamp(numBackups);
00907 return beliefNState;
00908 }
00909
00910 void SARSOP::writePolicy(string fileName, string problemName)
00911 {
00912 writeToFile(fileName, problemName);
00913 }
00914
00915 void SARSOP::writeToFile(const std::string& outFileName, string problemName)
00916 {
00917 lowerBoundSet->writeToFile(outFileName, problemName);
00918
00919 }
00920
00921 void SARSOP::printHeader(){
00922 cout << endl;
00923 printDivider();
00924 cout << " Time |#Trial |#Backup |LBound |UBound |Precision |#Alphas |#Beliefs " << endl;
00925 printDivider();
00926 }
00927
00928 void SARSOP::printDivider(){
00929 cout << "-------------------------------------------------------------------------------" << endl;
00930 }