summaryrefslogtreecommitdiff
path: root/fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp
blob: 0beb72299e8c004ff108f26d58eb63792d965487 (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
/*
 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/defuzzifier/WeightedAverageCustom.h"

#include "fl/term/Aggregated.h"

#include <map>

namespace fl {

    WeightedAverageCustom::WeightedAverageCustom(Type type) : WeightedDefuzzifier(type) { }

    WeightedAverageCustom::WeightedAverageCustom(const std::string& type) : WeightedDefuzzifier(type) { }

    WeightedAverageCustom::~WeightedAverageCustom() { }

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

    Complexity WeightedAverageCustom::complexity(const Term* term) const {
        Complexity result;
        result.comparison(3).arithmetic(1).function(1);
        const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term);
        if (fuzzyOutput) {
            result += term->complexity().arithmetic(3).comparison(2)
                    .multiply(scalar(fuzzyOutput->numberOfTerms()));
        }
        return result;
    }

    scalar WeightedAverageCustom::defuzzify(const Term* term,
            scalar minimum, scalar maximum) const {
        const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term);
        if (not fuzzyOutput) {
            std::ostringstream ss;
            ss << "[defuzzification error]"
                    << "expected an Aggregated term instead of"
                    << "<" << (term ? term->toString() : "null") << ">";
            throw Exception(ss.str(), FL_AT);
        }

        if (fuzzyOutput->isEmpty()) return fl::nan;

        minimum = fuzzyOutput->getMinimum();
        maximum = fuzzyOutput->getMaximum();

        SNorm* aggregation = fuzzyOutput->getAggregation();

        Type type = getType();
        if (type == Automatic) {
            type = inferType(&(fuzzyOutput->terms().front()));
        }

        scalar sum = 0.0;
        scalar weights = 0.0;
        const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms();
        if (type == TakagiSugeno) {
            //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions
            scalar w, z, wz;
            for (std::size_t i = 0; i < numberOfTerms; ++i) {
                const Activated& activated = fuzzyOutput->getTerm(i);
                w = activated.getDegree();
                z = activated.getTerm()->membership(w);
                const TNorm* implication = activated.getImplication();
                wz = implication ? implication->compute(w, z) : (w * z);
                if (aggregation) {
                    sum = aggregation->compute(sum, wz);
                    weights = aggregation->compute(weights, w);
                } else {
                    sum += wz;
                    weights += w;
                }
            }
        } else {
            scalar w, z, wz;
            for (std::size_t i = 0; i < numberOfTerms; ++i) {
                const Activated& activated = fuzzyOutput->getTerm(i);
                w = activated.getDegree();
                z = activated.getTerm()->tsukamoto(w, minimum, maximum);
                const TNorm* implication = activated.getImplication();
                wz = implication ? implication->compute(w, z) : (w * z);
                if (aggregation) {
                    sum = aggregation->compute(sum, wz);
                    weights = aggregation->compute(weights, w);
                } else {
                    sum += wz;
                    weights += w;
                }
            }
        }
        return sum / weights;
    }

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

    Defuzzifier* WeightedAverageCustom::constructor() {
        return new WeightedAverageCustom;
    }

}