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

  1. //
  2. // File:    FuzzyModelBase.h
  3. //
  4. // Purpose:    Interface for the FuzzyModelBase class.  This class holds
  5. //            all the information for a fuzzy model.
  6. //
  7. // Copyright ⌐ 1999-2001 Louder Than A Bomb! Software
  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. #if !defined(AFX_FuzzyModelBase_H__F9E8F561_B1FA_11D2_B77E_00A855C10000__INCLUDED_)
  14. #define AFX_FuzzyModelBase_H__F9E8F561_B1FA_11D2_B77E_00A855C10000__INCLUDED_
  15.  
  16.  
  17. #include "FFLLBase.h"  
  18.  
  19. class FuzzyVariableBase;
  20. class FuzzyOutVariable;
  21. class FuzzySetBase;
  22. class RuleArray;
  23.  
  24. // Class:    FuzzyModelBase
  25. //
  26. // Purpose:    This is the "main" class for a FFLL model. It contains the variables
  27. //            and rules that define a model.
  28. //
  29.  
  30. class FFLL_API FuzzyModelBase : virtual public FFLLBase  
  31. {
  32.     ////////////////////////////////////////
  33.     ////////// Member Functions ////////////
  34.     ////////////////////////////////////////
  35.  
  36.     public:
  37.  
  38.         // constructors/destructors
  39.         FuzzyModelBase();
  40.         virtual    ~FuzzyModelBase();
  41.           void init();
  42.  
  43.         // get functions
  44.         int get_defuzz_method() const;
  45.         int get_rule_index(int _var_idx, int _set_idx = -1) const;
  46.         int get_composition_method() const;     
  47.         DOMType get_dom(int var_idx, int set_idx, int x_position) const;
  48.         const char* get_msg_textA();
  49.         FFLL_INLINE int get_num_of_rules() const;
  50.         FFLL_INLINE int get_num_of_sets(int var_idx) const ;
  51.          FFLL_INLINE int get_input_var_count() const;
  52.          FFLL_INLINE int get_inference_method() const ;     
  53.         FFLL_INLINE RuleArrayType get_rule(int idx) const ;
  54.         FFLL_INLINE const char* get_model_name() const;
  55.  
  56.         // set functions
  57.          int set_defuzz_method(int method);
  58.         virtual int set_inference_method(int method);
  59.         virtual int set_composition_method(int method);
  60.  
  61.         // load file functions
  62.           virtual int load_from_fcl_file(const char* file_name);
  63.         int load(const char* file, short mode, bool from_loder = false);
  64.  
  65.         // save model functions
  66.         virtual int save_to_fcl_file(const char* file_name);
  67.  
  68.         // variable/set functions
  69.  
  70.         virtual int delete_variable(int _var_idx);
  71.         virtual int add_input_variable(const wchar_t* _name = NULL, RealType start_x = 0, RealType end_x = 100, bool create_unique_id = true);
  72.         virtual int add_output_variable(const wchar_t* _name = NULL, RealType start_x = 0, RealType end_x = 100, bool create_unique_id = true);
  73.         bool is_var_id_unique(const wchar_t* _id, int _var_idx) const;
  74.            virtual int delete_set(int _var_idx, int _set_idx);
  75.            int add_set(int var_idx, const FuzzySetBase* _set);
  76.  
  77.         // rule functions
  78.         FFLL_INLINE bool no_rules() const ;
  79.         FFLL_INLINE RuleArrayType rule_exists(int index) const ;
  80.         FFLL_INLINE void clear_rules();
  81.         virtual void add_rule(int index, RuleArrayType output_set);
  82.         virtual void remove_rule(int index);
  83.  
  84.         // misc functions
  85.          void calc_rule_components(int rule_index, int* set_idx_array) const; 
  86.         void calc_rule_index_wrapper(void);
  87.         RealType calc_output(short*  var_idx_arr, DOMType* out_set_dom_arr)  ;
  88.         ValuesArrCountType convert_value_to_idx(int var_idx, RealType value) const; 
  89.          static void validate_fcl_identifier(std::ofstream& file_contents, std::string identifier);
  90.  
  91.     protected:
  92.  
  93.         // get functions
  94.         FuzzyVariableBase* get_var(int idx) const;
  95.         FFLL_INLINE int get_total_var_count() const;
  96.  
  97.         // set functions
  98.         void set_model_name(const char* _name);
  99.         virtual int set_output_dom(DOMType* out_set_dom_arr, int set_idx, DOMType new_value) ;
  100.  
  101.         // load file functions
  102.          int load_vars_from_fcl_file(std::ifstream& file_contents, bool output = false);
  103.         int load_defuzz_block_from_fcl_file(std::ifstream& file_contents);
  104.         int load_rules_from_fcl_file(std::ifstream& file_contents);
  105.   
  106.         // save model functions
  107.          void save_rules_to_fcl_file(std::ofstream& file_contents) const;
  108.  
  109.         // variable functions
  110.         virtual FuzzyOutVariable* new_output_variable();
  111.          virtual FuzzyVariableBase* new_variable();
  112.         void delete_vars(); 
  113.         void add_input_var_to_list(FuzzyVariableBase* var );
  114.  
  115.         // misc functions
  116.         void calc_active_output_level_wrapper(short* var_idx_arr, DOMType* out_set_dom_arr);
  117.         int calc_rule_index(int var_idx);
  118.  
  119.     private:
  120.  
  121.         // get functions
  122.          const char* get_fcl_block_start() const;
  123.         const char* get_fcl_block_end() const;
  124.  
  125.         // rule functions
  126.         virtual RuleArray* new_rule_array();
  127.  
  128.          // misc functions
  129.         void calc_active_output_level(int var_num, DOMType activation_level, int rule_index, short* var_idx_arr, DOMType* out_set_dom_arr )   ;
  130.         int calc_num_of_rules() const;
  131.  
  132.     ////////////////////////////////////////
  133.     ////////// Class Variables /////////////
  134.     ////////////////////////////////////////
  135.     public:
  136.         // inference operation is the type of operation to perform when combining
  137.         // the "if" parts of the "if-then" rules.  If MIN we take the minimum
  138.         // value of all the anteceedents.  If MAX, we take the max.
  139.  
  140.         // NOTE: we broke composition (in FuzzyVariableOut) and inference into 2 parts for flexability.  
  141.         // you will often see them combined and refered to as "inference methods"
  142.         // such as MIN-MAX.
  143.         enum INFERENCE_OPERATION { MIN, MAX };
  144.  
  145.  
  146.     protected:
  147.   
  148.         FuzzyVariableBase**        input_var_arr;    // array of input variables that make up this rule
  149.         FuzzyOutVariable*        output_var;        // output variable for this rule
  150.          RuleArray*                rules;            // array of all combinations of rules. 
  151.  
  152.     private:
  153.         int                inference_method;    // inference method to use for this model
  154.           int                input_var_count;    // number of input variables that make up this rule
  155.          std::string        ascii_err_msg;        // string to enable conversion from wide chars to ascii chars     
  156.          std::string        model_name;            // name of the flile we've opened
  157.  
  158. }; // end class FuzzyModelBase
  159.  
  160. #else
  161.  
  162. // already included
  163. class FuzzyModelBase;
  164.  
  165. #endif // !defined(AFX_FuzzyModelBase_H__F9E8F561_B1FA_11D2_B77E_00A855C10000__INCLUDED_)
  166.