12#include <include/qlibs_types.hpp>
125 template<type fType,
size_t numberOfInputs,
size_t numberOfOutputs,
size_t numberOfInputSets,
size_t numberOfOutputSets,
size_t numberOfRules>
141 template<type fType,
size_t numberOfInputs,
size_t numberOfOutputs,
size_t numberOfInputSets,
size_t numberOfOutputSets,
size_t numberOfRules>
155 template<type fType,
size_t numberOfInputs,
size_t numberOfOutputs,
size_t numberOfInputSets,
size_t numberOfOutputSets,
size_t numberOfRules>
171 real_t v[ 4 ] = { 0.0_re, 0.0_re, 0.0_re, 0.0_re };
172 inline void getNextX(
const size_t i )
noexcept
174 x = min + ( (
static_cast<real_t>( i ) + 0.5_re )*res );
181 const size_t n )
noexcept;
182 template <
size_t numberOfPo
ints>
184 real_t (&yData)[ numberOfPoints ] )
noexcept
190 template<type fType,
size_t numberOfInputs,
size_t numberOfOutputs,
size_t numberOfInputSets,
size_t numberOfOutputSets,
size_t numberOfRules>
218 const real_t minValue = 0.0_re,
219 const real_t maxValue= 1.0_re );
314 const deFuzzState stage );
316 const deFuzzState stage );
318 const deFuzzState stage );
320 const deFuzzState stage );
322 const deFuzzState stage );
324 const deFuzzState stage );
326 const deFuzzState stage );
337 mfFunction shape{
nullptr };
338 const real_t *points{
nullptr };
342 inline size_t getIndex(
void )
const
349 fx = (
nullptr != shape ) ? h*shape( io, points, n ) : 0.0_re;
359 #define FIS_RULE_ITEM_SIZE 1
362 #if ( FIS_RULE_ITEM_SIZE == 1 )
382 using rules = int8_t;
384 constexpr fis::rules FIS_RULES_MIN_VALUE = INT8_MIN;
408 constexpr fis::rules FIS_RULES_MIN_VALUE = INT16_MIN;
424 constexpr fis::rules Q_FIS_RULES_BEGIN = FIS_RULES_MIN_VALUE;
425 constexpr fis::rules Q_FIS_RULES_END = FIS_RULES_MIN_VALUE + 1;
426 constexpr fis::rules Q_FIS_AND = FIS_RULES_MIN_VALUE + 2;
427 constexpr fis::rules Q_FIS_OR = FIS_RULES_MIN_VALUE + 3;
428 constexpr fis::rules Q_FIS_THEN = FIS_RULES_MIN_VALUE + 4;
430 #define Q_FIS_IF_STATEMENT ,
431 #define Q_FIS_AND_STATEMENT +1),fis::Q_FIS_AND,
432 #define Q_FIS_OR_STATEMENT +1),fis::Q_FIS_OR,
433 #define Q_FIS_THEN_STATEMENT +1),fis::Q_FIS_THEN,
434 #define Q_FIS_IS_STATEMENT ,(
435 #define Q_FIS_IS_NOT_STATEMENT ,-(
436 #define Q_FIS_END_STATEMENT +1)
457 #define FIS_RULES_BEGIN fis::Q_FIS_RULES_BEGIN
476 #define FIS_RULES_END ,fis::Q_FIS_RULES_END
478 #define IF Q_FIS_IF_STATEMENT
480 #define AND Q_FIS_AND_STATEMENT
482 #define OR Q_FIS_OR_STATEMENT
484 #define THEN Q_FIS_THEN_STATEMENT
486 #define IS Q_FIS_IS_STATEMENT
488 #define IS_NOT Q_FIS_IS_NOT_STATEMENT
490 #define END Q_FIS_END_STATEMENT
503 template<type fType,
size_t numberOfInputs,
size_t numberOfOutputs,
size_t numberOfInputSets,
size_t numberOfOutputSets,
size_t numberOfRules>
507 input *xInput{
nullptr };
508 output *xOutput{
nullptr };
510 mf *outMF{
nullptr };
511 methods_fcn andOp{ &
Min };
512 methods_fcn orOp{ &
Max };
513 methods_fcn implicate{ &
Min };
514 methods_fcn aggregate{ &
Max };
515 size_t (
instance::*inferenceState)(
size_t i );
516 size_t (
instance::*aggregationState)(
size_t i );
518 real_t *ruleWeight{
nullptr };
520 const rules *xRules{
nullptr };
521 size_t rules_cols{ 0U};
523 size_t nOutputs{ 0 };
524 size_t nMFInputs{ 0U };
525 size_t nMFOutputs{ 0U };
526 size_t nPoints{ 100 };
528 size_t ruleCount{ 0U };
529 real_t rStrength{ 0.0_re };
538 const real_t h )
noexcept;
539 static real_t parseFuzzValue( mf *
const mfIO,
540 rules index )
noexcept;
542 size_t inferenceAntecedent(
size_t i )
noexcept;
543 size_t inferenceReachEnd(
size_t i )
noexcept;
544 size_t aggregationFindConsequent(
size_t i )
noexcept;
545 size_t inferenceConsequent(
size_t i )
noexcept;
546 void fuzzyAggregate(
void )
noexcept;
547 static const size_t INFERENCE_ERROR;
576 input *
const inputs,
580 mf *
const mf_inputs,
582 mf *
const mf_outputs,
584 const rules *
const r,
586 real_t *rWeights =
nullptr ) noexcept;
605 template <
size_t numberInputs,
size_t numberOutputs,
size_t numberMFinputs,
size_t numberMFOutputs,
size_t numberRules>
607 input (&inputs)[ numberInputs ],
608 output (&outputs)[ numberOutputs ],
609 mf (&mf_inputs)[ numberMFinputs ],
610 mf (&mf_outputs)[ numberMFOutputs ],
611 const
rules * const r,
612 real_t (&rWeights)[ numberRules ] ) noexcept
614 return setup( t, inputs, numberInputs, outputs, numberOutputs, mf_inputs, numberMFinputs, mf_outputs, numberMFOutputs, r, numberRules, rWeights );
626 const real_t Max )
noexcept;
636 const real_t Max )
noexcept;
663 const real_t h = 1.0_re )
noexcept
665 return ( (
nullptr != inMF ) && ( nMFInputs > 0U ) ) ? setMF( inMF, io,
mf, s,
nullptr, cp, h ) :
false;
682 mfFunction customMfs,
684 const real_t h = 1.0_re )
noexcept
686 return ( (
nullptr != inMF ) && ( nMFInputs > 0U ) ) ? setMF( inMF, io,
mf,
custommf, customMfs, cp, h ) :
false;
714 const real_t h = 1.0_re )
noexcept
716 return ( (
nullptr != outMF ) && ( nMFOutputs > 0U ) ) ? setMF( outMF, io,
mf, s,
nullptr, cp, ( (
Mamdani == xType ) ? h : 1.0_re ) ) :
false;
733 mfFunction customMfs,
735 const real_t h = 1.0_re )
noexcept
737 return ( (
nullptr != outMF ) && ( nMFOutputs > 0U ) ) ? setMF( outMF, io,
mf,
custommf, customMfs, cp, ( (
Mamdani == xType ) ? h : 1.0_re ) ) :
false;
747 const real_t value )
noexcept;
757 real_t &value )
const noexcept;
822 return (
nullptr != xInput );
829 explicit operator bool() const noexcept {
852 return (
static_cast<size_t>( outTag ) < nOutputs ) ? xOutput[ outTag ].value : xInput[ nOutputs -1 ].value;
862 if (
static_cast<size_t>( t ) < nInputs ) {
874 if ( lastTag >= 0 ) {
875 xInput[ lastTag ].value =
static_cast<real_t>(value);
886 if ( lastTag >= 0 ) {
887 xInput[ lastTag ].value = value;
907 template<type fType,
size_t numberOfInputs,
size_t numberOfOutputs,
size_t numberOfInputSets,
size_t numberOfOutputSets,
size_t numberOfRules>
912 input inputs[ numberOfInputs ];
913 output outputs[ numberOfOutputs ];
914 mf MFin[ numberOfInputSets ], MFout[ numberOfOutputSets ];
915 real_t ruleStrength[ numberOfRules ];
925 return sys.setup( fType,
926 inputs, numberOfInputs,
927 outputs, numberOfOutputs,
928 MFin, numberOfInputSets,
929 MFout, numberOfOutputSets,
930 xRules, numberOfRules,
943 const real_t Max )
noexcept
945 return sys.setupInput( t, Min, Max );
957 const real_t Max )
noexcept
959 return sys.setupOutput( t, Min, Max );
987 const real_t h = 1.0_re )
noexcept
989 return sys.setupInputMF( io,
mf, s, cp, h );
1006 mfFunction customMfs,
1008 const real_t h = 1.0_re )
noexcept
1010 return sys.setupInputMF( io,
mf, customMfs, cp, h );
1038 const real_t h = 1.0_re )
noexcept
1040 return sys.setupOutputMF( io,
mf, s, cp, h );
1057 mfFunction customMfs,
1059 const real_t h = 1.0_re )
noexcept
1061 return sys.setupOutputMF( io,
mf, customMfs, cp, h );
1074 return sys.fuzzify();
1088 return sys.deFuzzify();
1099 return sys.inference();
1109 const real_t value )
noexcept
1111 return sys.setInput( t, value );
1122 real_t &value )
const noexcept
1124 return sys.getOutput( t, value );
1136 return sys.setParameter( p, x );
1151 return sys.setDeFuzzMethod( m );
1165 return sys.setRuleWeights( rWeights );
1187 return (
static_cast<size_t>( outTag ) < sys.nOutputs ) ? sys.xOutput[ outTag ].value : sys.xInput[ sys.nOutputs -1 ].value;
1197 if (
static_cast<size_t>( t ) < sys.nInputs ) {
1209 if ( sys.lastTag >= 0 ) {
1210 sys.xInput[ sys.lastTag ].value =
static_cast<real_t>( value );
1221 if ( sys.lastTag >= 0 ) {
1222 sys.xInput[ sys.lastTag ].value = value;
1232 return sys.isInitialized();
1239 explicit operator bool() const noexcept {
1240 return sys.isInitialized();
A FIS(Fuzzy Inference System) object.
Definition fis.hpp:500
bool setupOutputMF(const tag io, const tag mf, const shapeMF s, const real_t *cp, const real_t h=1.0_re) noexcept
Setup the output tag and points for the specified membership function.
Definition fis.hpp:710
bool setRuleWeights(real_t *rWeights) noexcept
Set weights to the rules of the inference system.
Definition fis.cpp:467
bool deFuzzify(void) noexcept
Perform the de-Fuzzification operation to compute the crisp outputs.
Definition fis.cpp:418
bool setupOutputMF(const tag io, const tag mf, mfFunction customMfs, const real_t *cp, const real_t h=1.0_re) noexcept
Set the output tag and points for the specified membership function.
Definition fis.hpp:731
bool setupInputMF(const tag io, const tag mf, const shapeMF s, const real_t *cp, const real_t h=1.0_re) noexcept
Setup the input tag and points for the specified membership function.
Definition fis.hpp:659
bool setDeFuzzMethod(deFuzzMethod m) noexcept
Change the default de-Fuzzification method of the FIS instance.
Definition fis.cpp:46
instance & operator<<(const int &value)
The value to set the previously selected input.
Definition fis.hpp:873
bool setup(const type t, input *const inputs, const size_t ni, output *const outputs, const size_t no, mf *const mf_inputs, const size_t nmi, mf *const mf_outputs, const size_t nmo, const rules *const r, const size_t n, real_t *rWeights=nullptr) noexcept
Setup and initialize the FIS instance.
Definition fis.cpp:71
bool isInitialized(void) const
Check if the FIS instance has been initialized.
Definition fis.hpp:821
size_t getNumberOfPoints(void) const noexcept
Get the number of points used on Mamdani to perform the de-fuzzification proccess.
Definition fis.hpp:838
bool setupOutput(const tag t, const real_t Min, const real_t Max) noexcept
Setup the output with the specified tag and set limits for it.
Definition fis.cpp:136
bool fuzzify(void) noexcept
Perform the fuzzification operation over the crisp inputs on the requested FIS object.
Definition fis.cpp:234
bool setParameter(const parameter p, const paramValue x) noexcept
Set parameters of the FIS instance.
Definition fis.cpp:7
virtual ~instance()
Definition fis.hpp:551
instance & operator<<(const real_t &value)
The value to set the previously selected input.
Definition fis.hpp:885
instance & operator<<(const tag &t)
Select the input to set using with the specified tag.
Definition fis.hpp:861
bool inference(void) noexcept
Perform the inference process on the FIS object.
Definition fis.cpp:478
bool setInput(const tag t, const real_t value) noexcept
Set a crisp value of the input with the specified tag.
Definition fis.cpp:170
friend class system
Definition fis.hpp:504
bool setupInput(const tag t, const real_t Min, const real_t Max) noexcept
Setup the input with the specified tag and set limits for it.
Definition fis.cpp:119
bool getOutput(const tag t, real_t &value) const noexcept
Get the de-fuzzified crisp value from the output with the specified tag.
Definition fis.cpp:185
real_t operator[](tag outTag) const
Get the de-fuzzified crisp value from the output with the specified tag.
Definition fis.hpp:849
friend class core
Definition fis.hpp:892
bool setupInputMF(const tag io, const tag mf, mfFunction customMfs, const real_t *cp, const real_t h=1.0_re) noexcept
Setup the input tag and points for the specified membership function.
Definition fis.hpp:680
friend class instance
Definition fis.hpp:139
virtual ~ioBase()
Definition fis.hpp:137
friend class system
Definition fis.hpp:142
friend class core
Definition fis.hpp:140
A FIS Membership Function.
Definition fis.hpp:335
friend class instance
Definition fis.hpp:355
virtual ~mf()
Definition fis.hpp:353
A FIS Output object.
Definition fis.hpp:163
friend class instance
Definition fis.hpp:188
virtual ~output()
Definition fis.hpp:178
bool storeAggregatedRegion(real_t *xData, real_t *yData, const size_t n) noexcept
Definition fis.cpp:155
bool storeAggregatedRegion(real_t(&xData)[numberOfPoints], real_t(&yData)[numberOfPoints]) noexcept
Definition fis.hpp:183
friend class system
Definition fis.hpp:191
friend class core
Definition fis.hpp:189
bool setupOutputMF(const tag io, const tag mf, mfFunction customMfs, const real_t *cp, const real_t h=1.0_re) noexcept
Set the output tag and points for the specified membership function.
Definition fis.hpp:1055
bool setupInputMF(const tag io, const tag mf, mfFunction customMfs, const real_t *cp, const real_t h=1.0_re) noexcept
Setup the input tag and points for the specified membership function.
Definition fis.hpp:1004
bool setupInputMF(const tag io, const tag mf, const shapeMF s, const real_t *cp, const real_t h=1.0_re) noexcept
Setup the input tag and points for the specified membership function.
Definition fis.hpp:983
system & operator<<(const int &value)
The value to set the previously selected input.
Definition fis.hpp:1208
bool setup(void)
Setup and initialize the FIS instance.
Definition fis.hpp:923
bool setParameter(const parameter p, const paramValue x) noexcept
Set parameters of the FIS instance.
Definition fis.hpp:1133
bool setupOutputMF(const tag io, const tag mf, const shapeMF s, const real_t *cp, const real_t h=1.0_re) noexcept
Set the output tag and points for the specified membership function.
Definition fis.hpp:1034
bool fuzzify(void) noexcept
Perform the fuzzification operation over the crisp inputs on the requested FIS object.
Definition fis.hpp:1072
real_t operator[](tag outTag) const
Get the de-fuzzified crisp value from the output with the specified tag.
Definition fis.hpp:1184
bool inference(void) noexcept
Perform the inference process on the FIS object.
Definition fis.hpp:1097
constexpr system(const rules *r)
Definition fis.hpp:917
size_t getNumberOfPoints(void) const noexcept
Get the number of points used on Mamdani to perform the de-fuzzification proccess.
Definition fis.hpp:1173
system & operator<<(const real_t &value)
The value to set the previously selected input.
Definition fis.hpp:1220
bool getOutput(const tag t, real_t &value) const noexcept
Get the de-fuzzified crisp value from the output with the specified tag.
Definition fis.hpp:1121
bool setupOutput(const tag t, const real_t Min, const real_t Max) noexcept
Setup the output with the specified tag and set limits for it.
Definition fis.hpp:955
bool isInitialized(void) const
Check if the FIS system has been initialized.
Definition fis.hpp:1231
bool setDeFuzzMethod(deFuzzMethod m) noexcept
Change the default de-Fuzzification method of the FIS instance.
Definition fis.hpp:1149
bool deFuzzify(void) noexcept
Perform the de-Fuzzification operation to compute the crisp outputs.
Definition fis.hpp:1086
system & operator<<(const tag &t)
Select the input to set using with the specified tag.
Definition fis.hpp:1196
bool setupInput(const tag t, const real_t Min, const real_t Max) noexcept
Setup the input with the specified tag and set limits for it.
Definition fis.hpp:941
bool setInput(const tag t, const real_t value) noexcept
Set a crisp value of the input with the specified tag.
Definition fis.hpp:1108
bool setRuleWeights(real_t *rWeights) noexcept
Set weights to the rules of the inference system.
Definition fis.hpp:1163
T Max(const T &first, const Args &... args)
Finds the maximum value among the provided arguments.
Definition ffmath.hpp:101
T Min(const T &first, const Args &... args)
Finds the minimum value among the provided arguments.
Definition ffmath.hpp:123
real_t(*)(const real_t a, const real_t b) fuzzyOperator
Definition fis.hpp:494
parameter
An enum with the allowed parameters that can be set on a FIS instance.
Definition fis.hpp:97
int16_t rules
Type definition to instantiate a set of fuzzy rules.
Definition fis.hpp:406
real_t(*)(output *const o, const deFuzzState stage) deFuzzFunction
Definition fis.hpp:493
type
An enum with the inference system types supported by qlibs::fis.
Definition fis.hpp:108
paramValue
An enum with the supported parameter values.
Definition fis.hpp:86
shapeMF
An enum with all the possible values to specify a membership function.
Definition fis.hpp:33
deFuzzMethod
An enum with all the possible de-Fuzzyfication methods.
Definition fis.hpp:70
rules tag
Used to define an enum of fis tags.
Definition fis.hpp:421
@ FIS_EvalPoints
Definition fis.hpp:102
@ FIS_Implication
Definition fis.hpp:98
@ FIS_AND
Definition fis.hpp:100
@ FIS_OR
Definition fis.hpp:101
@ FIS_Aggregation
Definition fis.hpp:99
@ Sugeno
Definition fis.hpp:110
@ Tsukamoto
Definition fis.hpp:111
@ Mamdani
Definition fis.hpp:109
@ FIS_PROD
Definition fis.hpp:88
@ FIS_MIN
Definition fis.hpp:87
@ FIS_PROBOR
Definition fis.hpp:90
@ FIS_SUM
Definition fis.hpp:91
@ FIS_MAX
Definition fis.hpp:89
@ sigmf
Definition fis.hpp:40
@ custommf
Definition fis.hpp:34
@ singletonmf
Definition fis.hpp:46
@ gbellmf
Definition fis.hpp:37
@ constantmf
Definition fis.hpp:53
@ tlinsmf
Definition fis.hpp:55
@ gaussmf
Definition fis.hpp:38
@ pimf
Definition fis.hpp:43
@ tconcavemf
Definition fis.hpp:57
@ concavemf
Definition fis.hpp:47
@ psigmf
Definition fis.hpp:42
@ tlinzmf
Definition fis.hpp:56
@ tzmf
Definition fis.hpp:60
@ cosmf
Definition fis.hpp:52
@ trimf
Definition fis.hpp:35
@ smf
Definition fis.hpp:44
@ linzmf
Definition fis.hpp:50
@ dsigmf
Definition fis.hpp:41
@ tsmf
Definition fis.hpp:59
@ gauss2mf
Definition fis.hpp:39
@ trampmf
Definition fis.hpp:61
@ spikemf
Definition fis.hpp:48
@ linsmf
Definition fis.hpp:49
@ zmf
Definition fis.hpp:45
@ trapmf
Definition fis.hpp:36
@ linearmf
Definition fis.hpp:54
@ tsigmf
Definition fis.hpp:58
@ rectmf
Definition fis.hpp:51
@ mom
Definition fis.hpp:73
@ wtsum
Definition fis.hpp:77
@ centroid
Definition fis.hpp:71
@ bisector
Definition fis.hpp:72
@ wtaver
Definition fis.hpp:76
@ lom
Definition fis.hpp:74
@ som
Definition fis.hpp:75
The Fuzzy Inference System namespace.
Definition fis.hpp:22
The qLibs++ library namespace.
Definition mat.hpp:18
float real_t
A type to instantiate a real variable double-precision of 64-bits IEEE 754.
Definition qlibs_types.hpp:43