-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouletteWheel.java
More file actions
80 lines (69 loc) · 1.81 KB
/
RouletteWheel.java
File metadata and controls
80 lines (69 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.ArrayList;
import java.util.Random;
/**
* Oct 28, 2015 10:29:55 PM
* RouletWheel
* @author Almgwary
*
*/
public class RouletteWheel {
private ArrayList<Double> fittness ;
private ArrayList<Double> fittnessPopulation ;
private ArrayList<Double> wheelProbiblty ;
private ArrayList<Integer> selection ;
int totalPopulationSize ;
int size ;
public RouletteWheel() {
}
public ArrayList<Integer> run (ArrayList<Double> fittness){
//initilazation
this.fittness = new ArrayList<Double>();
selection= new ArrayList<Integer>();
fittnessPopulation = new ArrayList<Double>();
wheelProbiblty = new ArrayList<Double>();
totalPopulationSize= 0 ;
this.size =0;
//initial values
this.fittness = fittness ;
this.size = fittness.size();
//RouletteWheel Algorithm
calculateTotalsize();
calculateFittnessPropablity();
calculateWheelProbalbity();
calculateSellection();
System.out.println(selection);
return selection;
}
private void calculateTotalsize (){
for(int i =0 ; i <size;++i){
totalPopulationSize +=fittness.get(i);
}
}
private void calculateFittnessPropablity (){
for(int i=0; i<size;++i){
fittnessPopulation.add((double) (fittness.get(i)/totalPopulationSize));
}
}
private void calculateWheelProbalbity(){
wheelProbiblty.add(fittnessPopulation.get(0));
for(int i=1;i<size;++i){
wheelProbiblty.add(fittnessPopulation.get(i)+wheelProbiblty.get(i-1));
}
}
private void calculateSellection (){
Random random = new Random ();
for(int i = 0; i<size ;++i){
double r = Math.random();//random.nextDouble()%100;
//System.out.println("-------------------"+r);
for(int j= 1; j<size ;++j){
if(wheelProbiblty.get(j)>r){
selection.add(j-1);
break ;
}
else if(j == size-1){
selection.add(j);
}
}
}
}
}