home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / janzen / memfunc.hpp < prev   
C/C++ Source or Header  |  1993-09-01  |  4KB  |  174 lines

  1. EXAMPLE FOUR (4):
  2.  
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. class membership_function
  7. {
  8.     public:
  9.         virtual fuzzy membership(const double value)
  10.         {
  11.             return fuzzy(0.0);
  12.         }
  13.         virtual double weight(const fuzzy value)
  14.         {
  15.             return 0.0;
  16.         }
  17. };
  18.  
  19. class trapezoid_membership : public membership_function
  20. {
  21.     private:
  22.         double  left_zero,
  23.                 left_high,
  24.                 right_high, 
  25.                 right_zero;
  26.     public:
  27.         trapezoid_membership(   const double, 
  28.                                 const double,
  29.                                 const double,
  30.                                 const double);
  31.         void set(   const double left_zero_in, 
  32.                     const double left_high_in,
  33.                     const double right_high_in,
  34.                     const double right_zero);
  35.         void get(double *, double *, double *,
  36.                 double *) const;
  37.         fuzzy membership(const double value) const;
  38.         double weight(const weight);
  39. };
  40.  
  41. trapezoid_membership::trapezoid_membership(
  42.                      const double left_zero_in = 0.0, 
  43.                      const double left_high_in  = 0.0,
  44.                      const double right_high_in = 0.0,
  45.                      const double right_zero_in = 0.0)
  46. {
  47.     left_zero = left_zero_in;
  48.     left_high = left_high_in;
  49.     right_high = right_high_in;
  50.     right_zero = right_zero_in;
  51.     if (left_high < left_zero)
  52.     {
  53.         left_high = left_zero;
  54.     }
  55.     if (right_high < left_high)
  56.     {
  57.         right_high = left_high;
  58.     }
  59.     if (right_zero < right_high)
  60.     {
  61.         right_zero = right_high;
  62.     }
  63.     return;
  64. }
  65.  
  66. void trapezoid_membership::set( 
  67.              const double left_zero_in = 0.0, 
  68.              const double left_high_in = 0.0,
  69.              const double right_high_in = 0.0,
  70.              const double right_zero_in = 0.0)
  71. {
  72.     left_zero = left_zero_in;
  73.     left_high = left_high_in;
  74.     right_high = right_high_in;
  75.     right_zero = right_zero_in;
  76.  
  77.     if (left_high < left_zero)
  78.     {
  79.         left_high = left_zero;
  80.     }
  81.     if (right_high < left_high)
  82.     {
  83.         right_high = left_high;
  84.     }
  85.     if (right_zero < right_high)
  86.     {
  87.         right_zero = right_high;
  88.     }
  89.     return;
  90. }
  91.  
  92. void trapezoid_membership::get( double *left_zero_out,  
  93.                                 double *left_high_out,
  94.                                 double *right_high_out, 
  95.                                 double *right_zero_out) const
  96. {
  97.     *left_zero_out = left_zero;
  98.     *left_high_out = left_high;
  99.     *right_high_out = right_high;
  100.     *right_zero_out = right_zero;
  101.  
  102.     return;
  103. }
  104.  
  105. fuzzy trapezoid_membership::membership(
  106.              const double value) const
  107. {
  108.     auto fuzzy truth;
  109.  
  110.     if ((value <= left_zero) || (value >= right_zero))
  111.     {
  112.         truth = 0.0;
  113.     }
  114.     else
  115.     {
  116.         if ((value >= left_high) 
  117.          && (value <= right_high))
  118.         {
  119.             truth = 1.0;
  120.         }
  121.         else
  122.         {
  123.             if (value < left_high)
  124.             {
  125.                 truth = (value - left_zero)
  126.                       / (left_high - left_zero);
  127.             }
  128.             else
  129.             {
  130.                 if (value > right_high)
  131.                 {
  132.                     truth = 1.0
  133.                     - ((value - right_high) 
  134.                     / (right_zero - right_high));
  135.                 }
  136.             }
  137.         }   
  138.     }
  139.     return truth;
  140. }
  141.  
  142. double trapezoid_membership::weight(const fuzzy value)
  143. {
  144.     auto double a,
  145.                 b,
  146.                 h,
  147.                 area,
  148.                 right_x,
  149.                 left_x;
  150.  
  151.     h = value.get();
  152.  
  153.     b = right_zero - left_zero;
  154.  
  155.     if (value == 1.0)
  156.     {
  157.         a = right_high - left_high;
  158.         area = (a + b) / 2.0;
  159.     }
  160.     else
  161.     {
  162.         right_x = -h * (right_zero - right_high) 
  163.             + right_zero;
  164.         left_x = h * (left_high - left_zero) 
  165.             + left_zero;
  166.         a = fabs(right_x - left_x);
  167.         area = (a + b) * h / 2.0;
  168.     }
  169.     return area;
  170. }
  171.  
  172.  
  173.  
  174.