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

Rule base, expressed as a vector of RULE_IDX. More...

#include <rule_base.hpp>

+ Collaboration diagram for slifis::RULE_BASE:

List of all members.

Public Member Functions

Constructors and associated functions
 RULE_BASE ()
 Constructor.
void Clear ()
void Changed () const
 Used for signaling from inside or outside that the rule base has been changed, so rb_table needs to be recomputed.
I/O functions
void Print (FILE *f=stderr, const char *msg=0) const
 Print Rules List (WIP), see Print2()
void Print2 (FILE *f=stderr, const char *msg=0) const
 Print Rules List (WIP) version 2, see Print()
void Print (std::string filename, const char *msg=0, bool OnlyIndexes=false) const
void PrintTable (FILE *f=stderr, const char *msg=0) const
bool ExportCSV (std::string fn) const
 Exports to file fn in CSV format, returns true on success.
Adding and accessing rules
size_t GetNbRules () const
void AddRule (const RULE_IDX &r)
 Add rule r to rule base. Assumes rule is valid, considering the FIS.
void DeleteRule (size_t idx)
 Delete rule idx.
bool AddNewRule (const std::vector< REALVAL > &in, const VALUE &out)
 Builds and adds a AND rule to the FIS, and compute its degree.
bool HasRule (RULE_IDX &r) const
 Returns true if given rule (considering only inputs indexes) is in the rule base, false if not.
const RULE_IDXGetRule (size_t idx) const
 Returns rule idx (const version)
RULE_IDXGetRule (size_t idx)
 Returns rule idx (non-const version)
int GetRuleIndex (const RULE_IDX &r)
 Searches the list of rules for a rule identical (only inputs) to r, and returns its index, or -1 if not found.
Algorithms
void Reduce (EN_REDUCE_METHOD method)
 Parse the rule base, and removes rules with equal premisses and different consequence part.
void Factorize ()
 Checks if some rules can be factorised by input in_idx. Returns nb of rules that were removed.
void Sort (int idx=0)
 Sorting of the rules according to index idx.
Misc.
bool rbIsValid () const
 Checks that no rules use an index of either input or output that does not exist in the FIS.
size_t GetNbInputs () const
 Returns nb of inputs associated to the fis associated to this rule base.
void AssignInputSets (const std::vector< FUZZY_IN > &ifs)
void AssignInputSets (const INPUT_SETS &ifs)
void AssignOutputSet (const FUZZY_OUT &ofs)
const FUZZY_INGetInputSet (size_t idx) const
 Returns input set nb idx associated to the fis associated to this rule base.
const FUZZY_OUTGetOutputSet () const
RB_TABLEGetRbtable () const
Operators
bool operator== (const RULE_BASE &m) const
bool operator!= (const RULE_BASE &m) const

Private Member Functions

bool P_FindBestMf (const FUZZY_ROOT &FuzzySet, REALVAL in, size_t &idx, FUZZYVAL &max) const
 Among the set of MF MfSet, finds the best function for input value in (Private)
RULE_IDX P_SelectBestRule (size_t, size_t, EN_REDUCE_METHOD method) const
 (Private) Helper function for void RULE_BASE::Reduce( EN_REDUCE_METHOD method ). Returns the index on the best rule among the ones
int P_FactorizeBy (size_t in_idx, bool DoRemove)
 Helper function for RULE_BASE::Factorize(), that actually does the job... Returns nb of rules that were removed.
void P_ComputeUsedInputs () const
 Computes how many inputs are effectively handled by the rule base (attribute _v_UsedInputs)
bool P_Write_bin (std::ofstream &f) const
bool P_Read_bin (std::ifstream &f)
 Reading of rule base in a stream.
bool P_Write_XML (TiXmlElement *root) const
bool P_Read_XML (TiXmlElement *pElem)
bool P_HaveSameOutput (const std::vector< std::vector< RULE_IDX >::iterator > &v) const
 Helper function for RULE_BASE::P_Factorize()
bool P_CoversAllInputValues (const std::vector< std::vector< RULE_IDX >::iterator > &v_iter, size_t in_idx) const
 Returns true if all the rules pointed by the iterators in v_iter cover all possible input values for input in_idx.
bool P_HasRefToInputs () const
 Returns true if rule base has a reference on some input set.

Private Attributes

std::vector< RULE_IDX_v_rules
 The rule base.
const std::vector< FUZZY_IN > * pv_InputFuzzySets
 A pointer on the input sets of functions of the SLIFIS object (see RULE_BASE description)
const INPUT_SETSp_inputset
const FUZZY_OUTp_OutputFuzzySet
 A pointer on the output set of functions of the SLIFIS object.
bool _HasBeenReduced
 True if rule base has been reduced.
EN_REDUCE_METHOD _redmeth
 Method used for reducing the nb of rules.
size_t _BadRulesCounter
 For counting the nb of rules where the FIS was unable to find either an input function, either an output function, in a learning situation (default value is 0)
std::vector< size_t > _v_UsedInputs
 Holds the indexes of inputs that are effectively used in the rule base.
RB_TABLE _rb_table
 This attribute is used ONLY for 2-inputs FIS and rule-bases.

Friends

class RB_TABLE
class SLIFIS
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<()

Detailed Description

Rule base, expressed as a vector of RULE_IDX.

It is stored as a vector of RULE_IDX objects. Since these are only made of indexes on input and output membership functions (inside sets of functions), we need to have access to these. This is achieved through the pointers pv_InputFuzzySets and p_OutputFuzzySet, that need to be initialized with a call to AssignMfSets(). Whithout a value value in these pointers, the rule base is meaningless!

These pointers are also used for plotting: when we want to plot a rule base, we need to have access to the underlying input sets, that are not part of the rule base.

For learning rules from data, see Learning rules from data.


Constructor & Destructor Documentation


Member Function Documentation

void slifis::RULE_BASE::Changed ( ) const [inline]

Used for signaling from inside or outside that the rule base has been changed, so rb_table needs to be recomputed.

This can happen when one extracts a rule from a rule base a reference with GetRule()

References slifis::RB_TABLE::_IsComputed, and _rb_table.

Referenced by AddNewRule(), AddRule(), slifis::RULE_IDX::AddTerm(), AssignInputSets(), AssignOutputSet(), Clear(), slifis::RULE_IDX::Clear(), DeleteRule(), slifis::RULE_IDX::operator=(), P_FactorizeBy(), slifis::RULE_IDX::P_Init(), P_Read_bin(), slifis::RULE_IDX::P_Read_bin(), Reduce(), slifis::RULE_IDX::RemoveTermForInput(), RULE_BASE(), slifis::RULE_IDX::SetOutMfIndex(), and Sort().

void slifis::RULE_BASE::Print ( FILE *  f = stderr,
const char *  msg = 0 
) const
void slifis::RULE_BASE::Print2 ( FILE *  f = stderr,
const char *  msg = 0 
) const
void slifis::RULE_BASE::Print ( std::string  filename,
const char *  msg = 0,
bool  OnlyIndexes = false 
) const
void slifis::RULE_BASE::PrintTable ( FILE *  f = stderr,
const char *  msg = 0 
) const
bool slifis::RULE_BASE::ExportCSV ( std::string  fn) const

Exports to file fn in CSV format, returns true on success.

Columns :

  • index
  • type of aggregation (AND or OR string) of terms
  • nb of input terms

for each term :

  • input index
  • function index x n
  • output index
  • degree value

Plus TS coeffs, if any (if the rules are of type 'TS')

References __IN__, __OUT__, _v_UsedInputs, slifis::ERR_RULE_BAD_TYPE, slifis::RULE_IDX::GetDegree(), slifis::ROOT_RULE::GetFisType(), GetNbRules(), slifis::RULE_IDX::GetNbTerms(), slifis::ROOT_RULE::GetOpType(), slifis::RULE_IDX::GetOutMfIndex(), GetRule(), slifis::RULE_IDX::GetTerms(), slifis::ROOT_RULE::GetTSCoeff(), slifis::ROOT_RULE::GetTSConstCoeff(), slifis::ROOT_RULE::GetTSVectorSize(), slifis::OPR_AND, SLIFIS_ERROR_2, SLIFIS_STAMP_S, SLIFIS_WARNING_1, slifis::TYPE_MAMDANI, and slifis::TYPE_SUGENO.

size_t slifis::RULE_BASE::GetNbRules ( ) const [inline]
void slifis::RULE_BASE::AddRule ( const RULE_IDX r) [inline]

Add rule r to rule base. Assumes rule is valid, considering the FIS.

References __IN__, __OUT__, _HasBeenReduced, _v_rules, Changed(), and P_ComputeUsedInputs().

Referenced by Mouse_CB_rb_2D(), Mouse_CB_rb_text(), and P_Read_XML().

void slifis::RULE_BASE::DeleteRule ( size_t  idx)
bool slifis::RULE_BASE::AddNewRule ( const std::vector< REALVAL > &  in,
const VALUE out 
)

Builds and adds a AND rule to the FIS, and compute its degree.

according to:

  • the given input values (one per input variable)
  • the given output value

Returns false if unable to find a matching function for at least one function (that is, for a given input, if the fuzzyfied value is always 0)

Parameters:
inThe vector of input values, in order of input indexes (numeric values only)
outThe output value, string or numeric

References __IN__, __OUT__, _BadRulesCounter, _HasBeenReduced, _v_rules, slifis::RULE_IDX::AddTerm(), Changed(), slifis::DT_NUMERIC, slifis::DT_STRING, slifis::ERR_BAD_INPUT_SIZE, slifis::ERR_RULEBASE_NO_POINTERS, slifis::ERR_UNKNOWN, slifis::FUZZY_ROOT::FindMf(), slifis::VALUE::GetFloat(), GetInputSet(), slifis::INPUT_SETS::GetMfSet(), GetNbInputs(), slifis::VALUE::GetString(), slifis::VALUE::GetType(), P_ComputeUsedInputs(), P_FindBestMf(), P_HasRefToInputs(), p_inputset, p_OutputFuzzySet, pv_InputFuzzySets, slifis::RULE_IDX::SetDegree(), slifis::RULE_IDX::SetOutMfIndex(), SLIFIS_DEBUG_LOG, SLIFIS_ERROR, SLIFIS_ERROR_2, SLIFIS_WARNING, SWITCH_ERROR, and slifis::FUZZY_ROOT::Trigger().

bool slifis::RULE_BASE::HasRule ( RULE_IDX rule) const

Returns true if given rule (considering only inputs indexes) is in the rule base, false if not.

If rule found, then it is completed with output membership function index and rule degree of rule rule. If not found, it is left unchanged.

Deprecated:
not used any more. Why was it written for at the beginning ?

References _v_rules, slifis::RULE_IDX::SetDegree(), and slifis::RULE_IDX::SetOutMfIndex().

const RULE_IDX & slifis::RULE_BASE::GetRule ( size_t  idx) const [inline]
RULE_IDX & slifis::RULE_BASE::GetRule ( size_t  idx) [inline]

Returns rule idx (non-const version)

References __IN__, __OUT__, _v_rules, slifis::ERR_RULE_BAD_INDEX, GetNbRules(), and SLIFIS_ERROR_2.

Searches the list of rules for a rule identical (only inputs) to r, and returns its index, or -1 if not found.

References _v_rules.

Referenced by Mouse_CB_rb_2D().

Parse the rule base, and removes rules with equal premisses and different consequence part.

  • The only one that is left depends on method argument, see slifis::EN_REDUCE_METHOD.
  • This function is useful in a learning situation.
  • Technically, it creates a new list, progressively removes rules in the original list, then swaps the old and new list content.

References __IN__, __OUT__, _HasBeenReduced, _redmeth, _v_rules, Changed(), GetNbRules(), P_SelectBestRule(), slifis::PremisesAreEqual(), slifis::RULE_IDX::SetOrgNbRules(), and Sort().

Checks if some rules can be factorised by input in_idx. Returns nb of rules that were removed.

For example, say we have (after reducing) in the rule base the following subset of rules, for a FIS of 2 inputs, with input 1 made of 2 functions, input 2 made of 3 functions:

  • if "in_1" is "1" and "in_2" is "1" then output is "1"
  • if "in_1" is "1" and "in_2" is "2" then output is "1"
  • if "in_1" is "1" and "in_2" is "3" then output is "1"

Then, it is clear that these 3 rules can be reduced to a single rule :

  • if "in_1" is "1" then output is "1"

Warning ! : this algorithm relies on a sorted rule base !

References __IN__, __OUT__, _v_UsedInputs, GetNbRules(), P_ComputeUsedInputs(), P_FactorizeBy(), and SLIFIS_WARNING.

void slifis::RULE_BASE::Sort ( int  idx = 0)

Sorting of the rules according to index idx.

if idx is equal to -1, then the rules will get sorted by output function index

References __IN__, __OUT__, _v_rules, Changed(), slifis::ERR_RULEBASE_NO_POINTERS, slifis::INPUT_SETS::GetNb(), P_HasRefToInputs(), p_inputset, p_OutputFuzzySet, pv_InputFuzzySets, rule2_cmp2, slifis::ROOT_RULE::SetNbInputs(), slifis::RULE_IDX::SetSortIndex(), and SLIFIS_ERROR.

Referenced by Reduce().

size_t slifis::RULE_BASE::GetNbInputs ( ) const [inline]
void slifis::RULE_BASE::AssignInputSets ( const std::vector< FUZZY_IN > &  ifs)

References Changed(), and p_OutputFuzzySet.

const FUZZY_IN & slifis::RULE_BASE::GetInputSet ( size_t  idx) const [inline]

Returns input set nb idx associated to the fis associated to this rule base.

Warning:
Here: __OUT__ macro could be triggered and a crash happening after while still in function...

References __IN__, __OUT__, slifis::ERR_BAD_INPUT_SIZE, slifis::ERR_RULEBASE_NO_POINTERS, slifis::INPUT_SETS::GetMfSet(), GetNbInputs(), P_HasRefToInputs(), p_inputset, pv_InputFuzzySets, SLIFIS_ERROR, and SLIFIS_ERROR_2.

Referenced by AddNewRule(), slifis_plot::PLOT_RB_TEXT::Draw(), slifis_plot::PLOT_RB_TEXT::p_ComputePlotSize(), P_CoversAllInputValues(), Print(), Print2(), and rbIsValid().

const FUZZY_OUT & slifis::RULE_BASE::GetOutputSet ( ) const [inline]
bool slifis::RULE_BASE::operator== ( const RULE_BASE m) const
bool slifis::RULE_BASE::operator!= ( const RULE_BASE m) const [inline]
bool slifis::RULE_BASE::P_FindBestMf ( const FUZZY_ROOT MfSet,
REALVAL  in,
size_t &  idx,
FUZZYVAL fmax 
) const [private]

Among the set of MF MfSet, finds the best function for input value in (Private)

  • On success, returns in idx the index of the best MF chosen, and in max the value of this maximum
  • Returns true on success, false if something bad happened
Parameters:
MfSet(in) Set of membership functions associated with numerical value
in(in) The input value
idx(out) index of the best membership function
fmax(out) fuzzy value computed with the latter membership function

References __IN__, __OUT__, slifis::MEMBFUNC::Fuzzify(), slifis::FUZZY_ROOT::GetMf(), slifis::FUZZY_ROOT::GetName(), slifis::FUZZY_ROOT::GetNbMf(), slifis::FUZZYVAL::IsZero(), slifis::FUZZY_ROOT::Print(), SLIFIS_ERROR_LOG, SLIFIS_WARNING, and SLIFIS_WARNING_1.

Referenced by AddNewRule().

RULE_IDX slifis::RULE_BASE::P_SelectBestRule ( size_t  idx_start,
size_t  NbRules,
EN_REDUCE_METHOD  method 
) const [private]

(Private) Helper function for void RULE_BASE::Reduce( EN_REDUCE_METHOD method ). Returns the index on the best rule among the ones

in the set that are between idx_start and idx_start + NbRules -1

The meaning of the "best" rule depends on method (see EN_REDUCE_METHOD)

Parameters:
idx_startStart index
NbRulesNb rules (?)
methodmethod for selecting best rule

References __IN__, __OUT__, _v_rules, slifis::RULE_IDX::GetDegree(), slifis::FUZZY_ROOT::GetNbMf(), p_OutputFuzzySet, slifis::REDM_HIGHEST, slifis::REDM_HIGHEST_NBRULES, slifis::REDM_HIGHEST_SUM, SLIFIS_DEBUG_LOG, SWITCH_ERROR, and VECTOR_ELEM.

Referenced by Reduce().

int slifis::RULE_BASE::P_FactorizeBy ( size_t  in_idx,
bool  DoRemove 
) [private]

Helper function for RULE_BASE::Factorize(), that actually does the job... Returns nb of rules that were removed.

Algorithm:

  • FOR each rule in set that uses input idx
    • find all other identical rules up to input idx (output value not considered), and fill set subset
    • IF the rules in subset are set on the same output, AND subset size > 1 AND the rules cover all the inputs THEN
      • delete from the current rule the common term
      • delete from original set all rules that are in subset
    • ENDIF
  • ENDFOR
Parameters:
in_idxindex of input that we want to factorize
DoRemoveIf false, then nothing happens (pseudo const function), we only count how much rules could be removed

References __IN__, __OUT__, _v_rules, Changed(), GetNbInputs(), GetNbRules(), slifis::RULE_IDX::GetNbTerms(), P_CoversAllInputValues(), P_HasRefToInputs(), P_HaveSameOutput(), slifis::RULE_IDX::P_IsEqualUpToInput(), slifis::RULE_IDX::RemoveTermForInput(), SLIFIS_DEBUG_LOG, and slifis::RULE_IDX::UsesInput().

Referenced by Factorize().

void slifis::RULE_BASE::P_ComputeUsedInputs ( ) const [private]

Computes how many inputs are effectively handled by the rule base (attribute _v_UsedInputs)

References __IN__, __OUT__, _v_UsedInputs, GetNbInputs(), GetNbRules(), GetRule(), and slifis::RULE_IDX::GetTerms().

Referenced by AddNewRule(), AddRule(), DeleteRule(), and Factorize().

bool slifis::RULE_BASE::P_Write_bin ( std::ofstream &  f) const [private]
bool slifis::RULE_BASE::P_Read_bin ( std::ifstream &  f) [private]
bool slifis::RULE_BASE::P_Write_XML ( TiXmlElement *  root) const [private]
bool slifis::RULE_BASE::P_Read_XML ( TiXmlElement *  pElem) [private]
bool slifis::RULE_BASE::P_HaveSameOutput ( const std::vector< std::vector< RULE_IDX >::iterator > &  v_rules) const [private]

Helper function for RULE_BASE::P_Factorize()

Returns true if and only if all the following conditions are met:

  • argument v_rules holds more than 1 rule
  • all the rules in v_rules have the same output idx,
  • all the rules in v_rules have the same nb of terms

Argument v_rules is a vector of iterators on a vector of RULE_IDX

References __IN__, __OUT__, slifis::RULE_IDX::GetNbTerms(), and slifis::RULE_IDX::GetOutMfIndex().

Referenced by P_FactorizeBy().

bool slifis::RULE_BASE::P_CoversAllInputValues ( const std::vector< std::vector< RULE_IDX >::iterator > &  v_iter,
size_t  in_idx 
) const [private]

Returns true if all the rules pointed by the iterators in v_iter cover all possible input values for input in_idx.

Algorithm:

  • build a vector v_result of n values, initialized to false
  • FOR each rule in the set
    • IF current rule uses input in_idx with function nf, THEN
      • set element n°nf of v_result to true
    • ENDIF
  • ENDFOR
  • if all the elements of v_result are true, then return true

References __IN__, __OUT__, GetInputSet(), slifis::FUZZY_ROOT::GetNbMf(), slifis::RULE_IDX::GetTerms(), P_HasRefToInputs(), and slifis::RULE_IDX::UsesInput().

Referenced by P_FactorizeBy().

bool slifis::RULE_BASE::P_HasRefToInputs ( ) const [inline, private]

Returns true if rule base has a reference on some input set.

References p_inputset, and pv_InputFuzzySets.

Referenced by AddNewRule(), GetInputSet(), GetNbInputs(), operator==(), P_CoversAllInputValues(), P_FactorizeBy(), Print(), Print2(), rbIsValid(), and Sort().


Friends And Related Function Documentation

friend class RB_TABLE [friend]
friend class SLIFIS [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<()

Referenced by Sort().


Member Data Documentation

std::vector<RULE_IDX> slifis::RULE_BASE::_v_rules [private]
const std::vector<FUZZY_IN>* slifis::RULE_BASE::pv_InputFuzzySets [private]

A pointer on the input sets of functions of the SLIFIS object (see RULE_BASE description)

Referenced by AddNewRule(), AssignInputSets(), GetInputSet(), GetNbInputs(), P_HasRefToInputs(), Print(), and Sort().

A pointer on the output set of functions of the SLIFIS object.

Referenced by AddNewRule(), AssignOutputSet(), GetOutputSet(), operator==(), P_SelectBestRule(), Print(), Print2(), rbIsValid(), and Sort().

True if rule base has been reduced.

Referenced by AddNewRule(), AddRule(), Clear(), Print(), Print2(), Reduce(), and RULE_BASE().

Method used for reducing the nb of rules.

Referenced by Print(), Print2(), and Reduce().

For counting the nb of rules where the FIS was unable to find either an input function, either an output function, in a learning situation (default value is 0)

Referenced by AddNewRule(), Print(), and RULE_BASE().

std::vector<size_t> slifis::RULE_BASE::_v_UsedInputs [mutable, private]

Holds the indexes of inputs that are effectively used in the rule base.

For example, a FIS can have 6 inputs, but its rule base only use 4 of them. Either because rules have been given directly, either because the learned rule base has been reduced and factorised

This vector is filled by RULE_BASE::P_ComputeUsedInputs()

Referenced by Clear(), ExportCSV(), Factorize(), and P_ComputeUsedInputs().

This attribute is used ONLY for 2-inputs FIS and rule-bases.

Referenced by Changed(), GetRbtable(), PrintTable(), and RULE_BASE().