sandeshMC

Set Operations on Fuzzy Sets

Jan 19th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class JavaApplication1 {
  6.  
  7.     static void printFuzzySet(double d[]) {
  8.  
  9.  
  10.         for (int i = 0; i < d.length; i++) {
  11.  
  12.             double newKB = Math.round(d[i] * 100.0) / 100.0;
  13.             System.out.print(newKB + "/" + (i + 1));
  14.             if(i != d.length-1)
  15.                 System.out.print("+");
  16.         }
  17.         System.out.println("\n");
  18.  
  19.     }
  20.  
  21.     static double[] complement(double[] d) {
  22.  
  23.         for (int i = 0; i < d.length; i++) {
  24.             d[i] = 1 - d[i];
  25.         }
  26.         return d;
  27.  
  28.     }
  29.  
  30.     static double[] union(double a[], double[] b) {
  31.         double[] union = new double[a.length];
  32.  
  33.         for (int i = 0; i < union.length; i++) {
  34.             if (a[i] < b[i]) {
  35.                 union[i] = b[i];
  36.             } else {
  37.                 union[i] = a[i];
  38.             }
  39.  
  40.         }
  41.  
  42.         return union;
  43.     }
  44.  
  45.     static double[] inter(double a[], double[] b) {
  46.         double[] inter = new double[a.length];
  47.  
  48.         for (int i = 0; i < inter.length; i++) {
  49.             if (a[i] > b[i]) {
  50.                 inter[i] = b[i];
  51.             } else {
  52.                 inter[i] = a[i];
  53.             }
  54.  
  55.         }
  56.  
  57.         return inter;
  58.     }
  59.  
  60.     static void difference(double[] a, double[] b) {
  61.         double[] diff = new double[a.length];
  62.  
  63.         b = complement(b);
  64.         diff = union(a, b);
  65.  
  66.         printFuzzySet(diff);
  67.     }
  68.  
  69.     /**
  70.      * @param args the command line arguments
  71.      */
  72.     public static void main(String[] args) {
  73.         // TODO code application logic here
  74.  
  75.         Scanner sc = new Scanner(System.in);
  76.         System.out.println("Enter the universal set limit: ");
  77.         int limit = sc.nextInt();
  78.  
  79.         double arr1[] = new double[limit];
  80.         double arr2[] = new double[limit];
  81.  
  82.         System.out.println("Enter the membership for each element in fuzzy set A");
  83.         for (int i = 0; i < limit; i++) {
  84.             System.out.println("For element " + (i + 1) + ":");
  85.             arr1[i] = sc.nextDouble();
  86.         }
  87.  
  88.         System.out.println("Enter the membership for each element in fuzzy set B");
  89.         for (int i = 0; i < limit; i++) {
  90.             System.out.println("For element " + (i + 1) + ":");
  91.             arr2[i] = sc.nextDouble();
  92.         }
  93.  
  94.         double union[] = union(arr1, arr2);
  95.  
  96.  
  97.         System.out.println("A \u222A B : ");
  98.         printFuzzySet(union);
  99.  
  100.         double inter[] = inter(arr1, arr2);
  101.  
  102.  
  103.         System.out.println("A \u2229 B : ");
  104.         printFuzzySet(inter);
  105.  
  106.         double[] arr1Comp = complement(arr1);
  107.         System.out.println("Complement of A is: ");
  108.         printFuzzySet(arr1Comp);
  109.         System.out.println("Complement of B is: ");
  110.         double[] arr2Comp = complement(arr2);
  111.         printFuzzySet(arr2Comp);
  112.  
  113.  
  114.         System.out.println("A - B: ");
  115.         difference(arr1, arr2);
  116.         System.out.println("B - A: ");
  117.         difference(arr2, arr1);
  118.        
  119.        
  120.         System.out.println("De Morgan's theorem: ");
  121.        
  122.         System.out.println(" Complement of A \u222A B is: ");
  123.        
  124.         double aub[] = complement(union(arr1,arr2));
  125.         printFuzzySet(aub);
  126.        
  127.         System.out.println(" ~A \u2229 ~B is: ");
  128.         aub = inter(complement(arr1),complement(arr2));
  129.         printFuzzySet(aub);
  130.  
  131.     }
  132.    
  133.    
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. /*
  141. run:
  142. Enter the universal set limit:
  143. 3
  144. Enter the membership for each element in fuzzy set A
  145. For element 1:
  146. 0.5
  147. For element 2:
  148. 0.6
  149. For element 3:
  150. 0.7
  151. Enter the membership for each element in fuzzy set B
  152. For element 1:
  153. 0.9
  154. For element 2:
  155. 0.8
  156. For element 3:
  157. 0.3
  158. A ∪ B :
  159. 0.9/1+0.8/2+0.7/3
  160.  
  161. A ∩ B :
  162. 0.5/1+0.6/2+0.3/3
  163.  
  164. Complement of A is:
  165. 0.5/1+0.4/2+0.3/3
  166.  
  167. Complement of B is:
  168. 0.1/1+0.2/2+0.7/3
  169.  
  170. A - B:
  171. 0.9/1+0.8/2+0.3/3
  172.  
  173. B - A:
  174. 0.9/1+0.8/2+0.7/3
  175.  
  176. De Morgan's theorem:
  177.  Complement of A ∪ B is:
  178. 0.1/1+0.2/2+0.3/3
  179.  
  180.  ~A ∩ ~B is:
  181. 0.1/1+0.2/2+0.3/3
  182. */
Add Comment
Please, Sign In to add comment