march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0


          代碼如下:
          import java.util.Scanner;


          public class Perceptron {
              private static int N = 3;
              private static int n = 2;
              private static double[][] X = null;
              private static double[] Y = null;
              private static double[][] G = null;
              private static double[] A = null;
              private static double[] W = null;
              private static double B = 0;
              private static double fi = 0.5;
              
              private static boolean check(int id) {
                  double ans = B;
                  for(int i=0;i<N;i++)
                      ans += A[i] * Y[i] * G[i][id];
                  if(ans * Y[id] > 0) return true;
                  return false;
              }
              
              
              public static void solve() {
                  Scanner in = new Scanner(System.in);
                  System.out.print("input N:"); N = in.nextInt();
                  System.out.print("input n:"); n = in.nextInt();
                  
                  X = new double[N][n];
                  Y = new double[N];
                  G = new double[N][N];
                  
                  System.out.println("input N * n datas X[i][j]:");
                  for(int i=0;i<N;i++)
                      for(int j=0;j<n;j++)
                          X[i][j] = in.nextDouble();
                  System.out.println("input N datas Y[i]");
                  for(int i=0;i<N;i++) 
                      Y[i] = in.nextDouble();
                  
                  for(int i=0;i<N;i++)
                      for(int j=0;j<N;j++) {
                          G[i][j] = 0;
                          for(int k=0;k<n;k++)
                              G[i][j] += X[i][k] * X[j][k];
                      }
                  
                  A = new double[N];
                  W = new double[n];
                  for(int i=0;i<n;i++) A[i] = 0;
                  B = 0;
                  
                  boolean ok = true;
                  while(ok == true) {
                      ok = false;
                      //這里在原來算法的基礎上不斷地將fi縮小,以避免跳來跳去一直達不到要求的點的效果。
                      for(int i=0;i<N;i++) {
                          //System.out.println("here " + i);
                          while(check(i) == false) {
                              ok = true;
                              A[i] += fi;
                              B += fi * Y[i];
                              //debug();
                          }
                      }
                      fi *= 0.5;
                  }
                  
                  for(int i=0;i<n;i++)
                      W[i] = 0;
                  for(int i=0;i<N;i++)
                      for(int j=0;j<n;j++) 
                          W[j] += A[i] * Y[i] * X[i][j];
              }
              
              public static void main(String[] args) {
                  solve();
                  System.out.print("W = [");
                  for(int i=0;i<n-1;i++) System.out.print(W[i] + ", ");
                  System.out.println(W[n-1] + "]");
                  System.out.println("B = " + B);
              }
          }
          posted on 2015-03-20 11:34 marchalex 閱讀(862) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 泉州市| 庆云县| 盐边县| 九寨沟县| 灵宝市| 云南省| 合作市| 文安县| 木里| 阿巴嘎旗| 江城| 册亨县| 松滋市| 大同县| 沾益县| 永济市| 克拉玛依市| 子洲县| 文成县| 佛山市| 晋城| 铜鼓县| 宁强县| 馆陶县| 台州市| 永胜县| 九龙城区| 绥芬河市| 湛江市| 鹤山市| 绥宁县| 永胜县| 大足县| 仁寿县| 县级市| 天门市| 新宁县| 万载县| 青龙| 阳谷县| 龙胜|