Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Attributes.hpp
00001 
00012 #ifndef LGL_ATTRIBUTES_HPP
00013 #define LGL_ATTRIBUTES_HPP
00014 
00015 #include <iostream>
00016 #include <map>
00017 #include <math.h>
00018 
00019 // Max entropy for probabilities in interval [0.0  1.0] is = -log(0.5) so:
00020 #define MAX_ENTROPY_BINARY 0.693147180559945 
00021 // Max entropy for tri-probabilities in interval [0.0  1.0] 
00022 #define MAX_ENTROPY_TRI    1.09861228866811
00023 // Max entropy for tetra-probabilities in interval [0.0  1.0] 
00024 #define MAX_ENTROPY_TETRA  1.38629436111989 
00025 #define ATT_MIN_VAL 0.0000000001
00026 #define ATT_MAX_VAL 0.9999999990
00027 
00028 namespace jafar {
00029   namespace lgl {
00030 
00031     enum attributeKey { 
00032       ATT_BEGIN=0,
00033       T_FREE=ATT_BEGIN, // anti = OCCUPIED
00034       T_ROUGH,
00035       T_LIMIT=T_ROUGH,
00036       T_OBSTACLE,
00037       ATT_END=T_OBSTACLE,
00038       ATT_MAX
00039     };
00040     typedef enum attributeKey attributeKey;
00041 
00048     enum attributeClass { 
00049       CLASS_BEGIN=0,
00050       T_SYMBOL=CLASS_BEGIN,
00051       T_CLASS,
00052       CLASS_MAX
00053     };
00054 
00055     typedef enum attributeClass attributeClass;
00056 
00057     
00058     struct Attributes { //{{{
00059 
00061       double certainty[ATT_MAX];
00062       bool hasbeenset[ATT_MAX];
00064       double  infotropy[CLASS_MAX];
00065 
00067       Attributes()
00068       { //{{{
00069         reset();
00070       } //}}}
00071 
00073       Attributes(attributeKey _at, double inval_)
00074       { //{{{
00075         reset();
00076         // Restricting values
00077         if (inval_ > 1.0 || inval_ < 0.0) { inval_ = 0.0; }
00078         // Initialize the key attribute _at
00079         set(_at, inval_);
00080 
00081       } //}}}
00082 
00088       void set(attributeKey _at, double inval_, bool reset=true)
00089       { //{{{
00090         // For other attributes
00091         if (!reset) 
00092         {
00093           if (hasbeenset[_at])
00094           {
00095             // Do nothing
00096             return;
00097           }
00098         }
00099 
00100         // Out of proba boundaries: shout not happen
00101         if (inval_ > 1.0 || inval_ < 0.0) 
00102         {
00103           certainty[_at] = 0.0;
00104           hasbeenset[_at] = false;
00105         } else {
00106           // Adjusting the input value
00107           /*
00108           if (inval_ == 0.0 && (_at >= T_FREE && _at <= T_OBSTACLE))
00109           {
00110             inval_ = ATT_MIN_VAL;
00111           }*/
00112 
00113           // Setting certainty 
00114           certainty[_at] = inval_;
00115           hasbeenset[_at] = true;
00116           /* 
00117           if (inval_ != 0.0) 
00118           {
00119             hasbeenset[_at] = true;
00120           } else {
00121             hasbeenset[_at] = false;
00122           }
00123           */
00124         }
00125       } //}}}
00126 
00132       void normalizeCertainties(attributeKey fromK, attributeKey toK)
00133       { //{{{
00134         // ONLY FOR T_ classes
00135         // Probability accumulation buffer and Normalizer
00136         double NPacc    = 0.0;
00137         for(int k = fromK; k <= toK; k++)
00138         {
00139           if (hasbeenset[(attributeKey)k])
00140           {
00141             NPacc = NPacc + certainty[(attributeKey) k];
00142           } else {
00143             certainty[(attributeKey) k] = ATT_MIN_VAL;
00144             NPacc = NPacc + certainty[(attributeKey) k];
00145           }
00146         }
00147 
00148         if (NPacc == 1.0)
00149         {
00150           // No nomarlization needed
00151           return;
00152         }
00153 
00154         if (NPacc > 0.0)
00155         {
00156           // Normalization
00157           NPacc = 1.0 / NPacc;
00158           for(int k = fromK; k <= toK; k++)
00159           {
00160             certainty[(attributeKey) k] = certainty[(attributeKey) k] * NPacc;
00161             hasbeenset[(attributeKey) k] = true;
00162           }
00163         } else {
00164           // Equi-distribution
00165           // Certainties are set to Zero when no input or when input is out of probability bounds [0:1]
00166           NPacc = 1.0 / (1.0 + (double)(toK - fromK));
00167           for(int k = fromK; k <= toK; k++)
00168           {
00169             certainty[(attributeKey) k] = NPacc;
00170             hasbeenset[(attributeKey) k] = true;
00171           }
00172         }
00173       } //}}}
00174 
00180       void distributeCertainties()
00181       { //{{{
00182         // ONLY FOR T_classes
00183         // Probability accumulation buffer
00184         double Pacc    = 0.0;
00185         // Number of state not set
00186         double Nnotset = 0.0;
00187         for(int k = T_FREE; k <= T_LIMIT; k++)
00188         {
00189           if (hasbeenset[(attributeKey) k])
00190           {
00191             Pacc = Pacc + certainty[(attributeKey) k];
00192           } else {
00193             Nnotset = Nnotset + 1.0;
00194           }
00195         }
00196 
00197         // Set the T_OBSTACLE value
00198         certainty[T_OBSTACLE] = (1.0 - Pacc - Nnotset * ATT_MIN_VAL);
00199 
00200         // The information source is considered consistant with \sum P_i = 1
00201         if (Nnotset == 0.0)
00202         {
00203           hasbeenset[T_OBSTACLE] = true;
00204         }
00205 
00206         // Distributing on left classes
00207         if (certainty[T_OBSTACLE] >= 0.0 && Nnotset > 0.0)
00208         {
00209           hasbeenset[T_OBSTACLE] = true;
00210           // Setting equi-rest for other traversability state
00211           Pacc = ATT_MIN_VAL;// (1.0 - Pacc)/Nnotset; //equi-rest
00212           for(int k = T_FREE; k <= T_LIMIT; k++)
00213           {
00214             if (! hasbeenset[(attributeKey)k])
00215             {
00216               certainty[(attributeKey)k]  = Pacc;
00217               hasbeenset[(attributeKey)k] = true;
00218             }
00219           }
00220         } else {
00221           if (certainty[T_OBSTACLE] < 0.0)
00222           {
00223             // Big problem and one solution is
00224             certainty[T_OBSTACLE] = 0.0;
00225             hasbeenset[T_OBSTACLE] = true;
00226             for(int k = T_FREE; k < ATT_MAX; k++)
00227             {
00228               if (! hasbeenset[(attributeKey)k])
00229               {
00230                 certainty[(attributeKey)k]  = 0.0;
00231                 hasbeenset[(attributeKey)k] = true;
00232               }
00233             }
00234             normalizeCertainties(ATT_BEGIN, (attributeKey)(ATT_MAX-1));
00235           } //else { all is ok. all other classes were already set}
00236         }
00237 
00238         updateEntropy(T_CLASS);
00239         updateEntropy(T_SYMBOL);
00240       } //}}}
00241 
00242       void updateEntropy(attributeClass _ac) 
00243       {/*{{{*/
00244         double tmp = 0.0;
00245         infotropy[_ac] = 0.0;
00246         // Full calculus of entropy on the given class - ready for multi states
00247         if(_ac == T_CLASS)
00248         {
00249           // Composing the full tri-state entropy
00250           for (int k = T_FREE; k <= T_OBSTACLE; k++)
00251           {
00252             if ((certainty[(attributeKey)k] <= ATT_MAX_VAL)
00253               &&
00254                 (certainty[(attributeKey)k] >= ATT_MIN_VAL))
00255             {
00256               tmp = - certainty[(attributeKey)k] * log (certainty[(attributeKey)k]);
00257             } else {
00258               if ((certainty[(attributeKey)k] > ATT_MAX_VAL))
00259               {
00260                 tmp = - ATT_MAX_VAL * log (ATT_MAX_VAL);
00261               } else {
00262                 tmp = - ATT_MIN_VAL * log (ATT_MIN_VAL);
00263               }
00264 
00265             }
00266             infotropy[_ac] = infotropy[_ac] + tmp;
00267           }
00268           infotropy[_ac] = infotropy[_ac];// / MAX_ENTROPY_TRI;
00269         }
00270 
00271         // Specific entropy based on the symbolic of each terrain
00272         // classe. We have two symbols "traversable" and "obstructed"
00273         // and we calculate an enntropy between them.
00274         if(_ac == T_SYMBOL)
00275         {
00276           // Composing the bi-state entropy
00277           for (int k = T_FREE; k <= T_LIMIT; k++)
00278           {
00279             tmp = tmp + certainty[(attributeKey)k];
00280           }
00281           if (tmp >= ATT_MIN_VAL && tmp <= ATT_MAX_VAL)
00282           {
00283             tmp = - tmp * log (tmp);
00284           } else {
00285             if ( tmp > ATT_MAX_VAL)
00286             {
00287               tmp = - ATT_MAX_VAL * log (ATT_MAX_VAL);
00288             } else {
00289               tmp = - ATT_MIN_VAL * log (ATT_MIN_VAL);
00290             }
00291 
00292           }
00293           // Calculating free/obst entropy
00294           if (certainty[T_OBSTACLE] >= ATT_MIN_VAL && certainty[T_OBSTACLE] <= ATT_MAX_VAL)
00295           {
00296             infotropy[_ac] = tmp
00297             - certainty[T_OBSTACLE] * log (certainty[T_OBSTACLE]);
00298           } else {
00299             if (certainty[T_OBSTACLE] > ATT_MIN_VAL)
00300             {
00301               infotropy[_ac] = tmp - ATT_MAX_VAL * log (ATT_MAX_VAL);
00302             } else {
00303               infotropy[_ac] = tmp - ATT_MIN_VAL * log (ATT_MIN_VAL);
00304             }
00305           }
00306           infotropy[_ac] = infotropy[_ac];// / MAX_ENTROPY_BINARY;
00307         }
00308       }/*}}}*/
00309 
00310       bool hasBeenSet(attributeKey _at)
00311       {//{{{
00312         return hasbeenset[_at];
00313       }//}}}
00314 
00315       void reset()
00316       { //{{{
00317         for (int k = ATT_BEGIN; k < ATT_MAX; k++)
00318         {
00319           certainty[(attributeKey)k] = 0.0;
00320           hasbeenset[(attributeKey)k] = false;
00321         }
00322         infotropy[T_SYMBOL] = 0.0;
00323         infotropy[T_CLASS] = 0.0;
00324       } //}}}
00325 
00326       void print(attributeKey _at)
00327       {/*{{{*/
00328         std::cout << "- Attribute (" << _at << ", " << certainty[_at] << ")" << std::endl;
00329       }/*}}}*/
00330 
00331       // ---- Operators
00333       Attributes& operator=(const Attributes& rhs)
00334       { //{{{
00335         for (int m = T_SYMBOL; m < CLASS_MAX; m++)
00336         {
00337           infotropy[(attributeClass)m]  = rhs.infotropy[(attributeClass)m];
00338         }
00339 
00340         for (int k = ATT_BEGIN; k < ATT_MAX; k++)
00341         {
00342           certainty[(attributeKey)k]  = rhs.certainty[(attributeKey)k];
00343           hasbeenset[(attributeKey)k] = rhs.hasbeenset[(attributeKey)k];
00344         }
00345 
00346         return (*this);
00347       } //}}}
00348 
00350       friend std::ostream& operator<<(std::ostream& out, const Attributes &att)
00351       { // {{{
00352         for (int k = ATT_BEGIN; k < ATT_MAX; k++)
00353         {
00354           out << (att.hasbeenset[(attributeKey)k]?"t[":"f[") << att.certainty[(attributeKey)k] << "]";
00355         }
00356         return out;
00357       } // }}}
00358   }; //}}}
00359 
00360     typedef struct Attributes Attributes;
00361 
00362   }
00363 }
00364 
00365 #endif /* LGL_ATTRIBUTES_HPP */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:20 for Jafar by doxygen 1.7.6.1
LAAS-CNRS