Changeset 791 for cpp/frams/neuro
- Timestamp:
- 05/29/18 16:24:39 (7 years ago)
- Location:
- cpp/frams/neuro/impl
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/neuro/impl/neuroimpl-body-sdk.h
r288 r791 8 8 #include <frams/neuro/neuroimpl.h> 9 9 10 class NI_Gyro : public NeuroImpl10 class NI_Gyro : public NeuroImpl 11 11 { 12 12 public: 13 NeuroImpl* makeNew(){return new NI_Gyro();} // for NeuroFactory14 int lateinit() {if (!neuro->joint) return 0; simorder=0; return 1;}15 void go() {setState(0);}13 NeuroImpl* makeNew(){ return new NI_Gyro(); } // for NeuroFactory 14 int lateinit() { if (!neuro->joint) return 0; simorder = 0; return 1; } 15 void go() { setState(0); } 16 16 }; 17 17 18 18 extern ParamEntry NI_Touch_tab[]; 19 class NI_Touch : public NeuroImpl19 class NI_Touch : public NeuroImpl 20 20 { 21 21 public: 22 double range;23 NeuroImpl* makeNew(){return new NI_Touch();} // for NeuroFactory24 int lateinit() {if (!neuro->part) return 0; simorder=0; return 1;}25 void go() {setState(0);}26 NI_Touch():range(1) {paramentries=NI_Touch_tab;}22 double range; 23 NeuroImpl* makeNew(){ return new NI_Touch(); } // for NeuroFactory 24 int lateinit() { if (!neuro->part) return 0; simorder = 0; return 1; } 25 void go() { setState(0); } 26 NI_Touch() :range(1) { paramentries = NI_Touch_tab; } 27 27 }; 28 28 29 class NI_Smell : public NeuroImpl29 class NI_Smell : public NeuroImpl 30 30 { 31 31 public: 32 NeuroImpl* makeNew(){return new NI_Smell();} // for NeuroFactory33 void go() {setState(0);}34 int lateinit() {if (!neuro->part) return 0; simorder=0; return 1;}32 NeuroImpl* makeNew(){ return new NI_Smell(); } // for NeuroFactory 33 void go() { setState(0); } 34 int lateinit() { if (!neuro->part) return 0; simorder = 0; return 1; } 35 35 }; 36 36 37 37 extern ParamEntry NI_BendMuscle_tab[]; 38 class NI_BendMuscle : public NeuroImpl38 class NI_BendMuscle : public NeuroImpl 39 39 { 40 40 public: 41 double power,bendrange;42 NeuroImpl* makeNew(){return new NI_BendMuscle();} // for NeuroFactory43 NI_BendMuscle() {paramentries=NI_BendMuscle_tab;}44 int lateinit() {if (!neuro->joint) return 0; simorder=2; return 1;}45 void go() {}41 double power, bendrange; 42 NeuroImpl* makeNew(){ return new NI_BendMuscle(); } // for NeuroFactory 43 NI_BendMuscle() { paramentries = NI_BendMuscle_tab; } 44 int lateinit() { if (!neuro->joint) return 0; simorder = 2; return 1; } 45 void go() {} 46 46 }; 47 47 48 48 extern ParamEntry NI_RotMuscle_tab[]; 49 class NI_RotMuscle : public NeuroImpl49 class NI_RotMuscle : public NeuroImpl 50 50 { 51 51 public: 52 double power;53 NeuroImpl* makeNew(){return new NI_RotMuscle();} // for NeuroFactory54 NI_RotMuscle() {paramentries=NI_RotMuscle_tab;}55 int lateinit() {if (!neuro->joint) return 0; simorder=2; return 1;}56 void go() {}52 double power; 53 NeuroImpl* makeNew(){ return new NI_RotMuscle(); } // for NeuroFactory 54 NI_RotMuscle() { paramentries = NI_RotMuscle_tab; } 55 int lateinit() { if (!neuro->joint) return 0; simorder = 2; return 1; } 56 void go() {} 57 57 }; 58 58 59 59 extern ParamEntry NI_LinearMuscle_tab[]; 60 class NI_LinearMuscle : public NeuroImpl60 class NI_LinearMuscle : public NeuroImpl 61 61 { 62 62 public: 63 double power;64 NeuroImpl* makeNew(){return new NI_RotMuscle();} // for NeuroFactory65 NI_LinearMuscle() {paramentries=NI_RotMuscle_tab;}66 int lateinit() {if (!neuro->joint) return 0; simorder=2; return 1;}67 void go() {}63 double power; 64 NeuroImpl* makeNew(){ return new NI_RotMuscle(); } // for NeuroFactory 65 NI_LinearMuscle() { paramentries = NI_RotMuscle_tab; } 66 int lateinit() { if (!neuro->joint) return 0; simorder = 2; return 1; } 67 void go() {} 68 68 }; 69 69 70 class NI_Sticky : public NeuroImpl70 class NI_Sticky : public NeuroImpl 71 71 { 72 72 public: 73 double power;74 NeuroImpl* makeNew(){return new NI_Sticky();} // for NeuroFactory75 int lateinit() {if (!neuro->part) return 0; simorder=0; return 1;}76 void go() {}73 double power; 74 NeuroImpl* makeNew(){ return new NI_Sticky(); } // for NeuroFactory 75 int lateinit() { if (!neuro->part) return 0; simorder = 0; return 1; } 76 void go() {} 77 77 }; 78 78 79 class NI_WaterDetect : public NeuroImpl79 class NI_WaterDetect : public NeuroImpl 80 80 { 81 81 public: 82 NeuroImpl* makeNew(){return new NI_WaterDetect();} // for NeuroFactory83 int lateinit() {if (!neuro->part) return 0; simorder=0; return 1;}84 void go() {setState(0);}82 NeuroImpl* makeNew(){ return new NI_WaterDetect(); } // for NeuroFactory 83 int lateinit() { if (!neuro->part) return 0; simorder = 0; return 1; } 84 void go() { setState(0); } 85 85 }; 86 86 87 class NI_Energy : public NeuroImpl87 class NI_Energy : public NeuroImpl 88 88 { 89 89 public: 90 NeuroImpl* makeNew(){return new NI_Energy();} // for NeuroFactory91 void go() {setState(0);}90 NeuroImpl* makeNew(){ return new NI_Energy(); } // for NeuroFactory 91 void go() { setState(0); } 92 92 }; 93 93 94 94 #endif 95 -
cpp/frams/neuro/impl/neuroimpl-channels.cpp
r286 r791 8 8 void NI_Channelize::go() 9 9 { 10 setChannelCount(getInputCount());11 for(int i=0;i<getInputCount();i++)12 setState(getWeightedInputState(i),i);10 setChannelCount(getInputCount()); 11 for (int i = 0; i < getInputCount(); i++) 12 setState(getWeightedInputState(i), i); 13 13 } 14 14 15 15 void NI_ChMux::go() 16 16 { 17 int c=getInputChannelCount(1);18 if (c<2) {setState(getWeightedInputState(1)); return;}19 double s=getWeightedInputState(0);20 s=(max(-1.0,min(1.0,s))+1.0)/2.0; // 0..121 int i1;22 i1=(int)(s*(c-1)); i1=max(0,min(i1,c-2));23 double sw=1.0/(c-1);24 double s1=sw*i1;25 double w1=fabs((s-s1)/sw);26 double w2=1.0-w1;27 double is1=getWeightedInputState(1,i1);28 double is2=getWeightedInputState(1,i1+1);29 setState(is1*w2 + is2*w1);17 int c = getInputChannelCount(1); 18 if (c < 2) { setState(getWeightedInputState(1)); return; } 19 double s = getWeightedInputState(0); 20 s = (max(-1.0, min(1.0, s)) + 1.0) / 2.0; // 0..1 21 int i1; 22 i1 = (int)(s*(c - 1)); i1 = max(0, min(i1, c - 2)); 23 double sw = 1.0 / (c - 1); 24 double s1 = sw*i1; 25 double w1 = fabs((s - s1) / sw); 26 double w2 = 1.0 - w1; 27 double is1 = getWeightedInputState(1, i1); 28 double is2 = getWeightedInputState(1, i1 + 1); 29 setState(is1*w2 + is2*w1); 30 30 } 31 31 32 32 void NI_ChSel::go() 33 33 { 34 setState(getWeightedInputState(0,ch));34 setState(getWeightedInputState(0, ch)); 35 35 } -
cpp/frams/neuro/impl/neuroimpl-channels.h
r286 r791 8 8 #include <frams/neuro/neuroimpl.h> 9 9 10 class NI_Channelize : public NeuroImpl10 class NI_Channelize : public NeuroImpl 11 11 { 12 12 public: 13 NeuroImpl* makeNew(){return new NI_Channelize();} // for NeuroFactory14 void go();13 NeuroImpl* makeNew(){ return new NI_Channelize(); } // for NeuroFactory 14 void go(); 15 15 }; 16 16 17 class NI_ChMux : public NeuroImpl17 class NI_ChMux : public NeuroImpl 18 18 { 19 19 public: 20 NeuroImpl* makeNew(){return new NI_ChMux();} // for NeuroFactory21 void go();20 NeuroImpl* makeNew(){ return new NI_ChMux(); } // for NeuroFactory 21 void go(); 22 22 }; 23 23 24 24 extern ParamEntry NI_ChSel_tab[]; 25 25 26 class NI_ChSel : public NeuroImpl26 class NI_ChSel : public NeuroImpl 27 27 { 28 28 public: 29 int ch; // channel30 NI_ChSel():ch(0) {paramentries=NI_ChSel_tab;}31 NeuroImpl* makeNew(){return new NI_ChSel();} // for NeuroFactory32 void go();29 int ch; // channel 30 NI_ChSel() :ch(0) { paramentries = NI_ChSel_tab; } 31 NeuroImpl* makeNew(){ return new NI_ChSel(); } // for NeuroFactory 32 void go(); 33 33 }; 34 34 … … 36 36 #endif 37 37 38 39 -
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 -
cpp/frams/neuro/impl/neuroimpl-fuzzy-f0.h
r286 r791 3 3 // See LICENSE.txt for details. 4 4 5 /// this is a module included into neuroimpl-fuzzy6 /// it converts string parameters - fuzzy sets and fuzzy rules - into appropriate variables5 /// This is a module included into neuroimpl-fuzzy. 6 /// It converts string parameters (fuzzy sets and fuzzy rules) into appropriate variables. 7 7 8 8 #ifndef _neuroimpl_fuzzy_f0_h … … 14 14 class FuzzyF0String 15 15 { 16 17 18 19 16 public: 17 static int convertStrToSets(const SString& str, double numbers[], int nrOfSets); 18 static int countInputsOutputs(const char* str, int ruldef[], int rulesNr); 19 static int convertStrToRules(const SString& str, const int ruledef[], int **rules, int setsNr, int rulesNr, int &maxOutputNr); 20 20 }; 21 21 -
cpp/frams/neuro/impl/neuroimpl-fuzzy.cpp
r348 r791 9 9 int NI_FuzzyNeuro::countOuts(const Model *m, const Neuro *fuzzy) 10 10 { 11 int outputs=0;12 for(int i=0;i<m->getNeuroCount();i++)13 for(int in=0;in<m->getNeuro(i)->getInputCount();in++)14 if (m->getNeuro(i)->getInput(in)==fuzzy) outputs++;15 11 int outputs = 0; 12 for (int i = 0; i < m->getNeuroCount(); i++) 13 for (int in = 0; in < m->getNeuro(i)->getInputCount(); in++) 14 if (m->getNeuro(i)->getInput(in) == fuzzy) outputs++; 15 return outputs; 16 16 } 17 17 18 18 int NI_FuzzyNeuro::lateinit() 19 19 { 20 21 22 23 if((fuzzySetsNr<1)||(rulesNr<1)||(fuzzySetString.len()==0)||(fuzzyRulesString.len()==0))24 25 26 27 fuzzySets = new double[4*fuzzySetsNr]; //because every fuzzy set consist of 4 numbers28 29 30 31 32 33 34 35 rulesDef = new int[2*rulesNr]; //for each rule remembers number of inputs and outputs36 37 38 39 40 41 42 for (i=0; i<rulesNr; i++) //...that contains rules body43 44 rules[i] = new int[2*(rulesDef[2*i]+rulesDef[2*i+1])]; //each rule can have different number of inputs and outputs45 46 47 48 49 50 51 52 53 54 55 20 int i, maxOutputNr; 21 22 //check correctness of given parameters: string must not be null, sets&rules number > 0 23 if ((fuzzySetsNr < 1) || (rulesNr < 1) || (fuzzySetString.len() == 0) || (fuzzyRulesString.len() == 0)) 24 return 0; //error 25 26 // this part contains transformation of fuzzy sets 27 fuzzySets = new double[4 * fuzzySetsNr]; //because every fuzzy set consist of 4 numbers 28 // converts fuzzy string from f0 to table of fuzzy numbers type 'double' 29 // (fill created space with numbers taken from string) 30 // also checks whether number of fuzzy sets in the string equals declared in the definition 31 if (FuzzyF0String::convertStrToSets(fuzzySetString, fuzzySets, fuzzySetsNr) != 0) 32 return 0; //error 33 34 // this part contains transformation of fuzzy rules and defuzzyfication parameters 35 rulesDef = new int[2 * rulesNr]; //for each rule remembers number of inputs and outputs 36 //check correctness of string and fill in the rulesDef 37 if (FuzzyF0String::countInputsOutputs(fuzzyRulesString.c_str(), rulesDef, rulesNr) == 0) 38 { 39 defuzzParam = new double[rulesNr]; // parameters used in defuzyfication process 40 // create space for rules according to rulesDef 41 rules = new int*[rulesNr]; //list of rules... 42 for (i = 0; i < rulesNr; i++) //...that contains rules body 43 { 44 rules[i] = new int[2 * (rulesDef[2 * i] + rulesDef[2 * i + 1])]; //each rule can have different number of inputs and outputs 45 defuzzParam[i] = 0; //should be done a little bit earlier, but why do not use this loop? 46 } 47 // fill created space with numbers taken from string 48 if (FuzzyF0String::convertStrToRules(fuzzyRulesString, rulesDef, rules, fuzzySetsNr, rulesNr, maxOutputNr) != 0) 49 return 0; //error 50 } 51 else 52 return 0; //error 53 54 setChannelCount(countOuts(neuro->owner, neuro)); 55 return 1; //success 56 56 } 57 57 58 58 NI_FuzzyNeuro::~NI_FuzzyNeuro() 59 59 { 60 if(rules) //delete rows and columns of **rules61 62 for (int i=0; i<rulesNr; i++) SAFEDELETEARRAY(rules[i])63 64 65 66 67 60 if (rules) //delete rows and columns of **rules 61 { 62 for (int i = 0; i < rulesNr; i++) SAFEDELETEARRAY(rules[i]) 63 SAFEDELETEARRAY(rules) 64 } 65 SAFEDELETEARRAY(defuzzParam) 66 SAFEDELETEARRAY(rulesDef) 67 SAFEDELETEARRAY(fuzzySets) 68 68 } 69 69 70 70 int NI_FuzzyNeuro::GetFuzzySetParam(int set_nr, double &left, double &midleft, double &midright, double &right) 71 71 { 72 if ( (set_nr>=0) && (set_nr<fuzzySetsNr))73 74 left = fuzzySets[4*set_nr];75 midleft = fuzzySets[4*set_nr+1];76 midright = fuzzySets[4*set_nr+2];77 right = fuzzySets[4*set_nr+3];78 79 80 81 72 if ((set_nr >= 0) && (set_nr < fuzzySetsNr)) 73 { 74 left = fuzzySets[4 * set_nr]; 75 midleft = fuzzySets[4 * set_nr + 1]; 76 midright = fuzzySets[4 * set_nr + 2]; 77 right = fuzzySets[4 * set_nr + 3]; 78 return 0; 79 } 80 else 81 return 1; 82 82 } 83 83 … … 85 85 void NI_FuzzyNeuro::go() 86 86 { 87 if (Fuzzyfication()!=0)88 89 if (Defuzzyfication()!=0)90 87 if (Fuzzyfication() != 0) 88 return; 89 if (Defuzzyfication() != 0) 90 return; 91 91 } 92 92 … … 97 97 int NI_FuzzyNeuro::Fuzzyfication() 98 98 { 99 100 101 102 103 for (i=0; i<rulesNr; i++)104 105 nrIn = rulesDef[2*i]; // nr of inputs in rule #i106 107 for (j=0; (j<nrIn)&&(minimumCut>0); j++) //minimumCut can not be <0, so if =0 then stop calculations108 109 nrFuzzySet = rules[i][j*2 + 1]; // j*2 moves pointer through each output, +1 moves to nr of fuzzy set110 inputNr = rules[i][j*2]; // as above but gives input number111 minimumCut = min(minimumCut, TrapeziumFuzz(nrFuzzySet, getWeightedInputState(inputNr))); // value of membership function for this input and given fuzzy set112 113 if ( (minimumCut>1) || (minimumCut<0))114 115 116 117 99 int i, j, nrIn, inputNr, nrFuzzySet; 100 double minimumCut; // actual minimal level of cut (= min. membership function) 101 102 // sets defuzzyfication parameters for each rule: 103 for (i = 0; i < rulesNr; i++) 104 { 105 nrIn = rulesDef[2 * i]; // nr of inputs in rule #i 106 minimumCut = 2; // the highest value of membership function is 1.0, so this value will definitely change 107 for (j = 0; (j < nrIn) && (minimumCut>0); j++) //minimumCut can not be <0, so if =0 then stop calculations 108 { 109 nrFuzzySet = rules[i][j * 2 + 1]; // j*2 moves pointer through each output, +1 moves to nr of fuzzy set 110 inputNr = rules[i][j * 2]; // as above but gives input number 111 minimumCut = min(minimumCut, TrapeziumFuzz(nrFuzzySet, getWeightedInputState(inputNr))); // value of membership function for this input and given fuzzy set 112 } 113 if ((minimumCut>1) || (minimumCut < 0)) 114 return 1; 115 defuzzParam[i] = minimumCut; 116 } 117 return 0; 118 118 } 119 119 … … 127 127 double NI_FuzzyNeuro::TrapeziumFuzz(int which_fuzzy_set, double input_val) 128 128 { 129 double range=0, left=0, midleft=0, midright=0, right=0;130 131 if ( (which_fuzzy_set < 0) || (which_fuzzy_set > fuzzySetsNr))132 133 if ( (input_val < -1) || (input_val > 1))134 135 136 137 138 139 if ( (input_val < left) || (input_val > right)) // greather than right value140 141 else if ( (input_val >= midleft) && (input_val <= midright)) // in the core of fuzzy set142 143 else if ( (input_val >= left) && (input_val < midleft)) // at the left side of trapezium144 145 146 return fabs(input_val-left)/((range>0)?range:1); // quotient of distance between input and extreme left point of trapezium and range of rising side, or 1147 148 else if ( (input_val > midright) && (input_val <= right)) // at the right side of trapezium149 150 151 return fabs(right-input_val)/((range>0)?range:1); // quotient of distance between input and extreme right point of trapezium and range of falling side, or 1152 153 154 155 129 double range = 0, left = 0, midleft = 0, midright = 0, right = 0; 130 131 if ((which_fuzzy_set < 0) || (which_fuzzy_set > fuzzySetsNr)) 132 return -2; 133 if ((input_val < -1) || (input_val > 1)) 134 return -3; 135 136 if (GetFuzzySetParam(which_fuzzy_set, left, midleft, midright, right) != 0) 137 return -4; 138 139 if ((input_val < left) || (input_val > right)) // greather than right value 140 return 0; 141 else if ((input_val >= midleft) && (input_val <= midright)) // in the core of fuzzy set 142 return 1; 143 else if ((input_val >= left) && (input_val < midleft)) // at the left side of trapezium 144 { 145 range = fabs(midleft - left); 146 return fabs(input_val - left) / ((range > 0) ? range : 1); // quotient of distance between input and extreme left point of trapezium and range of rising side, or 1 147 } 148 else if ((input_val > midright) && (input_val <= right)) // at the right side of trapezium 149 { 150 range = fabs(right - midright); 151 return fabs(right - input_val) / ((range > 0) ? range : 1); // quotient of distance between input and extreme right point of trapezium and range of falling side, or 1 152 }; 153 154 // should not occur 155 return 0; 156 156 157 157 } … … 165 165 int NI_FuzzyNeuro::Defuzzyfication() 166 166 { 167 int i, j, nrIn, nrOut, out, set, outputsNr; 168 double *numerators, *denominators, midleft, midright, unimp; 169 170 outputsNr = getChannelCount(); 171 172 numerators = new double[outputsNr]; 173 denominators = new double[outputsNr]; 174 175 for(i=0;i<outputsNr;i++) numerators[i] = denominators[i] = 0; 176 177 // for each rule... 178 for (i=0; i<rulesNr; i++) 179 { 180 nrIn = rulesDef[2*i]; // number of inputs in rule #i 181 nrOut = rulesDef[2*i + 1]; // number of outputs in rule #i 182 // ...calculate each output's product of middle fuzzy set value and minimum membership function (numerator) and sum of minimum membership function (denominator) 183 for (j=0; j<nrOut; j++) 184 { 185 out = rules[i][2*nrIn + 2*j]; //number of j-output 186 set = rules[i][2*nrIn + 2*j + 1]; //number of fuzzy set attributed to j-output 187 if (GetFuzzySetParam(set, unimp, midleft, midright, unimp) != 0) // gets range of core of given fuzzy set 188 { SAFEDELETEARRAY(denominators) SAFEDELETEARRAY(numerators) return 1; } 189 //defuzzParam[i] = minimum membership function for rule #i - calculated in fuzzyfication block 190 // defuzzyfication method of singletons (high): (fuzzy set modal value) * (minimum membership value) 191 numerators[out] += ((midleft + midright)/2.0) * defuzzParam[i]; 192 denominators[out] += defuzzParam[i]; 193 } 194 } 195 196 for (i=0; i<outputsNr; i++) 197 { 198 if (denominators[i] == 0) 199 setState(0, i); 200 else 201 setState(numerators[i]/denominators[i], i); 202 } 203 204 SAFEDELETEARRAY(denominators) 205 SAFEDELETEARRAY(numerators) 206 207 return 0; 208 } 209 167 int i, j, nrIn, nrOut, out, set, outputsNr; 168 double *numerators, *denominators, midleft, midright, unimp; 169 170 outputsNr = getChannelCount(); 171 172 numerators = new double[outputsNr]; 173 denominators = new double[outputsNr]; 174 175 for (i = 0; i < outputsNr; i++) numerators[i] = denominators[i] = 0; 176 177 // for each rule... 178 for (i = 0; i < rulesNr; i++) 179 { 180 nrIn = rulesDef[2 * i]; // number of inputs in rule #i 181 nrOut = rulesDef[2 * i + 1]; // number of outputs in rule #i 182 // ...calculate each output's product of middle fuzzy set value and minimum membership function (numerator) and sum of minimum membership function (denominator) 183 for (j = 0; j < nrOut; j++) 184 { 185 out = rules[i][2 * nrIn + 2 * j]; //number of j-output 186 set = rules[i][2 * nrIn + 2 * j + 1]; //number of fuzzy set attributed to j-output 187 if (GetFuzzySetParam(set, unimp, midleft, midright, unimp) != 0) // gets range of core of given fuzzy set 188 { 189 SAFEDELETEARRAY(denominators) SAFEDELETEARRAY(numerators) return 1; 190 } 191 //defuzzParam[i] = minimum membership function for rule #i - calculated in fuzzyfication block 192 // defuzzyfication method of singletons (high): (fuzzy set modal value) * (minimum membership value) 193 numerators[out] += ((midleft + midright) / 2.0) * defuzzParam[i]; 194 denominators[out] += defuzzParam[i]; 195 } 196 } 197 198 for (i = 0; i < outputsNr; i++) 199 { 200 if (denominators[i] == 0) 201 setState(0, i); 202 else 203 setState(numerators[i] / denominators[i], i); 204 } 205 206 SAFEDELETEARRAY(denominators) 207 SAFEDELETEARRAY(numerators) 208 209 return 0; 210 } -
cpp/frams/neuro/impl/neuroimpl-fuzzy.h
r286 r791 20 20 private: 21 21 22 22 double *fuzzySets; /// list of four digits which represents fuzzy sets: [0]-l, [1]-m, [2]-n, [3]-r, ... fuzzySet[4*i] = left, fuzzySet[4*i + 1] = midleft, fuzzySet[4*i + 2] = midright, fuzzySet[4*i + 3] = right 23 23 24 25 26 27 28 29 30 31 24 /** Determines, which fuzzy set is connected with each input of neuron. For instance third rule: 25 * 'IF input3 = fuzzy set #3 AND input5 = fuzzy set #1 then output2 = fuzzy set #6 AND output7 = fuzzy set #5' 26 * the variables shoul have values as shown below: 27 * RulesDef[4]=2; RulesDef[5]=2; //rule 3: 2 inputs, 2 outputs 28 * Rules[2][0]=3, Rules[2][1]=3, Rules[2][2]=5, Rules[2][3]=1, Rules[2][4]=2, Rules[2][5]=6, Rules[2][6]=7, Rules[2][3]=5 29 */ 30 int *rulesDef; ///list of rules definitions: nr of inputs in rule 1, nr of outputs in rule 1, ... and so on for each rule 31 int **rules; ///list of rules body: input nr, fuzzy set nr, ... , output nr, fuzzy set nr, ... and so on for each rule 32 32 33 34 35 36 37 38 33 /** 34 * Sets defuzzyfication parameters: determines - for each rule - cut level <0;1> (minimum membership function of current rule). 35 * In fact, defuzzParam remembers the values from 'first layer' - fuzzyfication layer (see neuron at documentation) 36 * i.e. rule 1: defuzzParam[0] = 0.3522 37 */ 38 double *defuzzParam; /// i.e.: defuzParam[5] = 0.455 means that rule #6 has got a minimum membership function (of given inputs set for this rule) at value 0.455 (it's cut level) 39 39 40 40 protected: 41 41 42 43 44 45 46 42 ///Fuzzy functions 43 double TrapeziumFuzz(int which_fuzzy_set, double input_val); 44 int Fuzzyfication(); 45 int Defuzzyfication(); 46 int GetFuzzySetParam(int set_nr, double &left, double &midleft, double &midright, double &right); 47 47 48 48 public: 49 49 50 51 52 53 50 int fuzzySetsNr; /// number of fuzzy sets 51 int rulesNr; ///number of rules 52 SString fuzzySetString; /// strings containing all fuzzy sets given in f0 53 SString fuzzyRulesString; /// strings containing all fuzzy rules given in f0 54 54 55 NI_FuzzyNeuro() {paramentries=NI_FuzzyNeuro_tab; fuzzySets=defuzzParam=NULL; rulesDef=NULL; rules=NULL;}56 57 58 59 60 61 62 63 64 65 66 **/67 68 55 NI_FuzzyNeuro() { paramentries = NI_FuzzyNeuro_tab; fuzzySets = defuzzParam = NULL; rulesDef = NULL; rules = NULL; } 56 ~NI_FuzzyNeuro(); 57 NeuroImpl* makeNew() { return new NI_FuzzyNeuro(); }; 58 void go(); 59 int lateinit(); 60 /** Function build model based on given genotype and conts number of neurons connected with fuzzy neuro, 61 also checks number of fuzzy neuron inputs. 62 \param genotype genotype to be scanned 63 \param inputs number of fuzzy neuron inputs 64 \param output number of fuzzy neuron outputs (= number of neurons connected to fuzzy neuron) 65 @return success or failure 66 **/ 67 static int countOuts(const Model *m, const Neuro *fuzzy); 68 69 69 }; 70 70 -
cpp/frams/neuro/impl/neuroimpl-simple.cpp
r726 r791 12 12 int NI_StdNeuron::lateinit() 13 13 { 14 istate=newstate+neuro->state; // neuro->state -> random initialization15 calcOutput();16 neuro->state=newstate;17 return 1;14 istate = newstate + neuro->state; // neuro->state -> random initialization 15 calcOutput(); 16 neuro->state = newstate; 17 return 1; 18 18 } 19 19 20 20 void NI_StdNeuron::calcInternalState() 21 21 { 22 double sum=getWeightedInputSum();23 velocity=force*(sum-istate)+inertia*velocity;24 istate+=velocity;25 if (istate>NEURO_MAX) istate=NEURO_MAX;26 else if (istate<-NEURO_MAX) istate=-NEURO_MAX;22 double sum = getWeightedInputSum(); 23 velocity = force*(sum - istate) + inertia*velocity; 24 istate += velocity; 25 if (istate > NEURO_MAX) istate = NEURO_MAX; 26 else if (istate < -NEURO_MAX) istate = -NEURO_MAX; 27 27 } 28 28 29 29 void NI_StdNeuron::go() 30 30 { 31 calcInternalState();32 calcOutput();31 calcInternalState(); 32 calcOutput(); 33 33 } 34 34 35 35 void NI_StdNeuron::calcOutput() 36 36 { 37 double s=istate * sigmo;38 if (s<-30.0) setState(-1);39 else setState(2.0/(1.0+exp(-s))-1.0); // -1...137 double s = istate * sigmo; 38 if (s < -30.0) setState(-1); 39 else setState(2.0 / (1.0 + exp(-s)) - 1.0); // -1...1 40 40 } 41 41 42 42 void NI_StdUNeuron::calcOutput() 43 43 { 44 double s=istate * sigmo;45 if (s<-30.0) setState(0);46 else setState(1.0/(1.0+exp(-s))); // 0...144 double s = istate * sigmo; 45 if (s < -30.0) setState(0); 46 else setState(1.0 / (1.0 + exp(-s))); // 0...1 47 47 } -
cpp/frams/neuro/impl/neuroimpl-simple.h
r726 r791 11 11 extern ParamEntry NI_StdNeuron_tab[]; 12 12 13 class NI_StdNeuron : public NeuroImpl13 class NI_StdNeuron : public NeuroImpl 14 14 { 15 protected: 16 double istate, velocity; 17 void calcInternalState(); 18 virtual void calcOutput(); 19 public: 20 double inertia,force,sigmo; 21 NI_StdNeuron():velocity(0),inertia(0),force(0),sigmo(0) 22 {paramentries=NI_StdNeuron_tab;} 23 NeuroImpl* makeNew(){return new NI_StdNeuron();} // for NeuroFactory 24 int lateinit(); 25 void go(); 15 protected: 16 double istate, velocity; 17 void calcInternalState(); 18 virtual void calcOutput(); 19 public: 20 double inertia, force, sigmo; 21 NI_StdNeuron() :velocity(0), inertia(0), force(0), sigmo(0) 22 { 23 paramentries = NI_StdNeuron_tab; 24 } 25 NeuroImpl* makeNew(){ return new NI_StdNeuron(); } // for NeuroFactory 26 int lateinit(); 27 void go(); 26 28 }; 27 29 28 30 extern ParamEntry NI_StdUNeuron_tab[]; 29 31 30 class NI_StdUNeuron : public NI_StdNeuron32 class NI_StdUNeuron : public NI_StdNeuron 31 33 { 32 public: 33 NI_StdUNeuron() 34 {paramentries=NI_StdUNeuron_tab;} 35 NeuroImpl* makeNew(){return new NI_StdUNeuron();} // for NeuroFactory 36 void calcOutput(); 34 public: 35 NI_StdUNeuron() 36 { 37 paramentries = NI_StdUNeuron_tab; 38 } 39 NeuroImpl* makeNew(){ return new NI_StdUNeuron(); } // for NeuroFactory 40 void calcOutput(); 37 41 }; 38 42 39 class NI_Const : public NeuroImpl43 class NI_Const : public NeuroImpl 40 44 { 41 45 public: 42 NeuroImpl* makeNew(){return new NI_Const();} // for NeuroFactory43 int lateinit()46 NeuroImpl* makeNew(){ return new NI_Const(); } // for NeuroFactory 47 int lateinit() 44 48 { 45 neuro->state=newstate=1.0;46 simorder=0;47 return 1;49 neuro->state = newstate = 1.0; 50 simorder = 0; 51 return 1; 48 52 } 49 53 }; … … 51 55 class NI_Diff : public NeuroImpl 52 56 { 53 double previous; 54 55 NeuroImpl* makeNew() { return new NI_Diff(); };57 double previous; 58 public: 59 NeuroImpl* makeNew() { return new NI_Diff(); }; 56 60 57 void go()61 void go() 58 62 { 59 double s=getWeightedInputSum();60 setState(s-previous);61 previous=s;63 double s = getWeightedInputSum(); 64 setState(s - previous); 65 previous = s; 62 66 } 63 int lateinit()67 int lateinit() 64 68 { 65 NeuroImpl::lateinit();66 previous=neuro->state;67 return 1;69 NeuroImpl::lateinit(); 70 previous = neuro->state; 71 return 1; 68 72 } 69 73 }; … … 71 75 class NI_Random : public NeuroImpl 72 76 { 73 74 NeuroImpl* makeNew() { return new NI_Random(); };75 void go() {setState(rnd01*2.0-1.0);}77 public: 78 NeuroImpl* makeNew() { return new NI_Random(); }; 79 void go() { setState(rnd01*2.0 - 1.0); } 76 80 }; 77 81 … … 80 84 class NI_Sinus : public NeuroImpl 81 85 { 82 public: 83 double f0,t; 84 NeuroImpl* makeNew() { return new NI_Sinus(); }; 85 NI_Sinus():f0(0),t(0) 86 {paramentries=NI_Sinus_tab;} 87 void go() 86 public: 87 double f0, t; 88 NeuroImpl* makeNew() { return new NI_Sinus(); }; 89 NI_Sinus() :f0(0), t(0) 88 90 { 89 t+=f0+getWeightedInputSum(); 90 setState(sin(t)); 91 paramentries = NI_Sinus_tab; 92 } 93 void go() 94 { 95 t += f0 + getWeightedInputSum(); 96 setState(sin(t)); 91 97 } 92 98 }; 93 99 94 100 #endif 95
Note: See TracChangeset
for help on using the changeset viewer.