summaryrefslogtreecommitdiff
path: root/fuzzylite/src/variable
diff options
context:
space:
mode:
Diffstat (limited to 'fuzzylite/src/variable')
-rw-r--r--fuzzylite/src/variable/InputVariable.cpp54
-rw-r--r--fuzzylite/src/variable/OutputVariable.cpp195
-rw-r--r--fuzzylite/src/variable/Variable.cpp244
3 files changed, 493 insertions, 0 deletions
diff --git a/fuzzylite/src/variable/InputVariable.cpp b/fuzzylite/src/variable/InputVariable.cpp
new file mode 100644
index 0000000..9fa2932
--- /dev/null
+++ b/fuzzylite/src/variable/InputVariable.cpp
@@ -0,0 +1,54 @@
+/*
+ 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.
+
+ */
+
+#include "fl/variable/InputVariable.h"
+
+#include "fl/imex/FllExporter.h"
+
+namespace fl {
+
+ InputVariable::InputVariable(const std::string& name, scalar minimum, scalar maximum)
+ : Variable(name, minimum, maximum), _inputValue(fl::nan) {
+ }
+
+ InputVariable::~InputVariable() {
+ }
+
+ void InputVariable::setInputValue(scalar inputValue) {
+ this->_inputValue = inputValue;
+ }
+
+ scalar InputVariable::getInputValue() const {
+ return this->_inputValue;
+ }
+
+ std::string InputVariable::fuzzyInputValue() const {
+ return fuzzify(_inputValue);
+ }
+
+ std::string InputVariable::toString() const {
+ return FllExporter().toString(this);
+ }
+
+}
diff --git a/fuzzylite/src/variable/OutputVariable.cpp b/fuzzylite/src/variable/OutputVariable.cpp
new file mode 100644
index 0000000..6e1d906
--- /dev/null
+++ b/fuzzylite/src/variable/OutputVariable.cpp
@@ -0,0 +1,195 @@
+/*
+ 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.
+
+ */
+
+#include "fl/variable/OutputVariable.h"
+
+#include "fl/defuzzifier/Defuzzifier.h"
+#include "fl/imex/FllExporter.h"
+#include "fl/norm/SNorm.h"
+#include "fl/term/Accumulated.h"
+#include "fl/term/Activated.h"
+
+namespace fl {
+
+ OutputVariable::OutputVariable(const std::string& name,
+ scalar minimum, scalar maximum)
+ : Variable(name, minimum, maximum),
+ _fuzzyOutput(new Accumulated(name, minimum, maximum)), _outputValue(fl::nan),
+ _previousOutputValue(fl::nan), _defaultValue(fl::nan),
+ _lockOutputValueInRange(false), _lockPreviousOutputValue(false) {
+ }
+
+ OutputVariable::OutputVariable(const OutputVariable& other) : Variable(other) {
+ copyFrom(other);
+ }
+
+ OutputVariable& OutputVariable::operator=(const OutputVariable& other) {
+ if (this != &other) {
+ _fuzzyOutput.reset(fl::null);
+ _defuzzifier.reset(fl::null);
+
+ Variable::operator=(other);
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ OutputVariable::~OutputVariable() {
+ }
+
+ void OutputVariable::copyFrom(const OutputVariable& other) {
+ _fuzzyOutput.reset(other._fuzzyOutput->clone());
+ if (other._defuzzifier.get()) _defuzzifier.reset(other._defuzzifier->clone());
+ _outputValue = other._outputValue;
+ _previousOutputValue = other._previousOutputValue;
+ _defaultValue = other._defaultValue;
+ _lockOutputValueInRange = other._lockOutputValueInRange;
+ _lockPreviousOutputValue = other._lockPreviousOutputValue;
+ }
+
+ void OutputVariable::setName(const std::string& name) {
+ Variable::setName(name);
+ this->_fuzzyOutput->setName(name);
+ }
+
+ Accumulated* OutputVariable::fuzzyOutput() const {
+ return this->_fuzzyOutput.get();
+ }
+
+ void OutputVariable::setMinimum(scalar minimum) {
+ Variable::setMinimum(minimum);
+ this->_fuzzyOutput->setMinimum(minimum);
+ }
+
+ void OutputVariable::setMaximum(scalar maximum) {
+ Variable::setMaximum(maximum);
+ this->_fuzzyOutput->setMaximum(maximum);
+ }
+
+ void OutputVariable::setDefuzzifier(Defuzzifier* defuzzifier) {
+ this->_defuzzifier.reset(defuzzifier);
+ }
+
+ Defuzzifier* OutputVariable::getDefuzzifier() const {
+ return this->_defuzzifier.get();
+ }
+
+ void OutputVariable::setOutputValue(scalar outputValue) {
+ this->_outputValue = outputValue;
+ }
+
+ scalar OutputVariable::getOutputValue() const {
+ return this->_outputValue;
+ }
+
+ void OutputVariable::setPreviousOutputValue(scalar previousOutputValue) {
+ this->_previousOutputValue = previousOutputValue;
+ }
+
+ scalar OutputVariable::getPreviousOutputValue() const {
+ return this->_previousOutputValue;
+ }
+
+ void OutputVariable::setDefaultValue(scalar defaultValue) {
+ this->_defaultValue = defaultValue;
+ }
+
+ scalar OutputVariable::getDefaultValue() const {
+ return this->_defaultValue;
+ }
+
+ void OutputVariable::setLockOutputValueInRange(bool lockOutputValueInRange) {
+ this->_lockOutputValueInRange = lockOutputValueInRange;
+ }
+
+ bool OutputVariable::isLockedOutputValueInRange() const {
+ return this->_lockOutputValueInRange;
+ }
+
+ void OutputVariable::setLockPreviousOutputValue(bool lockPreviousOutputValue) {
+ this->_lockPreviousOutputValue = lockPreviousOutputValue;
+ }
+
+ bool OutputVariable::isLockedPreviousOutputValue() const {
+ return this->_lockPreviousOutputValue;
+ }
+
+ void OutputVariable::defuzzify() {
+ if (fl::Op::isFinite(this->_outputValue)) {
+ this->_previousOutputValue = this->_outputValue;
+ }
+
+ scalar result = fl::nan;
+ bool isValid = this->_enabled and not this->_fuzzyOutput->isEmpty();
+ if (isValid) {
+ if (not _defuzzifier.get()) {
+ throw fl::Exception("[defuzzifier error] "
+ "defuzzifier needed to defuzzify output variable <" + _name + ">", FL_AT);
+ }
+ result = this->_defuzzifier->defuzzify(this->_fuzzyOutput.get(), _minimum, _maximum);
+ } else {
+ //if a previous defuzzification was successfully performed and
+ //and the output value is supposed not to change when the output is empty
+ if (_lockPreviousOutputValue and not Op::isNaN(_previousOutputValue)) {
+ result = _previousOutputValue;
+ } else {
+ result = _defaultValue;
+ }
+ }
+
+ if (_lockOutputValueInRange) {
+ result = fl::Op::bound(result, _minimum, _maximum);
+ }
+
+ this->_outputValue = result;
+ }
+
+ std::string OutputVariable::fuzzyOutputValue() const {
+ std::ostringstream ss;
+ for (std::size_t i = 0; i < _terms.size(); ++i) {
+ scalar degree = _fuzzyOutput->activationDegree(_terms.at(i));
+ if (i == 0) {
+ ss << fl::Op::str(degree);
+ } else {
+ if (fl::Op::isNaN(degree) or fl::Op::isGE(degree, 0.0))
+ ss << " + " << fl::Op::str(degree);
+ else
+ ss << " - " << fl::Op::str(std::fabs(degree));
+ }
+ ss << "/" << _terms.at(i)->getName();
+ }
+ return ss.str();
+ }
+
+ void OutputVariable::clear() {
+ _fuzzyOutput->clear();
+ setPreviousOutputValue(fl::nan);
+ setOutputValue(fl::nan);
+ }
+
+ std::string OutputVariable::toString() const {
+ return FllExporter().toString(this);
+ }
+
+}
diff --git a/fuzzylite/src/variable/Variable.cpp b/fuzzylite/src/variable/Variable.cpp
new file mode 100644
index 0000000..37cf25c
--- /dev/null
+++ b/fuzzylite/src/variable/Variable.cpp
@@ -0,0 +1,244 @@
+/*
+ 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.
+
+ */
+
+#include "fl/variable/Variable.h"
+
+#include "fl/defuzzifier/Centroid.h"
+#include "fl/imex/FllExporter.h"
+#include "fl/norm/Norm.h"
+#include "fl/term/Constant.h"
+#include "fl/term/Linear.h"
+#include "fl/term/Term.h"
+
+#include <algorithm>
+#include <map>
+#include <sstream>
+
+namespace fl {
+
+ Variable::Variable(const std::string& name, scalar minimum, scalar maximum)
+ : _name(name), _minimum(minimum), _maximum(maximum), _enabled(true) {
+ }
+
+ Variable::Variable(const Variable& other) {
+ copyFrom(other);
+ }
+
+ Variable& Variable::operator=(const Variable& other) {
+ if (this != &other) {
+ for (std::size_t i = 0; i < _terms.size(); ++i) {
+ delete _terms.at(i);
+ }
+ _terms.clear();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ void Variable::copyFrom(const Variable& other) {
+ _name = other._name;
+ _enabled = other._enabled;
+ _minimum = other._minimum;
+ _maximum = other._maximum;
+ for (std::size_t i = 0; i < other._terms.size(); ++i) {
+ _terms.push_back(other._terms.at(i)->clone());
+ }
+ }
+
+ Variable::~Variable() {
+ for (std::size_t i = 0; i < _terms.size(); ++i) {
+ delete _terms.at(i);
+ }
+ }
+
+ void Variable::setName(const std::string& name) {
+ this->_name = name;
+ }
+
+ std::string Variable::getName() const {
+ return this->_name;
+ }
+
+ void Variable::setRange(scalar minimum, scalar maximum) {
+ setMinimum(minimum);
+ setMaximum(maximum);
+ }
+
+ scalar Variable::range() const {
+ return this->_maximum - this->_minimum;
+ }
+
+ void Variable::setMinimum(scalar minimum) {
+ this->_minimum = minimum;
+ }
+
+ scalar Variable::getMinimum() const {
+ return this->_minimum;
+ }
+
+ void Variable::setMaximum(scalar maximum) {
+ this->_maximum = maximum;
+ }
+
+ scalar Variable::getMaximum() const {
+ return this->_maximum;
+ }
+
+ void Variable::setEnabled(bool enabled) {
+ this->_enabled = enabled;
+ }
+
+ bool Variable::isEnabled() const {
+ return this->_enabled;
+ }
+
+ std::string Variable::fuzzify(scalar x) const {
+ std::ostringstream ss;
+ for (std::size_t i = 0; i < _terms.size(); ++i) {
+ scalar fx = fl::nan;
+ try {
+ fx = _terms.at(i)->membership(x);
+ } catch (...) {
+ //ignore
+ }
+ if (i == 0) {
+ ss << fl::Op::str(fx);
+ } else {
+ if (fl::Op::isNaN(fx) or fl::Op::isGE(fx, 0.0))
+ ss << " + " << fl::Op::str(fx);
+ else
+ ss << " - " << fl::Op::str(std::fabs(fx));
+ }
+ ss << "/" << _terms.at(i)->getName();
+ }
+ return ss.str();
+ }
+
+ Term* Variable::highestMembership(scalar x, scalar* yhighest) const {
+ Term* result = fl::null;
+ scalar ymax = 0.0;
+ for (std::size_t i = 0; i < _terms.size(); ++i) {
+ scalar y = fl::nan;
+ try {
+ y = _terms.at(i)->membership(x);
+ } catch (...) {
+ //ignore
+ }
+ if (fl::Op::isGt(y, ymax)) {
+ ymax = y;
+ result = _terms.at(i);
+ }
+ }
+ if (yhighest) *yhighest = ymax;
+ return result;
+ }
+
+ std::string Variable::toString() const {
+ return FllExporter().toString(this);
+ }
+
+ /**
+ * Operations for datatype _terms
+ */
+
+ struct SortByCoG {
+ std::map<const Term*, scalar> centroids;
+
+ bool operator()(const Term* a, const Term * b) {
+ return fl::Op::isLt(
+ centroids.find(a)->second,
+ centroids.find(b)->second);
+ }
+ };
+
+ void Variable::sort() {
+ Centroid defuzzifier;
+ std::map<const Term*, scalar> centroids;
+ for (std::size_t i = 0; i < _terms.size(); ++i) {
+ Term* term = _terms.at(i);
+ try {
+ if (dynamic_cast<const Constant*> (term) or dynamic_cast<const Linear*> (term)) {
+ centroids[term] = term->membership(0);
+ } else {
+ centroids[term] = defuzzifier.defuzzify(term, _minimum, _maximum);
+ }
+ } catch (...) { //ignore error possibly due to Function not loaded
+ centroids[term] = fl::inf;
+ }
+ }
+ SortByCoG criterion;
+ criterion.centroids = centroids;
+ std::sort(_terms.begin(), _terms.end(), criterion);
+ }
+
+ void Variable::addTerm(Term* term) {
+ this->_terms.push_back(term);
+ }
+
+ void Variable::insertTerm(Term* term, int index) {
+ this->_terms.insert(this->_terms.begin() + index, term);
+ }
+
+ Term* Variable::getTerm(int index) const {
+ return this->_terms.at(index);
+ }
+
+ Term* Variable::getTerm(const std::string& name) const {
+ for (std::size_t i = 0; i < _terms.size(); ++i) {
+ if (_terms.at(i)->getName() == name) {
+ return _terms.at(i);
+ }
+ }
+ throw fl::Exception("[variable error] term <" + name + "> "
+ "not found in variable <" + this->_name + ">", FL_AT);
+ }
+
+ bool Variable::hasTerm(const std::string& name) const {
+ return getTerm(name) != fl::null;
+ }
+
+ Term* Variable::removeTerm(int index) {
+ Term* result = this->_terms.at(index);
+ this->_terms.erase(this->_terms.begin() + index);
+ return result;
+ }
+
+ int Variable::numberOfTerms() const {
+ return this->_terms.size();
+ }
+
+ const std::vector<Term*>& Variable::terms() const {
+ return this->_terms;
+ }
+
+ void Variable::setTerms(const std::vector<Term*>& terms) {
+ this->_terms = terms;
+ }
+
+ std::vector<Term*>& Variable::terms() {
+ return this->_terms;
+ }
+
+}
+