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
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
00090
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
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
00126
00127
00128
00129 string baseName = parseBaseNameWithPath(name);
00130 int slashIndex = baseName.find_last_of("/") ;
00131 int backSlashIndex = baseName.find_last_of("\\");
00132
00133
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
00153 return temp;
00154 }
00155
00156
00157 void GlobalResource::PBSolverFinishOneTrial()
00158 {
00159
00160
00161
00162
00163
00164
00165 gesTrialTime = solvingTimer.elapsed() - gesTrialTime;
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 currTrial ++;
00178
00179
00180
00181
00182
00183
00184 printf("TODO HERE....\n");
00185
00186 if(trialInterval > 0 )
00187 {
00188 if((currTrial % trialInterval) == 0)
00189 {
00190 saveIntermediatePolicy();
00191 }
00192 }
00193
00194 if(pbSolver->solverParams->interval > 0 )
00195 {
00196 double currentElapsed = solvingTimer.elapsed() - lastIntervalSaveTime;
00197 if(currentElapsed > pbSolver->solverParams->interval)
00198 {
00199 saveIntermediatePolicy();
00200
00201
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
00215
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
00241 lastIntervalSaveTime = solvingTimer.elapsed();
00242 }
00243
00244 GlobalResource::~GlobalResource()
00245 {
00246 shutdown();
00247 }
00248
00249
00250
00251 void GlobalResource::checkMemoryUsage()
00252 {
00253
00254 if(solverParams.memoryLimit != 0)
00255 {
00256
00257
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
00266 }
00267 }
00268 else
00269 {
00270 checkMemoryInterval++;
00271 }
00272
00273 }
00274 }
00275
00276 void GlobalResource::Benchmark()
00277 {
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00342
00344
00345
00347
00348
00349
00350
00351 }
00352
00353
00354
00355 }
00356