-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuzzyLogic.hpp
More file actions
195 lines (173 loc) · 4.8 KB
/
FuzzyLogic.hpp
File metadata and controls
195 lines (173 loc) · 4.8 KB
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
#include <iostream>
using namespace std;
/*
range := '{' <numerical> ',' <numerical> ',' <numerical> '}'
used as: {center, max width, min width}
max width
[]
___
/ | \
____/ | \____
center
[ ]
min width
int val = 6;
int fuzzied0 = apply_rule( rule( on(range(10, 0, 10), 10),
on(range(20, 0, 10), 30),
on(range(30, 0, 10), 50) ), val);
int fuzzied1 = Rule< On<Range<10,0,10>, 10>,
On<Range<20,0,10>, 30>,
On<Range<30,0,10>, 50> >::apply(val);
*/
template<class T>
class DRange {
public:
typedef T ValueType;
private:
ValueType center;
ValueType maxWidth;
ValueType minWidth;
public:
ValueType getCenter() {return center;}
ValueType getMaxWidth() {return maxWidth;}
ValueType getMinWidth() {return minWidth;}
DRange(ValueType _center, ValueType _maxWidth, ValueType _minWidth) : center(_center), maxWidth(maxWidth), minWidth(_minWidth) {}
};
template<int Center, int MaxWidth, int MinWidth>
class CRange {
public:
typedef int ValueType;
const static ValueType getCenter() {return Center;}
const static ValueType getMaxWidth() {return MaxWidth;}
const static ValueType getMinWidth() {return MinWidth;}
CRange() {}
typedef double WeightType;
const static WeightType getWeight(ValueType x)
{
if( x <= getCenter() - getMinWidth() or
x >= getCenter() + getMinWidth() )
return 0.0;
if( x >= getCenter() - getMaxWidth() and
x <= getCenter() + getMaxWidth() )
return 1.0;
//y=ax+b
//a=(y1-y0)/(x1-x0)
//b=(y0*x1-x0*y1)/(x1-x0)
//with y1=1,y0=0:
//y=(x-x0)/(x1-x0)
if( x < getCenter() )
{
const ValueType x0 = getCenter() - getMinWidth();
const ValueType x1 = getCenter() - getMaxWidth();
return (WeightType(x-x0)/(x1-x0));
}
if( x > getCenter() )
{
const ValueType x0 = getCenter() + getMinWidth();
const ValueType x1 = getCenter() + getMaxWidth();
return (WeightType(x-x0)/(x1-x0));
}
throw "";
}
};
template<class T>
DRange<T> range(T center, T maxWidth, T minWidth)
{
return DRange<T>(center, maxWidth, minWidth);
}
template<int A, int B, int C>
CRange<A,B,C> range()
{
return CRange<A,B,C>();
}
template<int A, int B, int C>
class Range : public CRange<A,B,C> {};
template<class R, class V>
class DRangeOnValue {
public:
typedef R RangeType;
typedef V ValueType;
private:
RangeType range;
ValueType value;
public:
RangeType getRange() {return range;}
ValueType getValue() {return value;}
DRangeOnValue(RangeType _range, ValueType _value) : range(_range), value(_value) {}
};
template<class R, int v>
class CRangeOnValue {
public:
typedef R RangeType;
typedef int ValueType;
public:
static RangeType getRange() {return RangeType();}
static ValueType getValue() {return v;}
static ValueType getWeightedVal(ValueType val)
{
return RangeType::getWeight(val) * getValue();
}
CRangeOnValue() {}
};
template<class R, class V>
DRangeOnValue<R,V> on(R range, V value)
{
return DRangeOnValue<R,V>(range, value);
}
template<class R, int v>
CRangeOnValue<R,v> on()
{
return CRangeOnValue<R,v>();
}
template<class R, int v>
class On : public CRangeOnValue<R,v> {};
class Empty {
public:
class EmptyRange {
public:
template<class T>
static const float getWeight(T) {return 0;}
};
typedef EmptyRange RangeType;
template<class T>
static const T getWeightedVal(T) {return 0;}
};
template<class _On0=Empty, class _On1=Empty, class _On2=Empty, class _On3=Empty, class _On4=Empty, class _On5=Empty>
class CRule {
public:
typedef _On0 On0;
typedef _On1 On1;
typedef _On2 On2;
typedef _On3 On3;
typedef _On4 On4;
typedef _On5 On5;
public:
template<class T>
static T apply(T val)
{
T summedWeight = On0::RangeType::getWeight(val) + On1::RangeType::getWeight(val) + On2::RangeType::getWeight(val) +
On3::RangeType::getWeight(val) + On4::RangeType::getWeight(val) + On5::RangeType::getWeight(val);
T summedValue = On0::getWeightedVal(val) + On1::getWeightedVal(val) + On2::getWeightedVal(val) +
On3::getWeightedVal(val) + On4::getWeightedVal(val) + On5::getWeightedVal(val);
if( summedWeight != 0 )
return summedValue / summedWeight;
std::cerr << "Rule does not have coverage!\n";
return 0;
}
};
int main()
{
typedef Range<0x00, 0x00, 0x10> cblack;
typedef Range<0x20, 0x00, 0x18> cdark;
typedef Range<0x40, 0x00, 0x20> cdim;
typedef Range<0x90, 0x00, 0x50> cbright;
typedef Range<0xF0, 0x10, 0x50> clight;
typedef CRule< On<cblack, 0x00>,
On<cdark, 0xA0>,
On<cdim, 0xC0>,
On<cbright,0xF0>,
On<clight, 0xFF> > MoveLight;
for(float i = 0; i < 255; ++i)
std::cout << MoveLight::apply(i) << std::endl;
return 0;
}