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

  1. //
  2. // File:    MOMDefuzzSetObj.cpp    
  3. //
  4. // Purpose:    Set object for Mean of Maximum Defuzzification method
  5. //
  6. // Copyright ⌐ 2001 Louder Than A Bomb! Software
  7. //
  8. // This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
  9. // It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
  10. //
  11.  
  12. // include files
  13. #include "MOMDefuzzSetObj.h"
  14. #include "FuzzyOutSet.h"
  15. #include "MemberFuncBase.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22.  
  23. //
  24. // Function:    MOMDefuzzSetObj()
  25. // 
  26. // Purpose:        Constructor
  27. //
  28. // Arguments:
  29. //
  30. //        FuzzyOutSet* par - set that this is part of
  31. //
  32. // Returns:
  33. //
  34. //        none
  35. //
  36. // Author:    Michael Zarozinski
  37. // Date:    6/01
  38. // 
  39. // Modification History
  40. // Author    Date        Modification
  41. // ------    ----        ------------
  42. //
  43. //
  44.  
  45. MOMDefuzzSetObj::MOMDefuzzSetObj(FuzzyOutSet* par)
  46. : DefuzzSetObj(par), FFLLBase(par)
  47. {
  48.     mean_value = -1.0;
  49.  
  50. }; // end MOMDefuzzSetObj::MOMDefuzzSetObj()
  51.  
  52. //
  53. // Function:    ~MOMDefuzzSetObj()
  54. // 
  55. // Purpose:        Destructor
  56. //
  57. // Arguments:
  58. //
  59. //        none
  60. //
  61. // Returns:
  62. //
  63. //        none
  64. //
  65. // Author:    Michael Zarozinski
  66. // Date:    6/01
  67. // 
  68. // Modification History
  69. // Author    Date        Modification
  70. // ------    ----        ------------
  71. //
  72. //
  73.  
  74.  
  75. MOMDefuzzSetObj::~MOMDefuzzSetObj()
  76. {
  77.  
  78. }; // end MOMDefuzzSetObj::~MOMDefuzzSetObj()
  79.  
  80.  
  81. //
  82. // Function:    calc()
  83. // 
  84. // Purpose:        Calculates the max 'x' nodes, their average, then sets the 'x' value
  85. //
  86. // Arguments:    
  87. //
  88. //        void
  89. //
  90. // Returns:
  91. //
  92. //        void
  93. //
  94. // Author:    Michael Zarozinski
  95. // Date:    8/01
  96. // 
  97. // Modification History
  98. // Author    Date        Modification
  99. // ------    ----        ------------
  100. //
  101. //
  102.  
  103. void MOMDefuzzSetObj::calc()
  104. {
  105.     int        i;                // counters
  106.       int        x_sum = 0;        // sum of the x values for the nodes with the max 'y'
  107.      int        max_y = -1;        // max y position
  108.  
  109.     // get the min val and idx_multiplier for the var...
  110.  
  111.     FuzzySetBase* set_base = get_parent();
  112.  
  113.     RealType idx_mult = set_base->get_idx_multiplier();
  114.     RealType min_x = set_base->get_left_x();
  115.  
  116.     // this gets a bit complex cuz of the S-Curve where it's possible to have
  117.     // more than 2 nodes all at the max value.  So we'll make this algorithm
  118.     // as generic (read: overkill) as possible.
  119.  
  120.     // loop through all the nodes and find the max y value and the
  121.     // index of the first and LAST node with that value
  122.  
  123.     int first_x, last_x;
  124.     int node_count = set_base->get_node_count();
  125.  
  126.     first_x = last_x = -1;
  127.  
  128.     NodePoint pt;
  129.  
  130.     for (i = 0; i < node_count; i++)
  131.         {
  132.         pt = set_base->get_node(i);
  133.         // y = set_base->member_func->get_node_y(i);
  134.  
  135.          if (pt.y > max_y)
  136.             {
  137.              max_y = pt.y;
  138.             }
  139.  
  140.         } // end loop through nodes
  141.  
  142.     // now find the first and last nodes that have that 'y' value
  143.     for (i = 0; i < node_count; i++)
  144.         {
  145.         pt = set_base->get_node(i);
  146.          // if we found a Y value greater than the previous one, 
  147.         // clear out any old information, we have a new maximum
  148.         if (pt.y == max_y)
  149.             {
  150.              if (first_x == -1)
  151.                 {
  152.                 // set them both so we can avoid special logic
  153.                 // when only one node is at the max y
  154.                 first_x = last_x = pt.x; 
  155.                 }
  156.             else 
  157.                 {
  158.                 // set the last node, if theres one furthur down
  159.                 // it'll overwrite this one
  160.                 last_x = pt.x; 
  161.                 }
  162.             } // end if y == max_y
  163.  
  164.         } // end loop through nodes
  165.  
  166.  
  167.     // since the moment invovles the distance from 0 and we use the x_idx
  168.     // above in our calcs, we need to multiply by the index multiplier
  169.     // to convert an index to a 'x' value.  Note: we could have multiplied
  170.     // above when we calculated each moment but it's faster to just perform
  171.     // ONE multiplication here
  172.     // We also add the starting x value of the output variable to get
  173.     // it in the correct position relative to the "origin" of the output variable
  174.  
  175.     set_mean_value(min_x +  ((first_x + (static_cast<RealType>(last_x - first_x)/2.0f)) * idx_mult ));
  176.  
  177. } // end MOMDefuzzSetObj::calc()
  178.  
  179.  
  180. /////////////////////////////////////////////////////////////////////
  181. ////////// Trivial Functions That Don't Require Headers /////////////
  182. /////////////////////////////////////////////////////////////////////
  183. RealType MOMDefuzzSetObj::get_mean_value() const
  184. {
  185.     return mean_value;
  186. };
  187. void MOMDefuzzSetObj::set_mean_value( RealType val)
  188. {  
  189.     mean_value= val;
  190. };
  191.  
  192. int MOMDefuzzSetObj::get_defuzz_type() const 
  193.     return DefuzzVarObj::DEFUZZ_TYPE::MOM; 
  194. };
  195.  
  196.