gaaselection.java
来自「Java实现的遗传算法工具集:GA Playground」· Java 代码 · 共 62 行
JAVA
62 行
public class GaaSelection {
GaaProblem problem;
GaaPopulation pop;
int selectionType;
public GaaSelection (GaaProblem pr, GaaPopulation pl) {
problem = pr;
pop = pl;
selectionType = problem.selectionType;
}
public double getSumFitness() {
int i;
double sum;
sum = 0;
for (i=0;i < pop.popSize;i++) {
sum += pop.fits[i];
}
return sum;
}
public String getParent() {
int i;
double rand, partsum;
String chrom = "";
String txt;
switch (selectionType) {
case 1:
default:
partsum = 0;
rand =(double) (Math.random()*pop.sumFitness);
for (i=0;i<pop.popSize;i++) {
partsum += pop.fits[i];
if (partsum >= rand) {
chrom = pop.chroms[i];
break;
}
}
break;
}
//System.out.println("i = "+i+" c: "+pop.chroms[i]);
//System.out.println("V: "+pop.vals[i]+" F: "+pop.fits[i]+" Sum: "+pop.sumFitness+" Rand: "+rand);
return chrom;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?