source: cpp/f0-fuzzy/neuroimpl-fuzzy.cpp @ 26

Last change on this file since 26 was 2, checked in by Maciej Komosinski, 15 years ago

added f0-fuzzy (the Fuzzy neuron and its f0-based mutation and crossover)

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