Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FuzzyController.cpp
00001 #include "FuzzyController.h" 00002 00003 FuzzyController::FuzzyController(void) 00004 { 00005 inputs = NULL; 00006 outputs = NULL; 00007 00008 inputsLenght = 0; 00009 outputsLenght = 0; 00010 00011 rulesAmount = 0; 00012 mfsIndexs = NULL; 00013 00014 outputsValues = NULL; 00015 } 00016 00017 FuzzyController::~FuzzyController(void) 00018 { 00019 } 00020 00021 void FuzzyController::addInput(FuzzyIO* input) 00022 { 00023 inputs = (FuzzyIO**) realloc(inputs, sizeof(FuzzyIO*) * (inputsLenght + 1)); 00024 00025 inputs[inputsLenght] = input; 00026 00027 inputsLenght++; 00028 } 00029 00030 void FuzzyController::addOutput(FuzzyIO* output) 00031 { 00032 outputs = (FuzzyIO**) realloc(outputs, sizeof(FuzzyIO*) * (outputsLenght + 1)); 00033 00034 outputs[outputsLenght] = output; 00035 00036 outputsLenght++; 00037 } 00038 00039 void FuzzyController::addRules(int rulesAmount, int** mfsIndexs) 00040 { 00041 this->rulesAmount = rulesAmount; 00042 this->mfsIndexs = mfsIndexs; 00043 } 00044 00045 float* FuzzyController::compute(float* values) 00046 { 00047 if (outputsValues == NULL) 00048 { 00049 inputsValues = new float[inputsLenght]; 00050 outputsValues = new float[outputsLenght]; 00051 00052 num = new float[outputsLenght]; 00053 den = new float[outputsLenght]; 00054 } 00055 00056 for (int i = 0; i < outputsLenght; i++) 00057 { 00058 num[i] = 0; 00059 den[i] = 0; 00060 } 00061 00062 for (int i = 0; i < rulesAmount; i++) 00063 { 00064 for (int j = 0; j < inputsLenght; j++) 00065 { 00066 inputsValues[j] = inputs[j]->getMFs(mfsIndexs[i][j])->getValue(values[j]); 00067 } 00068 00069 float m = min(inputsValues); 00070 00071 if (m == 0) 00072 continue; 00073 00074 for (int j = 0; j < outputsLenght; j++) 00075 { 00076 num[j] += m * outputs[j]->getMFs(mfsIndexs[i][inputsLenght + j])->centroid(m); 00077 den[j] += m; 00078 } 00079 } 00080 00081 for (int i = 0; i < outputsLenght; i++) 00082 { 00083 outputsValues[i] = num[i] / den[i]; 00084 } 00085 00086 return outputsValues; 00087 } 00088 00089 float FuzzyController::min(float* values) 00090 { 00091 float minValue = values[0]; 00092 00093 for (int i = 1; i < inputsLenght; i++) 00094 { 00095 if (values[i] < minValue) 00096 minValue = values[i]; 00097 } 00098 00099 return minValue; 00100 }
Generated on Tue Jul 19 2022 01:26:58 by
1.7.2