diff options
Diffstat (limited to 'fuzzylite/src/defuzzifier')
-rw-r--r-- | fuzzylite/src/defuzzifier/Bisector.cpp | 68 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/Centroid.cpp | 68 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp | 44 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/LargestOfMaximum.cpp | 65 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/MeanOfMaximum.cpp | 77 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp | 65 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/WeightedAverage.cpp | 100 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp | 117 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp | 76 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/WeightedSum.cpp | 97 | ||||
-rw-r--r-- | fuzzylite/src/defuzzifier/WeightedSumCustom.cpp | 112 |
11 files changed, 889 insertions, 0 deletions
diff --git a/fuzzylite/src/defuzzifier/Bisector.cpp b/fuzzylite/src/defuzzifier/Bisector.cpp new file mode 100644 index 0000000..ed61b6f --- /dev/null +++ b/fuzzylite/src/defuzzifier/Bisector.cpp @@ -0,0 +1,68 @@ +/* + 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. + */ + +#include "fl/defuzzifier/Bisector.h" + +#include "fl/term/Term.h" + +namespace fl { + + Bisector::Bisector(int resolution) + : IntegralDefuzzifier(resolution) { } + + Bisector::~Bisector() { } + + std::string Bisector::className() const { + return "Bisector"; + } + + Complexity Bisector::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2 + 5) + + term->complexity().comparison(1).arithmetic(1 + 5).multiply(getResolution()); + } + + scalar Bisector::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const scalar dx = (maximum - minimum) / getResolution(); + int counter = getResolution(); + int left = 0, right = 0; + scalar leftArea = 0, rightArea = 0; + scalar xLeft = minimum, xRight = maximum; + while (counter-- > 0) { + if (Op::isLE(leftArea, rightArea)) { + xLeft = minimum + (left + 0.5) * dx; + leftArea += term->membership(xLeft); + ++left; + } else { + xRight = maximum - (right + 0.5) * dx; + rightArea += term->membership(xRight); + ++right; + } + } + //Inverse weighted average to compensate + return (leftArea * xRight + rightArea * xLeft) / (leftArea + rightArea); + } + + Bisector* Bisector::clone() const { + return new Bisector(*this); + } + + Defuzzifier* Bisector::constructor() { + return new Bisector; + } + +} diff --git a/fuzzylite/src/defuzzifier/Centroid.cpp b/fuzzylite/src/defuzzifier/Centroid.cpp new file mode 100644 index 0000000..177da26 --- /dev/null +++ b/fuzzylite/src/defuzzifier/Centroid.cpp @@ -0,0 +1,68 @@ +/* + 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. + */ + +#include "fl/defuzzifier/Centroid.h" + +#include "fl/term/Term.h" + +namespace fl { + + Centroid::Centroid(int resolution) + : IntegralDefuzzifier(resolution) { } + + Centroid::~Centroid() { } + + std::string Centroid::className() const { + return "Centroid"; + } + + Complexity Centroid::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2 + 1) + + term->complexity().arithmetic(6).multiply(getResolution()); + } + + scalar Centroid::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar area = 0, xcentroid = 0; + //scalar ycentroid = 0; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + xcentroid += y * x; + //ycentroid += y * y; + area += y; + } + //Final results not computed for efficiency + //xcentroid /= area; + //ycentroid /= 2 * area; + //area *= dx; + return xcentroid / area; + } + + Centroid* Centroid::clone() const { + return new Centroid(*this); + } + + Defuzzifier* Centroid::constructor() { + return new Centroid; + } + +} diff --git a/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp b/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp new file mode 100644 index 0000000..3b06b42 --- /dev/null +++ b/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp @@ -0,0 +1,44 @@ +/* + 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. + */ + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +namespace fl { + + int IntegralDefuzzifier::_defaultResolution = 100; + + void IntegralDefuzzifier::setDefaultResolution(int defaultResolution) { + _defaultResolution = defaultResolution; + } + + int IntegralDefuzzifier::defaultResolution() { + return _defaultResolution; + } + + IntegralDefuzzifier::IntegralDefuzzifier(int resolution) + : Defuzzifier(), _resolution(resolution) { } + + IntegralDefuzzifier::~IntegralDefuzzifier() { } + + void IntegralDefuzzifier::setResolution(int resolution) { + this->_resolution = resolution; + } + + int IntegralDefuzzifier::getResolution() const { + return this->_resolution; + } + +} diff --git a/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp b/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp new file mode 100644 index 0000000..ff05707 --- /dev/null +++ b/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp @@ -0,0 +1,65 @@ +/* + 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. + */ + +#include "fl/defuzzifier/LargestOfMaximum.h" + +#include "fl/Exception.h" +#include "fl/term/Term.h" + +namespace fl { + + LargestOfMaximum::LargestOfMaximum(int resolution) + : IntegralDefuzzifier(resolution) { } + + LargestOfMaximum::~LargestOfMaximum() { } + + std::string LargestOfMaximum::className() const { + return "LargestOfMaximum"; + } + + Complexity LargestOfMaximum::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2) + + term->complexity().comparison(1).arithmetic(3).multiply(getResolution()); + } + + scalar LargestOfMaximum::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar ymax = -1.0, xlargest = maximum; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + if (Op::isGE(y, ymax)) { + ymax = y; + xlargest = x; + } + } + return xlargest; + } + + LargestOfMaximum* LargestOfMaximum::clone() const { + return new LargestOfMaximum(*this); + } + + Defuzzifier* LargestOfMaximum::constructor() { + return new LargestOfMaximum; + } + +} diff --git a/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp b/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp new file mode 100644 index 0000000..961e505 --- /dev/null +++ b/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp @@ -0,0 +1,77 @@ +/* + 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. + */ + +#include "fl/defuzzifier/MeanOfMaximum.h" + +#include "fl/Exception.h" +#include "fl/term/Term.h" + +namespace fl { + + MeanOfMaximum::MeanOfMaximum(int resolution) + : IntegralDefuzzifier(resolution) { } + + MeanOfMaximum::~MeanOfMaximum() { } + + std::string MeanOfMaximum::className() const { + return "MeanOfMaximum"; + } + + Complexity MeanOfMaximum::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2 + 2) + + term->complexity().comparison(4).arithmetic(3).multiply(getResolution()); + } + + scalar MeanOfMaximum::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar ymax = -1.0; + scalar xsmallest = minimum; + scalar xlargest = maximum; + bool samePlateau = false; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + if (Op::isGt(y, ymax)) { + ymax = y; + + xsmallest = x; + xlargest = x; + + samePlateau = true; + } else if (samePlateau and Op::isEq(y, ymax)) { + xlargest = x; + } else if (Op::isLt(y, ymax)) { + samePlateau = false; + } + } + + return 0.5 * (xlargest + xsmallest); + } + + MeanOfMaximum* MeanOfMaximum::clone() const { + return new MeanOfMaximum(*this); + } + + Defuzzifier* MeanOfMaximum::constructor() { + return new MeanOfMaximum; + } + +} diff --git a/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp b/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp new file mode 100644 index 0000000..7333702 --- /dev/null +++ b/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp @@ -0,0 +1,65 @@ +/* + 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. + */ + +#include "fl/defuzzifier/SmallestOfMaximum.h" + +#include "fl/Exception.h" +#include "fl/term/Term.h" + +namespace fl { + + SmallestOfMaximum::SmallestOfMaximum(int resolution) + : IntegralDefuzzifier(resolution) { } + + SmallestOfMaximum::~SmallestOfMaximum() { } + + std::string SmallestOfMaximum::className() const { + return "SmallestOfMaximum"; + } + + Complexity SmallestOfMaximum::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2) + + term->complexity().comparison(1).arithmetic(3).multiply(getResolution()); + } + + scalar SmallestOfMaximum::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar ymax = -1.0, xsmallest = minimum; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + if (Op::isGt(y, ymax)) { + xsmallest = x; + ymax = y; + } + } + return xsmallest; + } + + SmallestOfMaximum* SmallestOfMaximum::clone() const { + return new SmallestOfMaximum(*this); + } + + Defuzzifier* SmallestOfMaximum::constructor() { + return new SmallestOfMaximum; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedAverage.cpp b/fuzzylite/src/defuzzifier/WeightedAverage.cpp new file mode 100644 index 0000000..34af8c9 --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedAverage.cpp @@ -0,0 +1,100 @@ +/* + 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. + */ + +#include "fl/defuzzifier/WeightedAverage.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedAverage::WeightedAverage(Type type) : WeightedDefuzzifier(type) { } + + WeightedAverage::WeightedAverage(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedAverage::~WeightedAverage() { } + + std::string WeightedAverage::className() const { + return "WeightedAverage"; + } + + Complexity WeightedAverage::complexity(const Term* term) const { + Complexity result; + result.comparison(4).function(1); //for dynamic_cast + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(3).multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedAverage::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + scalar sum = 0.0; + scalar weights = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + sum += w * z; + weights += w; + } + } else { + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + sum += w * z; + weights += w; + } + } + return sum / weights; + } + + WeightedAverage* WeightedAverage::clone() const { + return new WeightedAverage(*this); + } + + Defuzzifier* WeightedAverage::constructor() { + return new WeightedAverage; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp b/fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp new file mode 100644 index 0000000..0beb722 --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp @@ -0,0 +1,117 @@ +/* + 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. + */ + +#include "fl/defuzzifier/WeightedAverageCustom.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedAverageCustom::WeightedAverageCustom(Type type) : WeightedDefuzzifier(type) { } + + WeightedAverageCustom::WeightedAverageCustom(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedAverageCustom::~WeightedAverageCustom() { } + + std::string WeightedAverageCustom::className() const { + return "WeightedAverageCustom"; + } + + Complexity WeightedAverageCustom::complexity(const Term* term) const { + Complexity result; + result.comparison(3).arithmetic(1).function(1); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(3).comparison(2) + .multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedAverageCustom::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + SNorm* aggregation = fuzzyOutput->getAggregation(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + scalar sum = 0.0; + scalar weights = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + weights = aggregation->compute(weights, w); + } else { + sum += wz; + weights += w; + } + } + } else { + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + weights = aggregation->compute(weights, w); + } else { + sum += wz; + weights += w; + } + } + } + return sum / weights; + } + + WeightedAverageCustom* WeightedAverageCustom::clone() const { + return new WeightedAverageCustom(*this); + } + + Defuzzifier* WeightedAverageCustom::constructor() { + return new WeightedAverageCustom; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp b/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp new file mode 100644 index 0000000..743c59f --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp @@ -0,0 +1,76 @@ +/* + 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. + */ + +#include "fl/defuzzifier/WeightedDefuzzifier.h" + +#include "fl/term/Activated.h" +#include "fl/term/Concave.h" +#include "fl/term/Constant.h" +#include "fl/term/Function.h" +#include "fl/term/Linear.h" +#include "fl/term/Ramp.h" +#include "fl/term/Sigmoid.h" +#include "fl/term/SShape.h" +#include "fl/term/ZShape.h" + +namespace fl { + + WeightedDefuzzifier::WeightedDefuzzifier(Type type) : _type(type) { } + + WeightedDefuzzifier::WeightedDefuzzifier(const std::string& type) { + if (type == "Automatic") setType(Automatic); + else if (type == "TakagiSugeno") setType(TakagiSugeno); + else if (type == "Tsukamoto") setType(Tsukamoto); + else { + setType(Automatic); + FL_LOG("[warning] incorrect type <" + type + "> of WeightedDefuzzifier" + + " has been defaulted to <Automatic>"); + } + } + + WeightedDefuzzifier::~WeightedDefuzzifier() { } + + std::string WeightedDefuzzifier::typeName(Type type) { + switch (type) { + case Automatic: return "Automatic"; + case TakagiSugeno: return "TakagiSugeno"; + case Tsukamoto: return "Tsukamoto"; + default: return ""; + } + } + + void WeightedDefuzzifier::setType(Type type) { + this->_type = type; + } + + WeightedDefuzzifier::Type WeightedDefuzzifier::getType() const { + return this->_type; + } + + std::string WeightedDefuzzifier::getTypeName() const { + return typeName(getType()); + } + + WeightedDefuzzifier::Type WeightedDefuzzifier::inferType(const Term* term) const { + if (dynamic_cast<const Constant*> (term) + or dynamic_cast<const Linear*> (term) + or dynamic_cast<const Function*> (term)) { + return TakagiSugeno; + } + return Tsukamoto; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedSum.cpp b/fuzzylite/src/defuzzifier/WeightedSum.cpp new file mode 100644 index 0000000..6c2343d --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedSum.cpp @@ -0,0 +1,97 @@ +/* + 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. + */ + +#include "fl/defuzzifier/WeightedSum.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedSum::WeightedSum(Type type) : WeightedDefuzzifier(type) { } + + WeightedSum::WeightedSum(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedSum::~WeightedSum() { } + + std::string WeightedSum::className() const { + return "WeightedSum"; + } + + Complexity WeightedSum::complexity(const Term* term) const { + Complexity result; + result.comparison(4).function(1); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(2).multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedSum::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + scalar sum = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + sum += w * z; + } + } else { + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + sum += w * z; + } + } + return sum; + } + + WeightedSum* WeightedSum::clone() const { + return new WeightedSum(*this); + } + + Defuzzifier* WeightedSum::constructor() { + return new WeightedSum; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedSumCustom.cpp b/fuzzylite/src/defuzzifier/WeightedSumCustom.cpp new file mode 100644 index 0000000..5a9084c --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedSumCustom.cpp @@ -0,0 +1,112 @@ +/* + 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. + */ + +#include "fl/defuzzifier/WeightedSumCustom.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedSumCustom::WeightedSumCustom(Type type) : WeightedDefuzzifier(type) { } + + WeightedSumCustom::WeightedSumCustom(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedSumCustom::~WeightedSumCustom() { } + + std::string WeightedSumCustom::className() const { + return "WeightedSumCustom"; + } + + Complexity WeightedSumCustom::complexity(const Term* term) const { + Complexity result; + result.comparison(4).function(1); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(2).comparison(2) + .multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedSumCustom::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + SNorm* aggregation = fuzzyOutput->getAggregation(); + + scalar sum = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + } else { + sum += wz; + } + } + } else { + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + } else { + sum += wz; + } + } + } + return sum; + } + + WeightedSumCustom* WeightedSumCustom::clone() const { + return new WeightedSumCustom(*this); + } + + Defuzzifier* WeightedSumCustom::constructor() { + return new WeightedSumCustom; + } + +} |