Changeset 791 for cpp/frams/neuro/impl/neuroimpl-fuzzy-f0.cpp
- Timestamp:
- 05/29/18 16:24:39 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/neuro/impl/neuroimpl-fuzzy-f0.cpp
r348 r791 14 14 int FuzzyF0String::convertStrToSets(const SString& str, double numbers[], int nrOfSets) 15 15 { 16 int pos=0;17 18 int have=0;19 int maxnumbers=4*nrOfSets; //number of semicolons should be equal 4*nrOfSets16 int pos = 0; 17 SString t; 18 int have = 0; 19 int maxnumbers = 4 * nrOfSets; //number of semicolons should be equal 4*nrOfSets 20 20 21 while (str.getNextToken(pos,t,';'))22 if (have>=maxnumbers)23 24 25 numbers[have++]=atof(t.c_str());21 while (str.getNextToken(pos, t, ';')) 22 if (have >= maxnumbers) 23 break; 24 else 25 numbers[have++] = atof(t.c_str()); 26 26 27 28 if (have != 4*nrOfSets)29 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 30 31 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 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 35 36 36 return 0; 37 37 } 38 38 … … 47 47 int FuzzyF0String::countInputsOutputs(const char* str, int ruldef[], int rulesNr) 48 48 { //ruledef will remember counted number of inputs and outputs for every rule 49 50 int separators=0, inouts=0;49 const char* t; 50 int separators = 0, inouts = 0; 51 51 52 for(t=str;*t;t++)53 54 while(isdigit(*t))55 56 57 58 if ( (*t==';')||(*t==':')||(*t=='/')) //found sth different than digit - it must be a separator59 60 61 if (*t!=';') // end of [conditional part of] rule62 63 if (inouts >= 2*rulesNr) //more rules declared in string than declared in rulesNr64 65 ruldef[inouts]=(separators+1)/2; //cause fuzzy sets - for 1 in/out there are 2 semicolons66 separators=0; //begin counting number of in/out from zero67 inouts++; //next part of rule / or next rule68 69 70 71 72 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 73 74 75 if (inouts == 2*rulesNr) //each rule has a conditional part (inputs) and decisional part (outputs)76 77 78 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 79 } 80 80 … … 83 83 int FuzzyF0String::convertStrToRules(const SString& str, const int ruledef[], int **rules, int setsNr, int rulesNr, int &maxOutputNr) 84 84 { 85 int pos=0, j, k, len=str.len();86 int dNr=0, sNr=0;87 88 89 bool conditional=true; //which part of rule: conditional or decisional85 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 90 91 maxOutputNr=0; //sets maximum output nr found in rules string91 maxOutputNr = 0; //sets maximum output nr found in rules string 92 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 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 { 102 sNr++; conditional = false; 103 } 104 else if ((str.charAt(pos) == '/') && (!conditional)) 105 { 106 dNr++; conditional = true; 107 } 108 else 109 return -4; //error - illegal character 110 pos++; 111 } 112 if ((dNr != sNr) || (dNr != rulesNr)) 113 return -5; //error - wrong number of rules 110 114 111 pos=0;115 pos = 0; 112 116 113 for(j=0;j<rulesNr;j++)114 115 inNr = 2*ruledef[2*j];116 outNr = 2*ruledef[2*j+1];117 for(k=0;k<inNr+outNr;k++)118 119 120 121 while ( (pos<len)&&(isdigit(str.charAt(pos))))122 123 124 rules[j][k]=atol(t.c_str()); //convert cut out string into number125 126 127 if ( ((k%2)!=0) && (rules[j][k] >= setsNr))128 129 if((k>=inNr)&&((k%2)==0))130 maxOutputNr=max(maxOutputNr,rules[j][k]);131 132 133 117 for (j = 0; j < rulesNr; j++) 118 { //sum of inputs and outputs 119 inNr = 2 * ruledef[2 * j]; 120 outNr = 2 * ruledef[2 * j + 1]; 121 for (k = 0; k < inNr + outNr; k++) 122 { 123 t = ""; //clear previous value 124 //cuts next integer values 125 while ((pos < len) && (isdigit(str.charAt(pos)))) 126 t += str.charAt(pos++); 127 pos++; 128 rules[j][k] = atol(t.c_str()); //convert cut out string into number 129 //fuzzy sets - odd index table - are counted from 0, 130 //so if 5 fuzzy sets declared, values acceptable are 0,1,2,3,4 131 if (((k % 2) != 0) && (rules[j][k] >= setsNr)) 132 return -1; 133 if ((k >= inNr) && ((k % 2) == 0)) 134 maxOutputNr = max(maxOutputNr, rules[j][k]); 135 } 136 } 137 return 0; 134 138 } 135
Note: See TracChangeset
for help on using the changeset viewer.