📄 function.java
字号:
public int codeToBinary(double d){
if(d>=0.5){
return 1;
}
else{
return 0;
}
}
public double gauss(double d){
double funValue;
funValue = (1.0/(2*Math.PI))*Math.exp((d*d)/2);
return funValue;
}
/**
* 2008-4-14
* author dbhunter
* @param printID 打印的方式 1:横向 2.详细
* @param targetGene
*/
public StringBuffer getSbLastGenes(int printID,Gene[] targetGene){
switch(printID){
case 1:
this.sblastGenes.delete(0, this.sblastGenes.length());
this.sblastGenes.append("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
for(int i=0;i<targetGene.length;i++){
this.sblastGenes.append("id = "+targetGene[i].getGeneID()+" ");
this.sblastGenes.append("【 ");
for(int j =0;j<targetGene[i].getVecGene().length;j++){
this.sblastGenes.append("X"+j+"= "+doubleToString(this.precision,getReal(binaryToDecimal(targetGene[i].getVecGene()[j])))+" , ");
}
this.sblastGenes.append("】 ");
this.sblastGenes.append("适应函数值:"+doubleToString(this.precision,targetGene[i].getEvaluateValue())+" ");
this.sblastGenes.append("选择概率:"+doubleToString(this.precision,targetGene[i].getSelectProbability())+" , ");
this.sblastGenes.append("期望值:"+doubleToString(this.precision,targetGene[i].getExpectValue())+", ");
this.sblastGenes.append("赌轮数: "+targetGene[i].getRouletteNum()+" " );
this.sblastGenes.append("评估函数值:"+doubleToString(this.precision, evaluateFun(targetGene[i].getVecGene()))+"\n");
this.sblastGenes.append("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
}
return this.sblastGenes;
case 2:
this.sblastGenes.delete(0, this.sblastGenes.length());
for(int i=0;i<targetGene.length;i++){
this.sblastGenes.append("基因编号: "+targetGene[i].getGeneID()+"\n");
for(int j =0;j<targetGene[i].getVecGene().length;j++){
this.sblastGenes.append("二进制码值: "+targetGene[i].getVecGene()[j]+" 十进制码值: "+ binaryToDecimal(targetGene[i].getVecGene()[j])+" 真实值: X"+j+": "+doubleToString(this.precision,getReal(binaryToDecimal(targetGene[i].getVecGene()[j])))+"\n");
}
this.sblastGenes.append("适应函数值 "+doubleToString(this.precision,targetGene[i].getEvaluateValue())+" , ");
this.sblastGenes.append("选择概率-f(x)/sum(f(x)) = "+doubleToString(this.precision,targetGene[i].getSelectProbability())+" , ");
this.sblastGenes.append("期望值-f(x)/avg(f(x)) = "+doubleToString(this.precision,targetGene[i].getExpectValue())+" , ");
this.sblastGenes.append("赌轮数 rouletteNum = "+targetGene[i].getRouletteNum()+" \n");
this.sblastGenes.append("评估函数值:"+doubleToString(this.precision, evaluateFun(targetGene[i].getVecGene()))+"\n");
this.sblastGenes.append("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
}
return this.sblastGenes;
default:
return this.sblastGenes;
}
}
/**
* 2008-4-14
* author dbhunter
* @param population
* @return
*/
public StringBuffer getSbBestGeneetail(Population population){
if(this.codeType=='b'){
this.codeName = "二进制编码";
}
else{
this.codeName = "";
}
if(population.getSelectionID()==1){
this.selectionName = "赌轮选择法";
}
else{
this.selectionName = "";
}
if(population.getCrossID()==1){
this.crossName = "单点交叉";
}
else if(population.getCrossID()==2){
this.crossName = "佳点集";
}
else{
this.crossName = "";
}
if(population.getGenovariationID()==1){
this.genovariationName = "点变异";
}
else{
this.genovariationName = "";
}
Gene bestGene = population.getGenes()[population.getBestGene()];
this.sbBestGeneetail.delete(0, this.sbBestGeneetail.length());
this.sbBestGeneetail.append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
this.sbBestGeneetail.append("系统的参数如下:\n");
this.sbBestGeneetail.append("函数: F"+population.getEf().funID+" ;编码方式:"+this.codeName+" ;选择算法:"+this.selectionName+" ;交叉算法:"+this.crossName+" ;变异算法:"+this.genovariationName+"\n");
this.sbBestGeneetail.append("种群规模:"+population.getPopulationNum()+" ,交叉概率:"+population.getPc()+" ,变异概率:"+population.getPv()+" ,迭代次数:"+population.getIterateNum()+"\n");
this.sbBestGeneetail.append("种群最优个体为:"+population.getBestGene()+" ");
this.sbBestGeneetail.append("适应度函数值 :"+doubleToString(this.precision,bestGene.getEvaluateValue())+" , ");
this.sbBestGeneetail.append("选择概率 :"+doubleToString(this.precision,bestGene.getSelectProbability())+" , ");
this.sbBestGeneetail.append("期望值 :"+doubleToString(this.precision,bestGene.getExpectValue())+" , ");
this.sbBestGeneetail.append("赌轮值: "+bestGene.getRouletteNum()+"\n");
this.sbBestGeneetail.append("函数参数为: ");
for(int i =0;i<bestGene.getVecGene().length;i++){
this.sbBestGeneetail.append("X"+i+"= "+doubleToString(this.precision,getReal(binaryToDecimal(bestGene.getVecGene()[i])))+" , ");
}
this.sbBestGeneetail.append("该评估函数的最小值为:"+doubleToString(this.precision, evaluateFun(bestGene.getVecGene()))+"\n");
return this.sbBestGeneetail;
}
/**
* 2008-4-12
* -----------------------------------------------------------------------------------------------------------------
* 此处为评估函数库
*/
/**
* f(x)= ∑(i=1...3)x(i), —5.12<=x(i)<=5.12
* 第一个评估函数
* f(x)' = f(x)+(-5.12*3)
* 输入为参数集合
* 输出为double
*/
public double fun(Vector[] vc){
double funValue = 0;
switch(this.funID){
case 1:
int n1 =3;
for(int i=0;i<n1;i++){
funValue = funValue + getReal(binaryToDecimal(vc[i]));
}
funValue = funValue +5.12*3;
return funValue;
case 2:
funValue = 100*(Math.pow(getReal(binaryToDecimal(vc[0])),2)-Math.pow(getReal(binaryToDecimal(vc[1])),2))+Math.pow((1-getReal(binaryToDecimal(vc[0]))),2);
funValue = funValue+100*Math.pow(2.048,2)+1;
return funValue;
case 3:
int n3 =5;
for(int i=0;i<n3;i++){
funValue = funValue + (int)getReal(binaryToDecimal(vc[i]));
}
funValue = funValue + 5*6;
return funValue;
case 4:
int n4 = 30;
for(int i=0;i<n4;i++){
funValue =(i+1)*Math.pow(getReal(binaryToDecimal(vc[i])),4)+gauss(getReal(binaryToDecimal(vc[i])));
}
return funValue;
case 5:
int n5 = 25;
double sum;
int[][] array = {{-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32},{-32,-32,-32,-32,32,-16,-16,-16,-16,-16,0,0,0,0,0,16,16,16,16,16,32,32,32,32,32}};
for(int i=1;i<=n5;i++){
sum =0;
for(int j=1;j<=2;j++){
sum = sum+ Math.pow((getReal(binaryToDecimal(vc[j-1]))-array[j-1][i-1]),6);
}
funValue = funValue+1.0/(i+sum);
}
funValue = funValue+0.002;
return funValue;
case 6:
int n6 =20;
double A =1 ;
for(int i=0;i<n6;i++){
funValue=Math.pow(getReal(binaryToDecimal(vc[i-1])),2)-A*Math.cos(2*Math.PI*getReal(binaryToDecimal(vc[i-1])));
}
funValue = n6*A +funValue;
return funValue;
case 7:
int n7=10;
for(int i=0;i<n7;i++){
funValue =getReal(binaryToDecimal(vc[i]))*Math.sin(Math.sqrt(Math.abs(getReal(binaryToDecimal(vc[i])))));
}
funValue = -funValue+5000;
return funValue;
case 8:
int n8 = 10;
double sum8=0;
double product =1;
for(int i=0;i<n8;i++){
sum8 = sum8+Math.pow(getReal(binaryToDecimal(vc[i])),2)/4000;
product = product*(Math.cos(getReal(binaryToDecimal(vc[i]))/Math.sqrt(i+1))+1);
}
funValue = sum8-product+20;
return funValue;
case 9:
case 10:
case 11:
case 12:
default : return funValue;
}
}
public double evaluateFun(Vector[] vc){
double funValue = 0;
switch(this.funID){
case 1:
int n1 =3;
for(int i=0;i<n1;i++){
funValue = funValue + getReal(binaryToDecimal(vc[i]));
}
return funValue;
case 2:
return funValue = 100*(Math.pow(getReal(binaryToDecimal(vc[0])),2)-Math.pow(getReal(binaryToDecimal(vc[1])),2))+Math.pow((1-getReal(binaryToDecimal(vc[0]))),2);
case 3:
int n3 =5;
for(int i=0;i<n3;i++){
funValue = funValue + (int)getReal(binaryToDecimal(vc[i]));
}
return funValue;
case 4:
int n4 = 30;
for(int i=0;i<n4;i++){
funValue =(i+1)*Math.pow(getReal(binaryToDecimal(vc[i])),4)+gauss(getReal(binaryToDecimal(vc[i])));
}
return funValue;
case 5:
int n5 = 25;
double sum=0;
int[][] array = {{-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32},{-32,-32,-32,-32,32,-16,-16,-16,-16,-16,0,0,0,0,0,16,16,16,16,16,32,32,32,32,32}};
for(int i=1;i<=n5;i++){
for(int j=1;j<=2;j++){
sum = sum+ Math.pow((getReal(binaryToDecimal(vc[j-1]))-array[j-1][i-1]),6);
}
funValue = funValue+1.0/(i+sum);
sum =0;
}
funValue = funValue+0.002;
return funValue;
case 6:
int n6 =20;
double A =1 ;
for(int i=0;i<n6;i++){
funValue=Math.pow(getReal(binaryToDecimal(vc[i])),2)-A*Math.cos(2*Math.PI*getReal(binaryToDecimal(vc[i])));
}
funValue = n6*A +funValue;
return funValue;
case 7:
int n7=10;
for(int i=0;i<n7;i++){
funValue =getReal(binaryToDecimal(vc[i]))*Math.sin(Math.sqrt(Math.abs(getReal(binaryToDecimal(vc[i])))));
}
funValue = -funValue;
return funValue;
case 8:
int n8 = 10;
double sum8=0;
double product =1;
for(int i=0;i<n8;i++){
sum8 = sum8+Math.pow(getReal(binaryToDecimal(vc[i])),2)/4000;
product = product*(Math.cos(getReal(binaryToDecimal(vc[i]))/Math.sqrt(i+1))+1);
}
funValue = sum8-product;
return funValue;
case 9:
case 10:
case 11:
case 12:
default : return funValue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -