sandeshMC

SDPTA

Apr 10th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. class SDPTA {
  5.  
  6.     public static void main(String args[]) {
  7.  
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         System.out.println("Enter number of the input vector");
  11.         float n = sc.nextFloat();
  12.  
  13.         ArrayList<ArrayList<Double>> x = new ArrayList<ArrayList<Double>>();
  14.         ArrayList<ArrayList<Double>> desired = new ArrayList<ArrayList<Double>>();
  15.  
  16.         for (int i = 0; i < n; i++) {
  17.  
  18.             System.out.println("Enter the input vector");
  19.             ArrayList<Double> temp1 = new ArrayList<Double>();
  20.             temp1.add(sc.nextDouble());
  21.             temp1.add(sc.nextDouble());
  22.             x.add(temp1);
  23.  
  24.             System.out.println("Enter the desired vector");
  25.             ArrayList<Double> temp = new ArrayList<Double>();
  26.             temp.add(sc.nextDouble());
  27.  
  28.             desired.add(temp);
  29.  
  30.         }
  31.  
  32.         ArrayList<Double> weight = new ArrayList<Double>();
  33.  
  34.         for (int i = 0; i < 2; i++) {
  35.             weight.add(1.0);
  36.         }
  37.  
  38.         System.out.println(weight + "Weight");
  39.  
  40.         double output = 0;
  41.  
  42.         int p = 0;
  43.         int k = 0;
  44.         double E = 0;
  45.         int flag = 0;
  46.         do {
  47.             output = mul(weight, x.get(p));
  48.  
  49.             if (output >= 0) {
  50.                 output = 1;
  51.             } else {
  52.                 output = 0;
  53.             }
  54. //System.out.println(output);
  55.  
  56.             double r = desired.get(p).get(0) - output;
  57.  
  58.             System.out.println(r + "is r");
  59.  
  60.             for (int i = 0; i < weight.size(); i++) {
  61.                 weight.set(i, weight.get(i) + 0.5 * 0.1 * r * x.get(p).get(i));
  62.             }
  63.  
  64.             if (flag == 0) {
  65.                 E += 0.5 * r * r;
  66.             } else {
  67.                 E = 0.5 * r * r;
  68.                 flag = 0;
  69.             }
  70.  
  71.             p++;
  72.             k++;
  73.  
  74.             if (p == n) {
  75.                 if (E == 0) {
  76.                     System.out.println("The input is " + x);
  77.                     System.out.println("The desired is " + desired);
  78.                     System.out.println("The weight is " + weight);
  79.                     System.out.println("k value is " + k);
  80.                 } else {
  81.                     E = 1000;
  82.                     flag = 1;
  83.                     p = 0;
  84.                     System.out.println("The weight is " + weight);
  85.                 }
  86.             }
  87.  
  88.         } while (E != 0);
  89.  
  90.         System.out.println("The input is " + x);
  91.         System.out.println("The desired is " + desired);
  92.         System.out.println("The weight is " + weight);
  93.         System.out.println("k value is " + k);
  94.  
  95.     }
  96.  
  97.     public static double mul(ArrayList<Double> weight, ArrayList<Double> x) {
  98.         int ans = 0;
  99.         for (int i = 0; i < weight.size(); i++) {
  100.             ans += weight.get(i) * x.get(i);
  101.         }
  102.         return ans;
  103.     }
  104.  
  105. }
  106.  
  107. /*Output:
  108.  The input is [[1.0, 1.0], [6.0, 3.0]]
  109.  The desired is [[0.0], [1.0]]
  110.  The weight is [0.5000000000000002, -1.0000000000000009]
  111.  k value is 141
  112.  */
Add Comment
Please, Sign In to add comment