Upstream release 4.02a
[rice.git] / tools / fuzzy.c
blob36e71e69ad4ea4cfa2e8ca1cfa4535dd7bc50799
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * RICE 4.0x Copyright (C) 1993 Rene' Jager *
3 * *
4 * *
5 * This toolbox is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This toolbox is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this toolbox; if not, write to the: *
17 * *
18 * Free Software Foundation, Inc. *
19 * 675 Mass Ave, Cambridge *
20 * MA 02139, USA. *
21 * *
22 * See the RICE documentation for more information on the toolbox. *
23 * The file COPYING for the complete GNU General Public License. *
24 * *
25 * You can reach me by (preferably e-mail): *
26 * *
27 * Rene' Jager *
28 * *
29 * Delft University of Technology *
30 * Department of Electrical Engineering *
31 * Control Laboratory *
32 * *
33 * Room ET 12.06 *
34 * *
35 * Mekelweg 4 *
36 * P.O.Box 5031 *
37 * 2600 GA Delft *
38 * The Netherlands *
39 * *
40 * e-mail: R.Jager@ET.TUDelft.NL *
41 * phone: +31-15-78 51 14 *
42 * fax: +31-15-62 67 38 *
43 * telex: 38151 butud nl *
44 * *
45 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
48 File: fuzzy.c
49 Author: Rene' Jager
50 Update: March 1, 1992
51 Info: utility source file, see file fuzzy.doc
55 /* header file */
57 #include <fuzzy.h>
60 /* macro's */
62 #define MAX(a,b) ((a) > (b) ? (a) : (b))
63 #define MIN(a,b) ((a) < (b) ? (a) : (b))
64 #define SQR(a) ((a) * (a))
67 /* smooth membership function(s) */
69 float fpi(float val, float p1, float p2, float p3, float p4)
71 if(p2 <= val && val <= p3)
72 return 1.0;
73 if(val <= p1 || p4 <= val)
74 return 0.0;
75 if(p1 < val && val <= (p1 + p2)/2.0)
76 return 2.0*SQR(val - p1)/SQR(p2 - p1);
77 if((p1 + p2)/2.0 <= val && val < p2)
78 return 1.0 - 2.0*SQR(val - p2)/SQR(p1 - p2);
79 if(p3 < val && val <= (p3 + p4)/2.0)
80 return 1.0 - 2.0*SQR(val - p3)/SQR(p3 - p4);
81 if((p3 + p4)/2.0 <= val && val < p4)
82 return 2.0*SQR(val - p4)/SQR(p3 - p4);
84 return 0.0;
88 /* hard membership function(s) */
90 float ftrapezium(float val, float p1, float p2, float p3, float p4)
92 if(p2 <= val && val <= p3)
93 return 1.0;
94 if(val <= p1 || p4 <= val)
95 return 0.0;
96 if(p1 < val && val < p2)
97 return (val - p1)/(p2 - p1);
98 if(p3 < val && val < p4)
99 return (p4 - val)/(p4 - p3);
101 return 0.0;
105 /* indexed-centre-of-gravity */
107 float ficog(int len, float *set, float lim)
109 register int i;
110 float up = 0.0, low = 0.0;
112 for(i = 1; i <= len; set++, i++)
113 if(*set >= lim)
115 up += *set*i;
116 low += *set;
119 up -= 1.0;
121 if(low == 0.0)
122 return -1.0;
124 return up/low;
128 /* centre-of-gravity */
130 float fcog(int len, float *set)
132 register int i;
133 float up = 0.0, low = 0.0;
135 for(i = 1; i <= len; set++, i++)
137 up += *set*i;
138 low += *set;
141 up -= 1.0;
143 if(low == 0.0)
144 return -1.0;
146 return up/low;
150 /* mean-of-maximum */
152 float fmom(int len, float *set)
154 int low = 0;
155 float up = 0.0, top;
157 top = fhgt(len, set);
158 set += len;
160 while(len)
162 if(*set == top)
164 up += len;
165 low++;
167 set--;
168 len--;
171 up -= 1.0;
173 if(low == 0)
174 return -1.0;
176 return up/low;
180 /* general norm operators */
182 float *fnorm(int len, float *dest, float *src, float (*norm)(float, float))
184 float *save = dest;
186 if(len < 0)
187 while(len++)
189 *dest = (*norm)(*dest, *src);
190 dest++;
192 else
193 while(len--)
195 *dest = (*norm)(*dest, *src);
196 dest++;
197 src++;
200 return save;
204 float *fxnorm(int len, float *dest, float *src,
205 float (*norm)(float, float, float), float par)
207 float *save = dest;
209 if(len < 0)
210 while(len++)
212 *dest = (*norm)(*dest, *src, par);
213 dest++;
215 else
216 while(len--)
218 *dest = (*norm)(*dest, *src, par);
219 dest++;
220 src++;
223 return save;
227 /* intersection operators */
229 float *fzand(int len, float *dest, float *src)
231 float *save = dest;
233 if(len < 0)
234 while(len++)
236 *dest = MIN(*dest, *src);
237 dest++;
239 else
240 while(len--)
242 *dest = MIN(*dest, *src);
243 dest++;
244 src++;
247 return save;
251 float *fpand(int len, float *dest, float *src)
253 float *save = dest;
255 if(len < 0)
256 while(len++)
258 *dest = *dest * *src;
259 dest++;
261 else
262 while(len--)
264 *dest = *dest * *src;
265 dest++;
266 src++;
269 return save;
273 float *fland(int len, float *dest, float *src)
275 float *save = dest;
277 if(len < 0)
278 while(len++)
280 *dest = MAX(*dest + *src - 1.0, 0.0);
281 dest++;
283 else
284 while(len--)
286 *dest = MAX(*dest + *src - 1.0, 0.0);
287 dest++;
288 src++;
291 return save;
295 /* union operators */
297 float *fzor(int len, float *dest, float *src)
299 float *save = dest;
301 if(len < 0)
302 while(len++)
304 *dest = MAX(*dest, *src);
305 dest++;
307 else
308 while(len--)
310 *dest = MAX(*dest, *src);
311 dest++;
312 src++;
315 return save;
319 float *fpor(int len, float *dest, float *src)
321 float *save = dest;
323 if(len < 0)
324 while(len++)
326 *dest = *dest + *src - *dest * *src;
327 dest++;
329 else
330 while(len--)
332 *dest = *dest + *src - *dest * *src;
333 dest++;
334 src++;
337 return save;
341 float *flor(int len, float *dest, float *src)
343 float *save = dest;
345 if(len < 0)
346 while(len++)
348 *dest = MIN(*dest + *src, 1.0);
349 dest++;
351 else
352 while(len--)
354 *dest = MIN(*dest + *src, 1.0);
355 dest++;
356 src++;
359 return save;
363 /* negation operator */
365 float *fnot(int len, float *dest)
367 float *save = dest;
369 while(len--)
371 *dest = 1.0 - *dest;
372 dest++;
375 return save;
379 /* height */
381 float fhgt(int len, float *set)
383 float value = 0.0;
385 while(len--)
387 value = MAX(value, *set);
388 set++;
391 return value;
395 /* alpha-cut and strong alpha-cut */
397 float *fcut(int len, float *dest, float *src, float cut)
399 float *save = dest;
401 while(len--)
403 *dest = (*src >= cut) ? *src : 0.0;
404 dest++;
405 src++;
408 return save;
412 float *fscut(int len, float *dest, float *src, float cut)
414 float *save = dest;
416 while(len--)
418 *dest = (*src > cut) ? *src : 0.0;
419 dest++;
420 src++;
423 return save;
428 #ifdef TEST_FUZZY
430 #include <stdio.h>
432 int main()
434 float val;
435 val = fpi(0.1, 1.0, 2.0, 3.0, 4.0);
436 printf("val = %f\n", val);
437 val = fpi(1.1, 1.0, 2.0, 3.0, 4.0);
438 printf("val = %f\n", val);
439 val = fpi(1.6, 1.0, 2.0, 3.0, 4.0);
440 printf("val = %f\n", val);
441 val = fpi(2.1, 1.0, 2.0, 3.0, 4.0);
442 printf("val = %f\n", val);
443 val = fpi(3.1, 1.0, 2.0, 3.0, 4.0);
444 printf("val = %f\n", val);
445 val = fpi(3.6, 1.0, 2.0, 3.0, 4.0);
446 printf("val = %f\n", val);
447 val = fpi(4.1, 1.0, 2.0, 3.0, 4.0);
448 printf("val = %f\n", val);
449 return 0;
452 #endif