summaryrefslogtreecommitdiff
path: root/fuzzylite/src/term/Accumulated.cpp
blob: 979af9f1f6ca5311b222dd0aa9bd6e991653848d (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
/*
 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/term/Accumulated.h"

#include "fl/imex/FllExporter.h"
#include "fl/norm/SNorm.h"
#include "fl/norm/s/Maximum.h"
#include "fl/term/Activated.h"


namespace fl {

    Accumulated::Accumulated(const std::string& name, scalar minimum, scalar maximum,
            SNorm* accumulation)
    : Term(name), _minimum(minimum), _maximum(maximum), _accumulation(accumulation) {
    }

    Accumulated::Accumulated(const Accumulated& other) : Term(other) {
        copyFrom(other);
    }

    Accumulated& Accumulated::operator=(const Accumulated& other) {
        if (this != &other) {
            clear();
            _accumulation.reset(fl::null);

            Term::operator=(other);
            copyFrom(other);
        }
        return *this;
    }

    Accumulated::~Accumulated() {
        clear();
    }

    void Accumulated::copyFrom(const Accumulated& source) {
        _minimum = source._minimum;
        _maximum = source._maximum;

        if (source._accumulation.get())
            _accumulation.reset(source._accumulation->clone());

        for (std::size_t i = 0; i < source._terms.size(); ++i) {
            _terms.push_back(source._terms.at(i)->clone());
        }
    }

    std::string Accumulated::className() const {
        return "Accumulated";
    }

    scalar Accumulated::membership(scalar x) const {
        if (fl::Op::isNaN(x)) return fl::nan;
        if (not (_terms.empty() or _accumulation.get())) { //Exception for IntegralDefuzzifiers
            throw fl::Exception("[accumulation error] "
                    "accumulation operator needed to accumulate " + toString(), FL_AT);
        }
        scalar mu = 0.0;
        for (std::size_t i = 0; i < _terms.size(); ++i) {
            mu = _accumulation->compute(mu, _terms.at(i)->membership(x));
        }
        return mu;
    }

    scalar Accumulated::activationDegree(const Term* forTerm) const {
        scalar result = 0.0;
        for (std::size_t i = 0; i < _terms.size(); ++i) {
            Activated* activatedTerm = _terms.at(i);
            if (activatedTerm->getTerm() == forTerm) {
                if (_accumulation.get()) result = _accumulation->compute(result, activatedTerm->getDegree());
                else result += activatedTerm->getDegree(); //Default for WeightDefuzzifier
            }
        }
        return result;
    }

    std::string Accumulated::parameters() const {
        FllExporter exporter;
        std::ostringstream ss;
        ss << exporter.toString(_accumulation.get());
        ss << " " << Op::str(_minimum) << " " << Op::str(_maximum) << " ";
        for (std::size_t i = 0; i < _terms.size(); ++i) {
            ss << " " << exporter.toString(_terms.at(i));
        }
        return ss.str();
    }

    void Accumulated::configure(const std::string& parameters) {
        (void) parameters;
    }

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

    std::string Accumulated::toString() const {
        std::vector<std::string> accumulate;
        for (std::size_t i = 0; i < _terms.size(); ++i) {
            accumulate.push_back(_terms.at(i)->toString());
        }
        FllExporter exporter;
        std::ostringstream ss;
        ss << _name << ": " << className() << " "
                << exporter.toString(_accumulation.get()) << "["
                << fl::Op::join(accumulate, ",") << "]";
        return ss.str();
    }

    void Accumulated::setMinimum(scalar minimum) {
        this->_minimum = minimum;
    }

    scalar Accumulated::getMinimum() const {
        return this->_minimum;
    }

    void Accumulated::setMaximum(scalar maximum) {
        this->_maximum = maximum;
    }

    scalar Accumulated::getMaximum() const {
        return this->_maximum;
    }

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

    scalar Accumulated::range() const {
        return this->_maximum - this->_minimum;
    }

    void Accumulated::setAccumulation(SNorm* accumulation) {
        this->_accumulation.reset(accumulation);
    }

    SNorm* Accumulated::getAccumulation() const {
        return this->_accumulation.get();
    }

    /**
     * Operations for std::vector _terms
     */


    void Accumulated::addTerm(const Term* term, scalar degree, const TNorm* activation) {
        this->_terms.push_back(new Activated(term, degree, activation));
    }

    void Accumulated::addTerm(Activated* term) {
        this->_terms.push_back(term);
    }

    Activated* Accumulated::removeTerm(int index) {
        Activated* term = this->_terms.at(index);
        this->_terms.erase(this->_terms.begin() + index);
        return term;
    }

    void Accumulated::clear() {
        for (std::size_t i = 0; i < _terms.size(); ++i) {
            delete _terms.at(i);
        }
        _terms.clear();
    }

    Activated* Accumulated::getTerm(int index) const {
        return this->_terms.at(index);
    }

    const std::vector<Activated*>& Accumulated::terms() const {
        return this->_terms;
    }

    std::vector<Activated*>& Accumulated::terms() {
        return this->_terms;
    }

    int Accumulated::numberOfTerms() const {
        return _terms.size();
    }

    bool Accumulated::isEmpty() const {
        return _terms.empty();
    }

}