source: cpp/frams/neuro/impl/neuroimpl-fuzzy.cpp @ 1277

Last change on this file since 1277 was 1158, checked in by Maciej Komosinski, 3 years ago

Cosmetic/minor improvements

  • Property svn:eol-style set to native
File size: 7.9 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1130]2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[109]4
5#include "neuroimpl-fuzzy.h"
6#include "neuroimpl-fuzzy-f0.h"
[1158]7#include <algorithm>
[109]8
9int NI_FuzzyNeuro::countOuts(const Model *m, const Neuro *fuzzy)
10{
[791]11        int outputs = 0;
12        for (int i = 0; i < m->getNeuroCount(); i++)
13                for (int in = 0; in < m->getNeuro(i)->getInputCount(); in++)
14                        if (m->getNeuro(i)->getInput(in) == fuzzy) outputs++;
15        return outputs;
[109]16}
17
18int NI_FuzzyNeuro::lateinit()
19{
[791]20        int i, maxOutputNr;
[109]21
[791]22        //check correctness of given parameters: string must not be null, sets&rules number > 0
[973]23        if ((fuzzySetsNr < 1) || (rulesNr < 1) || (fuzzySetString.length() == 0) || (fuzzyRulesString.length() == 0))
[791]24                return 0; //error
[109]25
[791]26        // this part contains transformation of fuzzy sets
27        fuzzySets = new double[4 * fuzzySetsNr]; //because every fuzzy set consist of 4 numbers
28        // converts fuzzy string from f0 to table of fuzzy numbers type 'double'
29        // (fill created space with numbers taken from string)
30        // also checks whether number of fuzzy sets in the string equals declared in the definition
31        if (FuzzyF0String::convertStrToSets(fuzzySetString, fuzzySets, fuzzySetsNr) != 0)
32                return 0; //error
[109]33
[791]34        // this part contains transformation of fuzzy rules and defuzzyfication parameters
35        rulesDef = new int[2 * rulesNr];    //for each rule remembers number of inputs and outputs
36        //check correctness of string and fill in the rulesDef
37        if (FuzzyF0String::countInputsOutputs(fuzzyRulesString.c_str(), rulesDef, rulesNr) == 0)
38        {
39                defuzzParam = new double[rulesNr]; // parameters used in defuzyfication process
40                // create space for rules according to rulesDef
41                rules = new int*[rulesNr];   //list of rules...
42                for (i = 0; i < rulesNr; i++)    //...that contains rules body
43                {
44                        rules[i] = new int[2 * (rulesDef[2 * i] + rulesDef[2 * i + 1])];  //each rule can have different number of inputs and outputs
45                        defuzzParam[i] = 0; //should be done a little bit earlier, but why do not use this loop?
46                }
47                // fill created space with numbers taken from string
48                if (FuzzyF0String::convertStrToRules(fuzzyRulesString, rulesDef, rules, fuzzySetsNr, rulesNr, maxOutputNr) != 0)
49                        return 0; //error
50        }
51        else
52                return 0; //error
[109]53
[791]54        setChannelCount(countOuts(neuro->owner, neuro));
55        return 1; //success
[109]56}
57
58NI_FuzzyNeuro::~NI_FuzzyNeuro()
59{
[791]60        if (rules) //delete rows and columns of **rules
61        {
62                for (int i = 0; i < rulesNr; i++) SAFEDELETEARRAY(rules[i])
63                        SAFEDELETEARRAY(rules)
64        }
65        SAFEDELETEARRAY(defuzzParam)
66                SAFEDELETEARRAY(rulesDef)
67                SAFEDELETEARRAY(fuzzySets)
[109]68}
69
70int NI_FuzzyNeuro::GetFuzzySetParam(int set_nr, double &left, double &midleft, double &midright, double &right)
71{
[791]72        if ((set_nr >= 0) && (set_nr < fuzzySetsNr))
73        {
74                left = fuzzySets[4 * set_nr];
75                midleft = fuzzySets[4 * set_nr + 1];
76                midright = fuzzySets[4 * set_nr + 2];
77                right = fuzzySets[4 * set_nr + 3];
78                return 0;
79        }
80        else
81                return 1;
[112]82}
[109]83
84/** Function conduct fuzzyfication of inputs and calculates - according to rules - crisp multi-channel output */
85void NI_FuzzyNeuro::go()
86{
[791]87        if (Fuzzyfication() != 0)
88                return;
89        if (Defuzzyfication() != 0)
90                return;
[112]91}
[109]92
93/**
94* Function conduct fuzzyfication process - calculates minimum membership function (of every input) for each rule,
95* and writes results into defuzzParam - variable that contains data necessary for defuzzyfication
96*/
97int NI_FuzzyNeuro::Fuzzyfication()
98{
[791]99        int i, j, nrIn, inputNr, nrFuzzySet;
100        double minimumCut; // actual minimal level of cut (= min. membership function)
[109]101
[791]102        // sets defuzzyfication parameters for each rule:
103        for (i = 0; i < rulesNr; i++)
104        {
105                nrIn = rulesDef[2 * i]; // nr of inputs in rule #i
106                minimumCut = 2; // the highest value of membership function is 1.0, so this value will definitely change
[907]107                for (j = 0; (j < nrIn) && (minimumCut > 0); j++) //minimumCut can not be <0, so if =0 then stop calculations
[791]108                {
109                        nrFuzzySet = rules[i][j * 2 + 1]; // j*2 moves pointer through each output, +1 moves to nr of fuzzy set
110                        inputNr = rules[i][j * 2]; // as above but gives input number
[1130]111                        minimumCut = std::min(minimumCut, TrapeziumFuzz(nrFuzzySet, getWeightedInputState(inputNr))); // value of membership function for this input and given fuzzy set
[791]112                }
[907]113                if ((minimumCut > 1) || (minimumCut < 0))
[791]114                        return 1;
115                defuzzParam[i] = minimumCut;
116        }
117        return 0;
[112]118}
[109]119
120/**
121* Function calculates value of the membership function of the set given by wchich_fuzzy_set for given crisp value input_val
122* In other words, this function fuzzyficates given crisp value with given fuzzy set, returning it's membership function
123* @param which_fuzzy_set - 0 < number of set < fuzzySetsNr
124* @param input_val - crisp value of input in range <-1; 1>
125* @return value of membership function (of given input for given set) in range <0;1> or, if error occur, negative value
126*/
127double NI_FuzzyNeuro::TrapeziumFuzz(int which_fuzzy_set, double input_val)
128{
[791]129        double range = 0, left = 0, midleft = 0, midright = 0, right = 0;
[109]130
[791]131        if ((which_fuzzy_set < 0) || (which_fuzzy_set > fuzzySetsNr))
132                return -2;
133        if ((input_val < -1) || (input_val > 1))
134                return -3;
[109]135
[791]136        if (GetFuzzySetParam(which_fuzzy_set, left, midleft, midright, right) != 0)
137                return -4;
[109]138
[791]139        if ((input_val < left) || (input_val > right)) // greather than right value
140                return 0;
141        else if ((input_val >= midleft) && (input_val <= midright)) // in the core of fuzzy set
142                return 1;
143        else if ((input_val >= left) && (input_val < midleft)) // at the left side of trapezium
144        {
145                range = fabs(midleft - left);
146                return fabs(input_val - left) / ((range > 0) ? range : 1); // quotient of distance between input and extreme left point of trapezium and range of rising side, or 1
147        }
148        else if ((input_val > midright) && (input_val <= right)) // at the right side of trapezium
149        {
150                range = fabs(right - midright);
151                return fabs(right - input_val) / ((range > 0) ? range : 1); // quotient of distance between input and extreme right point of trapezium and range of falling side, or 1
152        };
[109]153
[791]154        // should not occur
155        return 0;
[109]156
[112]157}
[109]158
159/**
160* Function conducts defuzzyfication process: multi-channel output values are calculates with singleton method (method of high).
161* For each rules, all outputs fuzzy sets are taken and cut at 'cut-level', that is at minumum membership function (of current rule).
162* For all neuro pseudo-outputs, answer is calculated according to prior computations.
163* In fact, there is one output with multi-channel answer and appropriate values are given to right channels.
164*/
165int NI_FuzzyNeuro::Defuzzyfication()
166{
[791]167        int i, j, nrIn, nrOut, out, set, outputsNr;
168        double *numerators, *denominators, midleft, midright, unimp;
[109]169
[791]170        outputsNr = getChannelCount();
[109]171
[791]172        numerators = new double[outputsNr];
173        denominators = new double[outputsNr];
[109]174
[791]175        for (i = 0; i < outputsNr; i++) numerators[i] = denominators[i] = 0;
[109]176
[791]177        // for each rule...
178        for (i = 0; i < rulesNr; i++)
179        {
180                nrIn = rulesDef[2 * i]; // number of inputs in rule #i
181                nrOut = rulesDef[2 * i + 1]; // number of outputs in rule #i
182                // ...calculate each output's product of middle fuzzy set value and minimum membership function (numerator) and sum of minimum membership function (denominator)
183                for (j = 0; j < nrOut; j++)
184                {
185                        out = rules[i][2 * nrIn + 2 * j]; //number of j-output
186                        set = rules[i][2 * nrIn + 2 * j + 1]; //number of fuzzy set attributed to j-output
187                        if (GetFuzzySetParam(set, unimp, midleft, midright, unimp) != 0) // gets range of core of given fuzzy set
188                        {
189                                SAFEDELETEARRAY(denominators) SAFEDELETEARRAY(numerators) return 1;
190                        }
191                        //defuzzParam[i] = minimum membership function for rule #i - calculated in fuzzyfication block
192                        // defuzzyfication method of singletons (high): (fuzzy set modal value) * (minimum membership value)
193                        numerators[out] += ((midleft + midright) / 2.0) * defuzzParam[i];
194                        denominators[out] += defuzzParam[i];
195                }
196        }
[109]197
[791]198        for (i = 0; i < outputsNr; i++)
199        {
200                if (denominators[i] == 0)
201                        setState(0, i);
202                else
203                        setState(numerators[i] / denominators[i], i);
204        }
[109]205
[791]206        SAFEDELETEARRAY(denominators)
207                SAFEDELETEARRAY(numerators)
[109]208
[791]209                return 0;
[112]210}
Note: See TracBrowser for help on using the repository browser.