summaryrefslogtreecommitdiff
path: root/fuzzylite/src/variable/Variable.cpp
blob: 516b466952c12c6751a5f550b04a16cd21d34917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 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/variable/Variable.h"

#include "fl/imex/FllExporter.h"
#include "fl/norm/Norm.h"
#include "fl/term/Constant.h"
#include "fl/term/Linear.h"

#include <queue>

namespace fl {

    Variable::Variable(const std::string& name, scalar minimum, scalar maximum)
    : _name(name), _description(""),
    _value(fl::nan), _minimum(minimum), _maximum(maximum),
    _enabled(true), _lockValueInRange(false) { }

    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;
        _description = other._description;
        _value = other._value;
        _minimum = other._minimum;
        _maximum = other._maximum;
        _enabled = other._enabled;
        _lockValueInRange = other._lockValueInRange;
        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::setDescription(const std::string& description) {
        this->_description = description;
    }

    std::string Variable::getDescription() const {
        return this->_description;
    }

    void Variable::setValue(scalar value) {
        this->_value = _lockValueInRange
                ? Op::bound(value, _minimum, _maximum)
                : value;
    }

    scalar Variable::getValue() const {
        return this->_value;
    }

    void Variable::setRange(scalar minimum, scalar maximum) {
        setMinimum(minimum);
        setMaximum(maximum);
    }

    scalar Variable::range() const {
        return getMaximum() - getMinimum();
    }

    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;
    }

    void Variable::setLockValueInRange(bool lockValueInRange) {
        this->_lockValueInRange = lockValueInRange;
    }

    bool Variable::isLockValueInRange() const {
        return this->_lockValueInRange;
    }

    Variable::Type Variable::type() const {
        return None;
    }

    Complexity Variable::complexity() const {
        Complexity result;
        if (isEnabled()) {
            for (std::size_t i = 0; i < _terms.size(); ++i) {
                result += _terms.at(i)->complexity();
            }
        }
        return result;
    }

    std::string Variable::fuzzify(scalar x) const {
        std::ostringstream ss;
        for (std::size_t i = 0; i < terms().size(); ++i) {
            Term* term = _terms.at(i);
            scalar fx = fl::nan;
            try {
                fx = term->membership(x);
            } catch (...) {
                //ignore
            }
            if (i == 0) {
                ss << Op::str(fx);
            } else {
                if (Op::isNaN(fx) or Op::isGE(fx, 0.0))
                    ss << " + " << Op::str(fx);
                else
                    ss << " - " << Op::str(std::abs(fx));
            }
            ss << "/" << term->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;
            Term* term = _terms.at(i);
            try {
                y = term->membership(x);
            } catch (...) {
                //ignore
            }
            if (Op::isGt(y, ymax)) {
                ymax = y;
                result = term;
            }
        }
        if (yhighest) *yhighest = ymax;
        return result;
    }

    std::string Variable::toString() const {
        return FllExporter().toString(this);
    }

    /**
     * Operations for datatype _terms
     */

    typedef std::pair<Term*, scalar> TermCentroid;

    struct Ascending {

        bool operator()(const TermCentroid& a, const TermCentroid& b) const {
            return a.second > b.second;
        }
    };

    void Variable::sort() {
        std::priority_queue <TermCentroid, std::vector<TermCentroid>, Ascending> termCentroids;
        Centroid defuzzifier;
        FL_DBG("Sorting...");
        for (std::size_t i = 0; i < _terms.size(); ++i) {
            Term* term = _terms.at(i);
            scalar centroid = fl::inf;
            try {
                if (dynamic_cast<const Constant*> (term) or dynamic_cast<const Linear*> (term)) {
                    centroid = term->membership(0);
                } else {
                    centroid = defuzzifier.defuzzify(term, getMinimum(), getMaximum());
                }
            } catch (...) { //ignore error possibly due to Function not loaded
                centroid = fl::inf;
            }
            termCentroids.push(TermCentroid(term, centroid));
            FL_DBG(term->toString() << " -> " << centroid)
        }

        std::vector<Term*> sortedTerms;
        while (termCentroids.size() > 0) {
            sortedTerms.push_back(termCentroids.top().first);
            FL_DBG(termCentroids.top().first->toString() << " -> " << termCentroids.top().second);
            termCentroids.pop();
        }
        setTerms(sortedTerms);
    }

    void Variable::addTerm(Term* term) {
        _terms.push_back(term);
    }

    void Variable::insertTerm(Term* term, std::size_t index) {
        _terms.insert(_terms.begin() + index, term);
    }

    Term* Variable::getTerm(std::size_t index) const {
        return _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 Exception("[variable error] term <" + name + "> "
                "not found in variable <" + getName() + ">", FL_AT);
    }

    bool Variable::hasTerm(const std::string& name) const {
        for (std::size_t i = 0; i < _terms.size(); ++i) {
            if (_terms.at(i)->getName() == name) {
                return true;
            }
        }
        return false;
    }

    Term* Variable::removeTerm(std::size_t index) {
        Term* result = _terms.at(index);
        _terms.erase(_terms.begin() + index);
        return result;
    }

    std::size_t Variable::numberOfTerms() const {
        return _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;
    }

    Variable* Variable::clone() const {
        return new Variable(*this);
    }

}