Skip to content

Instantly share code, notes, and snippets.

@pyk
Last active October 22, 2017 08:27
Show Gist options
  • Save pyk/433fbcd0eb9d9acea05528480386c19c to your computer and use it in GitHub Desktop.
Save pyk/433fbcd0eb9d9acea05528480386c19c to your computer and use it in GitHub Desktop.
Roulette-wheel selection via stochastic acceptance. More on http://arxiv.org/abs/1109.3627 https://en.wikipedia.org/wiki/Fitness_proportionate_selection
public class roulette {
public static void main(String [] args) {
int n=4;
double [] weight = new double [n];
weight[0]=0.4;
weight[1]=0.3;
weight[2]=1.2;
weight[3]=0.1;
double max_weight=1.2;
int [] counter = new int[n];
int n_select=1000;
int index=0;
boolean notaccepted;
for (int i=0; i<n_select; i++){
notaccepted=true;
while (notaccepted){
index= (int)(n*Math.random());
if(Math.random()<weight[index]/max_weight) {notaccepted=false;}
}
counter[index]++;
}
for (int i=0; i<n; i++){
System.out.println("counter["+i+"]="+counter[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment