GlobalResource.cpp
Go to the documentation of this file.
00001 
00005 #include <sstream>
00006 #include "GlobalResource.h"
00007 #include "CPMemUtils.h"
00008 
00009 using namespace std;
00010 
00011 namespace momdp
00012 {
00013 
00014         GlobalResource* GlobalResource::singleInstance = NULL;
00015 
00016         void GlobalResource::incTimeStamp()
00017         {
00018                 timeStamp ++;
00019         }
00020         int GlobalResource::getTimeStamp()
00021         {
00022                 return timeStamp;
00023         }
00024         void GlobalResource::setTimeStamp(int newTimeStamp)
00025         {
00026                 timeStamp = newTimeStamp;
00027         }
00028 
00029         double GlobalResource::getRunTime()
00030         {
00031                 return solvingTimer.elapsed();
00032         }
00033 
00034         void GlobalResource::init()
00035         {
00036         }
00037 
00038         GlobalResource::GlobalResource() 
00039         {
00040                 problem = NULL;
00041                 noPolicyOutput = false;
00042 
00043                 checkMemoryInterval = 0;
00044                 benchmarkMode = false;
00045                 simLookahead = false;
00046                 randSeed = time(0);
00047                 migsPathFile = NULL;
00048                 migsPathFileNum = -1;
00049 
00050                 binaryPolicy = false;
00051                 migsUniformSamplingMileStone = false;
00052 
00053                 timeStamp = 0;
00054                 solving = false;
00055                 noSarsop = false;
00056 
00057                 currTrial = 0;
00058 
00059                 userTerminatedG = false;
00060                 mdpSolution = false;
00061 
00062 
00063                 pomdpLoadTime = -1.0;
00064                 pomdpInitializationTime = -1.0;
00065 
00066                 // stats
00067                 hashCollision = 0;
00068                 hashRequest = 0;
00069                 hashSame = 0;
00070 
00071                 nInterStates = 0;
00072                 nSamples = 0;
00073                 nTimes = 0;
00074                 th = 0.0;
00075                 epi = 0.0;
00076                 stateGraphR = 0.0;
00077                 nInitMil = 0;
00078 
00079                 trialInterval = 0;
00080 
00081                 gesGenStateMapTime = 0.0;
00082                 gesGenGuideMapTime = 0.0;
00083                 gesSampleTime = 0.0;
00084                 gesTrialTime = 0.0;
00085                 totUpdGuideMapTime = 0.0;
00086                 limNotImproved =0.0;
00087 
00088                 wallClockTotalTimer.start();
00089                 //solvingTimer;
00090                 //solvingOneTrialTimer;
00091                 lastIntervalSaveTime = 0.0;
00092 
00093                 pbSolver = NULL;
00094 
00095                 memoryUsage = 0;
00096 
00097                 logLevel = 0;
00098 
00099                 policyIndex = 0;
00100         }
00101 
00102         void GlobalResource::PBSolverPostInit()
00103         {
00104                 pomdpInitializationTime = solvingTimer.elapsed();
00105                 printf("time spent in initializing [%.2fs] \n", pomdpInitializationTime);
00106                 //solvingTimer.restart();
00107                 if(pbSolver->solverParams->interval > 0 )
00108                 {
00109                         saveIntermediatePolicy();
00110                 }
00111 
00112         }
00113 
00114         string GlobalResource::parseBaseNameWithPath(string name)
00115         {
00116                 int extIndex = name.find_last_of(".");
00117                 if(extIndex ==string::npos)
00118                 {
00119                         return name;
00120                 }
00121                 return name.substr(0, extIndex);
00122         }
00123         string GlobalResource::parseBaseNameWithoutPath(string name)
00124         {
00125                 //string lowerName = name;
00126                 //transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
00127                 //int extIndex = lowerName.find_last_of(".pomdp") - 5;
00128 
00129                 string baseName = parseBaseNameWithPath(name);
00130                 int slashIndex = baseName.find_last_of("/") ;
00131                 int backSlashIndex = baseName.find_last_of("\\");
00132                 //cout << "slashIndex  " << slashIndex << endl;
00133                 //cout << "backSlashIndex " << backSlashIndex << endl;
00134 
00135                 int start = 0;
00136                 if(slashIndex != string::npos && backSlashIndex != string::npos)
00137                 {
00138                         start = max(slashIndex, backSlashIndex);
00139                 }
00140 
00141                 if(slashIndex != string::npos)
00142                 {
00143                         start = slashIndex;
00144                 }
00145 
00146                 if(backSlashIndex != string::npos )
00147                 {
00148                         start = backSlashIndex ;
00149                 }
00150                 start += 1;
00151                 string temp = name.substr(start);
00152                 //cout << "Basename: " << temp << endl;
00153                 return temp;
00154         }
00155 
00156 
00157         void GlobalResource::PBSolverFinishOneTrial()
00158         {
00159                 // print out stats
00160                 //
00161 
00162                 // How to get #alpha-vectors ? no AlphaPlanePool here.
00163                 // Is timeStamp == #backups ?
00164                 // rootNodeIndex in beliefCache, is it always 0 ?
00165                 gesTrialTime = solvingTimer.elapsed() - gesTrialTime;
00166 
00167 
00168                 // print stats
00169                 /*
00170                 printf("Time for backup %.2f s, #alpha %d, #backups %d, V(b0) %.5f\n",  
00171                 gesTrialTime, 
00172                 pbSolver->bounds->alphaPlanePool->planes.size() , 
00173                 getTimeStamp(),
00174                 pbSolver->bounds->beliefCache->getRow( 0)->LB );
00175 
00176                 */
00177                 currTrial ++;
00178                 //printf("Time for backup %.2f s, #alpha %d, #backups %d, V(b0) %.5f\n",        solvingTimer.elapsed(), pbSolver->bounds->alphaPlanePool->planes.size() , getTimeStamp(),       pbSolver->beliefCache->getRow( 0)->LB );
00179                 //printf("Time elapsed %.2f s, #alpha %d, #backups %d, V(b0) %.5f\n",
00180                 //      solvingTimer.elapsed(),
00181                 //      pbSolver->bounds->alphaPlanePool->planes.size() , 
00182                 //      getTimeStamp(),
00183                 //      pbSolver->beliefCache->getRow( 0)->LB );
00184                 printf("TODO HERE....\n");
00185 
00186                 if(trialInterval > 0 )
00187                 {
00188                         if((currTrial % trialInterval) == 0)
00189                         {
00190                                 saveIntermediatePolicy();
00191                         }
00192                 }
00193                 //check and write out policy file periodically
00194                 if(pbSolver->solverParams->interval > 0 )
00195                 {
00196                         double currentElapsed = solvingTimer.elapsed() - lastIntervalSaveTime;
00197                         if(currentElapsed > pbSolver->solverParams->interval)
00198                         {
00199                                 saveIntermediatePolicy();
00200 
00201                                 //pbSolver->solverParams->interval = pbSolver->solverParams->interval * 2;
00202 
00203                                 if(pbSolver->solverParams->interval > 60)
00204                                 {
00205                                         pbSolver->solverParams->interval = 60;
00206                                 }
00207                         }
00208                 }
00209 
00210         }
00211 
00212         void GlobalResource::saveIntermediatePolicy()
00213         {
00214                 //write out policy and reset parameters
00215                 // paused timer for writing policy
00216                 solvingTimer.pause();
00217 
00218                 stringstream fileName;
00219                 fileName << baseName<< "." << policyIndex << ".policy";
00220                 stringstream logLine;
00221                 logLine << "IntermediatePolicy," << fileName.str() << "," << solvingTimer.elapsed() ;
00222                 if(trialInterval > 0)
00223                 {
00224                         logLine << "," << currTrial;
00225                 }
00226                 logEntry(logLine.str());
00227 
00228                 policyIndex++;
00229                 if(benchmarkMode)
00230                 {
00231                         this->Benchmark();
00232                         cout << "Intermediate policy evaluated(interval: " << pbSolver->solverParams->interval << ") "  << endl;
00233                 }
00234                 else
00235                 {
00236                         pbSolver->writePolicy(fileName.str(), "");
00237                         cout << "Intermediate policy written(interval: " << pbSolver->solverParams->interval << "): " << fileName.str() << endl;
00238                 }
00239                 solvingTimer.resume();
00240                 //times(&last);//renew 'last' time flag
00241                 lastIntervalSaveTime = solvingTimer.elapsed();
00242         }
00243 
00244         GlobalResource::~GlobalResource()
00245         {
00246                 shutdown();
00247         }
00248 
00249 
00250 
00251         void GlobalResource::checkMemoryUsage()
00252         {
00253                 // check if memory usage is over limit
00254                 if(solverParams.memoryLimit != 0)
00255                 {
00256                         // if over limit , throw bad_alloc
00257                         //if(memoryUsage > memoryLimit)
00258                         if(checkMemoryInterval >= MEMORY_USAGE_CHECK_INTERVAL)
00259                         {
00260                                 checkMemoryInterval = 0;
00261                                 if(getCurrentProcessMemoryUsage() > solverParams.memoryLimit)
00262                                 {
00263                                         bad_alloc ba;
00264                                         throw ba;
00265                                         //throw new bad_alloc();
00266                                 }
00267                         }
00268                         else
00269                         {
00270                                 checkMemoryInterval++;
00271                         }
00272 
00273                 }
00274         }
00275 
00276         void GlobalResource::Benchmark()
00277         {
00278                 // TODO : benchmark
00279                 //belief_vector startBel;
00280                 //copy(startBel, problem->initialBelief);
00281                 //startBel.finalize();
00282                 //double globalRew = 0.0;
00283                 //double globalExpRew = 0.0;
00284 
00285                 //for(int currSim = 0;currSim < simNum; currSim++)
00286                 //{
00287                 //      unsigned int firstAction = 0;
00288 
00289                 //      std::ostringstream mystrm1;
00290 
00291                 //      BenchmarkEngine engine(*problem, *actS,tree);
00292 
00293                 //      if(startBel.filled() == 0)
00294                 //      {
00295                 //              int numStates = problem->getBeliefSize();
00296                 //              startBel.resize(numStates);
00297                 //              for(int i = 0;i<numStates; i++){
00298                 //                      startBel.push_back(i, ((double)1)/(double(numStates)));
00299                 //              }
00300                 //      }
00301 
00302                 //      double reward = 0, expReward = 0;
00303                 //      try
00304                 //      {
00305                 //              reward = 0;
00306                 //              expReward = 0;
00307                 //              firstAction = engine.runFor(simLen, startBel, reward, expReward, false);
00308                 //      }
00309                 //      catch(exception &e)
00310                 //      {
00311                 //              cout << "Memory limit reached, continuing without allocating new data structure";
00312                 //              cout << endl;
00313                 //              GlobalResource::getInstance()->memoryLimit = 0; // to stop new operator of DenseVector setting off bad_alloc
00314                 //              // should redo current round without allocating new memory
00315                 //              reward = 0;
00316                 //              expReward = 0;
00317                 //              firstAction = engine.runFor(simLen, startBel, reward, expReward, true);
00318                 //      }
00319 
00320                 //      //SYLMOD 07082008 temporary
00321                 //      //    cout << "Sim "<< currSim << ": reward = " <<reward <<", expected reward = " << expReward<<endl;
00322 
00323                 //      globalRew += reward/simNum;
00324                 //      globalExpRew += expReward/simNum;
00325                 //      expRewardRecord[currSim] = expReward;  //SYLADDED 07082008
00326                 //}
00327 
00329                 //double totalVar = 0;
00330                 //double currVar;
00331 
00332                 //for(int currSim = 0;currSim < simNum; currSim++)
00333                 //{
00334                 //      currVar = pow((expRewardRecord[currSim] - globalExpRew),2);
00335                 //      //cout << "expRewardRecord[currSim] : " << expRewardRecord[currSim] << " currVar : " << currVar << endl;
00336                 //      totalVar += currVar/simNum;
00337                 //}
00338 
00339                 //totalVar = sqrt(totalVar);    // sample's stddev
00340                 //double confInterval = 1.96 * totalVar/sqrt((double)simNum);   // stddev_mean = stddev_sample/sqrt(simNum)
00342 
00344                 //ostringstream stringStream;
00345                 //
00347                 //stringStream << "simNum = " << simNum<< " simLen = " << simLen;
00348                 //stringStream << " Global Reward = "<< globalRew << ", Global Exp Reward = " << globalExpRew << ", Global Exp Reward Stddev = " << totalVar<< ", 95Conf Interval = " << confInterval  << ", 95Conf Min = " << globalExpRew - confInterval << ", 95Conf Max = " << globalExpRew 
00349                 //      + confInterval << endl;
00350                 //this->logEntry(stringStream.str());
00351         }
00352 
00353 
00354         
00355 }
00356 


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29