blob: e6efb43ca6c9d19092c721989c0f83647f03d035 (
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
|
/*
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/term/Linear.h"
#include "fl/variable/InputVariable.h"
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";
}
Complexity Linear::complexity() const {
Complexity result;
result.comparison(1 + 1);
if (_engine) {
result.arithmetic(scalar(_engine->variables().size()));
result.comparison(scalar(_engine->variables().size())); //if (i < coefficients)
}
return result;
}
scalar Linear::membership(scalar x) const {
FL_IUNUSED(x);
if (not _engine)
throw Exception("[linear error] term <" + getName() + "> "
"is missing a reference to the engine", FL_AT);
scalar result = 0.0;
const std::size_t numberOfInputVariables = _engine->inputVariables().size();
const std::size_t numberOfCoefficients = _coefficients.size();
for (std::size_t i = 0; i < numberOfInputVariables; ++i) {
if (i < numberOfCoefficients)
result += _coefficients.at(i) * _engine->inputVariables().at(i)->getValue();
}
if (numberOfCoefficients > numberOfInputVariables) {
result += _coefficients.back();
}
return result;
}
void Linear::set(const std::vector<scalar>& coefficients, const Engine* engine) {
setCoefficients(coefficients);
setEngine(engine);
}
void Linear::setCoefficients(const std::vector<scalar>& coefficients) {
this->_coefficients = coefficients;
}
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) {
this->_coefficients.clear();
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);
}
void Linear::updateReference(const Engine* engine) {
setEngine(engine);
}
Term* Linear::constructor() {
return new Linear;
}
}
|