- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Estimation/Discrete/ModeEstimationPerformance/MEmemReqsDriver.cpp

    00001 //=============================================================================
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Author:                      Oliver B. Martin
    00010 // Organization:        Model-based Embedded and Robotic Systems Group
    00011 //                                      Massachusetts Institute of Technology
    00012 //
    00013 // Mode Estimation Memory Requirements Driver
    00014 //
    00015 //=============================================================================
    00016
    00017 #include <Mers/Estimation/Discrete/ModeEstimationPerformance/MEperformanceDriverUtils.h>
    00018
    00019 //#include "leaks.cpp"
    00020
    00021 void
    00022 printUse(void)
    00023 {
    00024         cout << "Use:" << endl;
    00025         cout << "MEmemReqsDriver.exe" << endl;
    00026         cout << "\t-m <CCA File (.mof)>" << endl;
    00027         cout << "\tOR" << endl;
    00028         cout << "\t-c <BaseModel File (.bm)> <CompiledObject File (.ccme)>" << endl;
    00029         cout << "\tOR" << endl;
    00030         cout << "\t-n <CCANNF File (.ccannf.xml)>" << endl;
    00031         cout << "\t[-b <Initialization Script File>]" << endl;
    00032         cout << "\t[-i <Input Script File>]" << endl;
    00033         cout << "\t[-o <Output File>]" << endl;
    00034         cout << "\t[-t <Memory Use Matlab Output File>]" << endl;
    00035         cout << "\t[-help]" << endl;
    00036         cout << "OR" << endl;
    00037         cout << "\t<CCA File>" << endl;
    00038         cout << endl;
    00039         return;
    00040 }
    00041
    00042
    00043 void
    00044 run_ME_shell(ModeEstimatorBase * meb, MEPerformanceMonitor * mepm, BaseModel &model, istream &inputStream, ostream &outStream, ostream &timeOutStream)
    00045 {
    00046         String          var_string, val_string;
    00047         String          inStr;
    00048         char            char_p[300];
    00049         bool            quit_shell = false;
    00050         bool            echoInput = false;
    00051         unsigned int    beliefEntryNum = 0;
    00052         bool            wasBeliefEntry = false;
    00053         bool            successfulEstimate = true;
    00054         bool            progressRequested = false;      // If a 'e -p' is issued, only an estimate is generated for testing
    00055                                                                                         // purposes. That way the test can run multiple times until sufficient
    00056                                                                                         // time has elapsed to get an accurate runtime. After the test is
    00057                                                                                         // complete, the progress is then issued to ME
    00058         double nominalProb = -1.0;
    00059         double faultProb = -1.0;
    00060
    00061         timeOutStream << "%------------------------------------------------------------------------------------------------------------------" << endl;
    00062         timeOutStream << "% ME 1.2 Memory Requirement Data" << endl;
    00063         timeOutStream << "%" << endl;
    00064         timeOutStream << "% #estimates | #faults | #conflicts | #elements | max #elements |  #bytes   | #candidates | #estimates |  belief" << endl;
    00065         timeOutStream << "% generated  |         |            | expanded  | in queue      | allocated |             | maintained |uncertainty" << endl;
    00066         timeOutStream << "%------------------------------------------------------------------------------------------------------------------" << endl << endl;
    00067         timeOutStream << "meMemoryData = [";
    00068
    00069         outStream << endl << "ME Memory Requirements Shell: Enter 'h' for help.";
    00070         outStream << endl << endl;
    00071
    00072         for(unsigned int entryNumber=1; !quit_shell && !inputStream.eof(); entryNumber++)
    00073         {
    00074                 outStream << entryNumber << "> " << flush;
    00075                 eat_space_or_tab(inputStream);
    00076
    00077                 inputStream >> char_p;
    00078                 if(echoInput)
    00079                         outStream << char_p << " ";
    00080                 inStr = char_p;
    00081
    00082                 if(isNumeric(inStr) || (inStr.size() > 1 && inStr.find('/') != 0))
    00083                 {                                                       // Asserting command/observation/state
    00084                         var_string = inStr;
    00085                         eat_space_or_tab(inputStream);
    00086                         if(inputStream.peek() != '\n')
    00087                         {
    00088                                 inputStream >> char_p;
    00089                                 if(echoInput)
    00090                                         outStream << char_p << " ";
    00091                                 val_string = char_p;
    00092                                 eat_space_or_tab(inputStream);
    00093                                 assign(var_string, val_string, meb, model, outStream);
    00094                                 while(get_assignment_pairs(inputStream, outStream, var_string, val_string, echoInput))
    00095                                 {
    00096                                         assign(var_string, val_string, meb, model, outStream);
    00097                                 }
    00098                         }
    00099                         if(echoInput)
    00100                                 outStream << endl;
    00101                 }
    00102                 else if(inStr.find('/') != 0)   // Input is command since there are no 1 char variable names
    00103                 {
    00104
    00105                         switch (*inStr.c_str())
    00106                         {
    00107                                 case 'a':
    00108                                 case 'A':
    00109                                         // 'a' or 'A' corresponds to assert a sequence of (var=val)
    00110                                         eat_space_or_tab(inputStream);
    00111                                         if (inputStream.peek() != '\n')
    00112                                         {
    00113                                                 if (inputStream.peek() == '-')
    00114                                                 {
    00115                                                         inputStream >> char_p;
    00116                                                         if(echoInput)
    00117                                                                 outStream << char_p << endl;
    00118                                                         if(entryNumber - beliefEntryNum == 1 && wasBeliefEntry)         // If there has only been one loop and prev entry was a belief entry
    00119                                                         {
    00120                                                                 if(get_directed_assign(char_p[1], inputStream, outStream, meb, model, true, echoInput) && (char_p[1] == 'b' || char_p[1] == 'B'))
    00121                                                                         wasBeliefEntry = true;
    00122                                                         }
    00123                                                         else
    00124                                                         {
    00125                                                                 if(get_directed_assign(char_p[1], inputStream, outStream, meb, model, false, echoInput) && (char_p[1] == 'b' || char_p[1] == 'B'))
    00126                                                                         wasBeliefEntry = true;
    00127                                                         }
    00128                                                         if(wasBeliefEntry)              // Reset entry number on last belief input
    00129                                                                 beliefEntryNum = entryNumber;
    00130                                                 }
    00131                                                 else
    00132                                                 {
    00133                                                         while(get_assignment_pairs(inputStream, outStream, var_string, val_string, echoInput))
    00134                                                         {
    00135                                                                 assign(var_string, val_string, meb, model, outStream);
    00136                                                         }
    00137                                                 }
    00138                                         }
    00139                                         if(echoInput)
    00140                                                 outStream << endl;
    00141                                         break;
    00142                                 case 'e':
    00143                                 case 'E':
    00144                                         // 'e' or 'E' corresponds to estimate. '-p' progresses into the estimate
    00145
    00146                                         if(progressRequested)                   // If the progressRequested flag hasn't been reset and
    00147                                         {                                                               //  another estimate request was issued, we must first
    00148                                                 meb->progressToEstimate();      //  progress before generating the next estimate
    00149                                                 progressRequested = false;
    00150                                         }
    00151
    00152                                         eat_space_or_tab(inputStream);
    00153                                         if (inputStream.peek() == '\n')
    00154                                         {
    00155                                                 if(echoInput)
    00156                                                         outStream << endl;
    00157 //                                              outStream << "Calculating next trajectories..." << endl << endl;
    00158                                                 if(meb->findModeEstimate() == NULL)
    00159                                                         successfulEstimate = false;
    00160                                                 else
    00161                                                         successfulEstimate = true;
    00162 //                                              outStream << endl;
    00163 //                                              meb->showNextTrajectories(outStream);
    00164                                         }
    00165                                         else
    00166                                         {
    00167                                                 inputStream >> char_p;
    00168                                                 if(echoInput)
    00169                                                         outStream << char_p << endl;
    00170                                                 if (char_p[0] == '-' && char_p[1] == 'p')
    00171                                                 {
    00172                                                         progressRequested = true;
    00173 //                                                      outStream << "Calculating next trajectories..." << endl;
    00174                                                         //if(meb->progressToEstimate() == NULL) // Temporarily just find ME for testing purposes
    00175                                                         if(meb->findModeEstimate() == NULL)             // Must actually progress once testing is completed
    00176                                                                 successfulEstimate = false;
    00177                                                         else
    00178                                                                 successfulEstimate = true;
    00179 //                                                      outStream << "Progressing into most likely estimate..." << endl << endl;
    00180 //                                                      meb->showCurrentTrajectories(outStream);
    00181                                                 }
    00182                                                 else
    00183                                                 {
    00184 //                                                      outStream << "Calculating next trajectories..." << endl;
    00185                                                         if(meb->findModeEstimate() == NULL)
    00186                                                                 successfulEstimate = false;
    00187                                                         else
    00188                                                                 successfulEstimate = true;
    00189 //                                                      outStream << endl;
    00190 //                                                      meb->showNextTrajectories(outStream);
    00191                                                 }
    00192                                         }
    00193                                         break;
    00194
    00195                                 case 't':
    00196                                 case 'T':
    00197                                         // 't' or 'T' corresponds to time. '-b' starts the timer and '-e' ends it and records the data.
    00198                                         // Default is -b option
    00199                                         eat_space_or_tab(inputStream);
    00200                                         if (inputStream.peek() == '\n')
    00201                                         {
    00202                                                 if(echoInput)
    00203                                                         outStream << endl;
    00204                                                 outStream << "Starting memory monitoring..." << endl << endl;
    00205                                                 //beginMonitoringStack();
    00206                                         }
    00207                                         else
    00208                                         {
    00209                                                 inputStream >> char_p;
    00210                                                 if(echoInput)
    00211                                                         outStream << char_p << endl;
    00212                                                 if (char_p[0] == '-' && char_p[1] == 'b')
    00213                                                 {
    00214                                                         outStream << "Starting memory monitoring..." << endl << endl;
    00215                                                         //beginMonitoringStack();
    00216                                                 }
    00217                                                 else if (char_p[0] == '-' && char_p[1] == 'e')
    00218                                                 {
    00219
    00220                                                         if(successfulEstimate)
    00221                                                         {
    00222                                                                 outStream << "Stopping memory monitoring..." << endl << endl;
    00223                                                                 outStream << "Number estatimtes generated: " << mepm->getNumEstimatesGenerated() << endl;
    00224                                                                 outStream << "Number of fault transitions: " << mepm->getNumberFaultTransitions() << endl;
    00225                                                                 outStream << "Number conflicts generated: " << mepm->getNumConflictsGenerated() << endl;
    00226                                                                 outStream << "Number elements expanded: " << mepm->getNumElementsExpanded() << endl;
    00227                                                                 outStream << "Max number elements in queue: " << mepm->getMaxQueueSize() << endl;
    00228                                                                 //outStream << "Number bytes allocated this test: " << maxIncreaseSinceBeginMonitor() << endl;
    00229                                                                 //outStream << "Total number bytes currently allocated: " << currentStackSize() << endl;
    00230                                                                 //outStream << "Largest the stack has ever been: " << maxStackEver() << endl;
    00231                                                                 outStream << "Number candidates generated: " << mepm->getNumCandidatesGenerated() << endl;
    00232                                                                 outStream << "Number estimates maintained: " << meb->findNextTrajectories()->getNumberEstimates() << endl;
    00233                                                                 outStream << "Accumulated Uncertainty: " << mepm->getAccumulatedUncertainty() << endl;
    00234
    00235
    00236
    00237                                                                 outStream << "Current Belief State: " << endl;
    00238                                                                 outStream << *(meb->findNextTrajectories()) << endl;
    00239
    00240
    00241                                                                 timeOutStream << mepm->getNumEstimatesGenerated() << ", " << mepm->getNumberFaultTransitions() << ", "
    00242                                                                         << mepm->getNumConflictsGenerated() << ", " << mepm->getNumElementsExpanded() << ", "
    00243                                                                         << mepm->getMaxQueueSize() << ", "
    00244                                                                         //<< maxIncreaseSinceBeginMonitor() << ", "
    00245                                                                         << mepm->getNumCandidatesGenerated() << ", "
    00246                                                                         << meb->findNextTrajectories()->getNumberEstimates() << ", "
    00247                                                                         << mepm->getAccumulatedUncertainty() << ", ";
    00248
    00249                                                                 const Trajectories * currentTrajs = meb->findNextTrajectories();
    00250                                                                 nominalProb = -1.0;
    00251                                                                 faultProb = -1.0;
    00252                                                                 for(unsigned int estNum = 0; estNum < currentTrajs->getNumberEstimates(); estNum++)
    00253                                                                 {
    00254                                                                         if((currentTrajs->getEstimate(estNum)->getAssignmentByUID(0))->getValue()->getUID() == 1 &&
    00255                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(4))->getValue()->getUID() == 3 &&
    00256                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(8))->getValue()->getUID() == 1 &&
    00257                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(12))->getValue()->getUID() == 3 &&
    00258                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(15))->getValue()->getUID() == 4 &&
    00259                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(17))->getValue()->getUID() == 2 &&
    00260                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(20))->getValue()->getUID() == 4 &&
    00261                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(26))->getValue()->getUID() == 2)
    00262                                                                                 nominalProb = (currentTrajs->getEstimate(estNum))->getProbability();
    00263                                                                         else if((currentTrajs->getEstimate(estNum)->getAssignmentByUID(0))->getValue()->getUID() == 1 &&
    00264                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(4))->getValue()->getUID() == 3 &&
    00265                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(8))->getValue()->getUID() == 1 &&
    00266                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(12))->getValue()->getUID() == 3 &&
    00267                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(15))->getValue()->getUID() == 4 &&
    00268                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(17))->getValue()->getUID() == 2 &&
    00269                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(20))->getValue()->getUID() == 1 &&
    00270                                                                                 (currentTrajs->getEstimate(estNum)->getAssignmentByUID(26))->getValue()->getUID() == 2)
    00271                                                                                 faultProb = (currentTrajs->getEstimate(estNum))->getProbability();
    00272
    00273                                                                 }
    00274                                                                 if(nominalProb < -0.000001)
    00275                                                                 {
    00276                                                                         cerr << "Couldn't find nominal mode!" << endl;
    00277                                                                         nominalProb = 0.0;
    00278                                                                 }
    00279                                                                 if(faultProb < -0.000001)
    00280                                                                 {
    00281                                                                         cerr << "Couldn't find single point failure mode!" << endl;
    00282                                                                         faultProb = 0.0;
    00283                                                                 }
    00284                                                                 timeOutStream << nominalProb << ", " << faultProb << ";" << endl;
    00285                                                         }
    00286                                                         if(progressRequested)                   // If the progressRequested flag was set, progress into
    00287                                                         {                                                               //  the estimate after test is complete
    00288                                                                 meb->progressToEstimate();
    00289                                                                 progressRequested = false;
    00290                                                         }
    00291                                                 }
    00292                                                 else
    00293                                                 {
    00294                                                         outStream << "Starting memory monitoring..." << endl << endl;
    00295                                                         //beginMonitoringStack();
    00296                                                 }
    00297                                         }
    00298
    00299                                         break;
    00300                                 case 'v':
    00301                                 case 'V':
    00302                                         // 'v' or 'V' corresponds to viewing data. Default is all variables
    00303                                         eat_space_or_tab(inputStream);
    00304                                         if (inputStream.peek() == '\n')
    00305                                         {
    00306                                                 if(echoInput)
    00307                                                         outStream << endl;
    00308                                                 print_estimator_variables('a', meb, outStream);
    00309                                         }
    00310                                         else
    00311                                         {
    00312                                                 inputStream >> char_p;
    00313                                                 if(echoInput)
    00314                                                         outStream << char_p << endl;
    00315                                                 if (char_p[0] == '-' && char_p[1] != '\0')
    00316                                                         print_estimator_variables(char_p[1], meb, outStream);
    00317                                                 else
    00318                                                         print_estimator_variables('a', meb, outStream);
    00319                                         }
    00320                                         break;
    00321                                 case 'd':
    00322                                 case 'D':
    00323                                         // 'd' or 'D' sets the debug level
    00324                                         eat_space_or_tab(inputStream);
    00325                                         if(inputStream.peek() != '\n')
    00326                                         {
    00327                                                 inputStream >> char_p;
    00328                                                 if(echoInput)
    00329                                                         outStream << char_p << endl;
    00330                                                 if(isNumeric(String(char_p)))
    00331                                                 {
    00332                                                         meb->setDebug(atoi(char_p));
    00333                                                         outStream << "Debug level set to " << char_p << endl;
    00334                                                 }
    00335                                                 else
    00336                                                         outStream << "Invalid debug level." << endl;
    00337                                         }
    00338                                         break;
    00339                                 case 'k':
    00340                                 case 'K':
    00341                                         // 'k' or 'K' sets the maximum number of trajectories
    00342                                         eat_space_or_tab(inputStream);
    00343                                         if(inputStream.peek() != '\n')
    00344                                         {
    00345                                                 inputStream >> char_p;
    00346                                                 if(echoInput)
    00347                                                         outStream << char_p << endl;
    00348                                                 if(isNumeric(String(char_p)))
    00349                                                 {
    00350                                                         outStream << "Setting maximum number of trajectories set to " << char_p << endl;
    00351                                                         meb->setNumberTrajectories(atoi(char_p));
    00352                                                 }
    00353                                                 else
    00354                                                         outStream << "Invalid maximum number of trajectories" << endl;
    00355                                         }
    00356                                         break;
    00357                                 case 'h':
    00358                                 case 'H':
    00359                                 case '?':
    00360                                         // 'h' or 'H' or '?' corresponds to display help.
    00361                                         eat_space_or_tab(inputStream);
    00362                                         if (inputStream.peek() == '\n')
    00363                                         {
    00364                                                 if(echoInput)
    00365                                                         outStream << endl;
    00366                                                 print_ME_shell_help('h', outStream);
    00367                                         }
    00368                                         else
    00369                                         {
    00370                                                 inputStream >> char_p;
    00371                                                 if(echoInput)
    00372                                                         outStream << char_p << endl;
    00373                                                 if (char_p[0] == '-' && char_p[1] != '\0')
    00374                                                 {
    00375                                                         print_ME_shell_help(char_p[1], outStream);
    00376                                                 }
    00377                                                 else
    00378                                                 {
    00379                                                         print_ME_shell_help('h', outStream);
    00380                                                 }
    00381                                         }
    00382                                         break;
    00383                                 case 'r':
    00384                                 case 'R':
    00385                                         // 'r' or 'R' corresponds to reseting asserted variables
    00386                                         meb->resetCommandsAndObservations();
    00387                                         break;
    00388                                 case 'q':
    00389                                 case 'Q':
    00390                                         // 'q' or 'Q' corresponds to quit.
    00391                                         quit_shell = true;
    00392                                         break;
    00393                                 case '#':
    00394                                         echoInput = !echoInput;
    00395                                         outStream << "Echo input: " << (echoInput ? "ON" : "OFF") << endl << endl;
    00396                                         break;
    00397                                 case '\0':
    00398                                 case '\n':
    00399                                         // a blank line - skip.
    00400                                 case '/':
    00401                                         // assumed to be a comment
    00402                                         break;
    00403                                 default:
    00404                                         outStream << "WARNING: '" << inStr << "' is not a recognized command." << endl;
    00405                                         break;
    00406                         }
    00407                         if(*inStr.c_str() != 'a' && *inStr.c_str() != 'A') // assert functions' responsible for this
    00408                                 clear_stream(inputStream);
    00409                 }
    00410                 else
    00411                 {
    00412                         if(echoInput)
    00413                         {
    00414                                 inputStream.get(char_p, 300, '\n');
    00415                                 outStream << char_p;
    00416                         }
    00417                         clear_stream(inputStream);
    00418                 }
    00419                 if(echoInput)
    00420                         outStream << endl;
    00421         }
    00422         timeOutStream << "];" << endl << endl;
    00423         //timeOutStream << "maxNumBytesAllocated = " << maxStackEver() << ";" << endl;
    00424
    00425         return;
    00426 }