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