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

# /Users/fpy/titan-1-5-MBARI/Mers/Generators/BFIG/BFIGVariable.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 //# CHECKED MJP 08-07-2002
    00014
    00015 #ifdef _MSC_VER
    00016 #pragma warning( disable : 4786 )
    00017 #endif
    00018
    00019 #include "BFIGVariable.h"
    00020
    00021 #include <Mers/Models/Base/BaseModelVariable.h>
    00022
    00023 #include <cstdlib>
    00024 #include <ctime>
    00025 #include <cstdio>
    00026 #include <cassert>
    00027 #include <cmath>
    00028 #include <iomanip>
    00029 #include "BFIGAssignment.h"
    00030
    00031 #include "ListManipulator.h"
    00032
    00033 using namespace std;
    00034 using namespace  Mers::Generators::BFIG;
    00035 using Mers::Utils::Lang::String;
    00036 using Mers::Utils::Containers::IntegerArray;
    00037 using Mers::Models::Base::BaseModelVariable;
    00038 using Mers::Models::Base::BaseModelDomain;
    00039
    00040 #include <Mers/Models/Base/BaseModelClause.h>
    00041 #include <Mers/Models/Base/BaseModelDomain.h>
    00042
    00043
    00044 namespace Mers {
    00045         namespace Generators {
    00046                 namespace BFIG {
    00049                         const double c_MinimumProbability = 1e-100;
    00050                 }
    00051         }
    00052 }
    00053
    00054 using Mers::Generators::BFIG::c_MinimumProbability;
    00055
    00056 //unsigned int BFIGVariable::varCount = 0;
    00057 //bool BFIGVariable::seeded = false;
    00058
    00059 /*#
    00060         // useful in conversions. Stupidly absent in C++...
    00061 //static String
    00062 //intString(int n)
    00063 //{
    00064 //      char buf[32];
    00065 //      sprintf(buf, "%d", n);
    00066 //      return String(buf);
    00067 //}
    00068 */
    00069
    00070 /*
    00071 static String
    00072 intString(unsigned int n)
    00073 {
    00074         char buf[32];
    00075         sprintf(buf, "%u", n);
    00076         return String(buf);
    00077 }
    00078 */
    00079
    00080 static bool
    00081 assignment_cost_less(BFIGAssignment const * a1,
    00082         BFIGAssignment const * a2)
    00083 {
    00085         return ( a1->getCost() < a2->getCost() );
    00086 }
    00087
    00088 //template void vector<BFIGAssignment *>::sort<assignment_comp_func>(assignment_comp_func);
    00089
    00090         //---------------------------------
    00091         //      CONSTRUCTORS
    00092         //---------------------------------
    00093 BFIGVariable::BFIGVariable(unsigned int index,
    00094         BaseModelVariable const * bmVariable,
    00095         vector<double> const & costList, bool probabilityValues)
    00096 : assignments(), assignmentsUnconst(),
    00097   bestAssignment(NULL), isSorted(false)
    00098 {
    00099         mBMVariable = bmVariable;
    00100         mIndex = index;
    00101         //varCount++;
    00102         //name = varCount;
    00103         //nameString = new String(varName);
    00104         //domainStrings = new String [domainList.size()];
    00105         //IntegerArray domainIntList;
    00106         //vector<String>::const_iterator dIter;
    00107         //unsigned int i;
    00108         //for (i = 0, dIter = domainList.begin(); dIter != domainList.end(); dIter++, i++)
    00109         //{
    00110         //      domainStrings[i] = (*dIter);
    00111         //      domainIntList.pushBack(i);
    00112         //}
    00113         init(costList, probabilityValues);
    00114 }
    00115
    00116 /*
    00117 BFIGVariable::BFIGVariable(unsigned int varName, IntegerArray const & domainList,
    00118                    vector<double> const & costList, bool probabilityValues,
    00119                    DynamicArrayTemplate<BaseModelSentence const> const & guardList)
    00120 : assignments(), assignmentsUnconst(), assignmentsReachable(),
    00121   bestAssignment(NULL), isSorted(false)
    00122 {
    00123         bmVariable = NULL;
    00124         nameString = NULL;
    00125         domainStrings = NULL;
    00126         name = varName;
    00127         init(domainList, costList, guardList, probabilityValues);
    00128 }
    00129
    00130
    00131 BFIGVariable::BFIGVariable(unsigned int varName, String varNameString,
    00132                    IntegerArray const & domainList,
    00133                    vector<String> const & domainListString,
    00134                    vector<double> const & costList, bool probabilityValues,
    00135                    DynamicArrayTemplate<BaseModelSentence const> const & guardList)
    00136 : assignments(), assignmentsUnconst(), assignmentsReachable(),
    00137   bestAssignment(NULL), isSorted(false)
    00138 {
    00139         assert(domainList.getLength() == domainListString.size());
    00140         bmVariable = NULL;
    00141         name = varName;
    00142         nameString = new String(varNameString);
    00143
    00144         // NOTE: the const int here is to ward off the internal
    00145         // compiler error on gcc 2.95.2
    00146         const int numDomainStrings = domainList.getLength();
    00147         domainStrings = new String [numDomainStrings];
    00148
    00149         vector<String>::const_iterator dIter;
    00150         unsigned int i;
    00151         for (i = 0, dIter = domainListString.begin();
    00152              dIter != domainListString.end(); dIter++, i++)
    00153         {
    00154                 domainStrings[i] = (*dIter);
    00155         }
    00156         init(domainList, costList, guardList, probabilityValues);
    00157 }
    00158 */
    00159
    00160 BFIGVariable::BFIGVariable(BFIGVariable const & v)
    00161 : assignments(v.assignments),
    00162   assignmentsUnconst(v.assignmentsUnconst),
    00163   bestAssignment(v.bestAssignment), isSorted(v.isSorted)
    00164 {
    00165 }
    00166
    00167
    00168 void
    00169 BFIGVariable::init(vector<double> costList, bool probabilityValues)
    00170 {
    00171         assert( (mBMVariable->getDomain()->getLength() == costList.size()));
    00172
    00173         if (probabilityValues)
    00174         {
    00175                 //cout << "Converting to probability costs." << endl;
    00176                 vector<double> logCostList;
    00177                 vector<double>::const_iterator costIter;
    00178                 unsigned int i;
    00179                 for (i = 0, costIter = costList.begin();
    00180                      costIter != costList.end(); costIter++, i++)
    00181                 {
    00182                         double c = (*costIter);
    00183                         logCostList.push_back(probToCost(c));
    00184                 }
    00185                 costList = logCostList;
    00186         }
    00187
    00188         BaseModelDomain const &domain = *(mBMVariable->getDomain());
    00189         BaseModelDomain::ConstantIterator valueIter;
    00190         vector<double>::const_iterator costIter;
    00191         // simple costs given
    00192         for (valueIter = domain.begin(), costIter = costList.begin();
    00193                 valueIter != domain.end(); valueIter++, costIter++)
    00194         {
    00195                 BFIGAssignment * newAssignment = new BFIGAssignment(this, &(*valueIter), (*costIter));
    00196                 assignments.push_back(newAssignment);
    00197                 assignmentsUnconst.push_back(newAssignment);
    00198         }
    00199
    00200         randomIndex = 0;
    00201
    00202         normalizeCosts();
    00203 }
    00204
    00205
    00206 BFIGVariable::~BFIGVariable()
    00207 {
    00208         // destroy the Assignments - no one may hang on to them anyway.
    00209         vector<BFIGAssignment *>::iterator aIter;
    00210         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00211         {
    00212                 delete (*aIter);
    00213         }
    00214 }
    00215
    00216
    00217         //---------------------------------
    00218         //      PUBLIC FUNCTION METHODS
    00219         //---------------------------------
    00220
    00221 unsigned int
    00222 BFIGVariable::getName()  const
    00223 {
    00224         return getNameInt();
    00225 }
    00226
    00227
    00228 String
    00229 BFIGVariable::getNameString()  const
    00230 {
    00231         return mBMVariable->getName();
    00232 }
    00233
    00234
    00235 unsigned int
    00236 BFIGVariable::getNameInt()  const
    00237 {
    00238         return mIndex;
    00239 }
    00240
    00241
    00242 unsigned int
    00243 BFIGVariable::valueStringToInt(String val)  const
    00244 {
    00245         BaseModelDomain const &domain = *(mBMVariable->getDomain());
    00246         for (unsigned int i = 0; i < domain.getLength(); i++)
    00247         {
    00248                 if (val == domain[i]->getName())
    00249                         return i;
    00250         }
    00251         cerr << "Error in Variable '" << getNameString() << "': unknown value string: '"
    00252                 << val << "'!" << endl;
    00253         return 0;
    00254 }
    00255
    00256
    00257 String
    00258 BFIGVariable::valueIntToString(unsigned int val)  const
    00259 {
    00260         BaseModelDomain const &domain = *(mBMVariable->getDomain());
    00261         if (val >= domain.getLength())
    00262         {
    00263                 cerr << "Error in Variable: value too large: " << val << "!" << endl;
    00264                 return "";
    00265         }
    00266         return domain[val]->getName();
    00267 }
    00268
    00269
    00270 bool
    00271 BFIGVariable::operator==(BFIGVariable const & v) const
    00272 {
    00273         return getName() == v.getName();
    00274 }
    00275
    00276
    00277 bool
    00278 BFIGVariable::operator< (BFIGVariable const & v)  const
    00279 {
    00280         return getName() < v.getName();
    00281 }
    00282
    00283
    00284 BFIGAssignment const *
    00285 BFIGVariable::getAssignmentByValue(unsigned int val) const
    00286 {
    00287         vector<BFIGAssignment const *>::const_iterator aIter;
    00288         for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00289         {
    00290                 if (val == (*aIter)->getValue())
    00291                         return (*aIter);
    00292         }
    00293
    00294         return NULL;
    00295 }
    00296
    00297
    00298 BFIGAssignment *
    00299 BFIGVariable::getAssignmentByValue(unsigned int val)
    00300 {
    00301         vector<BFIGAssignment *>::iterator aIter;
    00302         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00303         {
    00304                 if (val == (*aIter)->getValue())
    00305                         return (*aIter);
    00306         }
    00307
    00308         return NULL;
    00309 }
    00310
    00311
    00312 BFIGAssignment const *
    00313 BFIGVariable::getAssignmentByValue(String val) const
    00314 {
    00315         return getAssignmentByValue(valueStringToInt(val));
    00316 }
    00317
    00318 /*
    00319 void
    00320 BFIGVariable::reset()
    00321 {
    00322         setCurrent(defaultAssignment);
    00323         clearNext();
    00324 }
    00325 */
    00326
    00327 /*
    00328 void
    00329 BFIGVariable::setReachableAll(bool reachable)
    00330 {
    00331         bestAssignment = NULL;
    00332         vector<BFIGAssignment *>::const_iterator aIter;
    00333         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00334         {
    00335                 (*aIter)->setReachable(reachable);
    00336         }
    00337 }
    00338 */
    00339 /*
    00340 void
    00341 BFIGVariable::eraseUnreachable()
    00342 {
    00343         vector<BFIGAssignment *>::iterator aIter;
    00344         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); )
    00345         {
    00346                 if (!(*aIter)->isReachable())
    00347                 {
    00348                         //delete (*aIter);
    00349                         aIter = assignmentsUnconst.erase(aIter);
    00350                 } else
    00351                 {
    00352                         aIter++;
    00353                 }
    00354         }
    00355         vector<BFIGAssignment const *>::iterator a2Iter;
    00356         for (a2Iter = assignments.begin(); a2Iter != assignments.end(); )
    00357         {
    00358                 if (!(*a2Iter)->isReachable())
    00359                 {
    00360                         delete (*a2Iter);
    00361                         a2Iter = assignments.erase(a2Iter);
    00362                 } else
    00363                 {
    00364                         a2Iter++;
    00365                 }
    00366         }
    00367 }
    00368 */
    00369
    00370 /*
    00371 void
    00372 BFIGVariable::setAssignmentReachableByValue(unsigned int val, bool reachable)
    00373 {
    00374         bestAssignment = NULL;
    00375         vector<BFIGAssignment *>::const_iterator assignmentIter;
    00376         for (assignmentIter = assignmentsUnconst.begin();
    00377              assignmentIter != assignmentsUnconst.end();
    00378                  assignmentIter++)
    00379         {
    00380                 if (val == (*assignmentIter)->getValue())
    00381                 {
    00382                         (*assignmentIter)->setReachable(reachable);
    00383                         return;
    00384                 }
    00385         }
    00386
    00387         cerr << "Error: Variable " << getName() << " could not find assignment to "
    00388              << val << " to set reachability!" << endl;
    00389 }
    00390 */
    00391
    00392 /*
    00393 void
    00394 BFIGVariable::setAssignmentReachableByValue(String val, bool reachable)
    00395 {
    00396         setAssignmentReachableByValue(valueStringToInt(val), reachable);
    00397 }
    00398 */
    00399
    00400
    00401 void
    00402 BFIGVariable::setAssignmentCostByValue(unsigned int val, double cost)
    00403 {
    00404         isSorted = false;
    00405         bestAssignment = NULL;
    00406         vector<BFIGAssignment *>::const_iterator aIter;
    00407         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00408         {
    00409                 if (val == (*aIter)->getValue())
    00410                 {
    00411                         (*aIter)->setCost(cost);
    00412                         return;
    00413                 }
    00414         }
    00415
    00416         cerr << "Error: Variable " << getName() << " could not find assignment to "
    00417              << val << " to set cost!" << endl;
    00418 }
    00419
    00420 void
    00421 BFIGVariable::setAssignmentTrueCostByValue(String val, double cost)
    00422 {
    00423         setAssignmentTrueCostByValue(valueStringToInt(val), cost);
    00424 }
    00425
    00426 void
    00427 BFIGVariable::setAssignmentTrueCostByValue(unsigned int val, double cost)
    00428 {
    00429         isSorted = false;
    00430         bestAssignment = NULL;
    00431         vector<BFIGAssignment *>::const_iterator aIter;
    00432         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00433         {
    00434                 if (val == (*aIter)->getValue())
    00435                 {
    00436                         (*aIter)->setTrueCost(cost);
    00437                         return;
    00438                 }
    00439         }
    00440
    00441         cerr << "Error: Variable " << getName() << " could not find assignment to "
    00442              << val << " to set cost!" << endl;
    00443 }
    00444
    00446
    00447 void
    00448 BFIGVariable::setAssignmentCostByValue(String val, double cost)
    00449 {
    00450         setAssignmentCostByValue(valueStringToInt(val), cost);
    00451 }
    00452
    00454
    00455 double BFIGVariable::probToCost(double prob)
    00456 {
    00457 #ifdef ASSIGNMENT_MARGINAL_PROBABILITY
    00458         if ( prob < ASSIGNMENT_MARGINAL_PROBABILITY )
    00459         {
    00460                 prob = ASSIGNMENT_MARGINAL_PROBABILITY;
    00461                 //cout << "Assuming marginal probability for Variable " << name << endl;
    00462         }
    00463 #endif
    00464         // must account for log(0).  Clamp it at > c_MinimumProbability.
    00465         prob = std::max( prob, c_MinimumProbability );
    00466         return -log(prob);
    00467 }
    00468
    00470
    00471 int BFIGVariable::setCost(unsigned int valUID, double cost)
    00472 {
    00473         BFIGAssignment * asg = getAssignmentByValue(valUID);
    00474         if(asg == NULL)
    00475                 return 1;
    00476         asg->setTrueCost(cost);
    00477
    00478         //BFIGAssignment const * best = getBestAssignment();
    00479         //double bestReachableCost = 0.0;
    00480         //if (best)
    00481         //{
    00482         //      bestReachableCost = best->getTrueCost();
    00483         //}
    00484
    00485         //vector<BFIGAssignment *>::iterator aIter;
    00486         //for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00487         //{
    00488         //      if (bestReachableCost != 0.0)  //ugh
    00489         //              asg->setCost(cost - bestReachableCost);
    00490         //      else
    00491         //              asg->setCost(cost);
    00492         //}
    00493         return 0;
    00494 }
    00495
    00497
    00498 int BFIGVariable::setCosts(double costs[])
    00499 {
    00500         unsigned int x;
    00501         for (x = 0; x < assignmentsUnconst.size(); x++)
    00502         {
    00503                 assignmentsUnconst[x]->setTrueCost(costs[x]);
    00504         }
    00505         return 0;
    00506 }
    00507
    00509
    00510
    00511 /*
    00512 void
    00513 BFIGVariable::setTrueCostsByCurrent()
    00514 {
    00515         isSorted = false;
    00516         bestAssignment = NULL;
    00517         if (currentAssignment == NULL)
    00518         {
    00519                 //cerr << "Error: Variable " << getName() << " can't set costs by current value "
    00520                 //     << "with no current value!" << endl;
    00522                 setAllTrueCosts(0.0);
    00523                 return;
    00524         }
    00525         vector<BFIGAssignment *>::const_iterator assignmentIter;
    00526         for (assignmentIter = assignmentsUnconst.begin();
    00527              assignmentIter != assignmentsUnconst.end();
    00528                  assignmentIter++)
    00529         {
    00530                 (*assignmentIter)->setTrueCostByValue(currentAssignment->getValueInt());
    00531         }
    00532 }
    00533 */
    00534
    00535 /*
    00536 void
    00537 BFIGVariable::convertGuards(ModelConvertorInterface const * mc)
    00538 {
    00539         vector<BFIGAssignment *>::const_iterator assignmentIter;
    00540         for (assignmentIter = assignmentsUnconst.begin();
    00541              assignmentIter != assignmentsUnconst.end();
    00542                  assignmentIter++)
    00543         {
    00544                 (*assignmentIter)->convertGuards(mc);
    00545         }
    00546 }
    00547 */
    00548
    00549 /*
    00550 void
    00551 BFIGVariable::enableAssignmentsByGuards(AssignmentTesterInterface * currentSat)
    00552 {
    00553         isSorted = false;
    00554         bestAssignment = NULL;
    00555                 // start by assuming all are reachable:
    00556         setReachableAll(true);
    00557
    00558         if (currentAssignment == NULL)
    00559         {
    00560                 //cerr << "Error: Variable " << getName() << " can't check guards "
    00561                 //     << "with no current value!" << endl;
    00564                 return;
    00565         }
    00566
    00567         vector<BFIGAssignment *>::const_iterator assignmentIter;
    00568         for (assignmentIter = assignmentsUnconst.begin();
    00569              assignmentIter != assignmentsUnconst.end();
    00570                  assignmentIter++)
    00571         {
    00572                 (*assignmentIter)->enableByGuard(currentSat, currentAssignment->getValueInt());
    00573         }
    00574 }
    00575 */
    00576
    00577 /*
    00578 void
    00579 BFIGVariable::enableAssignmentsByGuards()
    00580 {
    00581         isSorted = false;
    00582         bestAssignment = NULL;
    00583                 // start by assuming all are reachable:
    00584         setReachableAll(true);
    00585
    00586         if (currentAssignment == NULL)
    00587         {
    00588                 //# stuck! no guards independent of current state!! What is right here???
    00589                 //#     cerr << "Error: Variable " << getName() << " can't check guards "
    00590                 //#          << "with no current value!" << endl;
    00591                 return;
    00592         }
    00593
    00594         vector<BFIGAssignment *>::const_iterator assignmentIter;
    00595         for (assignmentIter = assignmentsUnconst.begin();
    00596              assignmentIter != assignmentsUnconst.end();
    00597                  assignmentIter++)
    00598         {
    00599                 (*assignmentIter)->enableByGuard(currentAssignment->getValueInt());
    00600         }
    00601 }
    00602 */
    00603
    00604 void
    00605 BFIGVariable::normalizeCosts()
    00606 {
    00607         /*# must have in order for getBestAssignment to work!! - not currently. */
    00608         //orderCosts();
    00609
    00610         //BFIGAssignment const * best = getBestAssignment();
    00611         //double bestReachableCost = 0.0;
    00612         //if (best)
    00613         //{
    00614         //      bestReachableCost = best->getTrueCost();
    00615         //} else
    00616         //{
    00617                 //return
    00618         //}
    00619
    00620         //cout << "normalizing:  best = " << bestReachableCost << endl;
    00621
    00622         //# if (bestReachableCost == 0.0)  // unclean, but...
    00623         //#     return;
    00624         //vector<BFIGAssignment *>::iterator aIter;
    00625         //for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00626         //{
    00627         //      if (bestReachableCost != 0.0)  //ugh
    00628         //              (*aIter)->setCost((*aIter)->getTrueCost() - bestReachableCost);
    00629         //      else
    00630         //              (*aIter)->setCost((*aIter)->getTrueCost());
    00631         //}
    00632
    00633 /*#
    00634         //cout << "    ";
    00635         //for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00636         //{
    00637         //      cout << (*aIter)->getTrueCost() << "  ";
    00638         //}
    00639         //cout << endl;
    00640         //cout << "    ";
    00641         //for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00642         //{
    00643         //      cout << (*aIter)->getCost() << "  ";
    00644         //}
    00645         //cout << endl;
    00646 */
    00647 }
    00648
    00649
    00650 void
    00651 BFIGVariable::orderCosts()
    00652 {
    00653         if (isSorted)
    00654                 return;
    00655         ListManipulator<BFIGAssignment const *> lm;
    00656         lm.sort(assignments, assignment_cost_less);
    00657         isSorted = true;
    00658 }
    00659
    00660
    00661 BFIGAssignment const *
    00662 BFIGVariable::getBestAssignment()  //const
    00663 {
    00666         //orderCosts();
    00667
    00668         if (bestAssignment)
    00669                 return bestAssignment;
    00670         // assignments must remain as a sorted list, best (lowest) first. - (not for now)
    00671         BFIGAssignment const * res = NULL;
    00672         double bestFound = 999999999.9;
    00673         vector<BFIGAssignment const *>::const_iterator aIter;
    00674         for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00675         {
    00676                 if (!res || ( (*aIter)->getTrueCost() < bestFound ))
    00677                 {
    00678                         res = (*aIter);
    00679                         bestFound = res->getTrueCost();
    00680                 }
    00681         }
    00682         bestAssignment = res;
    00683         return res;
    00684 }
    00685
    00686 /*
    00687 BFIGAssignment const *
    00688 BFIGVariable::getCurrentAssignment() const
    00689 {
    00690         return currentAssignment;
    00691 }
    00692 */
    00693 /*
    00694 bool
    00695 BFIGVariable::setCurrent(BFIGAssignment const * a)
    00696 {
    00697         currentAssignment = a;
    00698         if (currentAssignment)
    00699         {
    00701                 setTrueCostsByCurrent();
    00702                 return true;
    00703         }
    00704         return false;
    00705 }
    00706 */
    00707 /*
    00708 bool
    00709 BFIGVariable::setCurrent(unsigned int val)
    00710 {
    00711         setCurrent(getAssignmentByValue(val));
    00712         if (currentAssignment)
    00713                 return true;
    00714
    00715         //if (debug)  // may want anyway!
    00716         //{
    00717         //      cerr << "Error: Variable " << getName() << " could not find assignment to "
    00718         //           << val << " to set current state!" << endl;
    00719         //}
    00720
    00721         return false;
    00722 }
    00723 */
    00724
    00725 /*
    00726 bool
    00727 BFIGVariable::setCurrent(String val)
    00728 {
    00729         setCurrent(getAssignmentByValue(val));
    00730         if (currentAssignment)
    00731                 return true;
    00732
    00733         //if (debug)  // may want anyway!
    00734         //{
    00735         //      cerr << "Error: Variable " << getName() << " could not find assignment to "
    00736         //           << val << " to set current state!" << endl;
    00737         //}
    00738
    00739         return false;
    00740 }
    00741 */
    00742
    00743 /*
    00744 bool
    00745 BFIGVariable::clearCurrent()
    00746 {
    00747         setCurrent((BFIGAssignment const *)NULL);
    00748         return true;
    00749 }
    00750 */
    00751
    00752 /*
    00753 BFIGAssignment const *
    00754 BFIGVariable::getNextAssignment() const
    00755 {
    00756         return nextAssignment;
    00757 }
    00758 */
    00759
    00760 /*
    00761 bool
    00762 BFIGVariable::setNext(BFIGAssignment const * a)
    00763 {
    00764         nextAssignment = a;
    00765         return true;
    00766 }
    00767 */
    00768
    00769 /*
    00770 bool
    00771 BFIGVariable::setNext(unsigned int val)
    00772 {
    00773         setNext(getAssignmentByValue(val));
    00774         if (nextAssignment)
    00775                 return true;
    00776
    00777         //if (debug)  // may want anyway!
    00778         //{
    00779         //      cerr << "Error: Variable " << getName() << " could not find assignment to "
    00780         //           << val << " to set current state!" << endl;
    00781         //}
    00782         return false;
    00783 }
    00784 */
    00785
    00786 /*
    00787 bool
    00788 BFIGVariable::setNext(String val)
    00789 {
    00790         setNext(getAssignmentByValue(val));
    00791         if (nextAssignment)
    00792                 return true;
    00793
    00794         //if (debug)  // may want anyway!
    00795         //{
    00796         //      cerr << "Error: Variable " << getName() << " could not find assignment to "
    00797         //           << val << " to set current state!" << endl;
    00798         //}
    00799         return false;
    00800 }
    00801 */
    00802
    00803 /*
    00804 bool
    00805 BFIGVariable::clearNext()
    00806 {
    00807         setNext((BFIGAssignment const *)NULL);
    00808         return true;
    00809 }
    00810 */
    00811
    00812 void
    00813 BFIGVariable::setAllCosts(double cost)
    00814 {
    00815         isSorted = false;
    00816         bestAssignment = NULL;
    00817         vector<BFIGAssignment *>::const_iterator aIter;
    00818         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00819         {
    00820                 (*aIter)->setCost(cost);
    00821         }
    00822 }
    00823
    00824
    00825 void
    00826 BFIGVariable::setAllTrueCosts(double cost)
    00827 {
    00828         isSorted = false;
    00829         bestAssignment = NULL;
    00830         vector<BFIGAssignment *>::const_iterator aIter;
    00831         for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
    00832         {
    00833                 (*aIter)->setTrueCost(cost);
    00834         }
    00835 }
    00836
    00837
    00838 IntegerArray
    00839 BFIGVariable::getDomain() const
    00840 {
    00841         IntegerArray res;
    00842         vector<BFIGAssignment const *>::const_iterator aIter;
    00843         for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00844         {
    00845                 res.pushBack((*aIter)->getValueInt());
    00846         }
    00847         return res;
    00848 }
    00849
    00850
    00851 vector<String>
    00852 BFIGVariable::getDomainStrings() const
    00853 {
    00854         vector<String> res;
    00855         vector<BFIGAssignment const *>::const_iterator aIter;
    00856         for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00857         {
    00858                 res.push_back((*aIter)->getValueString());
    00859         }
    00860         return res;
    00861 }
    00862
    00863
    00864 unsigned int
    00865 BFIGVariable::getDomainSize() const
    00866 {
    00867         return mBMVariable->getDomain()->getLength();
    00868         //unsigned int res = 0;
    00869         //vector<BFIGAssignment const *>::const_iterator aIter;
    00870         //for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00871         //{
    00872         //      if ((*aIter)->isReachable())
    00873         //              res++;
    00874         //}
    00875         //return res;
    00876 }
    00877
    00878
    00879 unsigned int
    00880 BFIGVariable::getDomainTotalSize() const
    00881 {
    00882         return mBMVariable->getDomain()->getLength();
    00883 }
    00884
    00885
    00886 IntegerArray
    00887 BFIGVariable::getDomainTotal() const
    00888 {
    00889         IntegerArray res;
    00890         vector<BFIGAssignment const *>::const_iterator aIter;
    00891         for (aIter = assignments.begin(); aIter != assignments.end(); aIter++)
    00892         {
    00893                 res.pushBack((*aIter)->getValueInt());
    00894         }
    00895         return res;
    00896 }
    00897
    00898
    00899 vector<BFIGAssignment const *>
    00900 BFIGVariable::getAssignments() const
    00901 {
    00902         if (allReachable())
    00903                 return assignments;
    00904         vector<BFIGAssignment const *> assignmentsC;
    00905         vector<BFIGAssignment const *>::const_iterator
    00906                 assignmentIter;
    00907         for (assignmentIter = assignments.begin(); assignmentIter != assignments.end();
    00908                  assignmentIter++)
    00909         {
    00910                 assignmentsC.push_back(*assignmentIter);
    00911         }
    00912         return assignmentsC;
    00913 }
    00914
    00915
    00916 vector<BFIGAssignment const *>
    00917 BFIGVariable::getAssignmentsSorted() const
    00918 {
    00919         if (!isSorted)
    00920                 ((BFIGVariable *)this)->orderCosts();
    00921         return getAssignments();
    00922 }
    00923
    00924
    00925 vector<BFIGAssignment const *> const &
    00926 BFIGVariable::getAssignmentsTotal()  const
    00927 {
    00928         return assignments;
    00929 /*#
    00930         //vector<BFIGAssignment const *> assignmentsC;
    00931         //vector<BFIGAssignment const *>::const_iterator assignmentIter;
    00932         //for (assignmentIter = assignments.begin(); assignmentIter != assignments.end();
    00933         //       assignmentIter++)
    00934         //{
    00935         //      assignmentsC.push_back(*assignmentIter);
    00936         //}
    00937         //return assignmentsC;
    00938 */
    00939 }
    00940
    00941
    00942 bool
    00943 BFIGVariable::allReachable()  const
    00944 {
    00945         //vector<BFIGAssignment const *>::const_iterator assignmentIter;
    00946         //for (assignmentIter = assignments.begin(); assignmentIter != assignments.end();
    00947         //       assignmentIter++)
    00948         //{
    00949         //      if (!(*assignmentIter)->isReachable())
    00950         //              return false;
    00951         //}
    00952         return true;
    00953 }
    00954
    00955
    00956 bool
    00957 BFIGVariable::nullDomain()  const
    00958 {
    00959         return mBMVariable->getDomain()->getLength() > 0;
    00960         //vector<BFIGAssignment const *>::const_iterator assignmentIter;
    00961         //for (assignmentIter = assignments.begin(); assignmentIter != assignments.end();
    00962         //       assignmentIter++)
    00963         //{
    00964         //      if ((*assignmentIter)->isReachable())
    00965         //              return false;
    00966         //}
    00967         //return true;
    00968 }
    00969
    00970
    00971 double
    00972 BFIGVariable::getEntropy(bool logProbabilities) const
    00973 {
    00974         double res = 0.0;
    00975         vector<BFIGAssignment const *>::const_iterator assignmentIter;
    00976         for (assignmentIter = assignments.begin(); assignmentIter != assignments.end();
    00977                  assignmentIter++)
    00978         {
    00979                 if (logProbabilities)
    00980                 {
    00981                         if ((*assignmentIter)->getTrueCost() >= c_MinimumProbability)
    00982                                 res += exp(-(*assignmentIter)->getTrueCost()) *
    00983                                             (*assignmentIter)->getTrueCost();
    00984                 } else
    00985                 {
    00986                         if ((*assignmentIter)->getTrueCost() >= c_MinimumProbability)
    00987                                 res += (*assignmentIter)->getTrueCost() * -1 *
    00988                                         log((*assignmentIter)->getTrueCost());
    00989                 }
    00990         }
    00991         return res;
    00992 }
    00993
    00994
    00995 double
    00996 BFIGVariable::getEntropyTotal(bool logProbabilities) const
    00997 {
    00998         double res = 0.0;
    00999         vector<BFIGAssignment const *>::const_iterator assignmentIter;
    01000         for (assignmentIter = assignments.begin(); assignmentIter != assignments.end();
    01001                  assignmentIter++)
    01002         {
    01003                 if (logProbabilities)
    01004                 {
    01005                         if ((*assignmentIter)->getTrueCost() >= c_MinimumProbability )
    01006                                 res += exp(-(*assignmentIter)->getTrueCost()) *
    01007                                             (*assignmentIter)->getTrueCost();
    01008                 } else
    01009                 {
    01010                         if ((*assignmentIter)->getTrueCost() >= c_MinimumProbability )
    01011                                 res += (*assignmentIter)->getTrueCost() * -1 *
    01012                                         log((*assignmentIter)->getTrueCost());
    01013                 }
    01014         }
    01015         return res;
    01016 }
    01017
    01018
    01019 BFIGAssignment const *
    01020 BFIGVariable::bestConsistentAssignment(vector<BFIGAssignment const *> const & otherAssignments)
    01021 {
    01022         orderCosts();
    01023
    01024         vector<BFIGAssignment const *>::const_iterator assignmentIter;
    01025         for (assignmentIter = assignments.begin(); assignmentIter != assignments.end();
    01026                  assignmentIter++)
    01027         {
    01028                 if ((*assignmentIter)->compatible(otherAssignments))
    01029                         return (*assignmentIter);
    01030         }
    01031         return NULL;
    01032 }
    01033
    01034
    01035 void
    01036 BFIGVariable::randomizeIndex()
    01037 {
    01038         //if (!seeded)
    01039         //{
    01040         //      srand((unsigned)time(NULL));
    01041         //      seeded = true;
    01042         //}
    01043         while (randomIndex == 0)
    01044                 randomIndex = rand();
    01045 }
    01046
    01047
    01048 int
    01049 BFIGVariable::getRandomIndex()
    01050 {
    01051         if (randomIndex == 0)
    01052                 randomizeIndex();
    01053         return randomIndex;
    01054 }
    01055
    01056
    01057 String
    01058 BFIGVariable::toString() const
    01059 {
    01060         BaseModelDomain const &domain = *(mBMVariable->getDomain());
    01061         unsigned int valIndex;
    01062         //IntegerArray varDomain = domain;
    01063         //IntegerArray::iterator varDomainIter;
    01064
    01065         String res = "";
    01066
    01067         res += "[";
    01068         res += getNameString() ;
    01069 /*#
    01070         //if (isAssigned())
    01071         //{
    01072         //      res += " = ";
    01073         //      res += getCurrentAssignment()->getValue() ;
    01074         //}
    01075 */
    01076         res += ", {" ;
    01077         for (valIndex = 0; valIndex != domain.getLength();
    01078                 valIndex++)
    01079         {
    01080                 if (valIndex != 0)
    01081                         res += ", " ;
    01082                 res += domain[valIndex]->getName() ;
    01083         }
    01084         res += "}]" ;
    01085         //res += "\n" ;
    01086         return res;
    01087 }
    01088
    01089 namespace Mers {
    01090         namespace Generators {
    01091                 namespace BFIG {
    01092
    01093                         ostream&
    01094                         operator<<(ostream& os, Mers::Generators::BFIG::BFIGVariable const & v)
    01095                         {
    01096                                 //# Apparently, MSVC does not understand the concept of "friends" very well.
    01097                                 int p = os.precision();
    01098                                 vector<BFIGAssignment *>::const_iterator domainIter;
    01099
    01100                                 os << "[" << v.getNameString() << "(" << v.getNameInt() << ")";
    01101                                 os << ":  { " ;
    01102                                 bool first = true;
    01103                                 for (domainIter = v.assignmentsUnconst.begin();
    01104                                                  domainIter != v.assignmentsUnconst.end(); domainIter++)
    01105                                 {
    01106                                         if (first)
    01107                                                 first = false;
    01108                                         else
    01109                                                 os << ", " ;
    01110                                         //if (!(*domainIter)->isReachable())
    01111                                         //      os << "-";
    01112                                         //if ((*domainIter) == v.getCurrentAssignment())
    01113                                         //      os << "+";
    01114                                         //if ((*domainIter) == v.getNextAssignment())
    01115                                         //      os << ">";
    01116                                         os << (*domainIter)->getValueString()
    01117                                                  << "(" << (*domainIter)->getValueInt() << ")" << ":" ;
    01118                                         os << setprecision(2) << (*domainIter)->getCost() << setprecision(p) ;
    01119                                 }
    01120                                 os << " }]" ;
    01121                                 return os;
    01122                         }
    01123                 }
    01124         }
    01125 }
    01126
    01127
    01128
    01129
    01130
    01131
    01132
    01133