// sFLC_Sample1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "conio.h" #include "time.h" #include "math.h" #include using namespace std; inline double MaxAbs(double x, double y) { double ax = fabs(x); double ay = fabs(y); if (ax>ay) return x; else return y; } int _tmain(int argc, _TCHAR* argv[]) { const int NumFLC = 2; CFLC * FLC = new CFLC[NumFLC]; // instantiate a number of FL 'empty' controllers printf("\nFLC Name: %s %s\n", FLC[0].Name().c_str(), FLC[1].Name().c_str()); int sn = 1; FLC[0].SetShape(sn); // Defines a 5x5 FLC model FLC[1].SetShape(sn); int nNumFuzzyStates = (sn == 0) ? 3 : 5; // set sample fuzzy models int m_nDefaultFuzzySetModel1[3] = { 0, 0, 0 }; int m_nDefaultRuleSetModel1 = 0; int m_nDefaultFuzzySetModel2[3] = { 0, 0, 0 }; int m_nDefaultRuleSetModel2 = 0; // FLC1 cout << "\nFLC1"; cout << "\nEnter Default Fuzzy Set Model for Input 1.(Min 0, Max " << FLC[0].FSModelMax() << ")" ; cin >> m_nDefaultFuzzySetModel1[0]; cout << "\nEnter Default Fuzzy Set Model for Input 2. (Min 0, Max " << FLC[0].FSModelMax() << ")" ; cin >> m_nDefaultFuzzySetModel1[1]; cout << "\nEnter Default Fuzzy Set Model for Output. (Min 0, Max " << FLC[0].FSModelMax() << ")" ; cin >> m_nDefaultFuzzySetModel1[2]; cout << "\nEnter Default Rule Set Model. (Min 0, Max:" << FLC[0].RSModelMax()<< ")";; cin >> m_nDefaultRuleSetModel1; // FLC2 cout << "\nFLC2"; cout << "\nEnter Default Fuzzy Set Model for Input 1.(Min 0, Max " << FLC[0].FSModelMax() << ")" ; cin >> m_nDefaultFuzzySetModel2[0]; cout << "\nEnter Default Fuzzy Set Model for Input 2. (Min 0, Max " << FLC[0].FSModelMax() << ")" ; cin >> m_nDefaultFuzzySetModel2[1]; cout << "\nEnter Default Fuzzy Set Model for Output. (Min 0, Max " << FLC[0].FSModelMax() << ")" ; cin >> m_nDefaultFuzzySetModel2[2]; cout << "\nEnter Default Rule Set Model. (Min 0, Max:" << FLC[0].RSModelMax()<< ")";; cin >> m_nDefaultRuleSetModel2; FLC[0].SetRSModel(m_nDefaultRuleSetModel1); FLC[1].SetRSModel(m_nDefaultRuleSetModel2); CFLVariable *pVars1, *pVars2; CFLRule *pFLR1, *pFLR2; CFuzzySet *pFSet1, *pFSet2; int ret; // initialize and load 3 fuzzy sets for each FLC ret = FLC[0].InitFuzzySets(3); ret = FLC[1].InitFuzzySets(3); pFSet1 = FLC[0].FSArray(); pFSet2 = FLC[1].FSArray(); for (int j = 0; j < FLC[0].GetNumFuzzySets(); j++) { ret = pFSet1[j].InitFuzzySet(nNumFuzzyStates,0); ret = pFSet1[j].DefineFuzzySet(m_nDefaultFuzzySetModel1[j]); if (ret == 0 ) printf("\nFLC1 %i. FS%i is defined with model %i: %s (Wording:%i)", j+1, j, m_nDefaultFuzzySetModel1[j], pFSet1[j].GetFSmodelname().c_str(), pFSet1[j].GetFSwording()); } for (int j = 0; j < FLC[1].GetNumFuzzySets(); j++) { ret = pFSet2[j].InitFuzzySet(nNumFuzzyStates,0); ret = pFSet2[j].DefineFuzzySet(m_nDefaultFuzzySetModel2[j]); if (ret == 0 ) printf("\nFLC2 %i. FS%i is defined with model %i: %s (Wording:%i)", j+1, j, m_nDefaultFuzzySetModel2[j], pFSet2[j].GetFSmodelname().c_str(), pFSet2[j].GetFSwording()); } ret = FLC[0].InitVarSet(3); ret = FLC[1].InitVarSet(3); pVars1 = FLC[0].VarArray(); pVars2 = FLC[1].VarArray(); for (int j = 0; j < FLC[0].GetNumVars(); j++) { // Assuming here the last variable is the output BOOL bInputOutput = (j == FLC[0].GetNumVars() - 1) ? FALSE : TRUE; ret = pVars1[j].LoadFuzzySet(pFSet1[j],bInputOutput); if (ret == 0) printf("\nFLC1 Var%i (%i) has FS%i loaded (num states: %i)", j, bInputOutput, j, pVars1[j].NumStates() ); } for (int j = 0; j < FLC[1].GetNumVars(); j++) { // Assuming here the last variable is the output BOOL bInputOutput = (j == FLC[1].GetNumVars() - 1) ? FALSE : TRUE; ret = pVars2[j].LoadFuzzySet(pFSet2[j],bInputOutput); if (ret == 0) printf("\nFLC2 Var%i (%i) has FS%i loaded (num states: %i)", j, bInputOutput, j, pVars2[j].NumStates() ); } ret = FLC[0].DetermineRuleSetSize(); if (ret < 0) { printf("\nError: %i", ret); FLC[0].Reset(); FLC[1].Reset(); delete [] FLC; _getch(); return -1; } ret = FLC[1].DetermineRuleSetSize(); if (ret < 0) { printf("\nError: %i", ret); FLC[0].Reset(); FLC[1].Reset(); delete [] FLC; _getch(); return -1; } ret = FLC[0].InitRuleSet(); ret = FLC[1].InitRuleSet(); ret = FLC[0].AllocateRuleSpace(); ret = FLC[1].AllocateRuleSpace(); int NumRules = FLC[0].GetRuleSetSize(); pFLR1 = FLC[0].RuleArray(); pFLR2 = FLC[1].RuleArray(); ret = FLC[0].LoadDefault55RuleSet(pFLR1, NumRules,m_nDefaultRuleSetModel1); ret = FLC[1].LoadDefault55RuleSet(pFLR2, NumRules,m_nDefaultRuleSetModel2); double ArrInput1[2]; double ArrInput2[2]; ArrInput1[0] = 0.5; ArrInput1[1] = -0.25; ArrInput2[0] = ArrInput1[0]; ArrInput2[1] = 0; for (int cnt = 1; cnt < 30; cnt++) { ArrInput1[0] = log((double)(10*cnt)+1)/(cnt); FLC[0].Calculate(ArrInput1); ArrInput2[0] = MaxAbs(ArrInput1[0],FLC[1].Output()); ArrInput2[1] = -FLC[0].Output(); FLC[1].Calculate(ArrInput2); ArrInput1[1] = -MaxAbs( ArrInput1[0] , FLC[1].Output() ); printf("\n%i I11:%.3f I12:%.3f -> Out:%.3f --> I21:%.3f I22: %.3f -> Out: %.3f", cnt + 1, ArrInput1[0], ArrInput1[1], FLC[0].Output(), ArrInput2[0], ArrInput2[1], FLC[1].Output()); _getch(); } printf("\n\nPress a key to close"); FLC[0].Reset(); FLC[1].Reset(); delete [] FLC; _getch(); return 0; }