This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
164 lines (137 loc) · 4.77 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* File: main.cpp
* Author: Karl
*
* Created on 5 mars 2013, 17:30
*/
#include <cstdlib>
#include <stdexcept>
#include <vector>
#include "src/fuzzy/NotMinus1.h"
#include "src/fuzzy/AndMin.h"
#include "src/fuzzy/OrMax.h"
#include "src/fuzzy/ThenMin.h"
#include "src/fuzzy/AggMax.h"
#include "src/fuzzy/CogDefuzz.h"
#include "src/fuzzy/ThenSugeno.h"
#include "src/fuzzy/FuzzyFactory.h"
#include "src/fuzzy/IsTriangle.h"
#include "src/fuzzy/IsTrapese.h"
#include "src/fuzzy/IsTrapeseLeft.h"
#include "src/fuzzy/IsTrapeseRight.h"
#include "src/fuzzy/IsGaussian.h"
#include "src/core/Expression.h"
#include "src/core/ValueModel.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
//mamdaniTest();
//sugenoTest();
try{
//operators
fuzzy::OrMax<float> opOr;
fuzzy::ThenMin<float> opThen;
fuzzy::AggMax<float> opAgg;
//mamdani
fuzzy::CogDefuzz<float> opDefuzz(0, 26, 0.1);
//sugeno
fuzzy::SugenoDefuzz<float> opSugeno;
fuzzy::ThenSugeno<float> opThenSugeno;
vector<float> coefs;
coefs.push_back(1.6);
coefs.push_back(2.1);
fuzzy::SugenoDefuzzConclusion<float> opConclusion = fuzzy::SugenoDefuzzConclusion<float>(&coefs);
//unused operators (for factory init only)
fuzzy::NotMinus1<float> opNot;
fuzzy::AndMin<float> opAnd;
//fuzzy expession factory
fuzzy::FuzzyFactory<float> f(&opAnd, &opOr, &opNot, &opThen, &opAgg, &opDefuzz, &opSugeno, &opConclusion);
//membership function
fuzzy::IsGaussian<float> poor(0, 6);
fuzzy::IsGaussian<float> good(5, 6);
fuzzy::IsGaussian<float> excellent(10, 6);
fuzzy::IsTrapeseLeft<float> rancid(1, 4);
fuzzy::IsTrapeseRight<float> delicious(6, 9);
fuzzy::IsTriangle<float> cheap(0, 6, 12);
fuzzy::IsTriangle<float> average(7, 13, 19);
fuzzy::IsTriangle<float> generous(14, 20, 26);
//values
core::ValueModel<float> service(0); //0-10
core::ValueModel<float> food(0); // 0-10
core::ValueModel<float> tips(0); // 0-26
//Mamdani system
core::Expression<float> *r =
f.newAgg(
f.newAgg(
f.newThen(
f.newOr(
f.newIs(&service, &poor),
f.newIs(&food, &rancid)
),
f.newIs(&tips,&cheap)
),
f.newThen(
f.newIs(&service,&good),
f.newIs(&tips,&average)
)
),
f.newThen(
f.newOr(
f.newIs(&service, &excellent),
f.newIs(&food, &delicious)
),
f.newIs(&tips,&generous)
)
);
//Mamdani system defuzzification
core::Expression<float> *system = f.newMamdaniDefuzz(&tips, r);
//Apply values
service.setValue(3);
food.setValue(8);
//Print mamdani result
cout << "Mamdani tip result : " << endl << system->evaluate() << endl;
//Change then for sugeno
f.changeThen(&opThenSugeno);
//Sugeno conclusions
vector<core::Expression<float>*> sugenoConcServiceFood;
sugenoConcServiceFood.push_back(&service);
sugenoConcServiceFood.push_back(&food);
vector<core::Expression<float>*> sugenoConcService;
sugenoConcService.push_back(&service);
//Sugeno system (use nary)
vector<core::Expression<float>*> sRules;
sRules.push_back(
f.newThen(
f.newOr(
f.newIs(&service,&poor),
f.newIs(&food,&rancid)
),
f.newConclusion(&sugenoConcServiceFood)
)
);
sRules.push_back(
f.newThen(
f.newIs(&service,&good),
f.newConclusion(&sugenoConcService)
)
);
sRules.push_back(
f.newThen(
f.newOr(
f.newIs(&service,&excellent),
f.newIs(&food,&delicious)
),
f.newConclusion(&sugenoConcServiceFood)
)
);
//Sugeno system defuzzification
core::Expression<float> *systemSugeno = f.newSugeno(&sRules);
//Print sugeno result
cout << "Sugeno tip result : " << endl << systemSugeno->evaluate();
} catch(const std::exception& ex){
cout << ex.what();
}
return 0;
}