📄 chromosome.java
字号:
tempD[i]=this.doEvaluation(argE, argA, argB, this.getPTC(argI, i));
return tempS.eval(tempD);
}
}
private double doEvaluation(double[] argPDT,double[] argDIST,int argI){
//System.out.println("henhen");
SymbolFactory tempSF=SymbolFactory.getInstance();
Symbol tempS=tempSF.creatSymbol(this.getGeneByIndex(argI));
if(tempS.isTerminal()){
double[] tempD=new double[tempS.getParameterSize()];
for(int i=0;i<tempS.getParameterSize();i++){
tempD[i]=this.getParameterByIndex(this.getPAC(argI, i));
// System.out.print(tempD[i]+" ");
}
tempS.setParameters(tempD);
return (tempS.getName().compareTo("k1")==0)?tempS.getValue(argPDT):tempS.getValue(argDIST);
}
else{
double[] tempD=new double[tempS.getArity()];
for(int i=0;i<tempS.getArity();i++)
tempD[i]=this.doEvaluation(argPDT, argDIST,this.getPTC(argI, i));
return tempS.eval(tempD);
}
}
private double doEvaluation(EuclideanMeasure argE,Instance argA,Instance argB){
// System.out.println("hehe");
double[] tempD=new double[this.getHeadLength()+this.getTailLength()];//每个节点(无论是运算符还是基本核函数)的值
double[] innerPDT=new double[]{argE.InnerProduct(argA, argB),
argE.InnerProduct(argA, argA),
argE.InnerProduct(argB, argB)};
double[] originalDIST=new double[]{argE.distanceInOriginalSpace(argA, argB, Double.MAX_VALUE),
argE.distanceInOriginalSpace(argA, argA, Double.MAX_VALUE),
argE.distanceInOriginalSpace(argB, argB, Double.MAX_VALUE)};
int tempI=0;
SymbolFactory tempSF=SymbolFactory.getInstance();
//最后一个内部节点
for(int i=this.headLength-1;i>=0;i--)
if(this.getPT(i).getNumberOfChildren()!=0){
tempI=i;
break;
}
// System.out.println(tempI);
//对所有在表达式中显现的基本核函数求值
int tempL=this.getHeadLength()+this.getTailLength();
for(int i=0;i<tempL;i++)
if(this.getPA(i).getNumberOfChildren()!=0){
Symbol tempS=tempSF.creatSymbol(this.getGeneByIndex(i));
double[] tempP=new double[tempS.getParameterSize()];
for(int j=0;j<tempP.length;j++){
tempP[j]=this.getParameterByIndex(this.getPAC(i, j));
// System.out.print("Param"+tempP[j]+" ");
}//获得基本核函数的参数
// System.out.println();
tempS.setParameters(tempP);
if(tempS.getName().compareTo("k1")==0)//多项式核
tempD[i]=tempS.getValue(innerPDT);
else
tempD[i]=tempS.getValue(originalDIST);
//tempD[i]=tempS.getValue(argE, argA, argB);//求值并保存
// System.out.print(i+" "+tempD[i]+" ");
}
for(int i=tempI;i>=0;i--){//从最后一个内部节点开始
if(this.getPT(i).getNumberOfChildren()!=0){
//System.out.print(i+" ");
Symbol tempS=tempSF.creatSymbol(this.getGeneByIndex(i));
double[] tempP=new double[tempS.getArity()];
// System.out.print(tempS.getName()+" ");
for(int j=0;j<tempP.length;j++){
tempP[j]=tempD[this.getPTC(i,j)];//取得运算符的操作数
// System.out.print(tempP[j]+" ");
}
// System.out.println();
tempD[i]=tempS.eval(tempP);
}
}
return tempD[0];
}
/*
public double evaluation(EuclideanMeasure argE,Instance argA,Instance argB){
double[] innerPDT=new double[]{argE.InnerProduct(argA, argB),
argE.InnerProduct(argA, argA),
argE.InnerProduct(argB, argB)};
double[] originalDIST=new double[]{argE.distanceInOriginalSpace(argA, argB, Double.MAX_VALUE),
argE.distanceInOriginalSpace(argA, argA, Double.MAX_VALUE),
argE.distanceInOriginalSpace(argB, argB, Double.MAX_VALUE)};
double tempD=this.doEvaluation(innerPDT,originalDIST,0);
//double tempD=this.doEvaluation(argE,argA,argB,0);
return Double.isInfinite(tempD)?Double.MAX_VALUE:tempD;
} */
public int compareTo(Chromosome argC){
int tempI=0;
if(this.getFitness()>argC.getFitness())
tempI=1;
else if(this.getFitness()<argC.getFitness())
tempI=-1;
return tempI;
}
public double[] getTerminalValues(EuclideanMeasure argE,Instance argA,Instance argB){
double[] innerPDT=new double[]{argE.InnerProduct(argA, argB),
argE.InnerProduct(argA, argA),
argE.InnerProduct(argB, argB)};
double[] originalDIST=new double[]{argE.distanceInOriginalSpace(argA, argB, Double.MAX_VALUE),
argE.distanceInOriginalSpace(argA, argA, Double.MAX_VALUE),
argE.distanceInOriginalSpace(argB, argB, Double.MAX_VALUE)};
double[] tempD=new double[this.parameterAssign.length];
SymbolFactory tempSF=SymbolFactory.getInstance();
for(int i=0;i<this.parameterAssign.length;i++)
if(this.getPA(i).getNumberOfChildren()!=0){
Symbol tempS=tempSF.creatSymbol(this.getGeneByIndex(i));
double[] tempP=new double[tempS.getParameterSize()];
for(int j=0;j<tempP.length;j++)
tempP[j]=this.getParameterByIndex(this.getPAC(i, j));
tempS.setParameters(tempP);
if(tempS.getName().compareTo("k1")==0)//多项式核
tempD[i]=tempS.getValue(innerPDT,argA, argB);
else
tempD[i]=tempS.getValue(originalDIST,argA,argB);
}
return tempD;
}
public void toExp(ArrayList<String> argS,int argI){//把基因串转换为算术表达式
SymbolFactory tempSF=SymbolFactory.getInstance();
Symbol tempS=tempSF.creatSymbol(this.getGeneByIndex(argI));
if(tempS.isTerminal()){
argS.add("argD["+argI+"]");//+"(instA,instB)");
}else{
argS.add("(");
switch(tempS.getArity()){
case 1:{//目前系统中一元运算符只有exp,如果加入别的一元运算符,需要修改方法
argS.add("Math.pow(Math.E,");
this.toExp(argS, this.getPTC(argI,0));
argS.add(")");
break;}
case 2:{
this.toExp(argS, this.getPTC(argI,0));
argS.add(tempS.getName());
this.toExp(argS, this.getPTC(argI,1));
break;}
default:{
for(int i=0;i<tempS.getArity();i++){
this.toExp(argS, this.getPTC(argI, i));
if(i<tempS.getArity())
argS.add(tempS.getName());
}
break;}
}
argS.add(")");
}
}
public void buildEvaluator(){//为本染色体生成求值类,只要能生成编译好的类文件就行了
try{
BufferedWriter tempFS=new BufferedWriter(new FileWriter("com/lqy/GEP/Evator"+this.getID()+".java"));
StringBuilder tempSB=new StringBuilder();
tempSB.append("package com.lqy.GEP;\r\n");
tempSB.append("public class Evator").append(this.getID()).append(" implements Evator{\r\n");
tempSB.append(" public double getValue(double[] argD){\r\n");
tempSB.append(" return ");
ArrayList<String> tempAS=new ArrayList<String>();
this.toExp(tempAS, 0);
for(int i=0;i<tempAS.size();i++)
tempSB.append(tempAS.get(i).trim());
tempSB.append(";\r\n");
tempSB.append(" }\r\n");
tempSB.append("}");
tempFS.write(tempSB.toString());
tempFS.flush();
tempFS.close();
Runtime tempR=Runtime.getRuntime();
// System.out.print("jikes Evator.java Evator"+this.getID()+".java");
//路径设置总出问题,现在把编译器放在工作空间根目录
Process tempP=tempR.exec("jikes com/lqy/GEP/Evator.java com/lqy/GEP/Evator"+this.getID()+".java");
tempP.waitFor();
//tempR.exec(command, envp, dir)
/*
BufferedReader rdr=new BufferedReader(InputStreamReader(tempP.getInputStream()));
System.out.print(r.readLine());
*/
this.eva=(Evator)Class.forName("com.lqy.GEP.Evator"+this.getID()).newInstance();
//System.setOut(new PrintStream(tempP.getOutputStream()));
// String msg="";
}catch(Exception e){
System.err.println(e);
}
}
public double evaluation(EuclideanMeasure argE,Instance argA,Instance argB){//利用已经生成的求值类求基因表达式的值
//System.out.print("flow");
//double[] d=
//double s=(d);
// for(int i=0;i<d.length;i++)
// System.out.println(i+" "+d[i]);
//System.out.println();
// System.out.println("v "+s);
// System.out.println();
/*
if(this.SIPDT==Double.NEGATIVE_INFINITY){
Instances tempI=argE.getInstances();
this.SIPDT=this.eva.getValue(this.getTerminalValues(argE,tempI.firstInstance(),tempI.firstInstance()));
}*/
return this.eva.getValue(this.getTerminalValues(argE, argA, argB));
// return this.eva.getValue(this.getTerminalValues(argE, argA, argB));
}
public Chromosome copyChromosome(){//一个染色体的核心部分仅仅是表达式串和与其关联的瞬态变量池
Chromosome tempC=new Chromosome();
int tempI=this.getTotalLength();
for(int i=0;i<tempI;i++)
tempC.setGeneByIndex(i,this.getGeneByIndex(i));
tempC.setCP(this.cp);
return tempC;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -