📄 testfitness.java
字号:
import org.jgap.*;
import java.io.*;
public class Testfitness extends FitnessFunction {
private void doCommand(String command, String dir) throws Exception{
String line;
//System.out.println("command: "+command);
Process p = Runtime.getRuntime().exec(command, null, new File(dir));
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
//System.out.println("OUTPUT: "+line);
}
input.close();
input = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
while ((line = input.readLine()) != null) {
//System.out.println("ERROR: "+line);
}
p.destroy();
}
public Testfitness(){
//dummy constructor
}
public double evaluate(IChromosome a_subject ){
double fitness;
String command;
String line;
BufferedReader input;
BufferedWriter output;
try{
//bad bad hardcoding to get to the directories needed!
String tmpDir = "/tmp/";
String oriDir =
"/biol/people/mres/asf500/vlab-4.0/oofs/ext/java";
//get the local ip/name
String hostName = InetAddress.getLocalHost().getHostName();
//String hostIP = InetAddress.getLocalHost().getHostAddress();
/* bruteforce batch since this doesnt work very well
//construct a temp dir
command = "mkdir "+tmpDir+"asf500_"+hostName;
doCommand(command, "/");
//move all stuff to a temp dir to work in
command = "cp -vr "+oriDir+"/* asf500_"+tmpDir;
doCommand(command, "/");*/
tmpDir += "asf500_"+hostName;
//open the files to read/write from/to
input = new BufferedReader(new
FileReader(oriDir+"/fractal.l.template"));
output = new BufferedWriter(new
FileWriter(tmpDir+"/fractal.l"));
//read in each line
while((line=input.readLine())!=null){
//replace special chars with gene values
for(int i=0; i<8; i++){
line = line.replaceAll("!GENE:"+i+"!",
a_subject.getGene(i).getAllele().toString());
}
//write it back out again
output.write(line+"\n");
//debug line
//System.out.println(in+" > "+line);
}
command = "cpfg -P preproc -m fractal.map -e fractal.e -g
fractal.l fractal.v fractal.a";
doCommand(command, tmpDir);
//read the output file
//and work out the fitness
input = new BufferedReader(new FileReader(tmpDir+"/test.log"));
int leafTotal = 0;
int leafIn = 0;
int leafOut = 0;
//read in each line
while((line=input.readLine())!=null){
line = line.substring(1,line.length()-1);
String [] positions = line.split(",");
if (positions.length == 3){
//System.out.println(positions[0]+" "+positions[1]+"
"+positions[2]);
float x = Float.parseFloat(positions[0]);
float y = Float.parseFloat(positions[1]);
float z = Float.parseFloat(positions[2]);
//System.out.println(x+" "+y+" "+z);
leafTotal++;
if (x > -2.0 && x < 2.0
&& y > 0.0 && y < 4.0
&& z > -2.0 && z < 2.0){
leafIn++;
}else{
leafOut++;
}
}
}
input.close();
//System.out.println("This solution has a fitness value of
"+fitness+" ("+leafIn+"/"+leafOut+")");
return fitness;
} catch (Exception err) {
err.printStackTrace();
}
//System.out.println("ERROR This solution has a fitness value of
"+1.0+"()");
return 1.0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -