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

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