⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gaaselection.java

📁 Java实现的遗传算法工具集:GA Playground
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -