fuzzylite  6.0
A Fuzzy Logic Control Library in C++
fl::Discrete Class Reference

The Discrete class is a basic Term that represents a discrete membership function. More...

#include <Discrete.h>

Inheritance diagram for fl::Discrete:
Collaboration diagram for fl::Discrete:

Public Types

typedef std::pair< scalar, scalarPair
 

Public Member Functions

 Discrete (const std::string &name="", const std::vector< Pair > &xy=std::vector< Pair >(), scalar height=1.0)
 
virtual ~Discrete () FL_IOVERRIDE
 
virtual std::string className () const FL_IOVERRIDE
 Returns the name of the class of the term. More...
 
virtual std::string parameters () const FL_IOVERRIDE
 Returns the parameters of the term as x1 y1 xn yn [height] More...
 
virtual void configure (const std::string &parameters) FL_IOVERRIDE
 Configures the term with the parameters given as x1 y1 xn yn [height] More...
 
virtual void sort ()
 Ascendantly sorts the pairs of values in this Discrete term by the \(x\)-coordinate. More...
 
virtual Complexity complexity () const FL_IOVERRIDE
 Computes the estimated complexity of evaluating the membership function. More...
 
virtual scalar membership (scalar x) const FL_IOVERRIDE
 Computes the membership function evaluated at \(x\) by using binary search to find the lower and upper bounds of \(x\) and then linearly interpolating the membership function between the bounds. More...
 
virtual void setXY (const std::vector< Pair > &pairs)
 Sets the vector of pairs defining the discrete membership function. More...
 
virtual const std::vector< Pair > & xy () const
 Gets an immutable vector of pairs defining the discrete membership function. More...
 
virtual std::vector< Pair > & xy ()
 Gets a mutable vector of pairs defining the discrete membership function. More...
 
virtual const Pairxy (std::size_t index) const
 Gets the immutable pair \((x_i,y_i)\) at index \(i\). More...
 
virtual Pairxy (std::size_t index)
 Gets the mutable pair \((x_i,y_i)\) at index \(i\). More...
 
virtual std::vector< scalarx () const
 Creates, fills and returns a vector containing the \(x\) values. More...
 
virtual scalar x (std::size_t index) const
 Gets the \(x\) value at the given index. More...
 
virtual scalarx (std::size_t index)
 Gets the reference to the \(x\) value at the given index. More...
 
virtual std::vector< scalary () const
 Creates, fills and returns a vector containing the \(y\) values. More...
 
virtual scalar y (std::size_t index) const
 Gets the \(y\) value at the given index. More...
 
virtual scalary (std::size_t index)
 Gets the reference to the \(y\) value at the given index. More...
 
virtual Discreteclone () const FL_IOVERRIDE
 Creates a clone of the term. More...
 
- Public Member Functions inherited from fl::Term
 Term (const std::string &name="", scalar height=1.0)
 
virtual ~Term ()
 
virtual void setName (const std::string &name)
 Sets the name of the term. More...
 
virtual std::string getName () const
 Gets the name of the term. More...
 
virtual void setHeight (scalar height)
 Sets the height of the term. More...
 
virtual scalar getHeight () const
 Gets the height of the term. More...
 
virtual std::string toString () const
 Returns the representation of the term in the FuzzyLite Language. More...
 
virtual void updateReference (const Engine *engine)
 Updates the references (if any) to point to the current engine (useful when cloning engines or creating terms within Importer objects. More...
 

Static Public Member Functions

static void sort (std::vector< Pair > &pairs)
 Ascendantly sorts the given pairs of values by the \(x\)-value, as it is required by the Discrete term. More...
 
static std::vector< scalartoVector (const std::vector< Pair > &xy)
 Creates a vector of fl::scalar from a vector of Pair given in the form \(\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)\). More...
 
static std::vector< PairtoPairs (const std::vector< scalar > &xy)
 Creates a vector of Pair from a vector of fl::scalar given in the form \((x_1,y_1,...,x_n,y_n)\). More...
 
static std::vector< PairtoPairs (const std::vector< scalar > &xy, scalar missingValue) FL_INOEXCEPT
 Creates a vector of Pair from a vector of fl::scalar given in the form \((x_1,y_1,...,x_n,y_n)\). More...
 
static std::string formatXY (const std::vector< Pair > &xy, const std::string &prefix="(", const std::string &innerSeparator=",", const std::string &suffix=")", const std::string &outerSeparator=" ")
 Formats a vector of Pair into a std::string in the form \((x_1,y_1) ... (x_n,y_n)\). More...
 
static Discretediscretize (const Term *term, scalar start, scalar end, int resolution=IntegralDefuzzifier::defaultResolution(), bool boundedMembershipFunction=true)
 Discretizes the given term. More...
 
static Termconstructor ()
 
template<typename T >
static Discretecreate (const std::string &name, int argc, T x1, T y1,...)
 Creates a Discrete term from a variadic set of values. More...
 

Additional Inherited Members

- Protected Attributes inherited from fl::Term
scalar _height
 

Detailed Description

The Discrete class is a basic Term that represents a discrete membership function.

The pairs of values in any Discrete term must be sorted ascendently because the membership function is computed using binary search to find the lower and upper bounds of \(x\).

discrete.svg
Author
Juan Rada-Vilela, Ph.D.
See also
Term
Variable
Since
4.0

Definition at line 42 of file Discrete.h.

Member Typedef Documentation

◆ Pair

typedef std::pair<scalar, scalar> fl::Discrete::Pair

Definition at line 44 of file Discrete.h.

Constructor & Destructor Documentation

◆ Discrete()

fl::Discrete::Discrete ( const std::string &  name = "",
const std::vector< Pair > &  xy = std::vector< Pair >(),
scalar  height = 1.0 
)
explicit

◆ ~Discrete()

virtual fl::Discrete::~Discrete ( )
virtual

Member Function Documentation

◆ className()

virtual std::string fl::Discrete::className ( ) const
virtual

Returns the name of the class of the term.

Returns
the name of the class of the term

Implements fl::Term.

◆ clone()

virtual Discrete* fl::Discrete::clone ( ) const
virtual

Creates a clone of the term.

Returns
a clone of the term

Implements fl::Term.

◆ complexity()

virtual Complexity fl::Discrete::complexity ( ) const
virtual

Computes the estimated complexity of evaluating the membership function.

Returns
the estimated complexity of evaluating the membership function

Implements fl::Term.

◆ configure()

virtual void fl::Discrete::configure ( const std::string &  parameters)
virtual

Configures the term with the parameters given as x1 y1 xn yn [height]

Parameters
parametersas x1 y1 xn yn [height]

Implements fl::Term.

◆ constructor()

static Term* fl::Discrete::constructor ( )
static

◆ create()

template<typename T >
Discrete * fl::Discrete::create ( const std::string &  name,
int  argc,
x1,
y1,
  ... 
)
inlinestatic

Creates a Discrete term from a variadic set of values.

Beware: this method is unsafe and must be used with care by ensuring:

  • the value argc correctly and exactly determines the number of values passed,
  • the data type of each variadic arguments is the same, e.g., \((1.0, 2.0, 3.0)\) are all fl::scalar, whereas in \((1.0, 2, 3.0)\) the second term is an integer, which will cause memory access issues due to the difference in size between int and fl::scalar.
    Parameters
    nameis the name of the resulting term
    argcis the number of values passed
    x1is the \(x\) value of the first Pair
    y1is the \(y\) value of the first Pair
    ...are the remaining pairs of values \(x_i\) and \(y_i\)
    Returns
    a new Discrete term with the given parameters

Definition at line 259 of file Discrete.h.

◆ discretize()

static Discrete* fl::Discrete::discretize ( const Term term,
scalar  start,
scalar  end,
int  resolution = IntegralDefuzzifier::defaultResolution(),
bool  boundedMembershipFunction = true 
)
static

Discretizes the given term.

Parameters
termis the term to discretize
startis the value from which discretization starts
endis the value at which discretization ends
resolutionis the number of equally-distributed samples to perform between start and end
boundedMembershipFunctionindicates whether to ensure that \(\mu(x)\in[0.0,1.0]\)
Returns
a Discrete term that approximates the given term

◆ formatXY()

static std::string fl::Discrete::formatXY ( const std::vector< Pair > &  xy,
const std::string &  prefix = "(",
const std::string &  innerSeparator = ",",
const std::string &  suffix = ")",
const std::string &  outerSeparator = " " 
)
static

Formats a vector of Pair into a std::string in the form \((x_1,y_1) ... (x_n,y_n)\).

Parameters
xyis the vector of Pair
prefixindicates the prefix of a Pair, e.g., ( results in \((x_i\)
innerSeparatorindicates the separator between \(x\) and \(y\), e.g., , results in \(x_i,y_i\)
suffixindicates the postfix of a Pair, e.g., ] results in \(y_i]\)
outerSeparatorindicates the separator between Pair, e.g., ; results in \((x_i,y_i);(x_j,y_j)\)
Returns
a formatted string containing the pairs of \((x,y)\) values

◆ membership()

virtual scalar fl::Discrete::membership ( scalar  x) const
virtual

Computes the membership function evaluated at \(x\) by using binary search to find the lower and upper bounds of \(x\) and then linearly interpolating the membership function between the bounds.

Parameters
x
Returns
\( \dfrac{h (y_{\max} - y_{\min})}{(x_{\max}- x_{\min})} (x - x_{\min}) + y_{\min}\)

where \(h\) is the height of the Term, \(x_{\min}\) and \(x_{\max}\)is are the lower and upper limits of \(x\) in xy (respectively), \(y_{\min}\) and \(y_{\max}\)is are the membership functions of \(\mu(x_{\min})\) and \(\mu(x_{\max})\) (respectively)

Implements fl::Term.

◆ parameters()

virtual std::string fl::Discrete::parameters ( ) const
virtual

Returns the parameters of the term as x1 y1 xn yn [height]

Returns
x1 y1 xn yn [height]

Implements fl::Term.

◆ setXY()

virtual void fl::Discrete::setXY ( const std::vector< Pair > &  pairs)
virtual

Sets the vector of pairs defining the discrete membership function.

Parameters
pairsis the vector of pairs defining the discrete membership function

◆ sort() [1/2]

static void fl::Discrete::sort ( std::vector< Pair > &  pairs)
static

Ascendantly sorts the given pairs of values by the \(x\)-value, as it is required by the Discrete term.

Parameters
pairsis a vector of pairs of values in the form \((x,y)\)

◆ sort() [2/2]

virtual void fl::Discrete::sort ( )
virtual

Ascendantly sorts the pairs of values in this Discrete term by the \(x\)-coordinate.

◆ toPairs() [1/2]

static std::vector<Pair> fl::Discrete::toPairs ( const std::vector< scalar > &  xy)
static

Creates a vector of Pair from a vector of fl::scalar given in the form \((x_1,y_1,...,x_n,y_n)\).

Parameters
xyis a vector of fl::scalar given as \((x_1,y_1,...,x_n,y_n)\)
Returns
a vector of Pair in the form \(\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)\)
Exceptions
fl::Exceptionif a value is missing, that is, if the length of \(xy\) is odd: \(|xy|\mod 2 = 1\)

◆ toPairs() [2/2]

static std::vector<Pair> fl::Discrete::toPairs ( const std::vector< scalar > &  xy,
scalar  missingValue 
)
static

Creates a vector of Pair from a vector of fl::scalar given in the form \((x_1,y_1,...,x_n,y_n)\).

Parameters
xyis a vector of fl::scalar given as \((x_1,y_1,...,x_n,y_n)\) possibly missing a value
missingValueis the replacement in the case a value is missing from \(xy\)
Returns
a vector of Pair in the form \(\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)\)

◆ toVector()

static std::vector<scalar> fl::Discrete::toVector ( const std::vector< Pair > &  xy)
static

Creates a vector of fl::scalar from a vector of Pair given in the form \(\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)\).

Parameters
xyis the vector of Pair
Returns
a vector of fl::scalar as \((x_1,y_1,...,x_n,y_n)\)

◆ x() [1/3]

virtual std::vector<scalar> fl::Discrete::x ( ) const
virtual

Creates, fills and returns a vector containing the \(x\) values.

Returns
a vector containing the \(x\) values

◆ x() [2/3]

virtual scalar fl::Discrete::x ( std::size_t  index) const
virtual

Gets the \(x\) value at the given index.

Returns
the \(x\) value at the given index

◆ x() [3/3]

virtual scalar& fl::Discrete::x ( std::size_t  index)
virtual

Gets the reference to the \(x\) value at the given index.

Returns
the reference to the \(x\) value at the given index

◆ xy() [1/4]

virtual const std::vector<Pair>& fl::Discrete::xy ( ) const
virtual

Gets an immutable vector of pairs defining the discrete membership function.

Returns
an immutable vector of pairs defining the discrete membership function

◆ xy() [2/4]

virtual std::vector<Pair>& fl::Discrete::xy ( )
virtual

Gets a mutable vector of pairs defining the discrete membership function.

Returns
a mutable vector of pairs defining the discrete membership function

◆ xy() [3/4]

virtual const Pair& fl::Discrete::xy ( std::size_t  index) const
virtual

Gets the immutable pair \((x_i,y_i)\) at index \(i\).

Parameters
indexis the index \(i\)
Returns
the immutable pair \((x_i,y_i)\)

◆ xy() [4/4]

virtual Pair& fl::Discrete::xy ( std::size_t  index)
virtual

Gets the mutable pair \((x_i,y_i)\) at index \(i\).

Parameters
indexis the index \(i\)
Returns
a mutable pair \((x_i,y_i)\)

◆ y() [1/3]

virtual std::vector<scalar> fl::Discrete::y ( ) const
virtual

Creates, fills and returns a vector containing the \(y\) values.

Returns
a vector containing the \(y\) values

◆ y() [2/3]

virtual scalar fl::Discrete::y ( std::size_t  index) const
virtual

Gets the \(y\) value at the given index.

Parameters
indexis the index
Returns
the \(y\) value at the given index

◆ y() [3/3]

virtual scalar& fl::Discrete::y ( std::size_t  index)
virtual

Gets the reference to the \(y\) value at the given index.

Parameters
indexis the index
Returns
the reference to the \(y\) value at the given index

The documentation for this class was generated from the following file: