📄 breedingprocess.java
字号:
package com.lqy.GEP;
import weka.core.*;
import weka.classifiers.*;
import weka.classifiers.lazy.*;
import java.util.*;
public class BreedingProcess {//繁衍过程
private int populationsize=25;
private Chromosome[] population=new Chromosome[this.populationsize];//种群
private Instances ists;//数据集
private int currentGeneration=0;//当前代数
private int maxGeneration=100;//最大代数
private RSGenerator rsg=new RSGenerator();//随机数发生器,用于选择算子
private Chromosome bestIndividual;//种群最优个体
private static BreedingProcess instance=new BreedingProcess();
public static BreedingProcess getInstance(){
return instance==null?new BreedingProcess():instance;
}
public void updateRSG(){//随机数发生器
ArrayList tempA=new ArrayList();
for(int i=0;i<this.populationsize;i++)
tempA.add(this.population[i].getFitness());
this.rsg.UpdateDiscreteDistribution(tempA);
}
public void setPopulationsize(int argI){
this.populationsize=argI;
}
public int getPopulationsize(){
return this.populationsize;
}
public Chromosome getIndividual(int argI){//
return argI<this.populationsize?this.population[argI]:null;
}
public void setIndividual(int argI,Chromosome argC){//ID改成字符串类型了 ID的编号如下 01 02 03..025,11 12 13 ...125
if(argI<this.populationsize){
StringBuilder tempSB=new StringBuilder();
tempSB.append(this.currentGeneration);
tempSB.append(argI);
argC.setID(tempSB.toString());
this.population[argI]=argC;
}
}
public void setInstances(Instances argI){//设置数据集
this.ists=argI;
}
public Instances getInstances(){//获取数据集
return this.ists;
}
public Chromosome[] selection(double argD){//选择算子,选择populationsize-1个染色体
Chromosome[] tempCS=new Chromosome[this.populationsize-1];
Random tempR=new Random();
for(int i=0;i<tempCS.length;i++){
int tempI=this.rsg.NextInt();
tempCS[i]=this.population[tempI].copyChromosome();
}
return tempCS;
}
public void updateBestChromosome(){//更新种群最优个体
//double tempD=Double.MIN_VALUE;
int tempI=0;
for(int i=1;i<this.populationsize;i++){
if(this.population[i].compareTo(this.population[tempI])==1)
tempI=i;
}
this.bestIndividual=this.population[tempI];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -