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

# /Users/fpy/titan-1-5-MBARI/Mers/Generators/BFIG/unit-tests/BFIGGeneratorTester.h

    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 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #ifndef GENERATOR_TESTER_HEADER_FILE
    00015 #define GENERATOR_TESTER_HEADER_FILE
    00016
    00017 #include "BFIGHelper.h"
    00018
    00019 #include <tut.h>
    00020 #include <Mers/Generators/BFIG/BFIGGenerator.h>
    00021 #include <Mers/Models/Base/BaseModelDomainArray.h>
    00022 #include <Mers/Utils/Containers/IntegerArray.h>
    00023 #include <Mers/Utils/Logging/logger.h>
    00024
    00025 using Mers::Generators::BFIG::BFIGGenerator;
    00026 using log4cplus::Logger;
    00027
    00028 namespace tut {
    00029
    00036         class BFIGGeneratorTester : public BFIGHelper
    00037         {
    00038         public:
    00042                 BFIGGeneratorTester();
    00043
    00047                 virtual ~BFIGGeneratorTester();
    00048
    00049         protected:
    00070                 void initBFIGGenTest(unsigned int numDomains,
    00071                         const struct BFIGDomainEntry * domains,
    00072                         bool isProbabilities,
    00073                         unsigned int numVariables,
    00074                         const struct BFIGVariableEntry * variables,
    00075                         double cutoffCost = 1e10) throw();
    00076
    00115                 template<int n> void testBFIG(unsigned int numberOfIConf,
    00116                                                                                                                                         const unsigned int initialConflictsData[][n],
    00117                                                                                                                                         unsigned int numberOfResults,
    00118                                                                                                                                         const unsigned int expectedResultsData[][n],
    00119                                                                                                                                         const unsigned int runtimeConflictsData[][n],
    00120                                                                                                                                         const bool acceptList[],
    00121                                                                                                                                         bool verifyTermination = true)
    00122                 {
    00123                         long maximumVisitedStateCount = 10000000;
    00124                         long maxSize = 10000000;
    00125                         bool expand = true;
    00126
    00127                         Logger log = Logger::getInstance(
    00128                                 "Mers.Generators.BFIG.unit-tests.BFIGGeneratorTester");
    00129
    00130                         LOG4CPLUS_DEBUG_STR(log, "*** Testing BFIG ***");
    00131                         LOG4CPLUS_DEBUG(log, "*** With " << numberOfIConf
    00132                                 << " initial conflicts and " << numberOfResults
    00133                                 << " expected results.");
    00134
    00135                         ensure("BFIG Generator != NULL", mGenerator != NULL);
    00136
    00137                         std::vector<Mers::Models::Base::BaseModelAssignmentArray *> expectedResults;
    00138                         std::vector<Mers::Models::Base::BaseModelAssignmentArray *> initialConflicts;
    00139                         std::vector<Mers::Models::Base::BaseModelAssignmentArray *> runtimeConflicts;
    00140
    00141                         loadAssignments(expectedResults, numberOfResults, expectedResultsData);
    00142                         loadAssignments(initialConflicts, numberOfIConf, initialConflictsData);
    00143                         loadAssignments(runtimeConflicts, numberOfResults, runtimeConflictsData);
    00144
    00145 #ifdef VERBOSE
    00146                         cout << endl << "Running with Initial Conflicts" << endl;
    00147                         for(unsigned int i = 0; i < initialConflicts.size(); i++) {
    00148                                 initialConflicts[i]->printArray(cout, ", ");
    00149                                 cout << endl;
    00150                         }
    00151                         cout << "Runtime Data" << endl;
    00152                         for(unsigned int i = 0; i < runtimeConflicts.size(); i++) {
    00153                                 cout << "Conf: ";
    00154                                 runtimeConflicts[i]->printArray(cout, ", ");
    00155                                 cout << "; Exp Res: ";
    00156                                 expectedResults[i]->printArray(cout, ", ");
    00157                                 cout << "; " << (acceptList[i] ? "Accept" : "Reject") << endl;
    00158                         }
    00159 #endif
    00160
    00161
    00162                         ensure_equals("Expected number of results",
    00163                                 expectedResults.size(), numberOfResults);
    00164                         ensure_equals("Expected number of initial conflicts",
    00165                                 initialConflicts.size(), numberOfIConf);
    00166                         ensure_equals("Expected number of runtime conflicts",
    00167                                 runtimeConflicts.size(), numberOfResults);
    00168
    00169                         for(unsigned int c = 0; c < initialConflicts.size(); c++)
    00170                         {
    00171                                 mGenerator->addConflict(initialConflicts[c]);
    00172                         }
    00173
    00174                         for(unsigned int s = 0; s < expectedResults.size(); s++)
    00175                         {
    00176                                 // Get the next solution.
    00177                                 Mers::Models::Base::BaseModelAssignmentArray const * actualSolution =
    00178                                         mGenerator->generateBestImplicant(maximumVisitedStateCount,
    00179                                         maxSize, expand);
    00180                                 // Get the next expected solution.
    00181                                 Mers::Models::Base::BaseModelAssignmentArray const * expectedSolution =
    00182                                         expectedResults[s];
    00183
    00184                                 ensure("Expecting another solution", actualSolution != NULL);
    00185                                 ensure_equals("Expected full solution length is as reported",
    00186                                                                                         expectedSolution->getLength(),
    00187                                                                                         mGenerator->getFullAssignmentSize());
    00188                                 ensure_equals("Actual full solution length is reported",
    00189                                                                                         actualSolution->getLength(),
    00190                                                                                         mGenerator->getFullAssignmentSize());
    00191                                 ensure_equals("Generated Solution",
    00192                                                                                         *actualSolution, *expectedSolution);
    00193
    00194 #ifdef VERBOSE
    00195                                 cout << "Solution: ";
    00196                                 actualSolution->printArray(cout, ", ");
    00197                                 cout << endl;
    00198 #endif
    00199
    00200                                 // Mark this candidate if we're supposed to.
    00201                                 if(acceptList[s]) {
    00202                                         mGenerator->markBestFeasible();
    00203
    00204 #ifdef VERBOSE
    00205                                         cout << "Marked" << endl;
    00206 #endif
    00207                                 }
    00208                                 // Add new conflicts, if present (this means we're disallowing
    00209                                 // the empty conflict).
    00210                                 if(runtimeConflicts[s]->getLength() > 0) {
    00211                                         mGenerator->addConflict(runtimeConflicts[s]);
    00212
    00213 #ifdef VERBOSE
    00214                                         cout << "Conflict: ";
    00215                                         runtimeConflicts[s]->printArray(cout, ", ");
    00216                                         cout << endl;
    00217 #endif
    00218                                 }
    00219                         }
    00220
    00221                         if(verifyTermination) {
    00222                                 ensure("No additional solutions",
    00223                                         mGenerator->generateBestImplicant(maximumVisitedStateCount,
    00224                                         maxSize, expand) == NULL);
    00225
    00226 #ifdef VERBOSE
    00227                                 cout << "All Done" << endl;
    00228 #endif
    00229                         }
    00230
    00231                         for(unsigned int s = 0; s < expectedResults.size(); s++)
    00232                                 delete expectedResults[s];
    00233                         for(unsigned int s = 0; s < initialConflicts.size(); s++)
    00234                                 delete initialConflicts[s];
    00235                         for(unsigned int s = 0; s < runtimeConflicts.size(); s++)
    00236                                 delete runtimeConflicts[s];
    00237
    00238                         LOG4CPLUS_DEBUG_STR(log, "*** BFIG Test Complete");
    00239                 }
    00240
    00241         protected:
    00243                 BFIGGenerator * mGenerator;
    00244         };
    00245
    00246 }
    00247
    00248 #endif