source: cpp/_old/f0-fuzzy/neuroimpl-fuzzy-f0.cpp @ 730

Last change on this file since 730 was 193, checked in by Maciej Komosinski, 11 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 4.3 KB
RevLine 
[193]1/// this is a module included into neuroimpl-fuzzy
2/// it converts string parameters - fuzzy sets and fuzzy rules - into appropriate variables
3
4#include "neuroimpl-fuzzy-f0.h"
5// nonstd z powodu max()
6#include "nonstd.h"
7
8//this part concerns fuzzy sets transformation
9
10/** This function converts given string into numbers representing fuzzy sets */
11int FuzzyF0String::convertStrToSets(const SString& str, double numbers[], int nrOfSets)
12{
13  int pos=0;
14  SString t;
15  int have=0;
16  int maxnumbers=4*nrOfSets; //number of semicolons should be equal 4*nrOfSets
17
18  while (str.getNextToken(pos,t,';'))
19    if (have>=maxnumbers)
20      break;
21    else
22      numbers[have++]=atof(t);
23
24  //check if number of read numbers (separated with semicolon) is equal to declared
25  if (have != 4*nrOfSets)
26    return -1; //number of sets found is lower than declared!
27
28  //check corectness of sets - must not be decreasing
29  for(int i=0;i<nrOfSets;i++)
30    if((numbers[4*i]>numbers[4*i+1])||(numbers[4*i+1]>numbers[4*i+2])||(numbers[4*i+2]>numbers[4*i+3]))
31      return -2; //error
32
33  return 0;
34}
35
36//this part concerns fuzzy rules transformation
37
38/** This function counts number of inputs and outputs given in string consisting fuzzy rules.
39It counts number of separators:
40; between inputs/outputs and fuzzy sets number
41: between conditional and decision part of a rule
42/ end of rule
43*/
44int FuzzyF0String::countInputsOutputs(const char* str, int ruldef[], int rulesNr)
45{ //ruledef will remember counted number of inputs and outputs for every rule
46  const char* t;
47  int separators=0, inouts=0;
48
49  for(t=str;*t;t++)
50  {
51    while(isdigit(*t))
52      t++; //only count, does not care about numbers now
53    if (!*t)
54      break; //end of the string - get out of 'for' loop
55    if ( (*t==';')||(*t==':')||(*t=='/') ) //found sth different than digit - it must be a separator
56    {
57      separators++; //one of separators
58      if (*t!=';') // end of [conditional part of] rule
59      {
60        if (inouts >= 2*rulesNr) //more rules declared in string than declared in rulesNr
61          return -2;
62        ruldef[inouts]=(separators+1)/2; //cause fuzzy sets - for 1 in/out there are 2 semicolons
63        separators=0;  //begin counting number of in/out from zero
64        inouts++; //next part of rule / or next rule
65      }
66    }
67    else // illegal character
68      return -1;
69  }
70
71  //check, if nr of found rules is equal to declared
72  if (inouts == 2*rulesNr) //each rule has a conditional part (inputs) and decisional part (outputs)
73    return 0;
74  else
75    return -5; // ShowMessage("Inconsistent number of rules!");
76}
77
78/** This function converts given string into variable 'rules' that remembers all inputs/outputs and fuzzy set.
79The procedure is conduct according to ruledef, calculated earlier.*/
80int FuzzyF0String::convertStrToRules(const SString& str, const int ruledef[], int **rules, int setsNr, int rulesNr, int &maxOutputNr)
81{
82  int pos=0, j, k, len=str.len();
83  int dNr=0, sNr=0;
84  int inNr, outNr; //number of inputs/outputs and corresponding fuzzy sets
85  SString t;
86  bool conditional=true; //which part of rule: conditional or decisional
87
88  maxOutputNr=0; //sets maximum output nr found in rules string
89
90  //check corectness of the string: number semicolon ... separated with colon or slash
91  while(pos<len)
92  {
93    while((pos<len)&&(isdigit(str.charAt(pos)))) pos++;
94    if(!(pos<len))
95      break; //end of the string
96    if(str.charAt(pos)!=';')
97      if((str.charAt(pos)==':')&&(conditional))
98        {sNr++; conditional=false;}
99      else if((str.charAt(pos)=='/')&&(!conditional))
100        {dNr++; conditional=true;}
101      else
102        return -4; //error - illegal character
103    pos++;
104  }
105  if( (dNr!=sNr) || (dNr!=rulesNr) )
106    return -5; //error - wrong number of rules
107
108  pos=0;
109
110  for(j=0;j<rulesNr;j++)
111  { //sum of inputs and outputs
112    inNr = 2*ruledef[2*j];
113    outNr = 2*ruledef[2*j+1];
114    for(k=0;k<inNr+outNr;k++)
115    {
116      t = ""; //clear previous value
117      //cuts next integer values
118      while ( (pos<len)&&(isdigit(str.charAt(pos))) )
119        t += str.charAt(pos++);
120      pos++;
121      rules[j][k]=atol(t); //convert cut out string into number
122      //fuzzy sets - odd index table - are counted from 0,
123      //so if 5 fuzzy sets declared, values acceptable are 0,1,2,3,4
124      if ( ((k%2)!=0) && (rules[j][k] >= setsNr) )
125        return -1;
126      if((k>=inNr)&&((k%2)==0))
127        maxOutputNr=max(maxOutputNr,rules[j][k]);
128    }
129  }
130  return 0;
131};
132
Note: See TracBrowser for help on using the repository browser.