Skip to content

Commit ab70362

Browse files
committed
Fuzzy "Completo"
Faltam testes exaustivos, correção de bugs... Estimo que esteja a 90%
1 parent e7dc504 commit ab70362

14 files changed

+158
-67
lines changed

FuzzyLogic.pro.user

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.2.1, 2017-05-04T14:02:38. -->
3+
<!-- Written by QtCreator 4.2.1, 2017-05-05T14:09:10. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

debug/FuzzyLogic.exe

14.7 KB
Binary file not shown.

debug/mainwindow.o

20 KB
Binary file not shown.

debug/moc_mainwindow.o

-12 Bytes
Binary file not shown.

fuzzy.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace ModelHandler {
1313
std::string str;
1414
public:
1515
modelString(){}
16+
~modelString(){str.clear();}
1617
modelString(std::string str) {this->str = str;}
1718
std::string getString(){return this->str;}
1819
void setString(std::string str){this->str = str;}
@@ -56,20 +57,23 @@ namespace ModelHandler {
5657

5758
void removeRules();
5859

60+
LinAlg::Matrix<Type> membershipFunctionPosition (std::map<std::string, std::map<std::string, advancedModelHandler::MembershipFunction<double> *> > &MF, const std::string &firstKey, const std::string &secondKey);
61+
LinAlg::Matrix<Type> membershipFunctionQuantities(std::map<std::string, std::map<std::string, advancedModelHandler::MembershipFunction<double> *> > &MF);
62+
5963
unsigned getMaxNumberOfMembershipFunctions(std::map<std::__cxx11::string, std::map<std::__cxx11::string, advancedModelHandler::MembershipFunction<double> *> > &Input);
6064

6165
LinAlg::Matrix<Type> fuzzyfication( LinAlg::Matrix<Type> Input);
6266

63-
LinAlg::Matrix<Type> rulesExecute(LinAlg::Matrix<Type> Output);
67+
LinAlg::Matrix<Type> rulesExecute(LinAlg::Matrix<Type> fuzzyficatedValue);
6468

65-
void defuzzyfication();
69+
LinAlg::Matrix<Type> defuzzyfication(LinAlg::Matrix<Type> rulesMatrix);
6670

6771
Type maxV(Type a, Type b);
6872
Type minV(Type a, Type b);
6973

7074
Type sim(Type x){}
7175
Type sim(Type x, Type y){}
72-
LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> x){}
76+
LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> x);
7377
LinAlg::Matrix<Type> sim(Type lmin, Type lmax, Type step){}
7478
LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> x, LinAlg::Matrix<Type> y){}
7579

fuzzy.hpp

+114-40
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,48 @@ void ModelHandler::Fuzzy<Type>::addInputMF(std::string InputName, std::string MF
1616

1717
}
1818

19+
template <typename Type>
20+
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::membershipFunctionQuantities(std::map<std::string, std::map<std::string, advancedModelHandler::MembershipFunction<double> *> > &MF)
21+
{
22+
std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>* > >::reverse_iterator iter = MF.rbegin();
23+
unsigned n = MF.size();
24+
LinAlg::Matrix<Type> ret(n,1);
25+
for(unsigned i = 1; i <= n; ++i)
26+
{
27+
ret(i,1) = iter->second.size();
28+
iter++;
29+
}
30+
return ret;
31+
}
32+
33+
template <typename Type>
34+
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::membershipFunctionPosition(std::map<std::string, std::map<std::string, advancedModelHandler::MembershipFunction<double> *> > &MF, const std::string &firstKey, const std::string &secondKey)
35+
{
36+
std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>* > >::reverse_iterator iterInputs = MF.rbegin();
37+
unsigned n = MF.size();
38+
LinAlg::Matrix<Type> ret(1,2);
39+
for(unsigned i = 1; i <= n; ++i)
40+
{
41+
if(iterInputs->first == firstKey)
42+
{
43+
ret(1,1) = i;
44+
std::map< std::string, advancedModelHandler::MembershipFunction<double>* >::reverse_iterator iterMF = iterInputs->second.rbegin();
45+
unsigned n2 = iterInputs->second.size();
46+
for(unsigned j = 1; j <= n2; ++j)
47+
{
48+
if(iterMF->first == secondKey)
49+
{
50+
ret(1,2) = j;
51+
return ret;
52+
}
53+
iterMF++;
54+
}
55+
}
56+
iterInputs++;
57+
}
58+
return ret;
59+
}
60+
1961
template <typename Type>
2062
void ModelHandler::Fuzzy<Type>::addOutputMF(std::string OutputName,
2163
std::string MFName,
@@ -129,49 +171,80 @@ std::string ModelHandler::Fuzzy<Type>::viewRules()
129171
}
130172

131173
template <typename Type>
132-
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> Output)
174+
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> fuzzyficatedValue)
133175
{
134-
// Onde Paramos: como definir quem é saida e regra no indice de ret;
135-
// verificar OR é min e AND é max?
136-
// Permitir o uso de maxV e minV
137-
// unsigned sizeOfRules = this->rules.size();
138-
// unsigned numberOfOutputs = this->outputMF.size();
139-
LinAlg::Matrix<Type> ret(this->OutputMF.size(),this->rules.getNumberOfRows()/2);
140-
for(unsigned i = 1; i <= this->rules.getNumberOfRows()/2; ++i)
176+
unsigned sumOfMFOutputs = 0;
177+
LinAlg::Matrix<Type> outputQuantities = membershipFunctionQuantities(this->OutputMF);
178+
for(unsigned i = 1; i <= outputQuantities.getNumberOfRows(); ++i)
179+
sumOfMFOutputs += outputQuantities(i,1);
180+
181+
LinAlg::Matrix<Type> ret(sumOfMFOutputs, this->rules.getNumberOfRows()/2);
182+
183+
for(unsigned i = 1; i <= this->rules.getNumberOfRows(); i += 2)
141184
{
142-
if(this->rules(1,1).getString() == "and")
143-
continue;
144-
else if(this->rules(1,1).getString() == "or")
145-
continue;
146-
// Algoritmo aqui deve ser:
147-
// ret deve receber o valor correspondente a qual entrada estiver na string e/ou a outra entrada
148-
// saber qual entrada e saida está na regra
149-
// realizar as operações e ou ou
150-
// atribuir a ret na sequência: às saídas que as regras afetam;
185+
Type tempMFValue;
186+
for(unsigned j = 2; j <= rules.getNumberOfColumns(); ++j)
187+
{
188+
LinAlg::Matrix<Type> inputPosition = this->membershipFunctionPosition(this->InputMF,this->rules(i,j).getString(),this->rules(i+1,j).getString());
189+
if(!LinAlg::isEqual(inputPosition,LinAlg::Matrix<Type>("0,0")))
190+
{
191+
if(j == 2)
192+
tempMFValue = fuzzyficatedValue(inputPosition(1,1),inputPosition(1,2));
193+
else if(this->rules(i,1).getString() == "and")
194+
tempMFValue = this->minV(tempMFValue,fuzzyficatedValue(inputPosition(1,1),inputPosition(1,2)));
195+
else if(this->rules(i,1).getString() == "or")
196+
tempMFValue = this->maxV(tempMFValue,fuzzyficatedValue(inputPosition(1,1),inputPosition(1,2)));
197+
198+
continue;
199+
}
200+
201+
LinAlg::Matrix<Type> outputPosition = this->membershipFunctionPosition(this->OutputMF,this->rules(i,j).getString(),this->rules(i+1,j).getString());
202+
if(!LinAlg::isEqual(outputPosition,LinAlg::Matrix<Type>("0,0")))
203+
{
204+
sumOfMFOutputs = 0;
205+
for(unsigned k = 2; k <= outputPosition(1,1); ++k)
206+
sumOfMFOutputs += outputQuantities(k-1,1);
207+
ret(outputPosition(1,2) + sumOfMFOutputs,(i+1)/2) = tempMFValue;
208+
}
209+
}
210+
}
211+
return ret;
212+
}
213+
214+
template <typename Type>
215+
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::defuzzyfication(LinAlg::Matrix<Type> rulesMatrix)
216+
{
217+
LinAlg::Matrix<Type> outputQuantities = membershipFunctionQuantities(this->OutputMF);
218+
LinAlg::Matrix<double> ret(outputQuantities.getNumberOfRows(),1);
219+
std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>* > >::reverse_iterator iterOutputs = this->OutputMF.rbegin();
220+
unsigned counter = 1;
221+
for(unsigned i = 1; i <= outputQuantities.getNumberOfRows(); ++i)
222+
{
223+
std::map< std::string, advancedModelHandler::MembershipFunction<double>* >::reverse_iterator iter = iterOutputs->second.rbegin();
224+
unsigned n2 = iterOutputs->second.size();
225+
Type den = 0, num = 0;
226+
for(unsigned j = 1; j <= n2; ++j)
227+
{
228+
for(unsigned k = 1; k <= rulesMatrix.getNumberOfColumns(); ++k)
229+
{
230+
num += rulesMatrix(counter,k)*iter->second->getAverage();
231+
den += rulesMatrix(counter,k);
232+
}
233+
counter++;
234+
iter++;
235+
}
236+
if(den != 0)
237+
ret(i,1) = num/den;
238+
iterOutputs++;
151239
}
152-
// std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
153-
// if(iter->first == "and")
154-
// {
155-
// iter++;
156-
// for(unsigned i = 1; i < sizeOfRules; i += 2)
157-
// {
158-
// std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
159-
// for esse for é para iterar as entradas na regra
160-
// ret(saida,regra) = maxV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
161-
// iter++;
162-
// }
163-
// }
164-
// else if(iter->first == "or")
165-
// {
166-
// iter++;
167-
// for(unsigned i = 1; i < sizeOfRules; i += 2)
168-
// {
169-
// std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
170-
// // for esse for é para iterar as entradas na regra
171-
// ret(saida,regra) = minV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
172-
// iter++;
173-
// }
174-
// }
240+
return ret;
241+
}
242+
243+
template <typename Type>
244+
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::sim(LinAlg::Matrix<Type> x)
245+
{
246+
this->fuzzyficatedValue = this->fuzzyfication(x);
247+
return this->defuzzyfication(this->rulesExecute(this->fuzzyficatedValue));
175248
}
176249

177250
template <typename Type>
@@ -191,3 +264,4 @@ Type ModelHandler::Fuzzy<Type>::minV(Type a, Type b)
191264
else
192265
return a;
193266
}
267+

gaussiana.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace advancedModelHandler {
1212
gaussiana(Type c, Type sigma);
1313
Type sim(Type input);
1414
LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> input);
15+
Type getAverage(){return this->c;}
1516
};
1617
}
1718

mainwindow.cpp

+11-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void MainWindow::on_simularButton_clicked()
1818
identifyVar(ui->lineEquacao->text().toStdString());
1919
}
2020

21-
MainWindow::calTF(std::string valor){
21+
void MainWindow::calTF(std::string valor){
2222

2323
std::string pol = valor;
2424
std::string num = pol.substr(0,pol.find('/'));
@@ -36,7 +36,7 @@ MainWindow::calTF(std::string valor){
3636
plotyG2 = new PlotHandler::plot<double> (Y, ui->widget_2);
3737
}
3838

39-
MainWindow::identifyVar(std::string str)
39+
void MainWindow::identifyVar(std::string str)
4040
{
4141
std::size_t pos = str.find('=');
4242
std::string var = str.substr(0,pos);
@@ -63,22 +63,17 @@ void MainWindow::on_pushButton_clicked()
6363
F.addInputMF("Bebida","+o- ", new advancedModelHandler::triangular<double>(5,6,7));
6464

6565
F.addOutputMF("Jogar","Fora", new advancedModelHandler::triangular<double>(5,6,7));
66+
F.addOutputMF("Jogar","Dentro", new advancedModelHandler::triangular<double>(3,4,5));
6667

67-
std::string str; str << F.fuzzyfication("2;4");
68-
ui->textEdit->append(str.c_str());
69-
str.clear();
7068
F.addRules("or,Comida:salgada,Bebida:ruim,Jogar:Fora");
71-
F.addRules("and,Comida:salgada,Bebida:boa,Jogar:Fora");
72-
ui->textEdit->append(F.viewRules().c_str());
73-
// F.addInputMF("Comida","doce", new advancedModelHandler::triangular<double>(1,2,3));
74-
// std::map<std::string, advancedModelHandler::MembershipFunction<double>*> M = F.getInputMF("Comida");
75-
// for(std::map<std::string,advancedModelHandler::MembershipFunction<double>*>::iterator iter = M.begin(); iter != M.end(); ++iter)
76-
// {
77-
// std::string k = iter->first;
78-
//// advancedModelHandler::MembershipFunction y = iter->second;
79-
// ui->textEdit->append(k.c_str());
80-
// }
81-
69+
F.addRules("or,Comida:salgada,Bebida:boa,Jogar:Dentro");
8270

71+
std::string str; str << F.sim(ui->lineEdit->text().toStdString().c_str());
72+
ui->textEdit->append(str.c_str());
8373

74+
// std::cout << F.fuzzyfication("1.5;1.5") << std::endl;
75+
// LinAlg::Matrix<double> rules = F.rulesExecute();
76+
// std::cout << rules << std::endl;
77+
// std::cout << F.defuzzyfication(rules) << std::endl;
78+
// ui->textEdit->append(F.viewRules().c_str());
8479
}

mainwindow.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ private slots:
2727

2828
private:
2929
Ui::MainWindow *ui;
30-
identifyVar(std::string str);
31-
calTF(std::string valor);
30+
void identifyVar(std::string str);
31+
void calTF(std::string valor);
3232
PlotHandler::plot<double> *plotyG1;
3333
PlotHandler::plot<double> *plotyG2;
3434
std::vector<std::string> x;

mainwindow.ui

+13-3
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@
209209
<widget class="QPushButton" name="pushButton">
210210
<property name="geometry">
211211
<rect>
212-
<x>130</x>
213-
<y>160</y>
212+
<x>200</x>
213+
<y>150</y>
214214
<width>75</width>
215215
<height>23</height>
216216
</rect>
217217
</property>
218218
<property name="text">
219-
<string>PushButton</string>
219+
<string>Fuzzy</string>
220220
</property>
221221
</widget>
222222
<widget class="QTextEdit" name="textEdit">
@@ -229,6 +229,16 @@
229229
</rect>
230230
</property>
231231
</widget>
232+
<widget class="QLineEdit" name="lineEdit">
233+
<property name="geometry">
234+
<rect>
235+
<x>80</x>
236+
<y>150</y>
237+
<width>113</width>
238+
<height>20</height>
239+
</rect>
240+
</property>
241+
</widget>
232242
</widget>
233243
</widget>
234244
</widget>

membershipfunction.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace advancedModelHandler {
1010
MembershipFunction(){}
1111
virtual Type sim(Type x) = 0;
1212
virtual LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> x) = 0;
13+
virtual Type getAverage() = 0;
1314

1415
protected:
1516
Type maxV(Type a, Type b)

trapezoidal.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace advancedModelHandler {
1111
trapezoidal(Type leftLimit, Type rigthLimit, Type peak1, Type peak2);
1212
Type sim(Type x);
1313
LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> x);
14+
Type getAverage(){ return (this->peak1 + this->peak2)/2; }
1415
};
1516
}
1617

triangular.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace advancedModelHandler {
1111
triangular(Type leftLimit, Type peak, Type rigthLimit);
1212
Type sim(Type x);
1313
LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> x);
14+
Type getAverage(){ return this->peak; }
1415
};
1516
}
1617

ui_mainwindow.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Ui_MainWindow
4545
QWidget *tab_3;
4646
QPushButton *pushButton;
4747
QTextEdit *textEdit;
48+
QLineEdit *lineEdit;
4849

4950
void setupUi(QMainWindow *MainWindow)
5051
{
@@ -111,10 +112,13 @@ class Ui_MainWindow
111112
tab_3->setObjectName(QStringLiteral("tab_3"));
112113
pushButton = new QPushButton(tab_3);
113114
pushButton->setObjectName(QStringLiteral("pushButton"));
114-
pushButton->setGeometry(QRect(130, 160, 75, 23));
115+
pushButton->setGeometry(QRect(200, 150, 75, 23));
115116
textEdit = new QTextEdit(tab_3);
116117
textEdit->setObjectName(QStringLiteral("textEdit"));
117118
textEdit->setGeometry(QRect(80, 50, 201, 81));
119+
lineEdit = new QLineEdit(tab_3);
120+
lineEdit->setObjectName(QStringLiteral("lineEdit"));
121+
lineEdit->setGeometry(QRect(80, 150, 113, 20));
118122
tabWidget->addTab(tab_3, QString());
119123
MainWindow->setCentralWidget(centralWidget);
120124

@@ -139,7 +143,7 @@ class Ui_MainWindow
139143
tabWidget->setTabText(tabWidget->indexOf(tab), QApplication::translate("MainWindow", "Tab 1", Q_NULLPTR));
140144
label_5->setText(QApplication::translate("MainWindow", "Gr\303\241ficos", Q_NULLPTR));
141145
tabWidget->setTabText(tabWidget->indexOf(tab_2), QApplication::translate("MainWindow", "Tab 2", Q_NULLPTR));
142-
pushButton->setText(QApplication::translate("MainWindow", "PushButton", Q_NULLPTR));
146+
pushButton->setText(QApplication::translate("MainWindow", "Fuzzy", Q_NULLPTR));
143147
tabWidget->setTabText(tabWidget->indexOf(tab_3), QApplication::translate("MainWindow", "Page", Q_NULLPTR));
144148
} // retranslateUi
145149

0 commit comments

Comments
 (0)