-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoFuzzy.cpp
210 lines (182 loc) · 6.27 KB
/
AutoFuzzy.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
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
// AutoFuzzy.cpp
#include "AutoFuzzy.h"
AutoFuzzy::AutoFuzzy()
{
varCount = 0;
ruleCount = 0;
}
void AutoFuzzy::addInput(const char* name, float min, float max)
{
if (varCount >= MAX_VARS)
return;
strncpy(vars[varCount].name, name, 19);
vars[varCount].name[19] = '\0';
vars[varCount].isInput = true;
vars[varCount].min = min;
vars[varCount].max = max;
vars[varCount].mfCount = 0;
varCount++;
}
void AutoFuzzy::addOutput(const char* name, float min, float max)
{
if (varCount >= MAX_VARS)
return;
strncpy(vars[varCount].name, name, 19);
vars[varCount].name[19] = '\0';
vars[varCount].isInput = false;
vars[varCount].min = min;
vars[varCount].max = max;
vars[varCount].mfCount = 0;
varCount++;
}
void AutoFuzzy::addTriangularMF(const char* varName, const char* mfName, float a, float b, float c)
{
// Find variable
int varIndex = -1;
for (int i = 0; i < varCount; i++) {
if (strcmp(vars[i].name, varName) == 0) {
varIndex = i;
break;
}
}
if (varIndex == -1 || vars[varIndex].mfCount >= MAX_MEMBERSHIP_FUNCTIONS)
return;
MembershipFunction& mf = vars[varIndex].mfs[vars[varIndex].mfCount];
strncpy(mf.name, mfName, 19);
mf.name[19] = '\0';
mf.type = 0; // Triangular
mf.params[0] = a;
mf.params[1] = b;
mf.params[2] = c;
vars[varIndex].mfCount++;
}
void AutoFuzzy::addTrapezoidalMF(const char* varName, const char* mfName, float a, float b, float c, float d)
{
// Find variable
int varIndex = -1;
for (int i = 0; i < varCount; i++) {
if (strcmp(vars[i].name, varName) == 0) {
varIndex = i;
break;
}
}
if (varIndex == -1 || vars[varIndex].mfCount >= MAX_MEMBERSHIP_FUNCTIONS)
return;
MembershipFunction& mf = vars[varIndex].mfs[vars[varIndex].mfCount];
strncpy(mf.name, mfName, 19);
mf.name[19] = '\0';
mf.type = 1; // Trapezoidal
mf.params[0] = a;
mf.params[1] = b;
mf.params[2] = c;
mf.params[3] = d;
vars[varIndex].mfCount++;
}
void AutoFuzzy::addRule(const char* ifVar, const char* ifMF, const char* thenVar, const char* thenMF)
{
if (ruleCount >= MAX_RULES)
return;
// Find variables and membership functions
int ifVarIndex = -1, thenVarIndex = -1;
int ifMFIndex = -1, thenMFIndex = -1;
for (int i = 0; i < varCount; i++) {
if (strcmp(vars[i].name, ifVar) == 0) {
ifVarIndex = i;
for (int j = 0; j < vars[i].mfCount; j++) {
if (strcmp(vars[i].mfs[j].name, ifMF) == 0) {
ifMFIndex = j;
break;
}
}
}
if (strcmp(vars[i].name, thenVar) == 0) {
thenVarIndex = i;
for (int j = 0; j < vars[i].mfCount; j++) {
if (strcmp(vars[i].mfs[j].name, thenMF) == 0) {
thenMFIndex = j;
break;
}
}
}
}
if (ifVarIndex == -1 || thenVarIndex == -1 || ifMFIndex == -1 || thenMFIndex == -1)
return;
rules[ruleCount].ifVar = ifVarIndex;
rules[ruleCount].ifMF = ifMFIndex;
rules[ruleCount].thenVar = thenVarIndex;
rules[ruleCount].thenMF = thenMFIndex;
ruleCount++;
}
float AutoFuzzy::calculateMembership(MembershipFunction& mf, float value)
{
if (mf.type == 0) { // Triangular
if (value <= mf.params[0] || value >= mf.params[2])
return 0;
if (value <= mf.params[1]) {
return (value - mf.params[0]) / (mf.params[1] - mf.params[0]);
}
return (mf.params[2] - value) / (mf.params[2] - mf.params[1]);
} else { // Trapezoidal
if (value <= mf.params[0] || value >= mf.params[3])
return 0;
if (value >= mf.params[1] && value <= mf.params[2])
return 1;
if (value < mf.params[1]) {
return (value - mf.params[0]) / (mf.params[1] - mf.params[0]);
}
return (mf.params[3] - value) / (mf.params[3] - mf.params[2]);
}
}
float AutoFuzzy::evaluate(float* inputs)
{
float outputSum = 0;
float weightSum = 0;
for (int i = 0; i < ruleCount; i++) {
float inputMembership = calculateMembership(
vars[rules[i].ifVar].mfs[rules[i].ifMF],
inputs[rules[i].ifVar]);
if (inputMembership > 0) {
// Use center of membership function as output value
float outputValue;
MembershipFunction& outMF = vars[rules[i].thenVar].mfs[rules[i].thenMF];
if (outMF.type == 0) { // Triangular
outputValue = outMF.params[1]; // Center of triangle
} else { // Trapezoidal
outputValue = (outMF.params[1] + outMF.params[2]) / 2; // Center of trapezoid
}
outputSum += outputValue * inputMembership;
weightSum += inputMembership;
}
}
return weightSum > 0 ? outputSum / weightSum : 0;
}
void AutoFuzzy::autoOptimize(int iterations)
{
// Simple genetic algorithm to optimize membership function parameters
const float mutationRate = 0.1;
const float mutationRange = 0.1;
for (int iter = 0; iter < iterations; iter++) {
// For each membership function
for (int i = 0; i < varCount; i++) {
for (int j = 0; j < vars[i].mfCount; j++) {
// Randomly mutate parameters
if (random(100) / 100.0 < mutationRate) {
MembershipFunction& mf = vars[i].mfs[j];
int paramCount = mf.type == 0 ? 3 : 4;
for (int k = 0; k < paramCount; k++) {
float range = (vars[i].max - vars[i].min) * mutationRange;
mf.params[k] += random(-range * 1000, range * 1000) / 1000.0;
// Keep within variable bounds
mf.params[k] = constrain(mf.params[k], vars[i].min, vars[i].max);
}
// Ensure parameters remain ordered
for (int k = 1; k < paramCount; k++) {
if (mf.params[k] < mf.params[k - 1]) {
mf.params[k] = mf.params[k - 1];
}
}
}
}
}
}
}