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

# /Users/fpy/titan-1-5-MBARI/Mers/Estimation/Discrete/ModeEstimationPerformance/MEtimeReqsDriver.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 Timing Requirements Driver
    00014 //
    00015 //=============================================================================
    00016
    00017 #define HIDE_DATA 1
    00018
    00019 #include <Mers/Estimation/Discrete/ModeEstimationPerformance/MEperformanceDriverUtils.h>
    00020
    00021 void
    00022 printUse(void)
    00023 {
    00024         cout << "Use:" << endl;
    00025         cout << "ModeEstimationTiming.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-p <CCA File (.mof)> <CompiledObject File (.ccme)>" << endl;
    00031         cout << "\tOR" << endl;
    00032         cout << "\t-n <CCANNF File (.ccannf.xml)>" << endl;
    00033         cout << "\t[-b <Initialization Script File>]" << endl;
    00034         cout << "\t[-i <Input Script File>]" << endl;
    00035         cout << "\t[-o <Output File>]" << endl;
    00036         cout << "\t[-t <Timing Matlab Output File>]" << endl;
    00037         cout << "\t[-help]" << endl;
    00038         cout << "OR" << endl;
    00039         cout << "\t<CCA File>" << endl;
    00040         cout << endl;
    00041         return;
    00042 }
    00043
    00044
    00045 void
    00046 run_ME_shell(ModeEstimatorBase * meb, MEPerformanceMonitor * mepm, BaseModel &model, istream &inputStream, ostream &outStream, ostream &timeOutStream)
    00047 {
    00048         String          var_string, val_string;
    00049         String          inStr;
    00050         char            char_p[300];
    00051         bool            quit_shell = false;
    00052         bool            echoInput = false;
    00053         unsigned int    beliefEntryNum = 0;
    00054         bool            wasBeliefEntry = false;
    00055         bool            successfulEstimate = true;
    00056         bool            progressRequested = false;      // If a 'e -p' is issued, only an estimate is generated for testing
    00057                                                                                         // purposes. That way the test can run multiple times until sufficient
    00058                                                                                         // time has elapsed to get an accurate runtime. After the test is
    00059                                                                                         // complete, the progress is then issued to ME
    00060         bool            resetRequested = false;         // If a reset was requested during a timing loop, a reset must be called
    00061                                                                                         // each time the same test is run.
    00062
    00063         vector<String> varSvector;
    00064         vector<String> valSvector;
    00065         clock_t startTime = clock(), timeDiff;
    00066         unsigned int numEstLoop = 0;
    00067         unsigned int totalNumConflicts = 0;
    00068         unsigned int maxNumConflicts = 0;
    00069         varSvector.resize(0);
    00070         valSvector.resize(0);
    00071
    00072         timeOutStream << "%------------------------------------------------------------------------------------------" << endl;
    00073         timeOutStream << "% ME 1.2 Time Requirement Data" << endl;
    00074         timeOutStream << "%" << endl;
    00075         timeOutStream << "% time | #loops | #estimates | #faults |    ave     |    max     | #candidates | #estimates" << endl;
    00076         timeOutStream << "% (ms) |        | generated  |         | #conflicts | #conflicts |             | maintained" << endl;
    00077         timeOutStream << "%------------------------------------------------------------------------------------------" << endl << endl;
    00078         timeOutStream << "meData = [";
    00079
    00080         outStream << endl << "ME Time Requirements Shell: Enter 'h' for help.";
    00081         outStream << endl << endl;
    00082
    00083
    00084         for(unsigned int entryNumber=1; !quit_shell && !inputStream.eof(); entryNumber++)
    00085         {
    00086                 outStream << entryNumber << "> " << flush;
    00087                 eat_space_or_tab(inputStream);
    00088
    00089                 inputStream >> char_p;
    00090                 if(echoInput)
    00091                         outStream << char_p << " ";
    00092                 inStr = char_p;
    00093
    00094                 if(isNumeric(inStr) || (inStr.size() > 1 && inStr.find('/') != 0))
    00095                 {                                                       // Asserting command/observation/state
    00096                         var_string = inStr;
    00097                         eat_space_or_tab(inputStream);
    00098                         if(inputStream.peek() != '\n')
    00099                         {
    00100                                 inputStream >> char_p;
    00101                                 if(echoInput)
    00102                                         outStream << char_p << " ";
    00103                                 val_string = char_p;
    00104                                 eat_space_or_tab(inputStream);
    00105                                 varSvector.resize(varSvector.size()+1);
    00106                                 valSvector.resize(valSvector.size()+1);
    00107                                 varSvector[varSvector.size()-1] = var_string;
    00108                                 valSvector[valSvector.size()-1] = val_string;
    00109                                 assign(var_string, val_string, meb, model, outStream);
    00110                                 while(get_assignment_pairs(inputStream, outStream, var_string, val_string, echoInput))
    00111                                 {
    00112                                         assign(var_string, val_string, meb, model, outStream);
    00113                                         varSvector.resize(varSvector.size()+1);
    00114                                         valSvector.resize(valSvector.size()+1);
    00115                                         varSvector[varSvector.size()-1] = var_string;
    00116                                         valSvector[valSvector.size()-1] = val_string;
    00117                                 }
    00118                         }
    00119                         if(echoInput)
    00120                                 outStream << endl;
    00121                 }
    00122                 else if(inStr.find('/') != 0)   // Input is command since there are no 1 char variable names
    00123                 {
    00124                         switch (*inStr.c_str())
    00125                         {
    00126                                 case 'a':
    00127                                 case 'A':
    00128                                         // 'a' or 'A' corresponds to assert a sequence of (var=val)
    00129                                         eat_space_or_tab(inputStream);
    00130                                         if (inputStream.peek() != '\n')
    00131                                         {
    00132                                                 if (inputStream.peek() == '-')
    00133                                                 {
    00134                                                         inputStream >> char_p;
    00135                                                         if(echoInput)
    00136                                                                 outStream << char_p << endl;
    00137                                                         if(entryNumber - beliefEntryNum == 1 && wasBeliefEntry)         // If there has only been one loop and prev entry was a belief entry
    00138                                                         {
    00139                                                                 if(get_directed_assign(char_p[1], inputStream, outStream, meb, model, true, echoInput) && (char_p[1] == 'b' || char_p[1] == 'B'))
    00140                                                                         wasBeliefEntry = true;
    00141                                                         }
    00142                                                         else
    00143                                                         {
    00144                                                                 if(get_directed_assign(char_p[1], inputStream, outStream, meb, model, false, echoInput) && (char_p[1] == 'b' || char_p[1] == 'B'))
    00145                                                                         wasBeliefEntry = true;
    00146                                                         }
    00147                                                         if(wasBeliefEntry)              // Reset entry number on last belief input
    00148                                                                 beliefEntryNum = entryNumber;
    00149                                                 }
    00150                                                 else
    00151                                                 {
    00152                                                         while(get_assignment_pairs(inputStream, outStream, var_string, val_string, echoInput))
    00153                                                         {
    00154                                                                 assign(var_string, val_string, meb, model, outStream);
    00155                                                                 varSvector.resize(varSvector.size()+1);
    00156                                                                 valSvector.resize(valSvector.size()+1);
    00157                                                                 varSvector[varSvector.size()-1] = var_string;
    00158                                                                 valSvector[valSvector.size()-1] = val_string;
    00159                                                         }
    00160                                                 }
    00161                                         }
    00162                                         if(echoInput)
    00163                                                 outStream << endl;
    00164                                         break;
    00165                                 case 'e':
    00166                                 case 'E':
    00167                                         // 'e' or 'E' corresponds to estimate. '-p' progresses into the estimate
    00168
    00169                                         if(progressRequested)                   // If the progressRequested flag hasn't been reset and
    00170                                         {                                                               //  another estimate request was issued, we must first
    00171                                                 meb->progressToEstimate();      //  progress before generating the next estimate
    00172                                                 progressRequested = false;
    00173                                         }
    00174
    00175                                         eat_space_or_tab(inputStream);
    00176                                         if (inputStream.peek() == '\n')
    00177                                         {
    00178                                                 if(echoInput)
    00179                                                         outStream << endl;
    00180 //                                              outStream << "Calculating next trajectories..." << endl << endl;
    00181                                                 if(meb->findModeEstimate() == NULL)
    00182                                                         successfulEstimate = false;
    00183                                                 else
    00184                                                         successfulEstimate = true;
    00185 //                                              outStream << endl;
    00186 #ifndef HIDE_DATA
    00187                                                 meb->showNextTrajectories(outStream);
    00188 #endif
    00189                                         }
    00190                                         else
    00191                                         {
    00192                                                 inputStream >> char_p;
    00193                                                 if(echoInput)
    00194                                                         outStream << char_p << endl;
    00195                                                 if (char_p[0] == '-' && char_p[1] == 'p')
    00196                                                 {
    00197                                                         progressRequested = true;
    00198 //                                                      outStream << "Calculating next trajectories..." << endl;
    00199                                                         //if(meb->progressToEstimate() == NULL) // Temporarily just find ME for testing purposes
    00200                                                         if(meb->findModeEstimate() == NULL)             // Must actually progress once testing is completed
    00201                                                                 successfulEstimate = false;
    00202                                                         else
    00203                                                                 successfulEstimate = true;
    00204 //                                                      outStream << "Progressing into most likely estimate..." << endl << endl;
    00205 #ifndef HIDE_DATA
    00206                                                         meb->showCurrentTrajectories(outStream);
    00207 #endif
    00208                                                 }
    00209                                                 else
    00210                                                 {
    00211 //                                                      outStream << "Calculating next trajectories..." << endl;
    00212                                                         if(meb->findModeEstimate() == NULL)
    00213                                                                 successfulEstimate = false;
    00214                                                         else
    00215                                                                 successfulEstimate = true;
    00216 //                                                      outStream << endl;
    00217 #ifndef HIDE_DATA
    00218                                                         meb->showNextTrajectories(outStream);
    00219 #endif
    00220                                                 }
    00221                                         }
    00222                                         if(successfulEstimate)
    00223                                         {
    00224                                                 totalNumConflicts += mepm->getNumConflictsGenerated();
    00225                                                 if(mepm->getNumConflictsGenerated() > maxNumConflicts)
    00226                                                         maxNumConflicts = mepm->getNumConflictsGenerated();
    00227
    00228                                         }
    00229                                         break;
    00230
    00231                                 case 't':
    00232                                 case 'T':
    00233                                         // 't' or 'T' corresponds to time. '-b' starts the timer and '-e' ends it and records the data.
    00234                                         // Default is -b option
    00235                                         eat_space_or_tab(inputStream);
    00236                                         if (inputStream.peek() == '\n')
    00237                                         {
    00238                                                 if(echoInput)
    00239                                                         outStream << endl;
    00240                                                 outStream << "Starting timer..." << endl << endl;
    00241                                                 varSvector.resize(0);
    00242                                                 valSvector.resize(0);
    00243                                                 totalNumConflicts = 0;
    00244                                                 maxNumConflicts = 0;
    00245                                                 resetRequested = false;
    00246                                                 startTime = clock();
    00247                                         }
    00248                                         else
    00249                                         {
    00250                                                 inputStream >> char_p;
    00251                                                 if(echoInput)
    00252                                                         outStream << char_p << endl;
    00253                                                 if (char_p[0] == '-' && char_p[1] == 'b')
    00254                                                 {
    00255                                                         outStream << "Starting timer..." << endl << endl;
    00256                                                         varSvector.resize(0);
    00257                                                         valSvector.resize(0);
    00258                                                         totalNumConflicts = 0;
    00259                                                         maxNumConflicts = 0;
    00260                                                         resetRequested = false;
    00261                                                         startTime = clock();
    00262                                                 }
    00263                                                 else if (char_p[0] == '-' && char_p[1] == 'e')
    00264                                                 {
    00265                                                         numEstLoop = 1;
    00266                                                         timeDiff = clock() - startTime;
    00267                                                         if(timeDiff < 5000)                             // 200ms is innacurrate
    00268                                                         {
    00269                                                                 while(timeDiff < 5000)
    00270                                                                 {
    00271                                                                         numEstLoop++;
    00272                                                                         if(resetRequested)
    00273                                                                         {
    00274                                                                                 meb->resetCommandsAndObservations();
    00275                                                                                 for(unsigned int i=0; i<varSvector.size(); i++)
    00276                                                                                         assign(varSvector[i], valSvector[i], meb, model, outStream);
    00277                                                                         }
    00278                                                                         else if(varSvector.size() > 0)
    00279                                                                         {
    00280                                                                                 cout << "<MEtimeReqsDriver> WARNING: assertions between timer begin and timer end have been ignored." << endl;
    00281                                                                                 cout << "       Use the reset (r) command between timer beging and timer end in order to add assertions to the test." << endl;
    00282                                                                         }
    00283                                                                         if(meb->findModeEstimate() == NULL)
    00284                                                                         {
    00285                                                                                 successfulEstimate = false;
    00286                                                                                 break;
    00287                                                                         }
    00288                                                                         else
    00289                                                                         {
    00290                                                                                 successfulEstimate = true;
    00291                                                                                 totalNumConflicts += mepm->getNumConflictsGenerated();
    00292                                                                                 if(mepm->getNumConflictsGenerated() > maxNumConflicts)
    00293                                                                                         maxNumConflicts = mepm->getNumConflictsGenerated();
    00294                                                                         }
    00295                                                                         timeDiff = clock() - startTime;
    00296                                                                 }
    00297                                                         }
    00298                                                         if(successfulEstimate)
    00299                                                         {
    00300                                                                 outStream << "Stopping timer..." << endl << endl;
    00301                                                                 outStream << endl << "Elapsed time in ms: " << timeDiff << endl;
    00302                                                                 outStream << "Number of estimate loops: " << numEstLoop << endl;
    00303                                                                 outStream << "Number estatimtes generated: " << mepm->getNumEstimatesGenerated() << endl;
    00304                                                                 outStream << "Total number fault transitions: " << mepm->getNumberFaultTransitions() << endl;
    00305                                                                 outStream << "Most likely estimate fault transitions: " << mepm->getEstimateFaultTransitions(0) << endl;
    00306                                                                 outStream << "Number conflicts generated: " << mepm->getNumConflictsGenerated() << endl;
    00307                                                                 outStream << "Number candidates generated: " << mepm->getNumCandidatesGenerated() << endl;
    00308                                                                 outStream << "Number estimates maintained: " << meb->findNextTrajectories()->getNumberEstimates() << endl;
    00309                                                                 outStream << "Next trajectories: " << endl << *meb->findNextTrajectories() << endl;
    00310
    00311                                                                 timeOutStream << timeDiff << ", " << numEstLoop << ", " << mepm->getNumEstimatesGenerated()
    00312                                                                         << ", " << mepm->getNumberFaultTransitions() << ", " << (double)totalNumConflicts/(double)numEstLoop << ", "
    00313                                                                         << maxNumConflicts << ", " << mepm->getNumCandidatesGenerated() << ", "
    00314                                                                         << meb->findNextTrajectories()->getNumberEstimates() << ";" << endl;
    00315                                                         }
    00316                                                         if(progressRequested)                   // If the progressRequested flag was set, progress into
    00317                                                         {                                                               //  the estimate after test is complete
    00318                                                                 meb->progressToEstimate();
    00319                                                                 progressRequested = false;
    00320                                                         }
    00321                                                         resetRequested = false;
    00322                                                 }
    00323                                                 else
    00324                                                 {
    00325                                                         outStream << "Starting timer..." << endl << endl;
    00326                                                         varSvector.resize(0);
    00327                                                         valSvector.resize(0);
    00328                                                         totalNumConflicts = 0;
    00329                                                         maxNumConflicts = 0;
    00330                                                         resetRequested = false;
    00331                                                         startTime = clock();
    00332                                                 }
    00333                                         }
    00334
    00335                                         break;
    00336                                 case 'v':
    00337                                 case 'V':
    00338                                         // 'v' or 'V' corresponds to viewing data. Default is all variables
    00339                                         eat_space_or_tab(inputStream);
    00340                                         if (inputStream.peek() == '\n')
    00341                                         {
    00342                                                 if(echoInput)
    00343                                                         outStream << endl;
    00344                                                 print_estimator_variables('a', meb, outStream);
    00345                                         }
    00346                                         else
    00347                                         {
    00348                                                 inputStream >> char_p;
    00349                                                 if(echoInput)
    00350                                                         outStream << char_p << endl;
    00351                                                 if (char_p[0] == '-' && char_p[1] != '\0')
    00352                                                         print_estimator_variables(char_p[1], meb, outStream);
    00353                                                 else
    00354                                                         print_estimator_variables('a', meb, outStream);
    00355                                         }
    00356                                         break;
    00357                                 case 'd':
    00358                                 case 'D':
    00359                                         // 'd' or 'D' sets the debug level
    00360                                         eat_space_or_tab(inputStream);
    00361                                         if(inputStream.peek() != '\n')
    00362                                         {
    00363                                                 inputStream >> char_p;
    00364                                                 if(echoInput)
    00365                                                         outStream << char_p << endl;
    00366                                                 if(isNumeric(String(char_p)))
    00367                                                 {
    00368                                                         meb->setDebug(atoi(char_p));
    00369                                                         outStream << "Debug level set to " << char_p << endl;
    00370                                                 }
    00371                                                 else
    00372                                                         outStream << "Invalid debug level." << endl;
    00373                                         }
    00374                                         break;
    00375                                 case 'k':
    00376                                 case 'K':
    00377                                         // 'k' or 'K' sets the maximum number of trajectories
    00378                                         eat_space_or_tab(inputStream);
    00379                                         if(inputStream.peek() != '\n')
    00380                                         {
    00381                                                 inputStream >> char_p;
    00382                                                 if(echoInput)
    00383                                                         outStream << char_p << endl;
    00384                                                 if(isNumeric(String(char_p)))
    00385                                                 {
    00386                                                         outStream << "Setting maximum number of trajectories set to " << char_p << endl;
    00387                                                         meb->setNumberTrajectories(atoi(char_p));
    00388                                                 }
    00389                                                 else
    00390                                                         outStream << "Invalid maximum number of trajectories" << endl;
    00391                                         }
    00392                                         break;
    00393                                 case 'h':
    00394                                 case 'H':
    00395                                 case '?':
    00396                                         // 'h' or 'H' or '?' corresponds to display help.
    00397                                         eat_space_or_tab(inputStream);
    00398                                         if (inputStream.peek() == '\n')
    00399                                         {
    00400                                                 if(echoInput)
    00401                                                         outStream << endl;
    00402                                                 print_ME_shell_help('h', outStream);
    00403                                         }
    00404                                         else
    00405                                         {
    00406                                                 inputStream >> char_p;
    00407                                                 if(echoInput)
    00408                                                         outStream << char_p << endl;
    00409                                                 if (char_p[0] == '-' && char_p[1] != '\0')
    00410                                                 {
    00411                                                         print_ME_shell_help(char_p[1], outStream);
    00412                                                 }
    00413                                                 else
    00414                                                 {
    00415                                                         print_ME_shell_help('h', outStream);
    00416                                                 }
    00417                                         }
    00418                                         break;
    00419                                 case 'r':
    00420                                 case 'R':
    00421                                         // 'r' or 'R' corresponds to reseting asserted variables
    00422                                         meb->resetCommandsAndObservations();
    00423                                         resetRequested = true;
    00424                                         break;
    00425                                 case 'q':
    00426                                 case 'Q':
    00427                                         // 'q' or 'Q' corresponds to quit.
    00428                                         quit_shell = true;
    00429                                         break;
    00430                                 case '#':
    00431                                         echoInput = !echoInput;
    00432                                         outStream << "Echo input: " << (echoInput ? "ON" : "OFF") << endl << endl;
    00433                                         break;
    00434                                 case '\0':
    00435                                 case '\n':
    00436                                         // a blank line - skip.
    00437                                 case '/':
    00438                                         // assumed to be a comment
    00439                                         break;
    00440                                 default:
    00441                                         outStream << "WARNING: '" << inStr << "' is not a recognized command." << endl;
    00442                                         break;
    00443                         }
    00444                         if(*inStr.c_str() != 'a' && *inStr.c_str() != 'A') // assert functions' responsible for this
    00445                                 clear_stream(inputStream);
    00446                 }
    00447                 else
    00448                 {
    00449                         if(echoInput)
    00450                         {
    00451                                 inputStream.get(char_p, 300, '\n');
    00452                                 outStream << char_p;
    00453                         }
    00454                         clear_stream(inputStream);
    00455                 }
    00456                 if(echoInput)
    00457                         outStream << endl;
    00458         }
    00459         timeOutStream << "];" << endl;
    00460         return;
    00461 }
    00462