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 閱讀(866) 評(píng)論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 吉水县| 韶山市| 四子王旗| 太康县| 信宜市| 达拉特旗| 武穴市| 同心县| 绥芬河市| 蒲城县| 南投市| 吉林省| 通河县| 怀化市| 西华县| 治多县| 汽车| 离岛区| 察隅县| 元阳县| 湖南省| 淄博市| 平泉县| 彰化市| 彭州市| 沈丘县| 昌黎县| 瓮安县| 漠河县| 屏东市| 宣恩县| 克东县| 苍溪县| 古交市| 莱阳市| 云龙县| 景东| 唐山市| 泰来县| 龙井市| 龙川县|