/Users/fpy/titan-1-5-MBARI/Mers/Generators/BFIG/unit-tests/BFIGHelper.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 // Organization:        Model-based Embedded and Robotic Systems Group
00010 //                                      Massachusetts Institute of Technology
00011 //
00013 
00014 #include "BFIGHelper.h"
00015 
00016 #include <Mers/Generators/BFIG/BFIGConstraint.h>
00017 
00019 
00020 using Mers::Generators::BFIG::BFIGVariable;
00021 using Mers::Generators::BFIG::BFIGConstraint;
00022 using Mers::Generators::BFIG::BFIGAssignment;
00023 using Mers::Models::Base::BaseModelVariable;
00024 using Mers::Models::Base::BaseModelDomain;
00025 using std::vector;
00026 
00028 
00029 tut::BFIGHelper::BFIGHelper() : 
00030         mBFIGVariables(false), mAllVariables(true), mDomains(true)
00031 {
00032 
00033 }
00034 
00036 
00037 tut::BFIGHelper::~BFIGHelper()
00038 {
00039         for(unsigned int d = 0; d < mBFIGVarList.size(); d++) {
00040                 if(mBFIGVarList[d] != NULL) {
00041                         delete mBFIGVarList[d];
00042                 }
00043         }
00044 }
00045 
00047 
00048 void tut::BFIGHelper::initBFIGData(unsigned int numDomains, 
00049         const struct BFIGDomainEntry * domains,
00050         unsigned int numVariables, const struct BFIGVariableEntry * variables)
00051         throw()
00052 {
00054         for(unsigned int d = 0; d < numDomains; d++) {
00055                 BaseModelDomain * domain = loadDomain(&(domains[d].bmData), d);
00056                 mDomains.pushBack(domain);
00057         }
00058 
00059         // Load our Variables & Probabilities/Costs
00060 
00061         // Note that our first probability/cost is stored in index 1.
00062         mProbabilities.push_back(0);
00063         for(unsigned int v = 0; v < numVariables; v++) {
00064                 BaseModelVariable * variable = 
00065                         loadVariable(mDomains, &(variables[v].bmData), v);
00066 
00067                 mAllVariables.pushBack(variable);
00068 
00069                 bool probsOrCostsPresent = variables[v].probsOrCosts != NULL;
00070                 if(variables[v].registered) {
00071 
00072                         mBFIGVariables.pushBack(variable);
00073                         mValueMap.pushBack(mProbabilities.size());
00074 
00075                         ensure("At least one cost metric provided", probsOrCostsPresent);
00076 
00077                         unsigned int domainSize = 
00078                                 mDomains[variables[v].bmData.domainIndex]->getLength();
00079                         double const * values = variables[v].probsOrCosts;
00080 
00081                         for(unsigned int l = 0; l < domainSize; l++) {
00082                                 mProbabilities.push_back(values[l]);
00083                         }
00084                 } else {
00085                         ensure("No cost metric provided for non-BFIG variables", 
00086                                                  !probsOrCostsPresent);
00087                 }
00088         }
00089 }
00090 
00092 
00093 void tut::BFIGHelper::initBFIGVariables(bool isProbabilities) throw()
00094 {
00095         ensure("Sufficient number of probabilities.", 
00096                                  mProbabilities.size() > mBFIGVariables.getLength());
00097         ensure("At least one variable.", mBFIGVariables.getLength() > 0);
00098         ensure("At value map matches variables.",
00099                                  mBFIGVariables.getLength() == mValueMap.getLength());
00100 
00101         unsigned int numVars = mBFIGVariables.getLength();
00102         mBFIGVarList.resize(numVars);
00103 
00104         for(unsigned int v = 0; v < numVars; v++) {
00105                 BaseModelVariable const * var = mBFIGVariables[v];
00106                 BaseModelDomain const * domain = var->getDomain();
00107                 const unsigned int domainSize = domain->getLength();
00108                 const unsigned int baseIndex = mValueMap[v];
00109                 vector<double> costs;
00110 
00111                 ensure("Value Map is in range.", 
00112                                          domainSize + baseIndex <= mProbabilities.size());
00113 
00114                 costs.resize(domainSize);
00115 
00116                 for(unsigned int d = 0; d < domainSize; d++) {
00117                         costs[d] = mProbabilities[d + baseIndex];
00118                 }
00119 
00120                 BFIGVariable * bfigVar = 
00121                         new BFIGVariable(v, var, costs, isProbabilities);
00122 
00123                 mBFIGVarList.push_back(bfigVar);
00124         }
00125 }
00126 
00128 
00129 void tut::BFIGHelper::loadAssignments(vector<BFIGAssignment *> &assignments,
00130         const unsigned int assignmentsData[])
00131 {
00132         const unsigned int noVal = (unsigned int) -1;
00133         const unsigned int n = mBFIGVarList.size();
00134 
00135         ensure("At least one variable exists.", n > 0);
00136 
00137         for(unsigned int v = 0; v < n; v++)
00138         {
00139                 unsigned int valIndex = assignmentsData[v];
00140                 if(valIndex != noVal) {
00141                         BFIGVariable * var = mBFIGVarList[v];
00142                         ensure("BFIGHelper::loadAssignments: Value Index is within range",
00143                                                  valIndex < var->getDomainSize());
00144                         
00145                         BFIGAssignment * asg = var->getAssignmentByValue(valIndex);
00146                         
00147                         assignments.push_back(asg);
00148                 }
00149         }
00150 }
00151 
00153 
00154 void tut::BFIGHelper::loadAssignments(
00155         vector<BFIGAssignment const *> &assignments,
00156         const unsigned int assignmentsData[])
00157 {
00158         vector<BFIGAssignment *> regAssignments;
00159         loadAssignments(regAssignments, assignmentsData);
00160 
00161         convertVector<BFIGAssignment>(assignments, regAssignments);
00162 }
00163 
00165 
00166 void tut::BFIGHelper::loadConstraints(vector<BFIGConstraint *> &constraints,
00167         const unsigned int constraintValues[], const bool constraintParity[])
00168 {
00169         const unsigned int noVal = (unsigned int) -1;
00170         const unsigned int n = mBFIGVarList.size();
00171 
00172         ensure("At least one variable exists.", n > 0);
00173 
00174         for(unsigned int v = 0; v < n; v++)
00175         {
00176                 unsigned int valIndex = constraintValues[v];
00177                 if(valIndex != noVal) {
00178                         BFIGVariable * var = mBFIGVarList[v];
00179                         ensure("BFIGHelper::loadConstraints: Value Index is within range",
00180                                                  valIndex < var->getDomainSize());
00181                         
00182                         BFIGAssignment * asg = var->getAssignmentByValue(valIndex);
00183                         
00184                         bool parity = constraintParity[v];
00185                         BFIGConstraint * cons = new BFIGConstraint(asg, parity);
00186 
00187                         constraints.push_back(cons);
00188                 }
00189         }
00190 }
00191 
00193 
00194 void tut::BFIGHelper::loadConstraints(
00195         vector<BFIGConstraint const *> &constraints,
00196         const unsigned int constraintValues[], const bool constraintParity[])
00197 {
00198         vector<BFIGConstraint *> regConstraints;
00199         loadConstraints(regConstraints, constraintValues, constraintParity);
00200 
00201         convertVector<BFIGConstraint>(constraints, regConstraints);
00202 }
00203 

Generated on Mon Dec 4 14:16:51 2006 for Mers by  doxygen 1.5.0