summaryrefslogtreecommitdiff
path: root/fuzzylite/fl/term/Aggregated.h
blob: af08c754f90d03bca427c648ac09a32e0f42669c (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
/*
 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 */