A fuzzy logic C++ library
Public Member Functions | Protected Member Functions | Protected Attributes | Friends
slifis::FUZZY_ROOT Class Reference

Virtual class, designed to be inherited into FUZZY_IN and FUZZY_OUT. More...

#include <fuzzy_root.hpp>

+ Inheritance diagram for slifis::FUZZY_ROOT:
+ Collaboration diagram for slifis::FUZZY_ROOT:

List of all members.

Public Member Functions

virtual ~FUZZY_ROOT ()
 Destructor (empty)
void SetTriangularSet (const std::vector< REALVAL > &v_mf)
 Creates in the set triangular membership functions.
void SetTriangularSet (REALVAL vmin, REALVAL vmax, size_t NbFunc, EN_FUNCSPACING s=FS_LIN)
 Creates in the set triangular membership functions.
void SetNormalisedSet (const std::vector< REALVAL > &v_pts)
 Creates in the set triangular membership functions plus one HL at the right, and one LH at the left, so set is normalised.
void SetNormalisedSet (REALVAL vmin, REALVAL vmax, size_t NbFunc, EN_FUNCSPACING s=FS_LIN)
 Creates in the set triangular membership functions plus one HL at the right, and one LH at the left, so set is normalised.
bool operator== (const FUZZY_ROOT &fr2) const
 Equality operator. Returns true if all the contained membership functions are equal.
bool operator!= (const FUZZY_ROOT &m) const
Fetching one of the functions of the set
const MEMBFUNCGetMf (size_t idx) const
 A 'const' version, so it can be called for read-only operation.
MEMBFUNCGetMf (size_t idx)
 Returns a reference on the function that has index idx.
MEMBFUNCGetMf (std::string n)
Editing set (adding or removing functions)
void DeleteMf (const std::string name)
 Delete contained function identified by name name.
void DeleteMf (size_t idx)
 Delete contained membership function identified by index idx. Returns true on success, false if index is invalid.
void DeleteAllMf ()
bool AddMf (const MEMBFUNC &mf)
 Adds the membership function mf to the set (with checking)
void Clear ()
 Clears vector of functions, keeps the name.
Managing counting of triggered functions in this set (see FUZZY_ROOT::_v_trigger)
void ClearCounters () const
 Clears all counters.
void Trigger (size_t idx) const
 Trigger function idx.
size_t GetNbHits () const
void GenerateHistogramDataFile (std::string s, size_t n=0) const
 Creates a data file in the current folder with ready-to-plot data of the triggers.
const std::vector< size_t > & GetTriggerValues () const
 returns the vector holding the triggered values
Information on set
size_t GetNbMf () const
bool IsNormalised () const
 Returns true if set is normalised, that is if for every x,.
virtual bool SetIsValid () const =0
Name related

Returns name of set.

const std::string & GetName () const
void SetName (std::string s)
 Edit name of set.
std::string GetMfName (size_t idx) const
 Returns a copy of the name of function that has index idx.
void SetMfName (size_t idx, std::string n)
 Edit name of one the the contained membership functions.
Misc.
int FindMf (const std::string &name) const
 Searches set of functions, and returns index of function that has name name. If not found, returns -1.
bool FindBestMf (REALVAL in, size_t &idx, FUZZYVAL &max) const
 Among the set of membership functions, finds the best function for input value inval.
void Print (FILE *f=stderr) const
 Prints out in f content of set in a human-readable form, no particular formatting.

Protected Member Functions

 FUZZY_ROOT (const std::string &n)
 Constructor. Here, we keep the reference argument passing, because no user code will use this constructor.
bool P_SetIsValid () const
 Return true if the set of functions is valid.
File I/O functions
bool P_Write_bin (std::ofstream &f) const
 Writes the set of membership function 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 *root) const
bool P_Read_XML (TiXmlElement *pElem)

Protected Attributes

std::vector< MEMBFUNC_v_mf
 The set of functions contained here.
std::string _name
 Name of this set of membership function (for example, "temperature").
std::vector< size_t > _v_trigger
 Used for counting the nb of timer a membership function has been triggered by an input in the set (see Density information)

Friends

class SLIFIS

Detailed Description

Virtual class, designed to be inherited into FUZZY_IN and FUZZY_OUT.

Contains a vector of membership functions, that are used to fuzzify a real value.

Contains functions for automatic filling with functions from numerical values


Constructor & Destructor Documentation

slifis::FUZZY_ROOT::FUZZY_ROOT ( const std::string &  n) [protected]

Constructor. Here, we keep the reference argument passing, because no user code will use this constructor.

Destructor (empty)


Member Function Documentation

void slifis::FUZZY_ROOT::SetTriangularSet ( const std::vector< REALVAL > &  v_pts)

Creates in the set triangular membership functions.

  • Set is not normalised
  • If vector has 'n' points, then the set will have n-2 functions
  • Functions are automatically given names: "MF0" for the most left one, and the followings are named "MF1", "MF2", ...

For example, if 4 points given, it will create the following partition (2 functions) :

   Fuzzy
    Axis
     |
     |
  1  +            ^      ^
     |           / \    / \
     |          /   \  /   \
 0.5 +         /     \/     \
     |        /      /\      \
     |       /      /  \      \
  0  +======+======+----+======+====== Real Axis
            v1    v2    v3    v4

Note : 4 points is the minimum accepted

  • Please note that values are internally sorted, so there will always be correct overlapping (unless two values are equal, in which case the function will issue an error)

References __IN__, __OUT__, AddMf(), Clear(), slifis::ERR_MFSET_NOT_ENOUGH_POINTS, slifis::MF_TRI, and SLIFIS_ERROR_2.

Referenced by slifis::FUZZY_OUT::FUZZY_OUT(), and SetTriangularSet().

void slifis::FUZZY_ROOT::SetTriangularSet ( REALVAL  vmin,
REALVAL  vmax,
size_t  NbFunc,
EN_FUNCSPACING  spacing = FS_LIN 
)

Creates in the set triangular membership functions.

  • Set is not normalised, this is mostly suited for output sets of functions
  • Functions are automatically given names: "MF0" for the most left one, and the followings are named "MF1", "MF2", ... (If input set, user can change names afterward with function SetName() )

For example, if 3 functions are requested between values 0 and 10, then 3 intermediate points will be created, and we end up with the following partition

   Fuzzy
    Axis
     |
     |
  1  |            ^     ^     ^
     |           / \   / \   / \
     |          /   \ /   \ /   \
     |         /     X     X     \
     |        /     / \   / \     \
     |       /     /   \ /   \     \
  0  |------+-----+-----+-----+-----+----     Real Axis
            v1    v2    v3    v4   v5
            0    2.5    5     7.5   10

References __IN__, __OUT__, slifis::ERR_BAD_VALUES, slifis::ERR_MFSET_INVALID_NB, slifis::FS_LIN, slifis::FS_LOG, SetTriangularSet(), SLIFIS_CATCH_RETHROW, SLIFIS_ERROR, SLIFIS_ERROR_2, and SLIFIS_MAXMFPERSET.

void slifis::FUZZY_ROOT::SetNormalisedSet ( const std::vector< REALVAL > &  v_pts)

Creates in the set triangular membership functions plus one HL at the right, and one LH at the left, so set is normalised.

  • This is suited for input functions
  • Functions are automatically given names: "MF0" for... the most left one, and "MF1", "MF2", ... for the other functions

For exmple, if a vector of 4 values is given (10,20,30,40), the following set of functions will be built:

   Fuzzy
    Axis
     |
     |
  1  |------+     ^     ^     +------
     |       \   / \   / \   /
     |        \ /   \ /   \ /
 0.5 |         X     X     X
     |        / \   / \   / \
     |       /   \ /   \ /   \
  0  |======+=====+=====+=====+======= Real Axis
            10    20    30    40

References __IN__, __OUT__, AddMf(), Clear(), slifis::ERR_MFSET_NOT_ENOUGH_POINTS, slifis::MF_HL, slifis::MF_LH, slifis::MF_TRI, and SLIFIS_ERROR_2.

Referenced by slifis::FUZZY_IN::FUZZY_IN(), main(), and SetNormalisedSet().

void slifis::FUZZY_ROOT::SetNormalisedSet ( REALVAL  vmin,
REALVAL  vmax,
size_t  NbFunc,
EN_FUNCSPACING  spacing = FS_LIN 
)

Creates in the set triangular membership functions plus one HL at the right, and one LH at the left, so set is normalised.

  • This is suited for input functions
  • Functions are automatically given names: "MF0" for... the most left one, and "MF1", "MF2", ... for the others functions

For example, if NbFunc=4, vmin=10 and vmax=40, it will create the following partition:

   Fuzzy
    Axis
     |
     |
  1  |------+     ^     ^     +------
     |       \   / \   / \   /
     |        \ /   \ /   \ /
 0.5 |         X     X     X
     |        / \   / \   / \
     |       /   \ /   \ /   \
  0  |======+=====+=====+=====+======= Real Axis
            10    20    30    40

References __IN__, __OUT__, slifis::ERR_BAD_VALUES, slifis::ERR_MFSET_INVALID_NB, slifis::FS_LIN, slifis::FS_LOG, SetNormalisedSet(), SLIFIS_ERROR_2, and SLIFIS_MAXMFPERSET.

const MEMBFUNC & slifis::FUZZY_ROOT::GetMf ( size_t  idx) const

Returns a reference on the function that has index idx.

References __IN__, __OUT__, _v_mf, slifis::ERR_MF_BAD_INDEX, GetNbMf(), and SLIFIS_ERROR_2.

MEMBFUNC & slifis::FUZZY_ROOT::GetMf ( std::string  n)
void slifis::FUZZY_ROOT::DeleteMf ( const std::string  name)

Delete contained function identified by name name.

  • Returns true on success, false if function not found

References __IN__, __OUT__, _v_mf, _v_trigger, slifis::ERR_MFSET_NONAME, FindMf(), and SLIFIS_ERROR_2.

void slifis::FUZZY_ROOT::DeleteMf ( size_t  idx)

Delete contained membership function identified by index idx. Returns true on success, false if index is invalid.

  • A warning is issued in log file if failure

References __IN__, __OUT__, _v_mf, _v_trigger, slifis::ERR_MFSET_BAD_INDEX, GetNbMf(), and SLIFIS_ERROR_2.

References _v_mf, and ClearCounters().

bool slifis::FUZZY_ROOT::AddMf ( const MEMBFUNC mf)

Adds the membership function mf to the set (with checking)

  • Returns false if unable (i.e. if already a function that has that name)
  • Triggers a fatal error if function is not valid

References __IN__, __OUT__, _v_mf, _v_trigger, slifis::ERR_MF_NOT_VALID, FindMf(), slifis::MEMBFUNC::GetName(), slifis::MEMBFUNC::IsValid(), SLIFIS_ERROR, and SLIFIS_WARNING_1.

Referenced by main(), P_Read_XML(), Process(), SetNormalisedSet(), and SetTriangularSet().

Clears vector of functions, keeps the name.

References _v_mf, and _v_trigger.

Referenced by main(), SetNormalisedSet(), and SetTriangularSet().

void slifis::FUZZY_ROOT::ClearCounters ( ) const [inline]

Clears all counters.

See SLIFIS::ClearCounters()

References _v_trigger.

Referenced by slifis::SLIFIS::ClearCounters(), and DeleteAllMf().

void slifis::FUZZY_ROOT::Trigger ( size_t  idx) const

References _v_trigger.

Referenced by GenerateHistogramDataFile().

void slifis::FUZZY_ROOT::GenerateHistogramDataFile ( std::string  s,
size_t  n = 0 
) const

Creates a data file in the current folder with ready-to-plot data of the triggers.

  • called with s = "in" if input data, and "out" if the (unique) output data
  • argument n will be 0 if output, or the index of the input (starting from 1)

See Density information

References __IN__, __OUT__, _v_trigger, slifis::ERR_IO_ERROR, GetMf(), GetName(), slifis::MEMBFUNC::GetName(), GetNbHits(), SLIFIS_ERROR_2, and SLIFIS_STAMP_S.

const std::vector< size_t > & slifis::FUZZY_ROOT::GetTriggerValues ( ) const [inline]

returns the vector holding the triggered values

See Density information

References _v_trigger.

bool slifis::FUZZY_ROOT::operator== ( const FUZZY_ROOT fr2) const

Equality operator. Returns true if all the contained membership functions are equal.

References __IN__, __OUT__, _v_mf, and GetNbMf().

bool slifis::FUZZY_ROOT::operator!= ( const FUZZY_ROOT m) const [inline]
size_t slifis::FUZZY_ROOT::GetNbMf ( ) const [inline]

Returns true if set is normalised, that is if for every x,.

Please note that an empty set will return false.

Method used :

  • FOR every function $ f_i $ of the set of $ n $ functions
    • FOR every point $ p_j $ with values $ (x_{ij},y_{ij}) $
      • Compute sum $ s = y_{ij} + \sum_{k=1,k \neq i}^n f_k(x_{ij}) $
      • IF $ s \neq 1 $ return false
      • ENDIF
    • ENDFOR
  • ENDFOR
  • return true

References __IN__, __OUT__, _v_mf, slifis::MEMBFUNC::Fuzzify(), GetNbMf(), slifis::MEMBFUNC::GetNbPoints(), slifis::MEMBFUNC::GetPoint(), slifis::FPOINT::GetX(), slifis::FPOINT::GetY(), and slifis::FUZZYVAL::IsOne().

virtual bool slifis::FUZZY_ROOT::SetIsValid ( ) const [pure virtual]

Implemented in slifis::FUZZY_IN, and slifis::FUZZY_OUT.

const std::string& slifis::FUZZY_ROOT::GetName ( ) const [inline]
void slifis::FUZZY_ROOT::SetName ( std::string  s) [inline]

Edit name of set.

References _name.

Referenced by P_Read_XML().

std::string slifis::FUZZY_ROOT::GetMfName ( size_t  idx) const
void slifis::FUZZY_ROOT::SetMfName ( size_t  idx,
std::string  n 
)

Edit name of one the the contained membership functions.

References __IN__, __OUT__, slifis::ERR_MF_BAD_INDEX, GetMf(), GetNbMf(), slifis::MEMBFUNC::SetName(), and SLIFIS_ERROR_2.

Referenced by main().

int slifis::FUZZY_ROOT::FindMf ( const std::string &  name) const

Searches set of functions, and returns index of function that has name name. If not found, returns -1.

References __IN__, __OUT__, _v_mf, and GetNbMf().

Referenced by AddMf(), slifis::RULE_BASE::AddNewRule(), and DeleteMf().

bool slifis::FUZZY_ROOT::FindBestMf ( REALVAL  inval,
size_t &  idx,
FUZZYVAL max 
) const

Among the set of membership functions, finds the best function for input value inval.

  • The "best function" means the function that gives the highest value for the given input value.
  • 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, and output values unchanged.
  • Failure can occur in two situations:
    • No functions in the set
    • No function provided a defuzzyfied value higher than zero
Parameters:
inval(in) The input value
idx(out) index of the best membership function
max(out) fuzzy value computed with the latter membership function

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

Referenced by slifis::SLIFIS::P_Evaluate_M().

void slifis::FUZZY_ROOT::Print ( FILE *  f = stderr) const

Prints out in f content of set in a human-readable form, no particular formatting.

References _name, _v_mf, and GetNbMf().

Referenced by FindBestMf(), and slifis::RULE_BASE::P_FindBestMf().

bool slifis::FUZZY_ROOT::P_SetIsValid ( ) const [protected]

Return true if the set of functions is valid.

  • the set has at least one function.
  • all the functions have left and right zero values.

References __IN__, __OUT__, GetMf(), GetNbMf(), and slifis::MEMBFUNC::IsValid().

Referenced by slifis::FUZZY_OUT::SetIsValid(), and slifis::FUZZY_IN::SetIsValid().

bool slifis::FUZZY_ROOT::P_Write_bin ( std::ofstream &  f) const [protected]

Writes the set of membership function into the (opened) binary stream f.

returns true on success, false if I/O error

References __IN__, __OUT__, _name, _v_mf, GetNbMf(), STREAM_OK_TEST, STREAM_WRITE_STRING, and STREAMCHECK_INIT.

bool slifis::FUZZY_ROOT::P_Read_bin ( std::ifstream &  f) [protected]

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

Assumes file pointer is correctly set This function is called only from SLIFIS::Read()

Please note that trigger values ARE NOT read and saved to/from file

References __IN__, __OUT__, _name, _v_mf, _v_trigger, slifis::MEMBFUNC::P_Read_bin(), STREAM_OK_TEST, STREAM_READ_STRING, and STREAMCHECK_INIT.

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

bool slifis::FUZZY_ROOT::P_Write_XML ( TiXmlElement *  root) const [protected]
bool slifis::FUZZY_ROOT::P_Read_XML ( TiXmlElement *  pElem) [protected]

Friends And Related Function Documentation

friend class SLIFIS [friend]

Member Data Documentation

std::vector<MEMBFUNC> slifis::FUZZY_ROOT::_v_mf [protected]
std::string slifis::FUZZY_ROOT::_name [protected]

Name of this set of membership function (for example, "temperature").

Referenced by GetName(), P_Read_bin(), P_Write_bin(), Print(), and SetName().

std::vector<size_t> slifis::FUZZY_ROOT::_v_trigger [mutable, protected]

Used for counting the nb of timer a membership function has been triggered by an input in the set (see Density information)

It can be accessed through 3 member functions:

Please note that for a given input value, several functions can be triggered, due to overlapping of functions.

If adding new code, please take care that this vector size must always be the same size than the the vec_mf vector.

It is declared 'mutable' because it is not a "real" attribute of the set of functions, just an indicator of how well the set is chosen.

Referenced by AddMf(), Clear(), ClearCounters(), DeleteMf(), GenerateHistogramDataFile(), GetNbHits(), GetTriggerValues(), P_Read_bin(), and Trigger().