2017-03-15 4 views
1

私は提供しているコードでDouble型の2次元配列をソートしようとしていますが、配列は0ではありません。ここでは、コード2次元配列(Double)ソートでは0の値が与えられます

Arrays.sort(merge, new Comparator<double[]>() { 
      @Override 
      public int compare(double[] o1, double[] o2) { 
       return Double.compare(o1[4], o2[4]); 
      } 
     }); 

をソート

は私の完全なコードですが、私は、配列に基づいてソートしたい[I] [4] すなわち第四のインデックス : -

public class EvoAlgo { 

static int k = 40;//generations 
static double al = 0, ah = 1, bl = -2, bh = 2, cl = -2, ch = 1, dl = 0.5, dh = 1; 
static double m1 = 0.02, m2 = 0.22, m3 = -0.11, m4 = 0.22; 

static double f(double a, double b, double c, double d) { 

    return (2 * a * a) - (2.5 * a * b * c * c) + (4 * b * c * d) + (0.25 * c * d) - (0.2 * d * d); 

} 

static double[] mutate(double[] child) { 
    double m = Math.random(); 
    if (m <= 0.25) { 
     child[0] = child[0] * m1; 
    } else if (m <= 0.5) { 
     child[1] = child[1] * m2; 

    } else if (m <= 0.75) { 
     child[2] = child[2] * m3; 

    } else { 
     child[3] = child[3] * m4; 
    } 
    child[4] = f(child[0], child[1], child[2], child[3]); 
    return child; 
} 

static double[][] initRandomPop() { 

    double[][] initPop = new double[25][5]; 
    for (int i = 0; i < initPop.length; i++) { 
     double ax = al + (Math.random() * ((ah - al) + 1)); 
     double bx = bl + (Math.random() * ((bh - bl) + 1)); 
     double cx = cl + (Math.random() * ((ch - cl) + 1)); 
     double dx = dl + (Math.random() * ((dh - dl) + 1)); 
     initPop[i][0] = ax; 
     initPop[i][1] = bx; 
     initPop[i][2] = cx; 
     initPop[i][3] = dx; 
     initPop[i][4] = f(ax, bx, cx, dx); 
    } 
    return initPop; 
} 

static double[][] merge(double[][] a, double[][] b) { 
    double[][] merge = new double[a.length + b.length][5]; 
    for (int i = 0; i < a.length; i++) { 
     merge[i] = a[i]; 
    } 
    for (int i = a.length; i < b.length; i++) { 
     merge[i] = a[i]; 
    } 
    return merge; 
} 

static double[][] newPop(double[][] a) { 
    double[][] newPop = new double[25][5]; 
    for (int i = 0; i < 25; i++) { 
     newPop[i] = a[i]; 
    } 
    return newPop; 
} 

public static void main(String[] args) { 

    double[][] initPop = initRandomPop(); 
    double[][] child = new double[40][5]; 
    int cc = 0; 
    for (int i = 0; i < k; i++) { 
     for (int j = 0; j < 20; j++) { 
      int ri1 = 0 + (int) (Math.random() * ((24 - 0) + 1)); 
      int ri2 = 0 + (int) (Math.random() * ((24 - 0) + 1)); 
      double[] ind1 = initPop[ri1]; 
      double[] ind2 = initPop[ri2]; 
      double[] c1 = new double[5]; 
      double[] c2 = new double[5]; 

      c1[0] = ind1[0]; 
      c1[1] = ind1[1]; 
      c1[2] = ind2[2]; 
      c1[3] = ind2[3]; 
      c1[4] = f(ind1[0], ind1[1], ind2[2], ind2[3]); 
      c2[0] = ind2[0]; 
      c2[1] = ind2[1]; 
      c2[2] = ind1[2]; 
      c2[3] = ind1[3]; 
      c2[4] = f(ind2[0], ind2[1], ind1[2], ind1[3]); 

      c1 = mutate(c1); 
      c2 = mutate(c2); 
      child[cc++] = c1; 
      child[cc++] = c2; 
     } 
     double[][] merge = merge(child, initPop); 

     Arrays.sort(merge, new Comparator<double[]>() { 
      @Override 
      public int compare(double[] o1, double[] o2) { 
       return Double.compare(o1[4], o2[4]); 
      } 
     }); 
     for (int j = 0; j < merge.length; j++) { 
      System.out.println(merge[j][4]); 
     } 
     initPop = newPop(merge); 
     cc = 0; 
    } 
    System.out.println("Fittest Person On Earth " + initPop[0][4]); 
} 

}

答えて

0

デフォルト値は0です。それはnullと比較することができるので、配列フィールドに値が与えられていない可能性が最も高いです。

0

ご迷惑をおかけして申し訳ありません!問題はmerge()メソッドにありました。 はこちらです

static double[][] merge(double[][] a, double[][] b) { 
    double[][] merge = new double[a.length + b.length][5]; 
    for (int i = 0; i < a.length; i++) { 
     merge[i] = a[i]; 
    } 
    int p = 0; 
    for (int i = a.length; i < merge.length; i++) { 
     merge[i] = b[p++]; 
    } 
    return merge; 
} 
関連する問題