A fuzzy logic C++ library
|
Rule defined as array of indexes on input and output sets of membership functions. More...
#include <rule_idx.hpp>
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_IDX & | operator= (const RULE_IDX &r) |
Assignment operator (copy of fields !) | |
Rule terms related functions | |
const RULE_IDX_TERM & | GetTerms () const |
RULE_IDX_TERM & | GetTerms () |
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_BASE * | s_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. |
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.
slifis::RULE_IDX::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).
References P_Init().
slifis::RULE_IDX::RULE_IDX | ( | const RULE & | r | ) |
slifis::RULE_IDX::RULE_IDX | ( | const RULE_IDX & | r | ) |
Copy constructor of RULE_IDX.
Calls copy constructor of ROOT_RULE
References __IN__, __OUT__, _Degree, _m_ini_mfi, _OrgNbRules, _OutputMf, and P_Init().
void slifis::RULE_IDX::Clear | ( | ) |
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] |
References _Degree.
Referenced by slifis::RULE_BASE::AddNewRule(), slifis::RULE_BASE::HasRule(), and P_Read_XML().
FUZZYVAL slifis::RULE_IDX::GetDegree | ( | ) | const [inline] |
References _Degree.
Referenced by slifis::RULE_BASE::ExportCSV(), slifis::RULE_BASE::P_SelectBestRule(), slifis::RULE_BASE::Print(), and slifis::RULE_BASE::Print2().
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().
RULE_NBFUNC_SIZE slifis::RULE_IDX::GetOutMfIndex | ( | ) | const [inline] |
Returns rule index of output membership function.
References _OutputMf.
Referenced by slifis::SLIFIS::AddRule(), slifis::RB_TABLE::Compute(), slifis_plot::PLOT_RB_TEXT::Draw(), slifis::RULE_BASE::ExportCSV(), Mouse_CB_rb_2D(), Mouse_CB_rb_text(), operator==(), slifis::SLIFIS::P_Evaluate_M(), slifis::RULE_BASE::P_HaveSameOutput(), slifis::SLIFIS::P_Write_FCL(), P_Write_XML(), slifis::RULE_BASE::Print(), slifis::RULE_BASE::Print2(), slifis::PrintDifferences(), and slifis::RULE_BASE::rbIsValid().
bool slifis::RULE_IDX::operator== | ( | const RULE_IDX & | r | ) | const |
Equality operator.
References slifis::ROOT_RULE::GetFisType(), GetOutMfIndex(), PremisesAreEqual, and slifis::TYPE_MAMDANI.
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.
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.
Referenced by slifis::SLIFIS::AddRule(), slifis::RB_TABLE::Compute(), slifis_plot::PLOT_RB_TEXT::Draw(), slifis::RULE_BASE::ExportCSV(), slifis_plot::PLOT_RB_TEXT::GetPositionInfo(), Mouse_CB_rb_text(), slifis::RULE_BASE::P_ComputeUsedInputs(), slifis::RULE_BASE::P_CoversAllInputValues(), slifis::SLIFIS::P_Write_FCL(), slifis::RULE_BASE::Print(), slifis::RULE_BASE::Print2(), and slifis::RULE_BASE::rbIsValid().
RULE_IDX_TERM& slifis::RULE_IDX::GetTerms | ( | ) | [inline] |
References _m_ini_mfi.
bool slifis::RULE_IDX::UsesInput | ( | size_t | idx | ) | const |
Returns true if the rule uses input idx
.
References _m_ini_mfi, and GetNbTerms().
Referenced by slifis::RB_TABLE::Compute(), slifis_plot::PLOT_RB_TEXT::GetPositionInfo(), Mouse_CB_rb_text(), slifis::RULE_BASE::P_CoversAllInputValues(), and slifis::RULE_BASE::P_FactorizeBy().
void slifis::RULE_IDX::AddTerm | ( | RULE_NBVAR_SIZE | NumInVar, |
RULE_NBFUNC_SIZE | NumFunc | ||
) |
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"
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] |
Returns the number of terms in the rule (antecedent)
References _m_ini_mfi.
Referenced by slifis::SLIFIS::AddRule(), slifis::RB_TABLE::Compute(), slifis_plot::PLOT_RB_TEXT::Draw(), slifis::RULE_BASE::ExportCSV(), Mouse_CB_rb_text(), operator<(), slifis::SLIFIS::P_Evaluate_M(), slifis::RULE_BASE::P_FactorizeBy(), slifis::RULE_BASE::P_HaveSameOutput(), P_IsEqualUpToInput(), P_Write_bin(), slifis::SLIFIS::P_Write_FCL(), P_Write_XML(), slifis::PremisesAreEqual(), slifis::RULE_BASE::Print(), Print(), slifis::RULE_BASE::Print2(), slifis::PrintDifferences(), slifis::rule2_cmp2(), and UsesInput().
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 |
References __IN__, __OUT__, _Degree, _m_ini_mfi, _OrgNbRules, _OutputMf, slifis::ROOT_RULE::GetFisType(), GetNbTerms(), slifis::ROOT_RULE::GetOpType(), slifis::ROOT_RULE::GetTSCoeff(), slifis::ROOT_RULE::GetTSConstCoeff(), slifis::ROOT_RULE::GetTSVectorSize(), slifis::OPR_AND, slifis::OPR_OR, SWITCH_ERROR, slifis::TYPE_MAMDANI, and slifis::TYPE_SUGENO.
Referenced by slifis::SLIFIS::AddRule(), GetRuleFiringStrength(), slifis::RULE_BASE::Print(), slifis::RULE_BASE::Print2(), and slifis::PrintDifferences().
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.
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 | ||
) |
Compares TS Rule to data subset used to build the rule, and computes the error.
References slifis::ESTIM_ERROR::diff_max, slifis::DATA_SET::GetDataPoint(), slifis::ROOT_RULE::GetFisType(), slifis::DATA_SET::GetNbInputFields(), slifis::DATA_SET::GetNbPoints(), slifis::DATA_SET::GetOutValue(), slifis::ROOT_RULE::GetTSCoeff(), slifis::ROOT_RULE::GetTSConstCoeff(), slifis::ROOT_RULE::GetTSVectorSize(), slifis::DATA_POINT::GetValue_f(), slifis::ESTIM_ERROR::mae, slifis::ESTIM_ERROR::mse, slifis::ESTIM_ERROR::NbPoints, and slifis::TYPE_SUGENO.
Referenced by slifis::SLIFIS::BuildTSRulesFromValues().
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
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
.
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] |
References _Degree, _m_ini_mfi, slifis::ROOT_RULE::GetFisType(), GetNbTerms(), slifis::ROOT_RULE::GetOpType(), GetOutMfIndex(), slifis::GetString(), slifis::ROOT_RULE::GetTSCoeff(), slifis::ROOT_RULE::GetTSConstCoeff(), slifis::ROOT_RULE::GetTSVectorSize(), slifis::TYPE_MAMDANI, and slifis::TYPE_SUGENO.
Referenced by slifis::RULE_BASE::P_Write_XML().
bool slifis::RULE_IDX::P_Read_XML | ( | TiXmlElement * | tiel | ) | [private] |
Read xml, see RULE_BASE::Read_XML()
References AddTerm(), slifis::ROOT_RULE::AllocateTSVector(), slifis::ROOT_RULE::GetFisType(), SetDegree(), slifis::ROOT_RULE::SetFisType(), slifis::ROOT_RULE::SetOpType(), SetOutMfIndex(), slifis::ROOT_RULE::SetTSCoeff(), slifis::ROOT_RULE::SetTSConstCoeff(), SLIFIS_ERROR_LOG, SLIFIS_XML_READ_DOUBLE_ATTRIB, SLIFIS_XML_READ_INT_ATTRIB, SLIFIS_XML_READ_STRING_ATTRIB, and slifis::TYPE_SUGENO.
Referenced by slifis::RULE_BASE::P_Read_XML().
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.
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.
EN_SNORM slifis::RULE_IDX::s_RuleOrNorm = SN_MAX [static] |
Referenced by GetRuleFiringStrength().
EN_TNORM slifis::RULE_IDX::s_RuleAndNorm = TN_PROD [static] |
Referenced by GetRuleFiringStrength().
RULE_IDX_TERM slifis::RULE_IDX::_m_ini_mfi [private] |
STL map of pairs (ini: INput variable Index, mfi: Membership Function Index)
Referenced by AddTerm(), Clear(), GetNbTerms(), GetRuleFiringStrength(), GetTerms(), operator<(), operator=(), P_IsEqualUpToInput(), P_Write_bin(), P_Write_XML(), slifis::PremisesAreEqual(), Print(), RemoveTermForInput(), RULE_IDX(), and UsesInput().
RULE_NBFUNC_SIZE slifis::RULE_IDX::_OutputMf [private] |
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().
FUZZYVAL slifis::RULE_IDX::_Degree [private] |
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().
int slifis::RULE_IDX::_OrgNbRules [private] |
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().