// This file is a part of the Framsticks GDK library. // Copyright (C) 2002-2011 Szymon Ulatowski. See LICENSE.txt for details. // Refer to http://www.framsticks.com/ for further information. /// this is a module included into neuroimpl-fuzzy /// it converts string parameters - fuzzy sets and fuzzy rules - into appropriate variables #include "neuroimpl-fuzzy-f0.h" #include //min,max //this part concerns fuzzy sets transformation /** This function converts given string into numbers representing fuzzy sets */ int FuzzyF0String::convertStrToSets(const SString& str, double numbers[], int nrOfSets) { int pos=0; SString t; int have=0; int maxnumbers=4*nrOfSets; //number of semicolons should be equal 4*nrOfSets while (str.getNextToken(pos,t,';')) if (have>=maxnumbers) break; else numbers[have++]=atof(t); //check if number of read numbers (separated with semicolon) is equal to declared if (have != 4*nrOfSets) return -1; //number of sets found is lower than declared! //check corectness of sets - must not be decreasing for(int i=0;inumbers[4*i+1])||(numbers[4*i+1]>numbers[4*i+2])||(numbers[4*i+2]>numbers[4*i+3])) return -2; //error return 0; } //this part concerns fuzzy rules transformation /** This function counts number of inputs and outputs given in string consisting fuzzy rules. It counts number of separators: ; between inputs/outputs and fuzzy sets number : between conditional and decision part of a rule / end of rule */ int FuzzyF0String::countInputsOutputs(const char* str, int ruldef[], int rulesNr) { //ruledef will remember counted number of inputs and outputs for every rule const char* t; int separators=0, inouts=0; for(t=str;*t;t++) { while(isdigit(*t)) t++; //only count, does not care about numbers now if (!*t) break; //end of the string - get out of 'for' loop if ( (*t==';')||(*t==':')||(*t=='/') ) //found sth different than digit - it must be a separator { separators++; //one of separators if (*t!=';') // end of [conditional part of] rule { if (inouts >= 2*rulesNr) //more rules declared in string than declared in rulesNr return -2; ruldef[inouts]=(separators+1)/2; //cause fuzzy sets - for 1 in/out there are 2 semicolons separators=0; //begin counting number of in/out from zero inouts++; //next part of rule / or next rule } } else // illegal character return -1; } //check, if nr of found rules is equal to declared if (inouts == 2*rulesNr) //each rule has a conditional part (inputs) and decisional part (outputs) return 0; else return -5; // ShowMessage("Inconsistent number of rules!"); } /** This function converts given string into variable 'rules' that remembers all inputs/outputs and fuzzy set. The procedure is conduct according to ruledef, calculated earlier.*/ int FuzzyF0String::convertStrToRules(const SString& str, const int ruledef[], int **rules, int setsNr, int rulesNr, int &maxOutputNr) { int pos=0, j, k, len=str.len(); int dNr=0, sNr=0; int inNr, outNr; //number of inputs/outputs and corresponding fuzzy sets SString t; bool conditional=true; //which part of rule: conditional or decisional maxOutputNr=0; //sets maximum output nr found in rules string //check corectness of the string: number semicolon ... separated with colon or slash while(pos= setsNr) ) return -1; if((k>=inNr)&&((k%2)==0)) maxOutputNr=max(maxOutputNr,rules[j][k]); } } return 0; }