-
Notifications
You must be signed in to change notification settings - Fork 1
/
FuzzyControllerTests.html
277 lines (221 loc) · 8.97 KB
/
FuzzyControllerTests.html
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!Doctype HTML>
<html>
<head>
<META http-equiv="Content-Script-Type" content="text/javascript">
<link rel="stylesheet" href="lib/qunit.css">
<script src="fuzzy.controller.js"></script>
<script src="lib/jquery.js"></script>
<script src="lib/qunit.js"></script>
<style>
*{
font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Calibri, Helvetica, Arial, sans-serif;
}
h2{
color:#0d3349;
}
</style>
</head>
<body>
<div><h2>UNIT tests for FuzzyController classes</h2></div>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script>
var varX,varDX,varY;
function setup(){
varX = new FuzzyVariable();
//add some fuzzy sets to it
varX.L = new FuzzySet(0.3,0.4,0.5);
varX.M = new FuzzySet(0.4,0.5,0.6);
varX.H = new FuzzySet(0.5,0.6,0.7);
varDX = new FuzzyVariable();
//add some fuzzy sets to it
varDX.L = new FuzzySet(0.3,0.4,0.5);
varDX.M = new FuzzySet(0.4,0.5,0.6);
varDX.H = new FuzzySet(0.5,0.6,0.7);
varY = new FuzzyVariable();
//add some fuzzy sets to it
varY.L = new FuzzySet(0.3,0.4,0.5);
varY.M = new FuzzySet(0.4,0.5,0.6);
varY.H = new FuzzySet(0.5,0.6,0.7);
}
// FuzzySet tests
test( "Test area calculations", function(){
var set = new FuzzySet(0.4,0.5,0.6);
equal(Math.floor(set.getArea()*100+0.5),10,"The fuzzy area of set is (0.4,0.5,0.6) is 0.1");
set = new FuzzySet(0.1,0.5,0.9);
equal(Math.floor(set.getArea()*100+0.5),40,"The fuzzy area of set is (0.1,0.5,0.9) is 0.4");
set = new FuzzySet(0,0.5,1);
equal(Math.floor(set.getArea()*100+0.5),50,"The fuzzy area of set is (0.1,0.5,0.9) is 0.5");
});
test( "Test weighed area calculations", function(){
var set = new FuzzySet(0.4,0.5,0.6);
equal(Math.floor(set.getFuzzyArea()*100+0.5),0,"Default fuzzy value and area is 0");
set.setFuzzyValue(0.0);
equal(Math.floor(set.getFuzzyArea()*100+0.5),0,"Fuzzy area of 0 is 0");
set.setFuzzyValue(0.5);
equal(Math.floor(set.getFuzzyArea()*1000+0.5),75,"The fuzzy area of 0.5 is 0.075");
set.setFuzzyValue(1);
equal(Math.floor(set.getFuzzyArea()*100+0.5),10,"The fuzzy area of 1 is 0.1");
});
test("test calculateFuzzyValue method", function() {
var set = new FuzzySet(0.4,0.5,0.6);
equal(Math.floor(set.calculateFuzzyValue(0)),0,"The fuzzy value of 0 is 0");
equal(Math.floor(set.calculateFuzzyValue(0.4)),0,"The fuzzy value of 0.4 is 0");
equal(Math.floor(set.calculateFuzzyValue(0.45)*100+0.5),50,"The fuzzy value of 0.45 is 0.5");
equal(Math.floor(set.calculateFuzzyValue(0.5)*100+0.5),100,"The fuzzy value of 0.5 is 1");
equal(Math.floor(set.calculateFuzzyValue(0.55)*100+0.5),50,"The fuzzy value of 0.55 is 0.5");
equal(Math.floor(set.calculateFuzzyValue(0.6)*100+0.5),0,"The fuzzy value of 0.6 is 0");
equal(Math.floor(set.calculateFuzzyValue(0.7)*100+0.5),0,"The fuzzy value of 0.7 is 0");
equal(Math.floor(set.calculateFuzzyValue(1)*100+0.5),0,"The fuzzy value of 1 is 0");
});
test( "Fuzzification of value 0", function() {
setup();
varX.fuzzyfy(0.4);
//check the calculated fuzzy set value for each set
equal(varX.L.getFuzzyValue(),1,"The fuzzy value for L at 0.4 is 1");
equal(varX.M.getFuzzyValue(),0,"The fuzzy value for M at 0.4 is 0");
equal(varX.H.getFuzzyValue(),0,"The fuzzy value for H at 0.4 is 0");
});
test( "Fuzzification of value 0.45", function() {
setup();
varX.fuzzyfy(0.45);
equal(Math.floor(varX.L.getFuzzyValue()*10+0.5),5,"The fuzzy value for L at 0.25 is 0.5");
equal(Math.floor(varX.M.getFuzzyValue()*10+0.5),5,"The fuzzy value for M at 0.25 is 0.5");
equal(Math.floor(varX.H.getFuzzyValue()*10+0.5),0,"The fuzzy value for H at 0.25 is 0");
});
test( "Fuzzification of value 0.5", function() {
setup();
varX.fuzzyfy(0.5);
equal(Math.floor(varX.L.getFuzzyValue()*10+0.5),0,"The fuzzy value for L at 0.5 is 0");
equal(Math.floor(varX.M.getFuzzyValue()*10+0.5),10,"The fuzzy value for M at 0.5 is 1");
equal(Math.floor(varX.H.getFuzzyValue()*10+0.5),0,"The fuzzy value for H at 0.5 is 0");
});
test( "Fuzzification of value 0.55", function() {
setup();
varX.fuzzyfy(0.55);
equal(Math.floor(varX.L.getFuzzyValue()*10+0.5),0,"The fuzzy value for L at 0.55 is 0");
equal(Math.floor(varX.M.getFuzzyValue()*10+0.5),5,"The fuzzy value for M at 0.55 is 0.5");
equal(Math.floor(varX.H.getFuzzyValue()*10+0.5),5,"The fuzzy value for H at 0.55 is 0.5");
});
test( "Fuzzy rule fire test with undefined rule", function() {
//calculate the set values and bind the sets to the variable
var val=0;
setup();
varX.fuzzyfy(0.45);
varDX.fuzzyfy(0.4);
var rule1 = new FuzzyRule();
rule1.addExpr([varX.L,varDX.L]);
varY.L.rule = rule1;
var rule2 = new FuzzyRule();
rule2.addExpr([varX.H,varDX.L]);
varY.M.rule = rule2;
//do not define a y.H rule
varY.fireRules(); //fire all set rules for this variable
//check the output fuzzy set values
equal(Math.floor(varY.L.getFuzzyValue()*10+0.5),5,"The fuzzy output value y.L for rule L is 0.5");
equal(varY.M.getFuzzyValue(),0,"The fuzzy output value for y.M rule L is 0");
equal(varY.H.getFuzzyValue(),0,"The fuzzy output value for y.H rule L is undefined");
});
test( "Fuzzy rule fire test w/three rules", function() {
//define fuzzy variables and their sets
setup();
//define the rules
varY.L.rule = new FuzzyRule();
varY.L.rule.addExpr([varX.L,varDX.L]);
varY.M.rule = new FuzzyRule();
varY.M.rule.addExpr([varX.M,varDX.M]);
varY.H.rule = new FuzzyRule();
varY.H.rule.addExpr([varX.H,varDX.H]);
//calculate the set values
varX.fuzzyfy(0.5);
varDX.fuzzyfy(0.55);
//fire all set rules for this variable
varY.fireRules();
//check the output fuzzy set values
equal(varY.L.getFuzzyValue(),0,"The fuzzy output value rule y.L is 0");
equal(Math.floor(varY.M.getFuzzyValue()*10+0.5),5,"The fuzzy output value for rule y.M is 0.5");
equal(varY.H.getFuzzyValue(),0,"The fuzzy output value for rule y.H is ?");
});
test("test defuzzification method",function(){
var varY = new FuzzyVariable();
var outputVariable;
//add some fuzzy sets to it
varY.L = new FuzzySet(0.3,0.4,0.5);
varY.M = new FuzzySet(0.4,0.5,0.6);
varY.H = new FuzzySet(0.5,0.6,0.7);
varY.L.calculateFuzzyValue(0.4);
varY.M.calculateFuzzyValue(0);
varY.H.calculateFuzzyValue(0.6);
equal(Math.floor(varY.L.getFuzzyValue()*10+0.5),Math.floor(varY.H.getFuzzyValue()*10+0.5),"Areas of both sets should be equal");
equal(Math.floor(varY.L.getFuzzyArea()*10+0.5),1,"Weighed area should be 0.1");
equal(Math.floor(varY.H.getFuzzyArea()*10+0.5),1,"Weighed area should be 0.1");
outputVariable = varY.defuzzify();
equal(outputVariable,0.5,"Centroid value should be 0.5");
varY.M.calculateFuzzyValue(0.5);
equal(Math.floor(varY.M.getFuzzyArea()*10+0.5),1,"Weighed area should be 0.1");
outputVariable = varY.defuzzify();
equal(outputVariable,0.5,"Centroid value should be 0.5");
});
test("test #2 defuzzification method",function(){
var varY = new FuzzyVariable();
var outputVariable;
//add some fuzzy sets to it
varY.L = new FuzzySet(0.3,0.4,0.5);
varY.H = new FuzzySet(0.5,0.6,0.7);
varY.L.calculateFuzzyValue(0.34);
varY.H.calculateFuzzyValue(0.6);
var areaL = varY.L.getFuzzyArea();
var areaH = varY.H.getFuzzyArea();
equal(Math.floor(areaL*1000+0.5),64,"Weighed area should approx 0.061");
equal(Math.floor(areaH*10+0.5),1,"Weighed area should be 0.1");
outputVariable = varY.defuzzify();
equal(Math.floor(outputVariable*1000+0.5),522,"Centroid value should be approx 0.519");
});
test("test fuzzy controller",function(){
var y;
setup();
//add some rules...
//if x is low and dx is low then y is low
//if x is medium and dx is medium then y is medium
//if x is hing and dx is high then y is high
//varY.L.rule = new FuzzyRule([varX.L,varDX.L]);
varY.L.rule = new FuzzyRule();
varY.L.rule.addExpr([varX.L,varDX.L]);
varY.L.rule.addExpr([varX.L,varDX.M]);
varY.L.rule.addExpr([varX.L,varDX.H]);
varY.M.rule = new FuzzyRule();
varY.M.rule.addExpr([varX.M,varDX.L]);
varY.M.rule.addExpr([varX.M,varDX.M]);
varY.M.rule.addExpr([varX.M,varDX.H]);
varY.H.rule = new FuzzyRule();
varY.H.rule.addExpr([varX.H,varDX.L]);
varY.H.rule.addExpr([varX.H,varDX.M]);
varY.H.rule.addExpr([varX.H,varDX.H]);
//now some x,dx inputs that trigger the y.L rule
varX.fuzzyfy(0.4);
varDX.fuzzyfy(0.4);
//fire all rules on y
varY.fireRules();
//get the output
y = varY.defuzzify();
equal(Math.floor(y*10+0.5),4,"should trigger only the low rule");
//now trigger the y.M rule
varX.fuzzyfy(0.5);
varDX.fuzzyfy(0.5);
//fire all rules on y
varY.fireRules();
//get the output
y = varY.defuzzify();
equal(Math.floor(y*10+0.5),5,"should trigger only the medium rule");
//and finally trigger the y.H rule
varX.fuzzyfy(0.6);
varDX.fuzzyfy(0.6);
//fire all rules on y
varY.fireRules();
//get the output
y = varY.defuzzify();
equal(Math.floor(y*10+0.5),6,"should trigger only the medium rule");
});
</script>
</body>
</html>