A fuzzy logic C++ library
Static Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes | Friends
slifis::RULE_IDX Class Reference

Rule defined as array of indexes on input and output sets of membership functions. More...

#include <rule_idx.hpp>

+ Inheritance diagram for slifis::RULE_IDX:
+ Collaboration diagram for slifis::RULE_IDX:

List of all members.

Public Member Functions

Constructors and destructors
 RULE_IDX (EN_FIS_TYPE rt=TYPE_MAMDANI, EN_RULE_OP op=OPR_AND)
 Constructor provided if one needs to create a rule with an association of rule terms different than the default one (see default ROOT_RULE::ROOT_RULE() constructor).
 RULE_IDX (const RULE &r1)
 Constructor of RULE_IDX from a RULE.
 RULE_IDX (const RULE_IDX &r2)
 Copy constructor of RULE_IDX.
void Clear ()
 Clears the antecedent part of the rule (rule terms), does NOT modify output function index. Resets degree to 1.
Rule output related functions
void SetDegree (FUZZYVAL d)
FUZZYVAL GetDegree () const
void SetOutMfIndex (RULE_NBFUNC_SIZE n)
 Assigns rule output membership function index.
RULE_NBFUNC_SIZE GetOutMfIndex () const
 Returns rule index of output membership function.
Operators
bool operator== (const RULE_IDX &r) const
 Equality operator.
bool operator!= (const RULE_IDX &m) const
bool operator< (const RULE_IDX &r) const
 Needed for sorting rules in a RULE_BASE. BEWARE: this operator is functional ONLY if the caller takes care of fixing the static attribute RULE_IDX::s_NbInputs, using static function RULE_IDX::SetNbInputs().
RULE_IDXoperator= (const RULE_IDX &r)
 Assignment operator (copy of fields !)
Rule terms related functions
const RULE_IDX_TERMGetTerms () const
RULE_IDX_TERMGetTerms ()
bool UsesInput (size_t idx) const
 Returns true if the rule uses input idx.
void AddTerm (RULE_NBVAR_SIZE NumInVar, RULE_NBFUNC_SIZE NumFunc)
 Adds a term to the rule, or change the index if already one.
size_t GetNbTerms () const
 Returns the number of terms in the rule (antecedent)
void RemoveTermForInput (size_t in_idx)
 Removes the term or rule that uses the input of index in_idx.
I/O functions and related
void Print (FILE *f=stderr, const char *msg=0) const

Static Public Attributes

static EN_SNORM s_RuleOrNorm = SN_MAX
static EN_TNORM s_RuleAndNorm = TN_PROD

Private Member Functions

void P_Init ()
 Private function, only used by constructors.
bool P_IsEqualUpToInput (size_t in_idx, const RULE_IDX &r) const
 Returns true if rule r is equal to current rule, output index NOT included, without considering input in_idx.
bool P_Write_bin (std::ofstream &f) const
 Writes the rule into the (opened) binary stream f.
bool P_Read_bin (std::ifstream &f)
 Reads the rule from the (opened) binary stream f.
bool P_Write_XML (TiXmlElement *tiel) const
bool P_Read_XML (TiXmlElement *tiel)
 Read xml, see RULE_BASE::Read_XML()

Private Attributes

RULE_IDX_TERM _m_ini_mfi
 STL map of pairs (ini: INput variable Index, mfi: Membership Function Index)
RULE_NBFUNC_SIZE _OutputMf
 Index of the output membership function in the output set of functions (for Mamdani type)
FUZZYVAL _Degree
 Degree of a rule, used only in case of a learning situation (see Wang & Mendel paper)
int _OrgNbRules
 In case of a reduced rule base, this member holds the number of rules that led to this one. Default value is 1.

Static Private Attributes

static size_t s_idx_sort = 0
 Holds the column used for sorting.
static RULE_BASEs_rb = 0
 Holds the RULE_BASE that "owns" this rule. This is needed for signaling the rule base that a rule has been edited. However, you can still build a RULE_IDX object without setting this pointer, it will be set to 0. This pointer can be initialised afterwards with the function RULE_IDX::AssignRuleBase().

Friends

class RULE_BASE
std::ostream & operator<< (std::ostream &Stream, const RULE_IDX &obj)
bool rule2_cmp2 (const RULE_IDX &a, const RULE_IDX &b)
 Alternate comparison function, sorts rule by considering first output function index, and second the rule terms, as defined by slifis::RULE_IDX::operator<()
bool PremisesAreEqual (const RULE_IDX &r1, const RULE_IDX &r2)
 Returns true id the antecedent part of rules are equal.
void PrintDifferences (FILE *f, const RULE_IDX &r1, const RULE_IDX &r2)
 Prints in f the differences between the two rules.

Misc.

void SetOrgNbRules (int nb)
 Used for fixing the original number of rules that led to this one, after reducing nb of rules.
FUZZYVAL GetRuleFiringStrength (const INPUT_SETS &InputFuzzySets) const
 Returns the firing strength of a rule, expressed as a fuzzy value, for given input values.
void ComputeTSError (const DATA_SET &subset, ESTIM_ERROR &error)
 Compares TS Rule to data subset used to build the rule, and computes the error.
static void AssignRuleBase (RULE_BASE *rule_base)
static void SetSortIndex (size_t idx)
 Used for selecting the index used for sorting the rule base.

Detailed Description

Rule defined as array of indexes on input and output sets of membership functions.

Of course, these indexes only make sense related to a given SLIFIS object, that contains these sets of functions.

Please note that different rules might store the index of a MF for a given input variable at a different "place" in the 'NumVar' vector. This is why the equality operator seems so complex ( see operator==() ).

At present, this class is designed so that it can hold a "Mamdani" or a "Takagi-Sugeno" rule.

See Handling rules for usage.

Internals : a RULE_IDX object is made (mainly) of 2 parts "terms", and an output values.

Medium Priority Todo:
Idea: separate into 2 different classes for Mamdani and TS rules. This way we can store TS coeffs and fitting errors only in a TS rule. This implies to rethink rules classes relations...

Constructor & Destructor Documentation

Constructor provided if one needs to create a rule with an association of rule terms different than the default one (see default ROOT_RULE::ROOT_RULE() constructor).

References P_Init().

Constructor of RULE_IDX from a RULE.

Calls copy constructor of ROOT_RULE If the rule is TS type, the vector (if allocated) will be copied by copy-constructor of ROOT_RULE

References P_Init().

Copy constructor of RULE_IDX.

Calls copy constructor of ROOT_RULE

References __IN__, __OUT__, _Degree, _m_ini_mfi, _OrgNbRules, _OutputMf, and P_Init().


Member Function Documentation

Clears the antecedent part of the rule (rule terms), does NOT modify output function index. Resets degree to 1.

References _Degree, _m_ini_mfi, slifis::RULE_BASE::Changed(), and s_rb.

void slifis::RULE_IDX::SetDegree ( FUZZYVAL  d) [inline]

Assigns rule output membership function index.

Defines the consequence for the rule, expressed as an index, defining one of the output membership functions.

References __IN__, __OUT__, _OutputMf, slifis::RULE_BASE::Changed(), slifis::ERR_RULE_BAD_TYPE, slifis::ROOT_RULE::GetFisType(), s_rb, SLIFIS_ERROR, and slifis::TYPE_SUGENO.

Referenced by slifis::RULE_BASE::AddNewRule(), slifis::SLIFIS::AddRule(), slifis::RULE_BASE::HasRule(), Mouse_CB_rb_2D(), Mouse_CB_rb_text(), and P_Read_XML().

bool slifis::RULE_IDX::operator== ( const RULE_IDX r) const
bool slifis::RULE_IDX::operator!= ( const RULE_IDX m) const [inline]
bool slifis::RULE_IDX::operator< ( const RULE_IDX r) const

Needed for sorting rules in a RULE_BASE. BEWARE: this operator is functional ONLY if the caller takes care of fixing the static attribute RULE_IDX::s_NbInputs, using static function RULE_IDX::SetNbInputs().

This is needed because we can only compare function indexes for the same input ! We could have implemented a counting function, but that would have slowed sorting.

References _m_ini_mfi, GetNbTerms(), s_idx_sort, and slifis::ROOT_RULE::s_NbInputs.

RULE_IDX & slifis::RULE_IDX::operator= ( const RULE_IDX r)

Assignment operator (copy of fields !)

References __IN__, __OUT__, _Degree, _m_ini_mfi, _OutputMf, slifis::RULE_BASE::Changed(), and s_rb.

const RULE_IDX_TERM& slifis::RULE_IDX::GetTerms ( ) const [inline]

References _m_ini_mfi.

bool slifis::RULE_IDX::UsesInput ( size_t  idx) const

Adds a term to the rule, or change the index if already one.

"AddTerm( x, y )" means adding to the current rule "input x is y"

Warning:
No checking done here, previous value for input NumInVar is lost.

References __IN__, __OUT__, _m_ini_mfi, slifis::RULE_BASE::Changed(), and s_rb.

Referenced by slifis::RULE_BASE::AddNewRule(), slifis::SLIFIS::AddRule(), slifis::SLIFIS::BuildTSRulesFromValues(), Mouse_CB_rb_2D(), Mouse_CB_rb_text(), P_Read_bin(), and P_Read_XML().

size_t slifis::RULE_IDX::GetNbTerms ( ) const [inline]
void slifis::RULE_IDX::RemoveTermForInput ( size_t  in_idx)

Removes the term or rule that uses the input of index in_idx.

For example, if the rule is:

* if in(0) is "1" and in(3) is "5" and in(6) is "2" then output is "whatever",

then making the following call:

r.RemoveTermForInput(3);

then the rule will become

* if in(0) is "1" and in(6) is "2" then output is "whatever",

If requested index is not included in rule, then an error will be generated.

References __IN__, __OUT__, _m_ini_mfi, slifis::RULE_BASE::Changed(), slifis::ERR_RULE_BAD_INDEX, s_rb, and SLIFIS_ERROR_2.

Referenced by Mouse_CB_rb_text(), and slifis::RULE_BASE::P_FactorizeBy().

void slifis::RULE_IDX::Print ( FILE *  f = stderr,
const char *  msg = 0 
) const
void slifis::RULE_IDX::SetOrgNbRules ( int  nb) [inline]

Used for fixing the original number of rules that led to this one, after reducing nb of rules.

References _OrgNbRules.

Referenced by slifis::RULE_BASE::Reduce().

FUZZYVAL slifis::RULE_IDX::GetRuleFiringStrength ( const INPUT_SETS InputFuzzySets) const

Returns the firing strength of a rule, expressed as a fuzzy value, for given input values.

  • Also known as "degree of fulfillment."
  • The t-norm (for AND operation) or s-norm (for OR-rules) that are used are defined by the static attributes s_RuleAndNorm and s_RuleOrNorm.

See:

References __IN__, __OUT__, _m_ini_mfi, slifis::ERR_MF_BAD_INDEX, slifis::ERR_MFSET_BAD_INDEX, slifis::MEMBFUNC::Fuzzify(), slifis::FUZZY_IN::GetInputValue(), slifis::FUZZY_ROOT::GetMf(), slifis::INPUT_SETS::GetMfSet(), slifis::INPUT_SETS::GetNb(), slifis::FUZZY_ROOT::GetNbMf(), slifis::ROOT_RULE::GetOpType(), slifis::OPR_AND, Print(), s_RuleAndNorm, s_RuleOrNorm, SLIFIS_DEBUG_LOG, SLIFIS_ERROR_2, slifis::SN_BSUM, slifis::SN_MAX, slifis::SN_PSUM, SWITCH_ERROR, slifis::TN_LUKA, slifis::TN_MIN, and slifis::TN_PROD.

Referenced by slifis::SLIFIS::P_Evaluate_M(), and slifis::SLIFIS::P_Evaluate_TS().

void slifis::RULE_IDX::ComputeTSError ( const DATA_SET subset,
ESTIM_ERROR error 
)
static void slifis::RULE_IDX::AssignRuleBase ( RULE_BASE rule_base) [inline, static]

References s_rb.

Referenced by slifis::RULE_BASE::RULE_BASE().

static void slifis::RULE_IDX::SetSortIndex ( size_t  idx) [inline, static]

Used for selecting the index used for sorting the rule base.

References s_idx_sort.

Referenced by slifis::RULE_BASE::Sort().

void slifis::RULE_IDX::P_Init ( ) [private]

Private function, only used by constructors.

References _Degree, _OrgNbRules, _OutputMf, slifis::RULE_BASE::Changed(), and s_rb.

Referenced by RULE_IDX().

bool slifis::RULE_IDX::P_IsEqualUpToInput ( size_t  in_idx,
const RULE_IDX r 
) const [private]

Returns true if rule r is equal to current rule, output index NOT included, without considering input in_idx.

Used in factorising rules

For example, with the following two rules : in0 in1 if "1" and "1" then output is "1" if "0" and "1" then output is "1"

invoking

  • ra.P_IsEqualUpToInput( 0, rb ) will return true
  • ra.P_IsEqualUpToInput( 1, rb ) will return false

Please note that the rule MUST have at least 2 terms !

Note: If the rules have a different nb of terms, then always return false

References __IN__, __OUT__, _m_ini_mfi, slifis::ERR_RULE_NO_TERMS, GetNbTerms(), and SLIFIS_ERROR_2.

Referenced by slifis::RULE_BASE::P_FactorizeBy().

bool slifis::RULE_IDX::P_Write_bin ( std::ofstream &  f) const [private]

Writes the rule into the (opened) binary stream f.

Returns true on success, false if I/O error

Please note that in order to avoid future incompatibilities between binary data files that would have been written by different version of this lib that eventually didn't use the same data size for the set stl::map, these values are always written (and read) as the biggest (common...) datatype available, that is: size_t.

Reimplemented from slifis::ROOT_RULE.

References __IN__, __OUT__, _Degree, _m_ini_mfi, _OutputMf, slifis::ROOT_RULE::GetFisType(), GetNbTerms(), STREAM_OK_TEST, and STREAMCHECK_INIT.

Referenced by slifis::RULE_BASE::P_Write_bin().

bool slifis::RULE_IDX::P_Read_bin ( std::ifstream &  f) [private]

Reads the rule from the (opened) binary stream f.

  • Assumes file pointer is correctly set
  • This function is called only from SLIFIS::Read(), so type of rule is automatically the same of the fis.

Reimplemented from slifis::ROOT_RULE.

References __IN__, __OUT__, _Degree, _OutputMf, AddTerm(), slifis::RULE_BASE::Changed(), s_rb, slifis::ROOT_RULE::SetFisType(), STREAM_OK_TEST, and STREAMCHECK_INIT.

Referenced by slifis::RULE_BASE::P_Read_bin().

bool slifis::RULE_IDX::P_Write_XML ( TiXmlElement *  tiel) const [private]
bool slifis::RULE_IDX::P_Read_XML ( TiXmlElement *  tiel) [private]

Friends And Related Function Documentation

friend class RULE_BASE [friend]
std::ostream& operator<< ( std::ostream &  Stream,
const RULE_IDX obj 
) [friend]
bool rule2_cmp2 ( const RULE_IDX a,
const RULE_IDX b 
) [friend]

Alternate comparison function, sorts rule by considering first output function index, and second the rule terms, as defined by slifis::RULE_IDX::operator<()

bool PremisesAreEqual ( const RULE_IDX r1,
const RULE_IDX r2 
) [friend]

Returns true id the antecedent part of rules are equal.

  • Two rules will be considered equal if there antecedent match, whatever the consequence part.
  • Used in RULE_BASE::Reduce() to remove similar rules, this is why we only compare the antecedent part of the rule.

Referenced by operator==().

void PrintDifferences ( FILE *  f,
const RULE_IDX r1,
const RULE_IDX r2 
) [friend]

Prints in f the differences between the two rules.


Member Data Documentation

Referenced by GetRuleFiringStrength().

Referenced by GetRuleFiringStrength().

Index of the output membership function in the output set of functions (for Mamdani type)

Referenced by GetOutMfIndex(), operator=(), P_Init(), P_Read_bin(), P_Write_bin(), Print(), slifis::rule2_cmp2(), RULE_IDX(), and SetOutMfIndex().

Degree of a rule, used only in case of a learning situation (see Wang & Mendel paper)

Referenced by Clear(), GetDegree(), operator=(), P_Init(), P_Read_bin(), P_Write_bin(), P_Write_XML(), Print(), RULE_IDX(), and SetDegree().

In case of a reduced rule base, this member holds the number of rules that led to this one. Default value is 1.

Referenced by P_Init(), Print(), RULE_IDX(), and SetOrgNbRules().

size_t slifis::RULE_IDX::s_idx_sort = 0 [static, private]

Holds the column used for sorting.

Referenced by operator<(), and SetSortIndex().

RULE_BASE * slifis::RULE_IDX::s_rb = 0 [static, private]

Holds the RULE_BASE that "owns" this rule. This is needed for signaling the rule base that a rule has been edited. However, you can still build a RULE_IDX object without setting this pointer, it will be set to 0. This pointer can be initialised afterwards with the function RULE_IDX::AssignRuleBase().

Referenced by AddTerm(), AssignRuleBase(), Clear(), operator=(), P_Init(), P_Read_bin(), RemoveTermForInput(), and SetOutMfIndex().