summaryrefslogtreecommitdiff
path: root/fuzzylite/src/term/Linear.cpp
blob: 4111a00676dc52c15e2fc0d2883293ad021ef541 (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
/*
 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/Linear.h"

#include "fl/Engine.h"
#include "fl/variable/InputVariable.h"

#include <cstdarg>

namespace fl {

    Linear::Linear(const std::string& name,
            const std::vector<scalar>& coefficients,
            const Engine* engine)
    : Term(name), _coefficients(coefficients), _engine(engine) {
    }

    Linear::~Linear() {
    }

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

    scalar Linear::membership(scalar x) const {
        (void) x;
        if (not _engine) throw fl::Exception("[linear error] term <" + getName() + "> "
                "is missing a reference to the engine", FL_AT);

        scalar result = 0.0;
        for (std::size_t i = 0; i < _engine->inputVariables().size(); ++i) {
            if (i < _coefficients.size())
                result += _coefficients.at(i) * _engine->inputVariables().at(i)->getInputValue();
        }
        if (_coefficients.size() > _engine->inputVariables().size()) {
            result += _coefficients.back();
        }
        return result;
    }

    void Linear::set(const std::vector<scalar>& coeffs, const Engine* engine) {
        setCoefficients(coeffs);
        setEngine(engine);
    }

    void Linear::setCoefficients(const std::vector<scalar>& coeffs) {
        this->_coefficients = coeffs;
    }

    const std::vector<scalar>& Linear::coefficients() const {
        return this->_coefficients;
    }

    std::vector<scalar>& Linear::coefficients() {
        return this->_coefficients;
    }

    void Linear::setEngine(const Engine* engine) {
        this->_engine = engine;
    }

    const Engine* Linear::getEngine() const {
        return this->_engine;
    }

    std::string Linear::parameters() const {
        return Op::join(this->_coefficients, " ");
    }

    void Linear::configure(const std::string& parameters) {
        if (parameters.empty()) return;
        std::vector<std::string> strValues = Op::split(parameters, " ");
        std::vector<scalar> values;
        for (std::size_t i = 0; i < strValues.size(); ++i) {
            values.push_back(Op::toScalar(strValues.at(i)));
        }
        this->_coefficients = values;
    }

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

    Term* Linear::constructor() {
        return new Linear;
    }

    template <typename T>
    Linear* Linear::create(const std::string& name,
            const Engine* engine, T firstCoefficient, ...) {// throw (fl::Exception) {
        if (not engine) throw fl::Exception("[linear error] cannot create term <" + name + "> "
                "without a reference to the engine", FL_AT);
        std::vector<scalar> coefficients;
        coefficients.push_back(firstCoefficient);

        va_list args;
        va_start(args, firstCoefficient);
        for (std::size_t i = 0; i < engine->inputVariables().size(); ++i) {
            coefficients.push_back((scalar) va_arg(args, T));
        }
        va_end(args);

        return new Linear(name, coefficients, engine);
    }

    template FL_API Linear* Linear::create(const std::string& name,
            const Engine* engine,
            double firstCoefficient, ...);

    template FL_API Linear* Linear::create(const std::string& name,
            const Engine* engine,
            int firstCoefficient, ...);
}