home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 02 Useful Techniques / 08 Zarozinski / src / ffllapi / COGDefuzzVarObj.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-13  |  4.8 KB  |  207 lines

  1. //
  2. // File:    COGDefuzzVarObj.h    
  3. //
  4. // Purpose:    Variable object for Center of Gravity Defuzzification method
  5. //
  6. // Copyright ⌐ 2001 Louder Than A Bomb! Software
  7. //
  8. //
  9. // This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
  10. // It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
  11. //
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. #include "COGDefuzzVarObj.h"
  19. #include "COGDefuzzSetObj.h"
  20. #include "FuzzyOutSet.h"
  21. #include "FuzzyOutVariable.h"
  22.  
  23.  
  24. //
  25. // Function:    COGDefuzzVarObj()
  26. // 
  27. // Purpose:        Constructor for COGDefuzzVarObj
  28. //
  29. // Arguments:    
  30. //
  31. //        FuzzyOutVariable* _parent - Output variable this object is part of
  32. //
  33. // Returns:
  34. //
  35. //        nothing
  36. //
  37. // Author:    Michael Zarozinski
  38. // Date:    8/01
  39. // 
  40. // Modification History
  41. // Author    Date        Modification
  42. // ------    ----        ------------
  43. //
  44. //
  45. COGDefuzzVarObj::COGDefuzzVarObj(FuzzyOutVariable* _parent) : DefuzzVarObj(_parent), FFLLBase(_parent)
  46. {
  47.  
  48. }
  49.  
  50.  
  51. //
  52. // Function:    ~COGDefuzzVarObj()
  53. // 
  54. // Purpose:        Destructor for COGDefuzzVarObj
  55. //
  56. // Arguments:    
  57. //
  58. //        none
  59. //
  60. // Returns:
  61. //
  62. //        nothing
  63. //
  64. // Author:    Michael Zarozinski
  65. // Date:    8/01
  66. // 
  67. // Modification History
  68. // Author    Date        Modification
  69. // ------    ----        ------------
  70. //
  71. //
  72. COGDefuzzVarObj::~COGDefuzzVarObj()
  73. {
  74.  
  75. }
  76.  
  77.  
  78. //
  79. // Function:    calc_value()
  80. // 
  81. // Purpose:        Calculate the defuzzified output value (using the COG 
  82. //                defuzzification method) for the output variable.
  83. //
  84. // Arguments:    
  85. //
  86. //        DOMType* out_set_dom_arr -    Array that holds the DOM value for each
  87. //                                    set in the output variable
  88. //
  89. // Returns:
  90. //
  91. //        RealType - the defuzzified output value. FLT_MIN is returned if no output sets are active
  92. //
  93. // Author:    Michael Zarozinski
  94. // Date:    8/01
  95. // 
  96. // Modification History
  97. // Author    Date        Modification
  98. // ------    ----        ------------
  99. //
  100. //
  101. RealType COGDefuzzVarObj::calc_value(DOMType* out_set_dom_arr  )
  102. {
  103.     RealType area_sum = 0.0;        // sum of the area
  104.     RealType moment_sum = 0.0;        // sum of the moments
  105.     RealType divisor = 0.0;        // value to divide cog_sum by
  106.     RealType tmp_area = 0.0;        // tmp var  
  107.     COGDefuzzSetObj*    defuzz;    // defuzzification object for the set
  108.      int     cog_idx;                // center of gravity index
  109.  
  110.     FuzzyOutVariable* parent = get_parent();
  111.  
  112.     int num_of_sets = parent->get_num_of_sets();
  113.  
  114.     // each set has it's individual COG components (area and moment) calculated
  115.     // for every possible DOM. These calculations treat each set as a point mass.
  116.     // Now we have multiple sets to find the COG for, with each treated as a
  117.     // point mass, we can calculate the COG via the simple formula:
  118.     //
  119.     //        Sum of Moments
  120.     //        ---------------
  121.     //        Sum of Mass (Note: mass is same as area)
  122.     //
  123.     // See COGDefuzzVarObj.h for a more detailed expanation of this formula and 
  124.     // how we use it.
  125.  
  126.     for (int i = 0; i < num_of_sets; ++i)
  127.         {
  128.          defuzz = get_set_defuzz_obj(i);  
  129.     
  130.         if (defuzz == NULL)
  131.             continue;    // nothing to calc for this set
  132.  
  133.          cog_idx = out_set_dom_arr[i]; 
  134.         
  135.         if (cog_idx == 255)
  136.             cog_idx = 0;
  137.      
  138.         tmp_area =  defuzz->get_area(cog_idx); 
  139.  
  140.         if (tmp_area) 
  141.             {
  142.             area_sum += tmp_area;
  143.             moment_sum += defuzz->get_moment(cog_idx);  
  144.             divisor++;
  145.             }
  146.  
  147.         } // end loop thru sets
  148.  
  149.     if (!divisor)
  150.         {
  151.         // if no output sets were active, return FLT_MIN - the special value that
  152.         // ensures we know that there is no output
  153.  
  154.         return FLT_MIN;    // return so we don't div by 0 below
  155.  
  156.         } // end if no divisor
  157.  
  158.     RealType left_x = parent->get_left_x();
  159.  
  160.     // be sure to account for the left x (start of the var)
  161.     return (left_x + (moment_sum / area_sum));
  162.  
  163. } // end COGDefuzzVarObj::calc_value()
  164.  
  165.  
  166. //
  167. // Function:    get_set_defuzz_obj()
  168. // 
  169. // Purpose:        Return the defuzzification object of the correct type
  170. //
  171. // Arguments:
  172. //
  173. //        int set_idx - index of the set to get the defuzzifiation object for
  174. //
  175. // Returns:
  176. //
  177. //        COGDefuzzSetObj* - defuzzification object for the set
  178. //
  179. // Author:    Michael Zarozinski
  180. // Date:    8/01
  181. // 
  182. // Modification History
  183. // Author    Date        Modification
  184. // ------    ----        ------------
  185. //
  186. //        
  187. COGDefuzzSetObj* COGDefuzzVarObj::get_set_defuzz_obj(int set_idx) const
  188. {
  189.     FuzzyOutVariable* parent = get_parent();
  190.     FuzzyOutSet* set = parent->get_set(set_idx);
  191.  
  192.     COGDefuzzSetObj* tmp_obj = dynamic_cast<COGDefuzzSetObj*>(set->get_defuzz_obj()); 
  193.     
  194.     return tmp_obj;
  195.  
  196. } // end COGDefuzzVarObj::get_set_defuzz_obj()
  197.  
  198. /////////////////////////////////////////////////////////////////////
  199. ////////// Trivial Functions That Don't Require Headers /////////////
  200. /////////////////////////////////////////////////////////////////////
  201.  
  202. int COGDefuzzVarObj::get_defuzz_type() const
  203.     return DefuzzVarObj::DEFUZZ_TYPE::COG; 
  204. };
  205.  
  206.