00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00013
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
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
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
00089
00090
00091
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
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 init(costList, probabilityValues);
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
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
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
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
00209 vector<BFIGAssignment *>::iterator aIter;
00210 for (aIter = assignmentsUnconst.begin(); aIter != assignmentsUnconst.end(); aIter++)
00211 {
00212 delete (*aIter);
00213 }
00214 }
00215
00216
00217
00218
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
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
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
00462 }
00463 #endif
00464
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
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
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
00513
00514
00515
00516
00517
00518
00519
00520
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604 void
00605 BFIGVariable::normalizeCosts()
00606 {
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
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()
00663 {
00666
00667
00668 if (bestAssignment)
00669 return bestAssignment;
00670
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
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
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
00869
00870
00871
00872
00873
00874
00875
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
00931
00932
00933
00934
00935
00936
00937
00938
00939 }
00940
00941
00942 bool
00943 BFIGVariable::allReachable() const
00944 {
00945
00946
00947
00948
00949
00950
00951
00952 return true;
00953 }
00954
00955
00956 bool
00957 BFIGVariable::nullDomain() const
00958 {
00959 return mBMVariable->getDomain()->getLength() > 0;
00960
00961
00962
00963
00964
00965
00966
00967
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
01039
01040
01041
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
01063
01064
01065 String res = "";
01066
01067 res += "[";
01068 res += getNameString() ;
01069
01070
01071
01072
01073
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
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
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
01111
01112
01113
01114
01115
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