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;
                      //這里在原來算法的基礎(chǔ)上不斷地將fi縮小,以避免跳來跳去一直達(dá)不到要求的點(diǎn)的效果。
                      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) 評(píng)論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 西和县| 镇平县| 攀枝花市| 宿迁市| 长岭县| 静宁县| 太湖县| 丰镇市| 栾川县| 平南县| 石门县| 綦江县| 西昌市| 高州市| 长武县| 荆门市| 德州市| 五河县| 乌鲁木齐市| 舒城县| 增城市| 嘉善县| 高要市| 灵璧县| 阳原县| 噶尔县| 宜丰县| 自贡市| 大悟县| 漾濞| 垫江县| 喀喇| 临沂市| 吴川市| 高邑县| 榆树市| 项城市| 宜良县| 瑞丽市| 屏边| 皮山县|