-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOGDefuzzVarObj.cpp
More file actions
210 lines (173 loc) · 4.8 KB
/
Copy pathCOGDefuzzVarObj.cpp
File metadata and controls
210 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// File: COGDefuzzVarObj.h
//
// Purpose: Variable object for Center of Gravity Defuzzification method
//
// Copyright © 2001 Louder Than A Bomb! Software
//
//
// This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
// It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
//
#include "COGDefuzzVarObj.h"
#include "COGDefuzzSetObj.h"
#include "FuzzyOutSet.h"
#include "FuzzyOutVariable.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#include "debug.h"
#endif
//
// Function: COGDefuzzVarObj()
//
// Purpose: Constructor for COGDefuzzVarObj
//
// Arguments:
//
// FuzzyOutVariable* _parent - Output variable this object is part of
//
// Returns:
//
// nothing
//
// Author: Michael Zarozinski
// Date: 8/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
COGDefuzzVarObj::COGDefuzzVarObj(FuzzyOutVariable* _parent) : DefuzzVarObj(_parent), FFLLBase(_parent)
{
}
//
// Function: ~COGDefuzzVarObj()
//
// Purpose: Destructor for COGDefuzzVarObj
//
// Arguments:
//
// none
//
// Returns:
//
// nothing
//
// Author: Michael Zarozinski
// Date: 8/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
COGDefuzzVarObj::~COGDefuzzVarObj()
{
}
//
// Function: calc_value()
//
// Purpose: Calculate the defuzzified output value (using the COG
// defuzzification method) for the output variable.
//
// Arguments:
//
// DOMType* out_set_dom_arr - Array that holds the DOM value for each
// set in the output variable
//
// Returns:
//
// RealType - the defuzzified output value. FLT_MIN is returned if no output sets are active
//
// Author: Michael Zarozinski
// Date: 8/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
RealType COGDefuzzVarObj::calc_value(DOMType* out_set_dom_arr )
{
RealType area_sum = 0.0; // sum of the area
RealType moment_sum = 0.0; // sum of the moments
RealType divisor = 0.0; // value to divide cog_sum by
RealType tmp_area = 0.0; // tmp var
COGDefuzzSetObj* defuzz; // defuzzification object for the set
int cog_idx; // center of gravity index
FuzzyOutVariable* parent = get_parent();
int num_of_sets = parent->get_num_of_sets();
// each set has it's individual COG components (area and moment) calculated
// for every possible DOM. These calculations treat each set as a point mass.
// Now we have multiple sets to find the COG for, with each treated as a
// point mass, we can calculate the COG via the simple formula:
//
// Sum of Moments
// ---------------
// Sum of Mass (Note: mass is same as area)
//
// See COGDefuzzVarObj.h for a more detailed expanation of this formula and
// how we use it.
for (int i = 0; i < num_of_sets; ++i)
{
defuzz = get_set_defuzz_obj(i);
if (defuzz == NULL)
continue; // nothing to calc for this set
cog_idx = out_set_dom_arr[i];
if (cog_idx == 255)
cog_idx = 0;
tmp_area = defuzz->get_area(cog_idx);
if (tmp_area)
{
area_sum += tmp_area;
moment_sum += defuzz->get_moment(cog_idx);
divisor++;
}
} // end loop thru sets
if (!divisor)
{
// if no output sets were active, return FLT_MIN - the special value that
// ensures we know that there is no output
return FLT_MIN; // return so we don't div by 0 below
} // end if no divisor
RealType left_x = parent->get_left_x();
// be sure to account for the left x (start of the var)
return (left_x + (moment_sum / area_sum));
} // end COGDefuzzVarObj::calc_value()
//
// Function: get_set_defuzz_obj()
//
// Purpose: Return the defuzzification object of the correct type
//
// Arguments:
//
// int set_idx - index of the set to get the defuzzifiation object for
//
// Returns:
//
// COGDefuzzSetObj* - defuzzification object for the set
//
// Author: Michael Zarozinski
// Date: 8/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
COGDefuzzSetObj* COGDefuzzVarObj::get_set_defuzz_obj(int set_idx) const
{
FuzzyOutVariable* parent = get_parent();
FuzzyOutSet* set = parent->get_set(set_idx);
COGDefuzzSetObj* tmp_obj = dynamic_cast<COGDefuzzSetObj*>(set->get_defuzz_obj());
return tmp_obj;
} // end COGDefuzzVarObj::get_set_defuzz_obj()
/////////////////////////////////////////////////////////////////////
////////// Trivial Functions That Don't Require Headers /////////////
/////////////////////////////////////////////////////////////////////
int COGDefuzzVarObj::get_defuzz_type() const
{
return DefuzzVarObj::DEFUZZ_COG;
};