diff options
Diffstat (limited to 'fuzzylite/fl/term')
25 files changed, 2274 insertions, 521 deletions
diff --git a/fuzzylite/fl/term/Accumulated.h b/fuzzylite/fl/term/Accumulated.h deleted file mode 100644 index 51625d0..0000000 --- a/fuzzylite/fl/term/Accumulated.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved - - This file is part of fuzzylite. - - fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. - - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. - - */ - -#ifndef FL_ACCUMULATED_H -#define FL_ACCUMULATED_H - -#include "fl/term/Term.h" - -#include <vector> - -namespace fl { - - class Activated; - class SNorm; - class TNorm; - - class FL_API Accumulated : public Term { - private: - void copyFrom(const Accumulated& source); - protected: - std::vector<Activated*> _terms; - scalar _minimum, _maximum; - FL_unique_ptr<SNorm> _accumulation; - public: - explicit Accumulated(const std::string& name = "", - scalar minimum = fl::nan, - scalar maximum = fl::nan, - SNorm* accumulation = fl::null); - Accumulated(const Accumulated& other); - Accumulated& operator=(const Accumulated& other); - virtual ~Accumulated() FL_IOVERRIDE; - FL_DEFAULT_MOVE(Accumulated) - - virtual std::string className() const FL_IOVERRIDE; - virtual std::string parameters() const FL_IOVERRIDE; - virtual void configure(const std::string& parameters) FL_IOVERRIDE; - - virtual Accumulated* clone() const FL_IOVERRIDE; - - virtual scalar membership(scalar x) const FL_IOVERRIDE; - virtual scalar activationDegree(const Term* forTerm) const; - - virtual std::string toString() const FL_IOVERRIDE; - - - virtual void setMinimum(scalar minimum); - virtual scalar getMinimum() const; - - virtual void setMaximum(scalar maximum); - virtual scalar getMaximum() const; - - virtual void setRange(scalar minimum, scalar maximum); - virtual scalar range() const; - - virtual void setAccumulation(SNorm* accumulation); - virtual SNorm* getAccumulation() const; - - /** - * Operations for std::vector _terms - */ - virtual void addTerm(const Term* term, scalar degree, const TNorm* activation); - virtual void addTerm(Activated* term); - virtual Activated* getTerm(int index) const; - virtual Activated* removeTerm(int index); - virtual int numberOfTerms() const; - virtual const std::vector<Activated*>& terms() const; - virtual std::vector<Activated*>& terms(); - virtual bool isEmpty() const; - virtual void clear(); - }; - -} -#endif /* FL_ACCUMULATED_H */ diff --git a/fuzzylite/fl/term/Activated.h b/fuzzylite/fl/term/Activated.h index 10c1b40..f122a25 100644 --- a/fuzzylite/fl/term/Activated.h +++ b/fuzzylite/fl/term/Activated.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_ACTIVATED_H @@ -30,36 +22,86 @@ namespace fl { class TNorm; + /** + The Activated class is a special Term that contains pointers to the + necessary information of a term that has been activated as part of the + Antecedent of a Rule. The ownership of the pointers is not transferred to + objects of this class. The Activated class was named + `Thresholded` in versions 4.0 and earlier. + + @author Juan Rada-Vilela, Ph.D. + @see OutputVariable + @see Term + @since 5.0 + */ class FL_API Activated : public Term { - protected: + private: const Term* _term; scalar _degree; - const TNorm* _activation; + const TNorm* _implication; public: explicit Activated(const Term* term = fl::null, scalar degree = 1.0, - const TNorm* activationOperator = fl::null); + const TNorm* implication = fl::null); virtual ~Activated() FL_IOVERRIDE; FL_DEFAULT_COPY_AND_MOVE(Activated) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"degree implication term"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Does nothing. + @param parameters are irrelevant + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the implication of the activation degree and the membership + function value of @f$x@f$ + @param x is a value + @return @f$d \otimes \mu(x)@f$, where @f$d@f$ is the activation degree + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; virtual std::string toString() const FL_IOVERRIDE; + /** + Sets the activated term + @param term is the activated term + */ virtual void setTerm(const Term* term); + /** + Gets the activated term + @return the activated term + */ virtual const Term* getTerm() const; + /** + Sets the activation degree of the term + @param degree is the activation degree of the term + */ virtual void setDegree(scalar degree); + /** + Gets the activation degree of the term + @return the activation degree of the term + */ virtual scalar getDegree() const; - virtual void setActivation(const TNorm* activation); - virtual const TNorm* getActivation() const; + /** + Sets the implication operator + @param implication is the implication operator + */ + virtual void setImplication(const TNorm* implication); + /** + Gets the implication operator + @return the implication operator + */ + virtual const TNorm* getImplication() const; virtual Activated* clone() const FL_IOVERRIDE; }; - } #endif /* FL_ACTIVATED_H */ diff --git a/fuzzylite/fl/term/Aggregated.h b/fuzzylite/fl/term/Aggregated.h new file mode 100644 index 0000000..af08c75 --- /dev/null +++ b/fuzzylite/fl/term/Aggregated.h @@ -0,0 +1,212 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_AGGREGATED_H +#define FL_AGGREGATED_H + +#include "fl/term/Term.h" + +#include "fl/term/Activated.h" + +#include <vector> + +namespace fl { + + class SNorm; + class TNorm; + + /** + The Aggregated class is a special Term that stores a fuzzy set with the + Activated terms from the Antecedent%s of a Rule, thereby serving mainly + as the fuzzy output value of the OutputVariable%s. The ownership of the + activated terms will be transfered to objects of this class, and + therefore their destructors will be called upon destruction of this term + (or calling Aggregated::clear()). + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Rule + @see OutputVariable + @see Activated + @see Term + @since 6.0 + */ + class FL_API Aggregated : public Term { + private: + std::vector<Activated> _terms; + scalar _minimum, _maximum; + FL_unique_ptr<SNorm> _aggregation; + + void copyFrom(const Aggregated& source); + public: + explicit Aggregated(const std::string& name = "", + scalar minimum = fl::nan, + scalar maximum = fl::nan, + SNorm* aggregation = fl::null); + Aggregated(const Aggregated& other); + Aggregated& operator=(const Aggregated& other); + virtual ~Aggregated() FL_IOVERRIDE; + FL_DEFAULT_MOVE(Aggregated) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"aggregation minimum maximum terms"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Does nothing + @param parameters are irrelevant + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Aggregated* clone() const FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + virtual Complexity complexityOfMembership() const; + virtual Complexity complexityOfActivationDegree() const; + + /** + Aggregates the membership function values of @f$x@f$ utilizing the + aggregation operator + @param x is a value + @return @f$\sum_i{\mu_i(x)}, i \in \mbox{terms}@f$ + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Computes the aggregated activation degree for the given term. + If the same term is present multiple times, the aggregation operator + is utilized to sum the activation degrees of the term. If the + aggregation operator is fl::null, a regular sum is performed. + @param forTerm is the term for which to compute the aggregated + activation degree + @return the aggregated activation degree for the given term + */ + virtual scalar activationDegree(const Term* forTerm) const; + + /** + Iterates over the Activated terms to find the term with the maximum + activation degree + @return the term with the maximum activation degree + */ + virtual const Activated* highestActivatedTerm() const; + + virtual std::string toString() const FL_IOVERRIDE; + + /** + Sets the minimum of the range of the fuzzy set + @param minimum is the minimum of the range of the fuzzy set + */ + virtual void setMinimum(scalar minimum); + /** + Gets the minimum of the range of the fuzzy set + @return the minimum of the range of the fuzzy set + */ + virtual scalar getMinimum() const; + + /** + Sets the maximum of the range of the fuzzy set + @param maximum is the maximum of the range of the fuzzy set + */ + virtual void setMaximum(scalar maximum); + /** + Gets the maximum of the range of the fuzzy set + @return the maximum of the range of the fuzzy set + */ + virtual scalar getMaximum() const; + + /** + Sets the range of the fuzzy set to `[minimum, maximum]` + @param minimum is the minimum of the range of the fuzzy set + @param maximum is the maximum of the range of the fuzzy set + */ + virtual void setRange(scalar minimum, scalar maximum); + /** + Returns the magnitude of the range of the fuzzy set, + @return the magnitude of the range of the fuzzy set, + i.e., `maximum - minimum` + */ + virtual scalar range() const; + + /** + Sets the aggregation operator + @param aggregation is the aggregation operator + */ + virtual void setAggregation(SNorm* aggregation); + /** + Gets the aggregation operator + @return the aggregation operator + */ + virtual SNorm* getAggregation() const; + + /** + Adds a new Activated term (from the parameters) to the fuzzy set + @param term is the activated term + @param degree is the activation degree + @param implication is the implication operator + */ + virtual void addTerm(const Term* term, scalar degree, const TNorm* implication); + /** + Adds the activated term to the fuzzy set. The activated term + will be deleted when Aggregated::clear() + @param term is the activated term + */ + virtual void addTerm(const Activated& term); + /** + Gets the term at the given index + @param index is the index of the term + @return the activated term at the given index + */ + virtual const Activated& getTerm(std::size_t index) const; + /** + Removes the term at the given index without deleting the term + @param index is the index of the term + @return the removed term + */ + virtual const Activated& removeTerm(std::size_t index); + /** + Returns the number of activated terms + @return the number of activated terms + */ + virtual std::size_t numberOfTerms() const; + + /** + Sets the activated terms + @param terms is the activated terms + */ + virtual void setTerms(const std::vector<Activated>& terms); + /** + Returns an immutable vector of activated terms + @return an immutable vector of activated terms + */ + virtual const std::vector<Activated>& terms() const; + /** + Returns a mutable vector of activated terms + @return a mutable vector of activated terms + */ + virtual std::vector<Activated>& terms(); + /** + Indicates whether the vector of activated terms is empty + @return whether the vector of activated terms is empty + */ + virtual bool isEmpty() const; + /** + Clears and deletes the activated terms + */ + virtual void clear(); + }; +} +#endif /* FL_AGGREGATED_H */ diff --git a/fuzzylite/fl/term/Bell.h b/fuzzylite/fl/term/Bell.h index 115479c..e27d9c9 100644 --- a/fuzzylite/fl/term/Bell.h +++ b/fuzzylite/fl/term/Bell.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_BELL_H @@ -29,8 +21,19 @@ namespace fl { + /** + The Bell class is an extended Term that represents the generalized bell + curve membership function. + + @image html bell.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Bell : public Term { - protected: + private: scalar _center; scalar _width; scalar _slope; @@ -44,18 +47,62 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Bell) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"center width slope [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"center width slope [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$h / (1 + \left(|x-c|/w\right)^{2s}@f$ + + where @f$h@f$ is the height of the Term, + @f$c@f$ is the center of the Bell, + @f$w@f$ is the width of the Bell, + @f$s@f$ is the slope of the Bell + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the center of the bell curve + @param center is the center of the bell curve + */ virtual void setCenter(scalar center); + /** + Gets the center of the bell curve + @return the center of the bell curve + */ virtual scalar getCenter() const; + /** + Sets the width of the bell curve + @param width is the width of the bell curve + */ virtual void setWidth(scalar width); + /** + Gets the width of the bell curve + @return the width of the bell curve + */ virtual scalar getWidth() const; + /** + Sets the slope of the bell curve + @param slope is the slope of the bell curve + */ virtual void setSlope(scalar slope); + /** + Gets the slope of the bell curve + @return the slope of the bell curve + */ virtual scalar getSlope() const; virtual Bell* clone() const FL_IOVERRIDE; @@ -63,6 +110,5 @@ namespace fl { static Term* constructor(); }; - } #endif /* FL_BELL_H */ diff --git a/fuzzylite/fl/term/Binary.h b/fuzzylite/fl/term/Binary.h new file mode 100644 index 0000000..c22a9e1 --- /dev/null +++ b/fuzzylite/fl/term/Binary.h @@ -0,0 +1,132 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_BINARY_H +#define FL_BINARY_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Binary class is an edge Term that represents the binary membership + function. + + @image html binary.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 6.0 + */ + class FL_API Binary : public Term { + private: + scalar _start; + scalar _direction; + public: + + /** + Direction is an enumerator that indicates the direction of the + edge. + */ + enum Direction { + /** `(_|)` increases to the right (infinity)*/ + Positive, + /** `(--)` direction is NaN */ + Undefined, + /** `(|_)` increases to the left (-infinity)*/ + Negative + }; + + explicit Binary(const std::string& name = "", scalar start = fl::nan, + scalar direction = fl::nan, scalar height = 1.0); + virtual ~Binary() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Binary) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start direction [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start direction [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 1h & \mbox{if $ \left(s < d \vedge x \in [s, d)\right) \wedge + \left( s > d \vedge x \in (d, s] \right) $} \cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the Binary edge, + @f$d@f$ is the direction of the Binary edge. + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the start of the binary edge + @param start is the start of the binary edge + */ + virtual void setStart(scalar start); + /** + Gets the start of the binary edge + @return the start of the binary edge + */ + virtual scalar getStart() const; + + /** + Sets the direction of the binary edge. + + @f$\begin{cases} + \text{Positive} & \mbox{if $ d > s $}\cr + \text{Negative} & \mbox{if $ d < s $}\cr + \mbox{\tt NaN} & \mbox{otherwise} + \end{cases} + @f$ + + where @f$d@f$ is the given direction, and + @f$s@f$ is the start of the Binary edge + + @param direction is the direction of the binary edge + */ + virtual void setDirection(scalar direction); + /** + Gets the direction of the binary edge + @return the direction of the binary edge + */ + virtual scalar getDirection() const; + + /** + Gets the Direction of the binary edge as an enumerator + @return the Direction of the binary edge as an enumerator + */ + virtual Direction direction() const; + + virtual Binary* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_BINARY_H */ diff --git a/fuzzylite/fl/term/Concave.h b/fuzzylite/fl/term/Concave.h index b3aabe6..12d7160 100644 --- a/fuzzylite/fl/term/Concave.h +++ b/fuzzylite/fl/term/Concave.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_CONCAVE_H @@ -27,11 +19,21 @@ #include "fl/term/Term.h" - namespace fl { + /** + The Concave class is an edge Term that represents the concave membership + function. + + @image html concave.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 5.0 + */ class FL_API Concave : public Term { - protected: + private: scalar _inflection, _end; public: explicit Concave(const std::string& name = "", @@ -42,15 +44,61 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Concave) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term as + @return `"inflection end [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters given + @param parameters as `"inflection end [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + h \times (e - i) / (2e - i - x) & \mbox{if $i \leq e \wedge x < e$ + (increasing concave)} \cr + h \times (i - e) / (-2e + i + x) & \mbox{if $i > e \wedge x > e$ + (decreasing concave)} \cr + h & \mbox{otherwise} \cr + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$i@f$ is the inflection of the Concave, + @f$e@f$ is the end of the Concave + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the inflection of the curve + @param inflection is the inflection of the curve + */ virtual void setInflection(scalar inflection); + /** + Gets the inflection of the curve + @return the inflection of the curve + */ virtual scalar getInflection() const; + /** + Sets the end of the curve + @param end is the end of the curve + */ virtual void setEnd(scalar end); + /** + Gets the end of the curve + @return the end of the curve + */ virtual scalar getEnd() const; virtual Concave* clone() const FL_IOVERRIDE; @@ -58,7 +106,5 @@ namespace fl { static Term* constructor(); }; } - - #endif /* FL_CONCAVE_H */ diff --git a/fuzzylite/fl/term/Constant.h b/fuzzylite/fl/term/Constant.h index c4ed17d..40ba77b 100644 --- a/fuzzylite/fl/term/Constant.h +++ b/fuzzylite/fl/term/Constant.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_CONSTANT_H @@ -29,8 +21,17 @@ namespace fl { + /** + The Constant class is a (zero) polynomial Term that represents a constant + value @f$ f(x) = k @f$ + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Constant : public Term { - protected: + private: scalar _value; public: @@ -40,12 +41,35 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Constant) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"value"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"value"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x is irrelevant + @return @f$c@f$, where @f$c@f$ is the constant value + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the constant value + @param value is the constant value + */ virtual void setValue(scalar value); + /** + Gets the constant value + @return the constant value + */ virtual scalar getValue() const; virtual Constant* clone() const FL_IOVERRIDE; @@ -53,6 +77,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_CONSTANT_H */ diff --git a/fuzzylite/fl/term/Cosine.h b/fuzzylite/fl/term/Cosine.h index 510669f..b45ab85 100644 --- a/fuzzylite/fl/term/Cosine.h +++ b/fuzzylite/fl/term/Cosine.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_COSINE_H @@ -29,8 +21,20 @@ namespace fl { + /** + The Cosine class is an extended Term that represents the cosine + membership function. + + @image html cosine.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 5.0 + */ + class FL_API Cosine : public Term { - protected: + private: scalar _center, _width; public: explicit Cosine(const std::string& name = "", @@ -41,15 +45,51 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Cosine) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"center width [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"center width [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x < c - 0.5w \vee x > c + 0.5w$} \cr + 0.5h \times ( 1 + \cos(2.0 / w\pi(x-c))) & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$c@f$ is the center of the Cosine, + @f$w@f$ is the width of the Cosine + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; - + /** + Sets the center of the cosine + @param center is the center of the cosine + */ virtual void setCenter(scalar center); + /** + Gets the center of the cosine + @return the center of the cosine + */ virtual scalar getCenter() const; + /** + Sets the width of the cosine + @param width is the width of the cosine + */ virtual void setWidth(scalar width); + /** + Gets the width of the cosine + @return the width of the cosine + */ virtual scalar getWidth() const; virtual Cosine* clone() const FL_IOVERRIDE; @@ -57,6 +97,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_COSINE_H */ diff --git a/fuzzylite/fl/term/Discrete.h b/fuzzylite/fl/term/Discrete.h index fc554b3..ba5ae70 100644 --- a/fuzzylite/fl/term/Discrete.h +++ b/fuzzylite/fl/term/Discrete.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_DISCRETE_H @@ -27,15 +19,30 @@ #include "fl/term/Term.h" +#include "fl/defuzzifier/IntegralDefuzzifier.h" + #include <vector> #include <utility> namespace fl { + /** + 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 @f$x@f$. + + @image html discrete.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Discrete : public Term { public: typedef std::pair<scalar, scalar> Pair; - protected: + private: std::vector<Pair> _xy; public: explicit Discrete(const std::string& name = "", @@ -45,37 +52,229 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Discrete) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term as `x1 y1 xn yn [height]` + @return `x1 y1 xn yn [height]` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters given as `x1 y1 xn yn [height]` + @param parameters as `x1 y1 xn yn [height]` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; - //Warning: this method is unsafe. Make sure you use it correctly. - template <typename T> - static Discrete* create(const std::string& name, int argc, - T x1, T y1, ...); // throw (fl::Exception); + /** + Ascendantly sorts the given pairs of values by the @f$x@f$-value, + as it is required by the Discrete term. + @param pairs is a vector of pairs of values in the form @f$(x,y)@f$ + */ + static void sort(std::vector<Pair>& pairs); + + /** + Ascendantly sorts the pairs of values in this Discrete term by the + @f$x@f$-coordinate + */ + virtual void sort(); + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ by using binary + search to find the lower and upper bounds of @f$x@f$ and then linearly + interpolating the membership function between the bounds. + @param x + @return @f$ \dfrac{h (y_{\max} - y_{\min})}{(x_{\max}- x_{\min})} (x - x_{\min}) + y_{\min}@f$ + where @f$h@f$ is the height of the Term, + @f$x_{\min}@f$ and @f$x_{\max}@f$is are the lower and upper limits + of @f$x@f$ in `xy` (respectively), + @f$y_{\min}@f$ and @f$y_{\max}@f$is are the membership functions + of @f$\mu(x_{\min})@f$ and @f$\mu(x_{\max})@f$ (respectively) + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the vector of pairs defining the discrete membership function + @param pairs is the vector of pairs defining the discrete membership function + */ virtual void setXY(const std::vector<Pair>& pairs); + /** + Gets an immutable vector of pairs defining the discrete membership function + @return an immutable vector of pairs defining the discrete membership function + */ virtual const std::vector<Pair>& xy() const; + /** + Gets a mutable vector of pairs defining the discrete membership function + @return a mutable vector of pairs defining the discrete membership function + */ virtual std::vector<Pair>& xy(); - virtual const Pair& xy(int index) const; - virtual Pair& xy(int index); + /** + Gets the immutable pair @f$(x_i,y_i)@f$ at index @f$i@f$ + @param index is the index @f$i@f$ + @return the immutable pair @f$(x_i,y_i)@f$ + */ + virtual const Pair& xy(std::size_t index) const; + /** + Gets the mutable pair @f$(x_i,y_i)@f$ at index @f$i@f$ + @param index is the index @f$i@f$ + @return a mutable pair @f$(x_i,y_i)@f$ + */ + virtual Pair& xy(std::size_t index); + + /** + Creates, fills and returns a vector containing the @f$x@f$ values + @return a vector containing the @f$x@f$ values + */ + virtual std::vector<scalar> x() const; + + /** + Gets the @f$x@f$ value at the given index + @return the @f$x@f$ value at the given index + */ + virtual scalar x(std::size_t index) const; + /** + Gets the reference to the @f$x@f$ value at the given index + @return the reference to the @f$x@f$ value at the given index + */ + virtual scalar& x(std::size_t index); + /** + Creates, fills and returns a vector containing the @f$y@f$ values + @return a vector containing the @f$y@f$ values + */ + virtual std::vector<scalar> y() const; + + /** + Gets the @f$y@f$ value at the given index + @param index is the index + @return the @f$y@f$ value at the given index + */ + virtual scalar y(std::size_t index) const; + + /** + Gets the reference to the @f$y@f$ value at the given index + @param index is the index + @return the reference to the @f$y@f$ value at the given index + */ + virtual scalar& y(std::size_t index); + /** + Creates a vector of fl::scalar from a vector of Pair given in the + form @f$\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)@f$ + @param xy is the vector of Pair + @return a vector of fl::scalar as @f$(x_1,y_1,...,x_n,y_n)@f$ + */ static std::vector<scalar> toVector(const std::vector<Pair>& xy); + /** + Creates a vector of Pair from a vector of fl::scalar given in the + form @f$(x_1,y_1,...,x_n,y_n)@f$ + @param xy is a vector of fl::scalar given as + @f$(x_1,y_1,...,x_n,y_n)@f$ + @return a vector of Pair in the form + @f$\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)@f$ + @throws fl::Exception if a value is missing, that is, if the length + of @f$xy@f$ is odd: @f$|xy|\mod 2 = 1@f$ + */ static std::vector<Pair> toPairs(const std::vector<scalar>& xy); + /** + Creates a vector of Pair from a vector of fl::scalar given in the + form @f$(x_1,y_1,...,x_n,y_n)@f$ + @param xy is a vector of fl::scalar given as + @f$(x_1,y_1,...,x_n,y_n)@f$ possibly missing a value + @param missingValue is the replacement in the case a value is missing + from @f$xy@f$ + @return a vector of Pair in the form + @f$\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)@f$ + */ static std::vector<Pair> toPairs(const std::vector<scalar>& xy, scalar missingValue) FL_INOEXCEPT; + /** + Formats a vector of Pair into a std::string in the form + @f$(x_1,y_1) ... (x_n,y_n)@f$ + @param xy is the vector of Pair + @param prefix indicates the prefix of a Pair, e.g., `(` results in + @f$(x_i@f$ + @param innerSeparator indicates the separator between + @f$x@f$ and @f$y@f$, e.g., `,` results in @f$x_i,y_i@f$ + @param suffix indicates the postfix of a Pair, e.g., `]` results in + @f$y_i]@f$ + @param outerSeparator indicates the separator between Pair, e.g., + `;` results in @f$(x_i,y_i);(x_j,y_j)@f$ + @return a formatted string containing the pairs of @f$(x,y)@f$ values + */ static std::string formatXY(const std::vector<Pair>& xy, const std::string& prefix = "(", const std::string& innerSeparator = ",", - const std::string& postfix = ")", const std::string& outerSeparator = " "); + const std::string& suffix = ")", const std::string& outerSeparator = " "); + + /** + Discretizes the given term + @param term is the term to discretize + @param start is the value from which discretization starts + @param end is the value at which discretization ends + @param resolution is the number of equally-distributed samples to + perform between start and end + @param boundedMembershipFunction indicates whether to ensure that + @f$\mu(x)\in[0.0,1.0]@f$ + @return a Discrete term that approximates the given term + */ + static Discrete* discretize(const Term* term, scalar start, scalar end, + int resolution = IntegralDefuzzifier::defaultResolution(), + bool boundedMembershipFunction = true); virtual Discrete* clone() const FL_IOVERRIDE; static Term* constructor(); + /** + 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., + @f$(1.0, 2.0, 3.0)@f$ are all fl::scalar, whereas in + @f$(1.0, 2, 3.0)@f$ the second term is an integer, which will cause + memory access issues due to the difference in size between + `int` and `fl::scalar`. + @param name is the name of the resulting term + @param argc is the number of values passed + @param x1 is the @f$x@f$ value of the first Pair + @param y1 is the @f$y@f$ value of the first Pair + @param ... are the remaining pairs of values @f$x_i@f$ and @f$y_i@f$ + @return a new Discrete term with the given parameters + */ + template <typename T> + static Discrete* create(const std::string& name, int argc, + T x1, T y1, ...); }; +} + +/** + Template implementation + */ + +namespace fl { + + template <typename T> + inline Discrete* Discrete::create(const std::string& name, int argc, + T x1, T y1, ...) { + std::vector<scalar> xy(argc); + xy.at(0) = x1; + xy.at(1) = y1; + va_list args; + va_start(args, y1); + for (int i = 2; i < argc; ++i) { + xy.at(i) = (scalar) va_arg(args, T); + } + va_end(args); + FL_unique_ptr<Discrete> result(new Discrete(name)); + if (xy.size() % 2 != 0) { + result->setHeight(xy.back()); + xy.pop_back(); + } + result->setXY(toPairs(xy)); + return result.release(); + } } #endif /* FL_DISCRETE_H */ diff --git a/fuzzylite/fl/term/Function.h b/fuzzylite/fl/term/Function.h index 99580b7..14e3427 100644 --- a/fuzzylite/fl/term/Function.h +++ b/fuzzylite/fl/term/Function.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_FUNCTION_H @@ -34,27 +26,77 @@ namespace fl { class Engine; + /** + The Function class is a polynomial Term that represents a generic + function @f$ f : x \mapsto f(x) @f$. Every Function object has a public + key-value map, namely Function::variables, that links variable names to + fl::scalar values, which are utilized to replace the variable names for + their respective values in the given formula whenever the function + @f$f@f$ is evaluated. Specifically, when the method + Function::membership() is called, the name and value of the variable + @f$x@f$ are automatically loaded into the map. Furthermore, if an Engine + is given, the names of its InputVariable%s and OutputVariable%s will also + be automatically loaded into the map linking to their respective input + values and (previously defuzzified) output values. The + Function::variables need to be manually loaded whenever variables other + than @f$x@f$, input variables, and output variables, are expressed in the + given formula, always having in mind that (a) the map replaces existing + keys, and (b) the variable @f$x@f$, and input variables and output + variables of an engine will automatically be replaced and will also take + precedence over previously loaded variables. + + Besides the use of Function as a linguistic Term, it is also utilized to + convert the text of the Antecedent of a Rule, expressed in infix + notation, into postfix notation. + + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @see FunctionFactory + @see Antecedent::load() + @since 4.0 + */ class FL_API Function : public Term { - /**************************** - * Parsing Elements - ****************************/ - public: typedef scalar(*Unary)(scalar); typedef scalar(*Binary)(scalar, scalar); + /** + The Element class represents a single element in a formula, be that + either a function or an operator. If the Element represents a + function, the function can be Unary or Binary, that is, the function + take one or two parameters (respectively). Else, if the Element + represents an operator, the parameters to be defined are its `arity`, + its `precedence`, and its `associativity`. + */ struct FL_API Element { + /** + Determines the type of the element + */ enum Type { - OPERATOR, FUNCTION + Operator, Function }; + /**Name of the element*/ std::string name; + /**Description of the element*/ std::string description; + /**Type of the element*/ Type type; + /**Pointer to unary method*/ Unary unary; + /**Pointer to binary method*/ Binary binary; + /**Number of operands required*/ int arity; - int precedence; //Operator + /**Precedence of the element: clarifies which procedures should be + performed first in a given mathematical expression + (https://en.wikipedia.org/wiki/Order_of_operations)*/ + int precedence; + /**Associativity of the element: determines how operators of the + same precedence are grouped in the absence of parentheses + (https://en.wikipedia.org/wiki/Operator_associativity)*/ int associativity; Element(const std::string& name, const std::string& description, Type type); Element(const std::string& name, const std::string& description, @@ -64,24 +106,46 @@ namespace fl { virtual ~Element(); FL_DEFAULT_COPY_AND_MOVE(Element) + /** + Indicates whether the element is a Type::Operator + @return whether the element is a Type::Operator + */ virtual bool isOperator() const; + /** + Indicates whether the element is a Type::Function + @return whether the element is a Type::Function + */ virtual bool isFunction() const; - + /** + Clones the element + @return a clone of the element + */ virtual Element* clone() const; + /** + Returns a description of the element and its members + @return a description of the element and its members + */ virtual std::string toString() const; }; - /************************** - * Tree elements, wrap Elements into Nodes. - **************************/ - + /** + The Node class structures a binary tree by storing pointers to a left + Node and a right Node, and storing its content as a + Function::Element, the name of an InputVariable or OutputVariable, or + a constant value. + */ struct FL_API Node { + /**The node takes an operation or a function*/ FL_unique_ptr<Element> element; + /**The node can have an expression tree on the left*/ FL_unique_ptr<Node> left; + /**The node can have an expression tree on the right*/ FL_unique_ptr<Node> right; + /**The node can refer to a variable by name*/ std::string variable; + /**The node can take an arbitrary floating-point value*/ scalar value; explicit Node(Element* element, Node* left = fl::null, Node* right = fl::null); @@ -92,14 +156,79 @@ namespace fl { virtual ~Node(); FL_DEFAULT_MOVE(Node) + /** + Evaluates the node and substitutes the variables therein for the + values passed in the map. The expression tree is evaluated + recursively. + + @param variables is a map of substitutions of variable names for + fl::scalar%s + + @return a fl::scalar indicating the result of the evaluation of + the node + */ virtual scalar evaluate(const std::map<std::string, scalar>* variables = fl::null) const; + /** + Computes the size of the subtree under the given node. The complexity + of calling this method is O(n). + @param node is the root of the subtree, which is this node if + fl::null is given + @return the size of the subtree under the given node + */ + virtual std::size_t treeSize(const Node* node = fl::null) const; + + /** + Computes the size of the subtree under the given node whose elements + are of the given type. The complexity of calling this method is O(n). + @param type is the type of elements to account for + @param node is the root of the subtree, which is this node if + fl::null is given + @return + */ + virtual std::size_t treeSize(Element::Type type, + const Node* node = fl::null) const; + + /** + Creates a clone of the node. + @return a clone of the node + */ virtual Node* clone() const; + /** + Returns a string with the name of the element, the name of the + variable, or the constant value, accordingly. + @return a string with the name of the element, the name of the + variable, or the constant value, accordingly. + */ virtual std::string toString() const; + /** + Returns a prefix representation of the expression tree under the + given node + @param node is the node to start the prefix representation from. + If the node is `fl::null`, then the starting point is `this` node + @return a prefix representation of the expression tree under the + given node + */ virtual std::string toPrefix(const Node* node = fl::null) const; + /** + Returns an infix representation of the expression tree under the + given node + @param node is the node to start the infix representation from. + If the node is `fl::null`, then the starting point is `this` node + @return an infix representation of the expression tree under the + given node + */ virtual std::string toInfix(const Node* node = fl::null) const; + /** + Returns a postfix representation of the expression tree under the + given node + @param node is the node to start the postfix representation from. + If the node is `fl::null`, then the starting point is `this` node + @return a postfix representation of the expression tree under the + given node + */ virtual std::string toPostfix(const Node* node = fl::null) const; private: void copyFrom(const Node& source); @@ -112,11 +241,12 @@ namespace fl { * Term ******************************/ - protected: + private: FL_unique_ptr<Node> _root; std::string _formula; const Engine* _engine; public: + /**A map of variables and substitution values**/ mutable std::map<std::string, scalar> variables; explicit Function(const std::string& name = "", const std::string& formula = "", const Engine* engine = fl::null); @@ -125,47 +255,145 @@ namespace fl { virtual ~Function() FL_IOVERRIDE; FL_DEFAULT_MOVE(Function) + /** + Creates a Function term with the given parameters + @param name is the name of the term + @param formula is the formula defining the membership function + @param engine is the engine to which the Function can have access + @return a Function term configured with the given parameters + @throws fl::Exception if the formula has a syntax error + */ static Function* create(const std::string& name, const std::string& formula, - const Engine* engine = fl::null); // throw (fl::Exception); - + const Engine* engine = fl::null); + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function value of @f$x@f$ at the root node. + If the engine has been set, the current values of the input variables + and output variables are added to the map of Function::variables. In + addition, the variable @f$x@f$ will also be added to the map. + @param x + @return the membership function value of @f$x@f$ at the root node + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; - virtual scalar evaluate(const std::map<std::string, scalar>* variables) const; + /** + Computes the function value of this term using the given map of + variable substitutions. + @param variables is a map of substitution variables + @return the function value of this term using the given map of + variable substitutions. + */ + virtual scalar evaluate(const std::map<std::string, scalar>* variables = fl::null) const; virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term as `formula` + @return `formula` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters given as `formula` + @param parameters as `formula` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + /** + Sets the formula of the function + @param formula is the formula of the function + */ virtual void setFormula(const std::string& formula); + /** + Gets the formula of the function + @return the formula of the function + */ virtual std::string getFormula() const; + /** + Sets the engine to which the formula can refer + @param engine is the engine to which the formula can refer + */ virtual void setEngine(const Engine* engine); + /** + Gets the engine to which the formula can refer + @return the engine to which the formula can refer + */ virtual const Engine* getEngine() const; + /** + Gets the root node of the expression tree defining the Function. The + root is `fl::null` if the formula has not been loaded. + @return the root node of the expression tree defining the Function, + or `fl::null` if the formula has not been loaded + */ virtual Node* root() const; + /** + Indicates whether the formula is loaded + @return whether the formula is loaded + */ virtual bool isLoaded() const; + /** + Unloads the formula and resets the map of substitution variables. + */ virtual void unload(); - virtual void load(); // throw (fl::Exception); - virtual void load(const std::string& formula); // throw (fl::Exception); - virtual void load(const std::string& formula, const Engine* engine); // throw (fl::Exception); - - virtual Node* parse(const std::string& formula); // throw (fl::Exception); - - virtual std::string toPostfix(const std::string& formula) const; //throw (fl::Exception); - + /** + Loads the current formula expressed in infix notation + */ + virtual void load(); + /** + Loads the given formula expressed in infix notation + @param formula is the right-hand side of a mathematical equation + @throws fl::Exception if the formula has syntax errors + */ + virtual void load(const std::string& formula); + /** + Loads the given formula expressed in infix notation, and sets the + engine holding the variables to which the formula refers. + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @param engine is the engine to which the formula can refer + @throws fl::Exception if the formula has syntax errors + */ + virtual void load(const std::string& formula, const Engine* engine); + /** + Creates a node representing a binary expression tree from the given formula + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @return a node representing a binary expression tree from the given formula + @throws fl::Exception if the formula has syntax errors + */ + virtual Node* parse(const std::string& formula); + + /** + Translates the given formula to postfix notation + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @return the formula represented in postfix notation + @throws fl::Exception if the formula has syntax errors + */ + virtual std::string toPostfix(const std::string& formula) const; + + /** + Adds spaces to the formula to separate parentheses, commas and + function operators such that these are treated as tokens when parsing + the function. + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @return the formula with spaces before and after parentheses, commas + and function operators + */ virtual std::string space(const std::string& formula) const; + virtual void updateReference(const Engine* engine) FL_IOVERRIDE; + virtual Function* clone() const FL_IOVERRIDE; static Term* constructor(); - static void main(); - }; - } - #endif /* FL_FUNCTION_H */ diff --git a/fuzzylite/fl/term/Gaussian.h b/fuzzylite/fl/term/Gaussian.h index a5b8055..9a7475d 100644 --- a/fuzzylite/fl/term/Gaussian.h +++ b/fuzzylite/fl/term/Gaussian.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_GAUSSIAN_H @@ -29,8 +21,19 @@ namespace fl { + /** + The Gaussian class is an extended Term that represents the %Gaussian + curve membership function. + + @image html gaussian.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Gaussian : public Term { - protected: + private: scalar _mean; scalar _standardDeviation; @@ -43,21 +46,54 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Gaussian) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"mean standardDeviation [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"mean standardDeviation [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h \times \exp(-(x-\mu)^2/(2\sigma^2))@f$ + + where @f$h@f$ is the height of the Term, + @f$\mu@f$ is the mean of the Gaussian, + @f$\sigma@f$ is the standard deviation of the Gaussian + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; - virtual void setMean(scalar c); + /** + Sets the mean of the Gaussian curve + @param mean is the mean of the Gaussian curve + */ + virtual void setMean(scalar mean); + /** + Gets the mean of the Gaussian curve + @return the mean of the Gaussian curve + */ virtual scalar getMean() const; - virtual void setStandardDeviation(scalar sigma); + /** + Sets the standard deviation of the Gaussian curve + @param standardDeviation is the standard deviation of the Gaussian curve + */ + virtual void setStandardDeviation(scalar standardDeviation); + /** + Gets the standard deviation of the Gaussian curve + @return the standard deviation of the Gaussian curve + */ virtual scalar getStandardDeviation() const; virtual Gaussian* clone() const FL_IOVERRIDE; static Term* constructor(); }; - } #endif /* FL_GAUSSIAN_H */ diff --git a/fuzzylite/fl/term/GaussianProduct.h b/fuzzylite/fl/term/GaussianProduct.h index bd1eaaa..e4a500c 100644 --- a/fuzzylite/fl/term/GaussianProduct.h +++ b/fuzzylite/fl/term/GaussianProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_GAUSSIANPRODUCT_H @@ -29,8 +21,19 @@ namespace fl { + /** + The GaussianProduct class is an extended Term that represents the + two-sided %Gaussian membership function. + + @image html gaussianProduct.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API GaussianProduct : public Term { - protected: + private: scalar _meanA; scalar _standardDeviationA; scalar _meanB; @@ -47,21 +50,83 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(GaussianProduct) virtual std::string className() const FL_IOVERRIDE; + /** + Provides the parameters of the term + @return `"meanA standardDeviationA meanB standardDeviationB [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"meanA standardDeviationA meanB + standardDeviationB [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h \left((1 - i) + i \times \exp(-(x - \mu_a)^2 / + (2\sigma_a^2))\right) + \left((1 - j) + j \times \exp(-(x - \mu_b)^2 / (2 \sigma_b)^2)\right) + @f$ + + where @f$h@f$ is the height of the Term, + @f$\mu_a@f$ is the mean of the first GaussianProduct, + @f$\sigma_a@f$ is the standard deviation of the first + GaussianProduct, + @f$\mu_b@f$ is the mean of the second GaussianProduct, + @f$\sigma_b@f$ is the standard deviation of the second + GaussianProduct, + @f$i=\begin{cases}1 & \mbox{if $x \leq \mu_a$} \cr 0 + &\mbox{otherwise}\end{cases}@f$, + @f$j=\begin{cases}1 & \mbox{if $x \geq \mu_b$} \cr 0 + &\mbox{otherwise}\end{cases}@f$ + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the mean of the first %Gaussian curve + @param meanA is the mean of the first %Gaussian curve + */ virtual void setMeanA(scalar meanA); + /** + Gets the mean of the first %Gaussian curve + @return the mean of the first %Gaussian curve + */ virtual scalar getMeanA() const; - virtual void setStandardDeviationA(scalar sigmaA); + /** + Sets the standard deviation of the first %Gaussian curve + @param standardDeviationA is the standard deviation of the first %Gaussian curve + */ + virtual void setStandardDeviationA(scalar standardDeviationA); + /** + Gets the standard deviation of the first %Gaussian curve + @return the standard deviation of the first %Gaussian curve + */ virtual scalar getStandardDeviationA() const; + /** + Sets the mean of the second %Gaussian curve + @param meanB is the mean of the second %Gaussian curve + */ virtual void setMeanB(scalar meanB); + /** + Gets the mean of the second %Gaussian curve + @return the mean of the second %Gaussian curve + */ virtual scalar getMeanB() const; - virtual void setStandardDeviationB(scalar sigmaB); + /** + Sets the standard deviation of the second %Gaussian curve + @param standardDeviationB is the standard deviation of the second %Gaussian curve + */ + virtual void setStandardDeviationB(scalar standardDeviationB); + /** + Gets the standard deviation of the second %Gaussian curve + @return the standard deviation of the second %Gaussian curve + */ virtual scalar getStandardDeviationB() const; virtual GaussianProduct* clone() const FL_IOVERRIDE; diff --git a/fuzzylite/fl/term/Linear.h b/fuzzylite/fl/term/Linear.h index 67ede04..1105a1d 100644 --- a/fuzzylite/fl/term/Linear.h +++ b/fuzzylite/fl/term/Linear.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_LINEAR_H @@ -30,8 +22,23 @@ namespace fl { class Engine; + /** + The Linear class is a linear polynomial Term expressed as @f$f(x)= + \mathbf{c}\mathbf{v}+k = \sum_i c_iv_i + k@f$, where variable @f$x@f$ is + not utilized, @f$\mathbf{v}@f$ is a vector of values from the input + variables, @f$\mathbf{c}@f$ is a vector of coefficients, and @f$k@f$ is a + constant. Hereinafter, the vector @f$\mathbf{c}^\star=\{c_1, \ldots, c_i, + \ldots, c_n, k\}@f$ refers to a vector containing the coefficients of + @f$\mathbf{c}@f$ and the constant @f$k@f$. + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Linear : public Term { - protected: + private: + /**Contains the coefficients @f$c_i@f$ and the constant @f$k@f$*/ std::vector<scalar> _coefficients; const Engine* _engine; public: @@ -42,31 +49,131 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Linear) virtual std::string className() const FL_IOVERRIDE; + + /** + Returns the vector @f$\mathbf{c}^\star@f$ + @return `"c1 ... ci ... cn k"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the values of @f$\mathbf{c}^\star@f$ + @param parameters as `"c1 ... ci ... cn k"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; - virtual scalar membership(scalar x) const FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; - virtual void set(const std::vector<scalar>& coeffs, const Engine* engine); + /** + Computes the linear function @f$f(x)=\sum_i c_iv_i +k@f$, + where @f$v_i@f$ is the value of the input variable @f$i@f$ registered + in the Linear::getEngine() + @param x is not utilized + @return @f$\sum_i c_ix_i +k@f$ + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; - virtual void setCoefficients(const std::vector<scalar>& coeffs); + /** + Sets the vector @f$\mathbf{c}^\star@f$ and the Engine from which + vector @f$\mathbf{v}@f$ will be retrieved when necessary + @param coefficients is the vector @f$\mathbf{c}^\star@f$ + @param engine is the engine from which @f$\mathbf{v}@f$ will be + retrieved when necessary + */ + virtual void set(const std::vector<scalar>& coefficients, const Engine* engine); + + /** + Sets the vector @f$\mathbf{c}^\star@f$ of the linear function + @param coefficients is the vector @f$\mathbf{c}^\star@f$ + */ + virtual void setCoefficients(const std::vector<scalar>& coefficients); + /** + Gets the immutable vector @f$\mathbf{c}^\star@f$ + @return the immutable vector @f$\mathbf{c}^\star@f$ + */ virtual const std::vector<scalar>& coefficients() const; + /** + Gets the mutable vector @f$\mathbf{c}^\star@f$ + @return the mutable vector @f$\mathbf{c}^\star@f$ + */ virtual std::vector<scalar>& coefficients(); + /** + Sets the engine from which the vector @f$\mathbf{v}@f$ will be + obtained upon computing the Linear::membership() + @param engine is the engine from which the vector @f$\mathbf{v}@f$ + will be obtained + */ virtual void setEngine(const Engine* engine); + /** + Gets the engine from which the vector @f$\mathbf{v}@f$ will be + obtained upon computing the Linear::membership() + @return the engine from which the vector @f$\mathbf{v}@f$ will be + obtained + */ virtual const Engine* getEngine() const; virtual Linear* clone() const FL_IOVERRIDE; + virtual void updateReference(const Engine* engine) FL_IOVERRIDE; + static Term* constructor(); - //Warning: this method is unsafe, make sure you use it correctly. + /** + Creates a Linear term from a variadic set of coefficients. + Beware: this method is unsafe and must be used with care by + ensuring: + + - the data type of each variadic arguments is the same, e.g., + @f$(1.0, 2.0, 3.0)@f$ are all fl::scalar%s. You *need* to avoid the + case of @f$(1.0, 2, 3.0)@f$, where the second term will be + considered as an `int` (different from the others) and cause memory + issues due to the difference in size between an `int` and + `fl::scalar`; and + + - the number of variadic arguments is exactly the same as the number + of input variables in the engine plus one in order to match the + size of the vector @f$\mathbf{c}^\star@f$ + + @param name is the name of the term + @param engine is the engine from which the vector @f$\mathbf{v}@f$ + will be obtained + @param firstCoefficient is the coefficient for the first input + variable, namely @f$c_1@f$ + @param ... is a variadic number of coefficients whose type need to be + the same as the first coefficient + @return a new Linear term with the given parameters + */ template <typename T> static Linear* create(const std::string& name, const Engine* engine, - T firstCoefficient, ...); // throw (fl::Exception); + T firstCoefficient, ...); }; - } +/** + Template implementation + */ + +#include "fl/Engine.h" + +namespace fl { + + template <typename T> + inline Linear* Linear::create(const std::string& name, + const Engine* engine, T firstCoefficient, ...) { + if (not engine) throw Exception("[linear error] cannot create term <" + name + "> " + "without a reference to the engine", FL_AT); + std::vector<scalar> coefficients; + coefficients.push_back((scalar) firstCoefficient); + + va_list args; + va_start(args, firstCoefficient); + for (std::size_t i = 0; i < engine->inputVariables().size(); ++i) { + coefficients.push_back((scalar) va_arg(args, T)); + } + va_end(args); + + return new Linear(name, coefficients, engine); + } +} #endif /* FL_LINEAR_H */ diff --git a/fuzzylite/fl/term/PiShape.h b/fuzzylite/fl/term/PiShape.h index a0c2f78..24e98ed 100644 --- a/fuzzylite/fl/term/PiShape.h +++ b/fuzzylite/fl/term/PiShape.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_PISHAPE_H @@ -29,8 +21,19 @@ namespace fl { + /** + The PiShape class is an extended Term that represents the Pi-shaped curve + membership function. + + @image html piShape.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API PiShape : public Term { - protected: + private: scalar _bottomLeft; scalar _topLeft; scalar _topRight; @@ -47,21 +50,82 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(PiShape) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"bottomLeft topLeft topRight bottomRight [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"bottomLeft topLeft topRight bottomRight + [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \leq b_l$}\cr + 2h \left((x - b_l) / (t_l-b_l)\right)^2 & \mbox{if $x \leq 0.5(a+b)$}\cr + h (1 - 2 \left((x - t_l) / (t_l-b_l)\right)^2) & \mbox{if $ x < t_l$}\cr + h & \mbox{if $x \leq t_r$}\cr + h (1 - 2\left((x - t_r) / (b_r - t_r)\right)^2) & \mbox{if $x \leq 0.5(t_r + b_r)$}\cr + 2h \left((x - b_r) / (b_r-t_r)\right)^2 & \mbox{if $x < b_r$} \cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$b_l@f$ is the bottom left of the PiShape, + @f$t_l@f$ is the top left of the PiShape, + @f$t_r@f$ is the top right of the PiShape + @f$b_r@f$ is the bottom right of the PiShape, + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; - virtual void setBottomLeft(scalar a); + /** + Sets the bottom-left value of the curve + @param bottomLeft is the bottom-left value of the curve + */ + virtual void setBottomLeft(scalar bottomLeft); + /** + Gets the bottom-left value of the curve + @return the bottom-left value of the curve + */ virtual scalar getBottomLeft() const; - virtual void setTopLeft(scalar b); + /** + Sets the top-left value of the curve + @param topLeft is the top-left value of the curve + */ + virtual void setTopLeft(scalar topLeft); + /** + Gets the top-left value of the curve + @return the top-left value of the curve + */ virtual scalar getTopLeft() const; - virtual void setTopRight(scalar d); + /** + Sets the top-right value of the curve + @param topRight is the top-right value of the curve + */ + virtual void setTopRight(scalar topRight); + /** + Gets the top-right value of the curve + @return the top-right value of the curve + */ virtual scalar getTopRight() const; - virtual void setBottomRight(scalar c); + /** + Sets the bottom-right value of the curve + @param bottomRight is the bottom-right value of the curve + */ + virtual void setBottomRight(scalar bottomRight); + /** + Gets the bottom-right value of the curve + @return the bottom-right value of the curve + */ virtual scalar getBottomRight() const; virtual PiShape* clone() const FL_IOVERRIDE; @@ -69,6 +133,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_PISHAPE_H */ diff --git a/fuzzylite/fl/term/Ramp.h b/fuzzylite/fl/term/Ramp.h index 63a683c..dc49301 100644 --- a/fuzzylite/fl/term/Ramp.h +++ b/fuzzylite/fl/term/Ramp.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_RAMP_H @@ -29,14 +21,30 @@ namespace fl { + /** + The Ramp class is an edge Term that represents the ramp membership + function. + + @image html ramp.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Ramp : public Term { - protected: + private: scalar _start, _end; public: + /** + Direction is an enumerator that indicates the direction of the ramp. + */ enum Direction { - POSITIVE, ZERO, NEGATIVE + /** `(_/)` increases to the right */ Positive, + /** `(--)` slope is zero */ Zero, + /** `(\\_)` increases to the left */ Negative }; explicit Ramp(const std::string& name = "", scalar start = fl::nan, @@ -46,17 +54,76 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Ramp) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return + @f$\begin{cases} + + 0h & \mbox{if $x = e$}\cr + + \begin{cases} + 0h & \mbox{if $x \leq s$}\cr + 1h & \mbox{if $x \geq e$}\cr + h (x - s) / (e - s) & \mbox{otherwise}\cr + \end{cases} & \mbox{if $s < e$}\cr + + \begin{cases} + 0h & \mbox{if $x \geq s$}\cr + 1h & \mbox{if $x \leq e$}\cr + h (s - x) / (s - e) & \mbox{otherwise} + \end{cases} & \mbox{if $s > e$}\cr + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the Ramp, + @f$e@f$ is the end of the Ramp + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the start of the ramp + @param start is the start of the ramp + */ virtual void setStart(scalar start); + /** + Gets the start of the ramp + @return the start of the ramp + */ virtual scalar getStart() const; + /** + Sets the end of the ramp + @param end is the end of the ramp + */ virtual void setEnd(scalar end); + /** + Gets the end of the ramp + @return the end of the ramp + */ virtual scalar getEnd() const; + /** + Returns the direction of the ramp + @return the direction of the ramp + */ virtual Direction direction() const; virtual Ramp* clone() const FL_IOVERRIDE; diff --git a/fuzzylite/fl/term/Rectangle.h b/fuzzylite/fl/term/Rectangle.h index 23b5eda..0e29cd2 100644 --- a/fuzzylite/fl/term/Rectangle.h +++ b/fuzzylite/fl/term/Rectangle.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_RECTANGLE_H @@ -29,8 +21,19 @@ namespace fl { + /** + The Rectangle class is a basic Term that represents the rectangle + membership function. + + @image html rectangle.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Rectangle : public Term { - protected: + private: scalar _start, _end; public: @@ -42,21 +45,58 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Rectangle) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 1h & \mbox{if $x \in [s, e]$} \cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the Rectangle, + @f$e@f$ is the end of the Rectangle. + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the start of the rectangle + @param start is the start of the rectangle + */ virtual void setStart(scalar start); + /** + Gets the start of the rectangle + @return the start of the rectangle + */ virtual scalar getStart() const; + /** + Sets the end of the rectangle + @param end is the end of the rectangle + */ virtual void setEnd(scalar end); + /** + Gets the end of the rectangle + @return the end of the rectangle + */ virtual scalar getEnd() const; virtual Rectangle* clone() const FL_IOVERRIDE; static Term* constructor(); }; - } #endif /* FL_RECTANGLE_H */ diff --git a/fuzzylite/fl/term/SShape.h b/fuzzylite/fl/term/SShape.h index 0891357..1d107a5 100644 --- a/fuzzylite/fl/term/SShape.h +++ b/fuzzylite/fl/term/SShape.h @@ -1,28 +1,19 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ - #ifndef FL_SSHAPE_H #define FL_SSHAPE_H @@ -30,8 +21,19 @@ namespace fl { + /** + The SShape class is an edge Term that represents the S-shaped membership + function. + + @image html sShape.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API SShape : public Term { - protected: + private: scalar _start, _end; public: @@ -43,15 +45,59 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(SShape) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \leq s$} \cr + h(2 \left((x - s) / (e-s)\right)^2) & \mbox{if $x \leq 0.5(s+e)$}\cr + h(1 - 2\left((x - e) / (e-s)\right)^2) & \mbox{if $x < e$}\cr + 1h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the SShape, + @f$e@f$ is the end of the SShape. + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the start of the edge + @param start is the start of the edge + */ virtual void setStart(scalar start); + /** + Gets the start of the edge + @return the start of the edge + */ virtual scalar getStart() const; + /** + Sets the end of the edge + @param end is the end of the edge + */ virtual void setEnd(scalar end); + /** + Gets the end of the edge + @return the end of the edge + */ virtual scalar getEnd() const; virtual SShape* clone() const FL_IOVERRIDE; @@ -59,6 +105,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_SSHAPE_H */ diff --git a/fuzzylite/fl/term/Sigmoid.h b/fuzzylite/fl/term/Sigmoid.h index b37b1be..bde9c2f 100644 --- a/fuzzylite/fl/term/Sigmoid.h +++ b/fuzzylite/fl/term/Sigmoid.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_SIGMOID_H @@ -29,14 +21,31 @@ namespace fl { + /** + The Sigmoid class is an edge Term that represents the sigmoid membership + function. + + @image html sigmoid.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Sigmoid : public Term { - protected: + private: scalar _inflection; scalar _slope; public: + /** + Direction is an enumerator that indicates the direction of the + sigmoid. + */ enum Direction { - POSITIVE, ZERO, NEGATIVE + /** `(_/)` increases to the right */ Positive, + /** `(--)` slope is zero */ Zero, + /** `(\\_)` increases to the left */ Negative }; explicit Sigmoid(const std::string& name = "", scalar inflection = fl::nan, @@ -46,23 +55,66 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Sigmoid) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"inflection slope [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"inflection slope [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h / (1 + \exp(-s(x-i)))@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the slope of the Sigmoid, + @f$i@f$ is the inflection of the Sigmoid + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the inflection of the sigmoid + @param inflection is the inflection of the sigmoid + */ virtual void setInflection(scalar inflection); + /** + Gets the inflection of the sigmoid + @return the inflection of the sigmoid + */ virtual scalar getInflection() const; + /** + Sets the slope of the sigmoid + @param slope is the slope of the sigmoid + */ virtual void setSlope(scalar slope); + /** + Gets the slope of the sigmoid + @return the slope of the sigmoid + */ virtual scalar getSlope() const; + /** + Returns the direction of the sigmoid + @return the direction of the sigmoid + */ virtual Direction direction() const; virtual Sigmoid* clone() const FL_IOVERRIDE; static Term* constructor(); }; - } #endif /* FL_SIGMOID_H */ diff --git a/fuzzylite/fl/term/SigmoidDifference.h b/fuzzylite/fl/term/SigmoidDifference.h index 020f164..4861584 100644 --- a/fuzzylite/fl/term/SigmoidDifference.h +++ b/fuzzylite/fl/term/SigmoidDifference.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_SIGMOIDDIFFERENCE_H @@ -29,8 +21,19 @@ namespace fl { + /** + The SigmoidDifference class is an extended Term that represents the + difference between two sigmoidal membership functions. + + @image html sigmoidDifference.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API SigmoidDifference : public Term { - protected: + private: scalar _left; scalar _rising; scalar _falling; @@ -47,21 +50,76 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(SigmoidDifference) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"left rising falling right [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"left rising falling right [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h (a-b)@f$ + + where @f$h@f$ is the height of the Term, + @f$a= 1 / (1 + \exp(-s_l \times (x - i_l))) @f$, + @f$b = 1 / (1 + \exp(-s_r \times (x - i_r)))@f$, + @f$i_l@f$ is the left inflection of the SigmoidDifference, + @f$s_l@f$ is the left slope of the SigmoidDifference, + @f$i_r@f$ is the right inflection of the SigmoidDifference, + @f$s_r@f$ is the right slope of the SigmoidDifference + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the inflection of the left sigmoidal curve + @param leftInflection is the inflection of the left sigmoidal curve + */ virtual void setLeft(scalar leftInflection); + /** + Gets the inflection of the left sigmoidal curve + @return the inflection of the left sigmoidal curve + */ virtual scalar getLeft() const; + /** + Sets the slope of the left sigmoidal curve + @param risingSlope is the slope of the left sigmoidal curve + */ virtual void setRising(scalar risingSlope); + /** + Gets the slope of the left sigmoidal curve + @return the slope of the left sigmoidal curve + */ virtual scalar getRising() const; + /** + Sets the slope of the right sigmoidal curve + @param fallingSlope is the slope of the right sigmoidal curve + */ virtual void setFalling(scalar fallingSlope); + /** + Gets the slope of the right sigmoidal curve + @return the slope of the right sigmoidal curve + */ virtual scalar getFalling() const; + /** + Sets the inflection of the right sigmoidal curve + @param rightInflection is the inflection of the right sigmoidal curve + */ virtual void setRight(scalar rightInflection); + /** + Gets the inflection of the right sigmoidal curve + @return the inflection of the right sigmoidal curve + */ virtual scalar getRight() const; virtual SigmoidDifference* clone() const FL_IOVERRIDE; diff --git a/fuzzylite/fl/term/SigmoidProduct.h b/fuzzylite/fl/term/SigmoidProduct.h index cb83f30..19cd4d7 100644 --- a/fuzzylite/fl/term/SigmoidProduct.h +++ b/fuzzylite/fl/term/SigmoidProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_SIGMOIDPRODUCT_H @@ -29,8 +21,19 @@ namespace fl { + /** + The SigmoidProduct class is an extended Term that represents the product + of two sigmoidal membership functions. + + @image html sigmoidProduct.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API SigmoidProduct : public Term { - protected: + private: scalar _left; scalar _rising; scalar _falling; @@ -47,23 +50,79 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(SigmoidProduct) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"left rising falling right [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"left rising falling right [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h (a \times b)@f$ + + where @f$h@f$ is the height, + @f$a= 1 / (1 + \exp(-s_l *\times (x - i_l))) @f$, + @f$b = 1 / (1 + \exp(-s_r \times (x - i_r)))@f$, + @f$i_l@f$ is the left inflection of the SigmoidProduct, + @f$s_l@f$ is the left slope of the SigmoidProduct, + @f$i_r@f$ is the right inflection of the SigmoidProduct, + @f$s_r@f$ is the right slope of the SigmoidProduct + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the inflection of the left sigmoidal curve + @param leftInflection is the inflection of the left sigmoidal curve + */ virtual void setLeft(scalar leftInflection); + /** + Gets the inflection of the left sigmoidal curve + @return the inflection of the left sigmoidal curve + */ virtual scalar getLeft() const; + /** + Sets the slope of the left sigmoidal curve + @param risingSlope is the slope of the left sigmoidal curve + */ virtual void setRising(scalar risingSlope); + /** + Gets the slope of the left sigmoidal curve + @return the slope of the left sigmoidal curve + */ virtual scalar getRising() const; + /** + Sets the slope of the right sigmoidal curve + @param fallingSlope is the slope of the right sigmoidal curve + */ virtual void setFalling(scalar fallingSlope); + /** + Gets the slope of the right sigmoidal curve + @return the slope of the right sigmoidal curve + */ virtual scalar getFalling() const; + /** + Sets the inflection of the right sigmoidal curve + @param rightInflection is the inflection of the right sigmoidal curve + */ virtual void setRight(scalar rightInflection); + /** + Gets the inflection of the right sigmoidal curve + @return the inflection of the right sigmoidal curve + */ virtual scalar getRight() const; + virtual SigmoidProduct* clone() const FL_IOVERRIDE; static Term* constructor(); diff --git a/fuzzylite/fl/term/Spike.h b/fuzzylite/fl/term/Spike.h index 7e520c9..7349307 100644 --- a/fuzzylite/fl/term/Spike.h +++ b/fuzzylite/fl/term/Spike.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_SPIKE_H @@ -29,8 +21,19 @@ namespace fl { + /** + The Spike class is an extended Term that represents the spike membership + function. + + @image html spike.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 5.0 + */ class FL_API Spike : public Term { - protected: + private: scalar _center, _width; public: explicit Spike(const std::string& name = "", @@ -41,15 +44,49 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Spike) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"center width [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"center width [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$h \times \exp(-|10 / w (x - c)|)@f$ + + where @f$h@f$ is the height of the Term, + @f$w@f$ is the width of the Spike, + @f$c@f$ is the center of the Spike + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the center of the spike + @param center is the center of the spike + */ virtual void setCenter(scalar center); + /** + Gets the center of the spike + @return the center of the spike + */ virtual scalar getCenter() const; + /** + Sets the width of the spike + @param width is the width of the spike + */ virtual void setWidth(scalar width); + /** + Gets the width of the spike + @return the width of the spike + */ virtual scalar getWidth() const; virtual Spike* clone() const FL_IOVERRIDE; @@ -57,6 +94,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_SPIKE_H */ diff --git a/fuzzylite/fl/term/Term.h b/fuzzylite/fl/term/Term.h index 6c2b2b1..31dcabd 100644 --- a/fuzzylite/fl/term/Term.h +++ b/fuzzylite/fl/term/Term.h @@ -1,34 +1,26 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ - #ifndef FL_TERM_H #define FL_TERM_H #include "fl/fuzzylite.h" #include "fl/Operation.h" +#include "fl/Complexity.h" #include <cmath> #include <string> @@ -37,9 +29,33 @@ namespace fl { class Engine; + /** + The Term class is the abstract class for linguistic terms. The linguistic + terms in this library can be divided in four groups as: `basic`, + `extended`, `edge`, and `function`. The `basic` terms are Triangle, + Trapezoid, Rectangle, and Discrete. The `extended` terms are Bell, + Binary, Cosine, Gaussian, GaussianProduct, PiShape, SigmoidDifference, + SigmoidProduct, and Spike. The `edge` terms are Concave, Ramp, Sigmoid, + SShape, and ZShape. The `function` terms are Constant, Linear, and + Function. + + In the figure below, the `basic` terms are represented in the first + column, and the `extended` terms in the second and third columns. The + `edge` terms are represented in the fifth and sixth rows, and the + `function` terms in the last row. + + @image html terms.svg + + @author Juan Rada-Vilela, Ph.D. + @see Variable + @see InputVariable + @see OutputVariable + @since 4.0 + */ class FL_API Term { - protected: + private: std::string _name; + protected: scalar _height; public: @@ -47,25 +63,103 @@ namespace fl { virtual ~Term(); FL_DEFAULT_COPY_AND_MOVE(Term) + /** + Sets the name of the term + @param name is the name of term + */ virtual void setName(const std::string& name); + /** + Gets the name of the term + @return the name of the term + */ virtual std::string getName() const; + /** + Sets the height of the term + @param height is the height of the term + */ virtual void setHeight(scalar height); + /** + Gets the height of the term + @return the height of the term + */ virtual scalar getHeight() const; + /** + Returns the representation of the term in the FuzzyLite Language + @return the representation of the term in FuzzyLite Language + @see FllExporter + */ virtual std::string toString() const; + /** + Returns the name of the class of the term + @return the name of the class of the term + */ virtual std::string className() const = 0; + /** + Returns the parameters to configure the term. The parameters are + separated by spaces. If there is one additional parameter, the + parameter will be considered as the height of the term; otherwise, + the height will be set to @f$1.0@f$ + @return the parameters to configure the term (@see Term::configure()) + */ virtual std::string parameters() const = 0; + /** + Configures the term with the given parameters. The parameters are + separated by spaces. If there is one additional parameter, the + parameter will be considered as the height of the term; otherwise, + the height will be set to @f$1.0@f$ + @param parameters is the parameters to configure the term + */ virtual void configure(const std::string& parameters) = 0; + /** + Computes the estimated complexity of evaluating the membership function + @return the estimated complexity of evaluating the membership function + */ + virtual Complexity complexity() const = 0; + + /** + Computes the membership function value at @f$x@f$ + @param x + @return the membership function value @f$\mu(x)@f$ + */ virtual scalar membership(scalar x) const = 0; + /** + Creates a clone of the term + @return a clone of the term + */ virtual Term* clone() const = 0; - - //FIXME: This should not be static, and may not be here either. - static void updateReference(Term* term, const Engine* engine); - }; + /** + Updates the references (if any) to point to the current engine (useful + when cloning engines or creating terms within Importer objects + @param engine is the engine to which this term belongs to + */ + virtual void updateReference(const Engine* engine); + + /** + For monotonic terms, computes the tsukamoto value of the term for the + given activation degree @f$\alpha@f$, that is, + @f$ g_j(\alpha) = \{ z \in\mathbb{R} : \mu_j(z) = \alpha \} $@f. If + the term is not monotonic (or does not override this method) the + method computes the membership function @f$\mu(\alpha)@f$. + @param activationDegree is the activationDegree + @param minimum is the minimum value of the range of the term + @param maximum is the maximum value of the range of the term + @return the tsukamoto value of the term for the given activation degree + if the term is monotonic (or overrides this method), or + the membership function for the activation degree otherwise. + */ + virtual scalar tsukamoto(scalar activationDegree, scalar minimum, scalar maximum) const; + + /** + Indicates whether the term is monotonic. + @return whether the term is monotonic. + */ + virtual bool isMonotonic() const; + }; } #endif /* FL_TERM_H */ diff --git a/fuzzylite/fl/term/Trapezoid.h b/fuzzylite/fl/term/Trapezoid.h index f896997..ebf5ed8 100644 --- a/fuzzylite/fl/term/Trapezoid.h +++ b/fuzzylite/fl/term/Trapezoid.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_TRAPEZOID_H @@ -29,8 +21,19 @@ namespace fl { + /** + The Trapezoid class is a basic Term that represents the trapezoidal + membership function. + + @image html trapezoid.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Trapezoid : public Term { - protected: + private: scalar _vertexA, _vertexB, _vertexC, _vertexD; public: explicit Trapezoid(const std::string& name = "", @@ -43,27 +46,84 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Trapezoid) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"vertexA vertexB vertexC vertexD [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"vertexA vertexB vertexC vertexD [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \not\in[a,d]$}\cr + h \times \min(1, (x - a) / (b - a)) & \mbox{if $x < b$}\cr + 1h & \mbox{if $x \leq c$}\cr + h (d - x) / (d - c) & \mbox{if $x < d$}\cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$a@f$ is the first vertex of the Trapezoid, + @f$b@f$ is the second vertex of the Trapezoid, + @f$c@f$ is the third vertex of the Trapezoid, + @f$d@f$ is the fourth vertex of the Trapezoid + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the first vertex of the trapezoid + @param a is the first vertex of the trapezoid + */ virtual void setVertexA(scalar a); + /** + Gets the first vertex of the trapezoid + @return the first vertex of the trapezoid + */ virtual scalar getVertexA() const; + /** + Sets the second vertex of the trapezoid + @param b is the second vertex of the trapezoid + */ virtual void setVertexB(scalar b); + /** + Gets the second vertex of the trapezoid + @return the second vertex of the trapezoid + */ virtual scalar getVertexB() const; + /** + Sets the third vertex of the trapezoid + @param c is the third vertex of the trapezoid + */ virtual void setVertexC(scalar c); + /** + Gets the third vertex of the trapezoid + @return the third vertex of the trapezoid + */ virtual scalar getVertexC() const; + /** + Sets the fourth vertex of the trapezoid + @param d is the fourth vertex of the trapezoid + */ virtual void setVertexD(scalar d); + /** + Gets the fourth vertex of the trapezoid + @return the fourth vertex of the trapezoid + */ virtual scalar getVertexD() const; virtual Trapezoid* clone() const FL_IOVERRIDE; static Term* constructor(); }; - } #endif /* FL_TRAPEZOID_H */ diff --git a/fuzzylite/fl/term/Triangle.h b/fuzzylite/fl/term/Triangle.h index 9311687..8b16fa9 100644 --- a/fuzzylite/fl/term/Triangle.h +++ b/fuzzylite/fl/term/Triangle.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_TRIANGLE_H @@ -29,8 +21,19 @@ namespace fl { + /** + The Triangle class is a basic Term that represents the triangular + membership function. + + @image html triangle.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ class FL_API Triangle : public Term { - protected: + private: scalar _vertexA; scalar _vertexB; scalar _vertexC; @@ -44,18 +47,66 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(Triangle) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"vertexA vertexB vertexC [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"vertexA vertexB vertexC [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \not\in [a,c]$}\cr + 1h & \mbox{if $x = b$}\cr + h (x - a) / (b - a) & \mbox{if $x < b$} \cr + h (c - x) / (c - b) & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$a@f$ is the first vertex of the Triangle, + @f$b@f$ is the second vertex of the Triangle, + @f$c@f$ is the third vertex of the Triangle + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the first vertex of the triangle + @param a is the first vertex of the triangle + */ virtual void setVertexA(scalar a); + /** + Gets the first vertex of the triangle + @return the first vertex of the triangle + */ virtual scalar getVertexA() const; + /** + Sets the second vertex of the triangle + @param b is the second vertex of the triangle + */ virtual void setVertexB(scalar b); + /** + Gets the second vertex of the triangle + @return the second vertex of the triangle + */ virtual scalar getVertexB() const; + /** + Sets the third vertex of the triangle + @param c is the third vertex of the triangle + */ virtual void setVertexC(scalar c); + /** + Gets the third vertex of the triangle + @return the third vertex of the triangle + */ virtual scalar getVertexC() const; virtual Triangle* clone() const FL_IOVERRIDE; @@ -63,6 +114,5 @@ namespace fl { static Term* constructor(); }; - } #endif /* FL_TRIANGLE_H */ diff --git a/fuzzylite/fl/term/ZShape.h b/fuzzylite/fl/term/ZShape.h index b53906e..1737891 100644 --- a/fuzzylite/fl/term/ZShape.h +++ b/fuzzylite/fl/term/ZShape.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite™ is a trademark of FuzzyLite Limited. + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + fuzzylite is a registered trademark of FuzzyLite Limited. */ #ifndef FL_ZSHAPE_H @@ -29,8 +21,20 @@ namespace fl { + /** + The ZShape class is an edge Term that represents the Z-shaped membership + function. + + @image html zShape.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API ZShape : public Term { - protected: + private: scalar _start, _end; public: @@ -42,15 +46,59 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(ZShape) virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ virtual void configure(const std::string& parameters) FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ \begin{cases} + 1h & \mbox{if $x \leq s$} \cr + h(1 - 2\left((x - s) / (e-s)\right)^2) & \mbox{if $x \leq 0.5(s+e)$}\cr + h(2 \left((x - e) / (e-s)\right)^2) & \mbox{if $x < e$}\cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the ZShape, + @f$e@f$ is the end of the ZShape. + */ virtual scalar membership(scalar x) const FL_IOVERRIDE; + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the start of the edge + @param start is the start of the edge + */ virtual void setStart(scalar start); + /** + Gets the start of the edge + @return the start of the edge + */ virtual scalar getStart() const; + /** + Sets the end of the edge + @param end is the end of the edge + */ virtual void setEnd(scalar end); + /** + Gets the end of the edge + @return the end of the edge + */ virtual scalar getEnd() const; virtual ZShape* clone() const FL_IOVERRIDE; |