-
-
Save srw/353c3c66e6fcc0f978109b2468cb7d40 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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