2015-04-20 61 views
0

我正試圖在java中實現roullete輪。在Java中用roullete輪定義概率

我創建了兩種方法。一個是創造roullete輪和另一個旋轉roullete輪。

我應該怎麼做,以實施probalibities。我的方法沒有這樣做。 我的方法來創建roullete輪是完全錯誤的。

例如,如果我得到:

50%的染色體與健身86

35%的染色體與健身88

15%,染色體與健身90

我怎樣才能確保這15%的人有更多機會被選中?

public void createRolette(Population population) throws Exception { 

    int swap; 

     for (int i=0; i<populationSize; i++){ 
      population.getIndividual(i); 
      Simulator.allocateTask(i); 
      rouletteId.add(i); 
      rouletteFit.add(calcFitness(i)); 
     } 


     for (int i = 0; i < rouletteFit.size() - 1; i++) { 
      for (int j = 0; j < rouletteFit.size() - i - 1; j++) { 
       if (rouletteFit.get(j) > rouletteFit.get(j+1)) 
       { 
        swap = rouletteFit.get(j); 
        rouletteFit.set(j, rouletteFit.get(j+1)); 
        rouletteFit.set(j+1, swap); 

        swap = rouletteId.get(j); 
        rouletteId.set(j, rouletteId.get(j+1)); 
        rouletteId.set(j+1, swap); 
       } 
      } 
     }  
     System.out.println(rouletteId.toString()); 
     System.out.println(rouletteFit.toString()); 

     } 



     public int rotateRolette(Population population) {   
       Integer SelectedIndv = (int)(Math.random()* (rouletteId.size())); // faz a escolha entre 0 e o numero maximo de elementos; 
       return rouletteId.get(SelectedIndv); 
     } 

回答

0

我不確定你的代碼是幹什麼的。但對於輪盤賭,你應該做的事端是這樣的:

  • 創建一個數組把全民健身值
  • 計算所有個體的適應,並將其保存在陣列
  • 在計算所有健身的總和值並將每個健身除以該總和(標準化)
  • 用累加和創建一個數組。對於實例:對於適應值[0.1,0.2,0.3.0.4]你的數組shuld是[0.1,0.3,0.6,1.0]
  • 畫一個數[0,1]
  • 循環throuhgh你的數組與累積和值並找到比您的隨機值更大的第一個值。身份證是你個人的身份證。例如:如果你畫0.05,你的ID將是0(第一個人)。如果繪製0.8您的ID應該是3(最後一個個體)
0

一種可能的實現(使用Java 8)在以前的答案中描述的步驟:

class RouletteWheelSelector { 

public Population<DoubleGene, Double> 
select(Population<DoubleGene, Double> population, int count) { 
    // The incremental fitness probability array. 
    final double[] incremental = incremental(probabilities(population)); 
    final Random random = new Random(); 

    return IntStream.range(0, count) 
     // Select the individual index. 
     .map(i -> indexOf(incremental, random.nextDouble())) 
     // Select the individual. 
     .mapToObj(i -> population.get(i)) 
     // Create the new population object. 
     .collect(Population.toPopulation()); 
} 

// Calculate the fitness probabilities of the given population. 
static double[] probabilities(Population<?, Double> population) { 
    final double[] fitness = population.stream() 
     .mapToDouble(pt -> pt.getFitness()) 
     .toArray(); 
    final double sum = Arrays.stream(fitness).sum(); 

    return Arrays.stream(fitness) 
     .map(v -> v/sum) 
     .toArray(); 
} 

// Create the incremental probability array. 
static double[] incremental(final double[] values) { 
    for (int i = 1; i < values.length; ++i) { 
     values[i] += values[i - 1]; 
    } 
    return values; 
} 

// Binary search for the individual index for a given probability. 
static int indexOf(final double[] incr, final double v) { 
    int imin = 0; 
    int imax = incr.length; 
    int index = -1; 

    while (imax > imin && index == -1) { 
     final int imid = (imin + imax) >>> 1; 

     if (imid == 0 || (incr[imid] >= v && incr[imid - 1] < v)) { 
      index = imid; 
     } else if (incr[imid] <= v) { 
      imin = imid + 1; 
     } else if (incr[imid] > v) { 
      imax = imid; 
     } 
    } 

    return index; 
} 
} 

的代碼已經從提取Jenetics項目的'RouletteWheelSelector'。