1 /*
2  fuzzylite (R), a fuzzy logic control library in C++.
3  Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved.
4  Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com>
5 
6  This file is part of fuzzylite.
7 
8  fuzzylite is free software: you can redistribute it and/or modify it under
9  the terms of the FuzzyLite License included with the software.
10 
11  You should have received a copy of the FuzzyLite License along with
12  fuzzylite. If not, see <http://www.fuzzylite.com/license/>.
13 
14  fuzzylite is a registered trademark of FuzzyLite Limited.
15  */
16 
17 #ifndef FL_COMPLEXITY_H
18 #define FL_COMPLEXITY_H
19 
20 #include "fl/fuzzylite.h"
21 
22 #include <vector>
23 
24 namespace fl {
25     class Engine;
26     class InputVariable;
27     class OutputVariable;
28     class Variable;
29     class RuleBlock;
30     class Rule;
31 
32     /**
33       The Complexity class is used throughout the library to estimate the
34       computational cost of the different components of the library
35 
36       @author Juan Rada-Vilela, Ph.D.
37       @see Engine
38       @see Variable
39       @see OutputVariable
40       @see RuleBlock
41       @see Activation
42       @see Rule
43       @see Antecedent
44       @see Consequent
45       @see Hedge
46       @see Norm
47       @since 6.0
48      */
49 
50     class FL_API Complexity {
51     private:
52         scalar _comparison;
53         scalar _arithmetic;
54         scalar _function;
55 
56     public:
57         explicit Complexity(scalar all = 0.0);
58         explicit Complexity(scalar comparison, scalar arithmetic, scalar function);
59         virtual ~Complexity();
60         FL_DEFAULT_COPY_AND_MOVE(Complexity)
61 
62         Complexity& operator+=(const Complexity& other);
63         Complexity& operator-=(const Complexity& other);
64         Complexity& operator*=(const Complexity& other);
65         Complexity& operator/=(const Complexity& other);
66 
67         Complexity operator+(const Complexity& rhs) const;
68         Complexity operator-(const Complexity& rhs) const;
69         Complexity operator*(const Complexity& rhs) const;
70         Complexity operator/(const Complexity& rhs) const;
71 
72         bool operator==(const Complexity& rhs) const;
73         bool operator!=(const Complexity& rhs) const;
74         bool operator<(const Complexity& rhs) const;
75         bool operator<=(const Complexity& rhs) const;
76         bool operator>(const Complexity& rhs) const;
77         bool operator>=(const Complexity& rhs) const;
78 
79         /**
80          Increases the comparison measure by the given amount
81          @param comparison is the amount to increase the comparison measure by
82          @return the reference to the Complexity object with the updated comparison
83          measure
84          */
85         virtual Complexity& comparison(scalar comparison);
86         virtual void setComparison(scalar comparison);
87         virtual scalar getComparison() const;
88 
89         /**
90          Increases the arithmetic measure by the given amount
91          @param arithmetic is the amount to increase the comparison measure by
92          @return the reference to the Complexity object with the updated arithmetic
93          measure
94          */
95         virtual Complexity& arithmetic(scalar arithmetic);
96         virtual void setArithmetic(scalar arithmetic);
97         virtual scalar getArithmetic() const;
98 
99         /**
100          Increases the function measure by the given amount
101          @param function is the amount to increase the function measure by
102          @return the reference to the Complexity object with the updated function
103          measure
104          */
105         virtual Complexity& function(scalar function);
106         virtual void setFunction(scalar function);
107         virtual scalar getFunction() const;
108 
109         /**
110          Returns a vector containing the measures of complexity
111          @return a vector containing the measures of complexity
112          */
113         typedef std::pair<std::string, scalar> Measure;
114         virtual std::vector<Measure> measures() const;
115 
116         /**
117          Increases the complexity by the given parameter
118          @param x is the addend
119          @return the reference to the updated complexity
120          */
121         virtual Complexity& plus(const Complexity& x);
122         /**
123          Reduces the complexity by the given parameter
124          @param x is the subtrahend
125          @return the reference to the updated complexity object
126          */
127         virtual Complexity& minus(const Complexity& x);
128         /**
129          Multiplies the complexity by the given parameter
130          @param x is the multiplicand
131          @return the reference to the updated complexity object
132          */
133         virtual Complexity& multiply(const Complexity& x);
134         /**
135          Divides the complexity by the given parameter
136          @param x is the divisor
137          @return the reference to the updated complexity object
138          */
139         virtual Complexity& divide(const Complexity& x);
140 
141         /**
142          Increases each measure by the given parameter
143          @param x is the addend
144          @return the reference to the updated complexity
145          */
146         virtual Complexity& plus(scalar x);
147         /**
148          Reduces each measure by the given parameter
149          @param x is the subtrahend
150          @return the reference to the updated complexity
151          */
152         virtual Complexity& minus(scalar x);
153         /**
154          Multiplies each measure by the given parameter
155          @param x is the multiplicand
156          @return the reference to the updated complexity
157          */
158         virtual Complexity& multiply(scalar x);
159         /**
160          Divides each measure by the given parameter
161          @param x is the divisor
162          @return the reference to the updated complexity
163          */
164         virtual Complexity& divide(scalar x);
165 
166         /**
167          Compares the complexity for equality to another with the given tolerance
168          @param x is the complexity to compare against
169          @param macheps is the tolerance to compare floating-point values
170          @return `true` if every measure in this satisfies Op::isEq(this, x, macheps),
171          and `false` otherwise
172          */
173         virtual bool equals(const Complexity& x, scalar macheps = fuzzylite::macheps()) const;
174         /**
175          Compares the complexity for strict inequality (less than) to another
176          with the given tolerance
177          @param x is the complexity to compare against
178          @param macheps is the tolerance to compare floating-point values
179          @return `true` if every measure in this satisfies Op::isLt(this, x, macheps),
180          and `false` otherwise
181          */
182         virtual bool lessThan(const Complexity& x, scalar macheps = fuzzylite::macheps()) const;
183         /**
184          Compares the complexity for inequality (less than or equal to) to another
185          with the given tolerance
186          @param x is the complexity to compare against
187          @param macheps is the tolerance to compare floating-point values
188          @return `true` if every measure in this satisfies Op::isLE(this, x, macheps),
189          and `false` otherwise
190          */
191         virtual bool lessThanOrEqualsTo(const Complexity& x, scalar macheps = fuzzylite::macheps()) const;
192         /**
193          Compares the complexity for strict inequality (greater than) to another
194          with the given tolerance
195          @param x is the complexity to compare against
196          @param macheps is the tolerance to compare floating-point values
197          @return `true` if every measure in this satisfies Op::isGt(this, x, macheps),
198          and `false` otherwise
199          */
200         virtual bool greaterThan(const Complexity& x, scalar macheps = fuzzylite::macheps()) const;
201         /**
202          Compares the complexity for inequality (greater than or equal to) to
203          another with the given tolerance
204          @param x is the complexity to compare against
205          @param macheps is the tolerance to compare floating-point values
206          @return `true` if every measure in this satisfies Op::isGE(this, x, macheps),
207          and `false` otherwise
208          */
209         virtual bool greaterThanOrEqualsTo(const Complexity& x, scalar macheps = fuzzylite::macheps()) const;
210 
211         /**
212          Computes the sum of the measures
213          @return the sum of the measures
214          */
215         virtual scalar sum() const;
216 
217         /**
218          Computes the norm of the complexity
219          @return the norm of the complexity
220          */
221         virtual scalar norm() const;
222 
223         /**
224          Returns the measures of the complexity
225          @return the measures of the complexity
226          */
227         virtual std::string toString() const;
228 
229         /**
230          Computes the complexity of the given engine as the sum of complexities
231          of the rule blocks
232          @param engine is the engine for which to compute the complexity
233          @return the complexity of the given engine as the sum of complexities
234          of the rule blocks
235          */
236         virtual Complexity compute(const Engine* engine) const;
237 
238         /**
239          Computes the complexity of the given input variable
240          @param inputVariable is the input variable for which to compute the complexity
241          @return the complexity of the given input variable
242          */
243         virtual Complexity compute(const InputVariable* inputVariable) const;
244         /**
245          Computes the complexity of the given output variable
246          @param outputVariable is the output variable for which to compute the complexity
247          @return the complexity of the given output variable
248          */
249         virtual Complexity compute(const OutputVariable* outputVariable) const;
250 
251         /**
252          Computes the complexity of the given input variables
253          @param inputVariables is the vector of input variables for which to
254          compute the complexity
255          @return the complexity of the given input variables
256          */
257         virtual Complexity compute(const std::vector<InputVariable*>& inputVariables) const;
258         /**
259          Computes the complexity of the given output variables
260          @param outputVariables is the vector of output variables for which to
261          compute the complexity
262          @param complexityOfDefuzzification indicates whether to compute the
263          complexity of the variable including the defuzzification process
264          @return the complexity of the given output variables
265          */
266         virtual Complexity compute(const std::vector<OutputVariable*>& outputVariables,
267                 bool complexityOfDefuzzification = false) const;
268         /**
269          Computes the complexity of the given variables
270          @param variables is the vector of variables for which to compute the
271          complexity
272          @return the complexity of the given variables
273          */
274         virtual Complexity compute(const std::vector<Variable*>& variables) const;
275 
276         /**
277          Computes the complexity of the given rule block
278          @param ruleBlock is the rule block for which to compute the complexity
279          @return the complexity of the given rule block
280          */
281         virtual Complexity compute(const RuleBlock* ruleBlock) const;
282 
283         /**
284          Computes the complexity of the given rule blocks
285          @param ruleBlocks is the vector of rule blocks for which to compute the
286          complexity
287          @return Computes the complexity of the given rule blocks
288          */
289         virtual Complexity compute(const std::vector<RuleBlock*>& ruleBlocks) const;
290 
291     };
292 
293 
294 }
295 
296 #endif /* COMPLEXITY_H */
297 
298