📄 chromosome.java
字号:
package com.lqy.GEP;
import java.util.*;
import weka.core.*;
import java.io.*;
public class Chromosome implements java.io.Serializable,java.lang.Comparable<Chromosome>{
private double fitness=0;
private int headLength=5;
private boolean constructed=false;
private String[] functionSet=new String[]{"+2","*2","exp"};
private String[] terminalSet=new String[]{"k1","k2","k3"};
private String[] gtype=new String[this.getTotalLength()];
private TreeThread[] ptype=new TreeThread[this.getHeadLength()];//染色体中参数部分存放的是常数池的下标
private double[] cp=new double[10];//常数池,存放[1,10]随机实数
private String id;//染色体的唯一标识
public Evator eva;
public double SIPDT=Double.NEGATIVE_INFINITY;
//private EVATOR eva;
// private int tailLength;
// private int parameterLength;
//非终结符的子节点
private TreeThread[] parameterAssign=new TreeThread[this.getHeadLength()+this.getTailLength()];
//终结符的参数,是染色体中下标
public Chromosome(){
//this.randomInit();
}
public String[] getFunctionSet(){
return this.functionSet;
}
public String[] getTerminalSet(){
return this.terminalSet;
}
public double[] getCP(){
return this.cp;
}
public void setCP(double[] argD){
this.cp=argD;
}
public void setID(String argI){
this.id=argI.trim();
}
public String getID(){
return this.id;
}
public void updateFitness(double argF){
this.fitness=argF;
}
public double getFitness(){
return this.fitness;
}
public void updateSIPDT(EuclideanMeasure argE){
Instances tempI=argE.getInstances();
this.SIPDT=this.eva.getValue(this.getTerminalValues(argE,tempI.firstInstance(),tempI.firstInstance()));
}
public void randomInit(){
Random tempR=new Random();
this.setGeneByIndex(0, this.functionSet[tempR.nextInt(this.functionSet.length)]);
//树根必须是操作符
for(int i=1;i<this.getHeadLength();i++)
this.setGeneByIndex(i,
tempR.nextDouble()>0.5?this.functionSet[tempR.nextInt(this.functionSet.length)]:
this.terminalSet[tempR.nextInt(this.terminalSet.length)]);
//初始化头部
for(int i=this.headLength;i<this.headLength+this.getTailLength();i++)
this.setGeneByIndex(i, this.terminalSet[tempR.nextInt(this.terminalSet.length)]);
//初始化尾部
for(int i=0;i<this.cp.length;i++)
this.cp[i]=9*Math.random();
//初始化参数池,0-10之间的实数
for(int i=this.getHeadLength()+this.getTailLength();i<this.getTotalLength();i++)
this.setGeneByIndex(i,String.valueOf((tempR.nextInt(this.cp.length))));
//初始化参数
this.construct();
System.out.print("cd");
//创建树结构
}
public void construct(){
/*
int tempArity=Integer.MIN_VALUE;
SymbolFactory tempSF=new SymbolFactory();
for(int i=0;i<this.functionSet.length;i++){
int tempI=tempSF.creatSymbol(this.functionSet[i]).getArity();
if(tempI>tempArity)
tempArity=tempI;
}
this.tailLength=this.headLength*(tempArity-1)+1;
int tempPS=Integer.MIN_VALUE;
//SymbolFactory tempSF=new SymbolFactory();
for(int i=0;i<this.terminalSet.length;i++){
int tempI=tempSF.creatSymbol(this.terminalSet[i]).getParameterSize();
if(tempI>tempPS)
tempPS=tempI;
}
this.parameterLength=tempPS*this.getTailLength();
*/
if(this.constructed) return;
else{
//System.out.println("now constructing");
this.constructPType();
this.constructParameterAssign();
this.constructed=true;
}
}
public int getPTC(int argP,int argC){
return this.ptype[argP].getChild(argC);
}
public TreeThread getPT(int argI){
return this.ptype[argI];
}
public int getPAC(int argP,int argC){
return this.parameterAssign[argP].getChild(argC);
}
public TreeThread getPA(int argI){
return this.parameterAssign[argI];
}
private void constructPType(){//已经解决终止树构造的问题
try{
int tempM=1;
SymbolFactory tempSF=SymbolFactory.getInstance();
for(int i=0;i<this.headLength;i++){
Symbol tempS=tempSF.creatSymbol(this.getGeneByIndex(i));
this.ptype[i]=new TreeThread();
if(!tempS.isTerminal()){
for(int j=tempM;j<=tempM-1+tempS.getArity();j++)
this.ptype[i].addChild(j);
tempM=tempM+tempS.getArity();
}
}
for(int i=0;i<this.headLength;i++){
TreeThread tempT=this.getPT(i);
boolean tempB=false;
for(int j=0;j<tempT.getNumberOfChildren();j++)
if(tempT.getChild(j)<=i){
tempB=true;
break;
}
if(tempB){
for(int j=i;j<this.getHeadLength();j++)
this.ptype[j].clear();
break;
}
}
}catch(Exception e){System.err.println(e);}
}
private void constructParameterAssign(){
try{
int tempM=this.getHeadLength()+this.getTailLength();
//令tempM指向第一个参数
SymbolFactory tempSF=SymbolFactory.getInstance();
for(int i=0;i<this.getHeadLength()+this.getTailLength();i++){
Symbol tempS=tempSF.creatSymbol(this.getGeneByIndex(i));
this.parameterAssign[i]=new TreeThread();
if(tempS.isTerminal()){
for(int j=tempM;j<tempM+tempS.getParameterSize();j++)
this.parameterAssign[i].addChild(j);
tempM=tempM+tempS.getParameterSize();
}
}
for(int i=this.headLength-1;i>=0;i--){
if(this.ptype[i].getNumberOfChildren()>0){
int tempF=this.ptype[i].getChild(this.ptype[i].getNumberOfChildren()-1);
for(int j=tempF+1;j<this.parameterAssign.length;j++)
this.parameterAssign[j].clear();
break;
}
}
}catch(Exception e){System.out.println(e);}
}
public String getGeneByIndex(int argI){//如果在参数位置,返回在cp中的下标
if((argI<0)||(argI>this.gtype.length))
return "";
return this.gtype[argI];
}
public double getParameterByIndex(int argI){
return this.cp[Integer.parseInt(this.getGeneByIndex(argI))];
}
public void setGeneByIndex(int argI,String argS){
this.gtype[argI]=argS;
}
public int getHeadLength(){
return this.headLength;
}
public int getTailLength(){
//return this.tailLength;
/*
if(this.tailLength!=0)
return this.tailLength;
*/
int tempArity=Integer.MIN_VALUE;
SymbolFactory tempSF=SymbolFactory.getInstance();
for(int i=0;i<this.functionSet.length;i++){
int tempI=tempSF.creatSymbol(this.functionSet[i]).getArity();
if(tempI>tempArity)
tempArity=tempI;
}
return this.headLength*(tempArity-1)+1;
}
public int getParameterLength(){
//return this.parameterLength;
/*
if(this.parameterLength!=0)
return this.parameterLength;
*/
int tempPS=Integer.MIN_VALUE;
SymbolFactory tempSF=SymbolFactory.getInstance();
for(int i=0;i<this.terminalSet.length;i++){
int tempI=tempSF.creatSymbol(this.terminalSet[i]).getParameterSize();
if(tempI>tempPS)
tempPS=tempI;
}
return tempPS*this.getTailLength();
}
public int getTotalLength(){
return this.headLength+this.getTailLength()+this.getParameterLength();
}
private double doEvaluation(EuclideanMeasure argE,Instance argA,Instance argB,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.getValue(argE, argA, argB);
}
else{
double[] tempD=new double[tempS.getArity()];
for(int i=0;i<tempS.getArity();i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -