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

  1. //
  2. // File:    MemberFuncSingle.cpp
  3. //
  4. // Purpose:    Implementation of the Singelton membership function
  5. //
  6. // Copyright ⌐ 1999-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 "MemberFuncSingle.h"
  13. #include "FuzzyModelBase.h"
  14. #include "FuzzySetBase.h"
  15. #include "FuzzyVariableBase.h"
  16.  
  17. #ifdef _DEBUG  
  18. #undef THIS_FILE
  19. static char THIS_FILE[]=__FILE__;
  20. #endif
  21.  
  22.  
  23. //
  24. // Function:    MemberFuncSingle()
  25. // 
  26. // Purpose:        Constructor
  27. //
  28. // Arguments:
  29. //
  30. //        FuzzySetBase* _parent - Set this member function is part of
  31. //
  32. // Returns:
  33. //
  34. //        nothing
  35. //
  36. // Author:    Michael Zarozinski
  37. // Date:    8/99
  38. // 
  39. // Modification History
  40. // Author    Date        Modification
  41. // ------    ----        ------------
  42. //
  43. // 
  44. MemberFuncSingle::MemberFuncSingle(FuzzySetBase* _parent) : MemberFuncBase(_parent), FFLLBase(_parent)
  45. {
  46.     // create the nodes array
  47.     alloc_nodes( get_node_count() );
  48.  
  49. } // MemberFuncSingle::MemberFuncSingle()
  50.  
  51.  
  52. //
  53. // Function:    ~MemberFuncSingle()
  54. // 
  55. // Purpose:        Destructor
  56. //
  57. // Arguments:
  58. //
  59. //        none
  60. //
  61. // Returns:
  62. //
  63. //        nothing
  64. //
  65. // Author:    Michael Zarozinski
  66. // Date:    8/99
  67. // 
  68. // Modification History
  69. // Author    Date        Modification
  70. // ------    ----        ------------
  71. //
  72. // 
  73.  
  74. MemberFuncSingle::~MemberFuncSingle() 
  75. {
  76.     // nothing to do, base class(es) handle everything
  77. }
  78.  
  79.  
  80. //
  81. // Function: set_node()
  82. // 
  83. // Purpose:    This function moves a point to a new position. It performs
  84. //            checks to make sure the nodes are at valid positions (such as
  85. //            not allowing the last point to be BEFORE the mid point.
  86. // 
  87. // Arguments:
  88. //
  89. //        int            idx            -    index of the point to move
  90. //        int            x            -    x position to move to (in variable coords)
  91. //        int            y            -    x position to move to (in variable coords)
  92. //        bool        validate    -    indicates if we should perorm sanity checks (default is true)
  93. //
  94. // Returns:
  95. //
  96. //        void
  97. // 
  98. // Globals:
  99. //
  100. // Author:    Michael Zarozinski    
  101. // Date:    6/21/99
  102. // 
  103. //    Modification History
  104. //    Author        Date        Modification
  105. //    ------        ----        ------------
  106. //     
  107. //
  108. void MemberFuncSingle::set_node(int idx, int x, int y, bool validate /* = true */)
  109. {
  110.      nodes[idx].x = x;
  111.  
  112.     if (idx == 0)
  113.         nodes[idx].y = 0;
  114.  
  115. } // end MemberFuncSingle::set_node()
  116.  
  117. //
  118. // Function:    init_nodes()
  119. // 
  120. // Purpose:        Initialized all the nodes to reasonable values depending on the 
  121. //                mid-point and width passed in.
  122. //
  123. // Arguments:
  124. //
  125. //            int        mid_pt_x    -    mid point of the member func
  126. //            int        width        -    how wide this set is
  127. // 
  128. // Returns:
  129. //
  130. //            none
  131. //
  132. // Author:    Michael Zarozinski
  133. // Date:    6/21/99
  134. // 
  135. // Modification History
  136. // Author    Date        Modification
  137. // ------    ----        ------------
  138. //
  139. //
  140.  
  141. void MemberFuncSingle::init_nodes(int mid_pt_x, int term_width)
  142. {
  143.     nodes[0].x = mid_pt_x;  
  144.  
  145. } // end MemberFuncSingle::set_all_anchors()
  146.  
  147. //
  148. // Function:    calc()
  149. // 
  150. // Purpose:        Virtual function that calculates the 'y' points for this curve
  151. //                 and fills in the values[] array.
  152. //
  153. // Arguments:
  154. //
  155. //        none
  156. //
  157. // Returns:
  158. //
  159. //        void
  160. //
  161. // Author:    Michael Zarozinski
  162. // Date:    5/99
  163. // 
  164. // Modification History
  165. // Author    Date        Modification
  166. // ------    ----        ------------
  167. //
  168. void MemberFuncSingle::calc()
  169. {
  170.       // zero out beyond the bookends...
  171.     clear_values( );
  172.   
  173.     set_value(nodes[0].x,FuzzyVariableBase::get_dom_array_max_idx());
  174.   
  175. } // end MemberFuncSingle::calc()
  176.  
  177.   
  178.  
  179. //
  180. // Function:    save_to_fcl_file()
  181. // 
  182. // Purpose:        Virtual func to write out the FCL (Fuzzy Control Language) for this membership function.
  183. //                This is the "membership_function" part of the set's information which
  184. //                has the format (as defined by the IEC 61131-7 International Standard):
  185. //
  186. //                    TERM term_name:= membership_function;
  187. //
  188. //                Singletons are special so just the x value is written out.
  189. //
  190. // Arguments:
  191. //
  192. //        std::ofstream& file_contents - STL stream to write out to a file
  193. //
  194. // Returns:
  195. //
  196. //        void
  197. //
  198. // Author:    Michael Zarozinski
  199. // Date:    9/01
  200. // 
  201. // Modification History
  202. // Author    Date        Modification
  203. // ------    ----        ------------
  204. //
  205. //
  206. void MemberFuncSingle::save_to_fcl_file(std::ofstream& file_contents)
  207. {
  208.      RealType fvalue = get_parent()->convert_idx_to_value(get_node_x(0));
  209.  
  210.     char  val[6];
  211.     int precision ;    // how many decimal places to show  
  212.  
  213.     if (fmod(fvalue, 1) == 0)
  214.         precision = 0;
  215.     else
  216.         precision = 2;
  217.  
  218.      // trim 'x' value to the specified precision
  219.     sprintf( val, "%.*lf", precision, fvalue );
  220.  
  221.      file_contents << val << ";";
  222.  
  223. } // end MemberFuncSingle::save_to_file()
  224.  
  225. /////////////////////////////////////////////////////////////////////
  226. ////////// Trivial Functions That Don't Require Headers /////////////
  227. /////////////////////////////////////////////////////////////////////
  228.  
  229. void MemberFuncSingle::expand(int x_delta ) 
  230. {
  231.     // empty
  232. void MemberFuncSingle::shrink(int x_delta ) 
  233. {
  234.     // empty
  235.  
  236.  
  237. void MemberFuncSingle::set_ramp(int hi_lo_ind, int left_right_ind)
  238. {
  239.     ramp = MemberFuncBase::RAMP::NA; // N/A for this member func
  240. }; 
  241. // the "mid" x is sepecific to the shape... the start/end x funcs
  242. // are in the ancestor
  243. int MemberFuncSingle::get_center_x(void) const 
  244. {
  245.     return nodes[0].x;
  246. };
  247.  
  248. int MemberFuncSingle::get_func_type() const 
  249. {
  250.     return MemberFuncBase::SINGLETON;
  251. };
  252. int MemberFuncSingle::get_node_count() const 
  253. { return 1;
  254. };
  255.