1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| package FitnessEvaluation.PatternMatch;
import Exceptions.BadParameterException;
import Individuals.Individual;
import Individuals.Phenotype;
import java.util.Properties;
import FitnessEvaluation.FitnessFunction;
import Util.Constants;
//import jxl.*;
//import jxl.Workbook;
//import jxl.read.biff.*;
//import jxl.write.WriteException;
import java.util.Locale;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.BufferedWriter;
/*import weka.core.converters.ArffLoader;
import weka.core.*;
import weka.classifiers.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import weka.classifiers.evaluation.ThresholdCurve;
import weka.classifiers.rules.JRip;
import weka.classifiers.rules.OneR;
import weka.classifiers.rules.Ridor;
import weka.classifiers.rules.Rule;
import weka.classifiers.trees.J48;
import weka.gui.visualize.PlotData2D;
import weka.gui.visualize.ThresholdVisualizePanel;*/
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
public class MyFitness implements FitnessFunction {
private static String s;
/** Creates a new instance of MyFitness */
public MyFitness () {
s = "geva";
}
/**
* Creates a new instance of MyFitness
* @param s String to match
*/
public MyFitness (String s) {
MyFitness .s = s;
}
public void setProperties(Properties p) {
String key = Constants.WORDMATCH_WORD;
String value;
value = p.getProperty(key,"geva");
MyFitness .s = value;
}
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
private static final String FOLDER = "C:\\Users\\Phd\\Desktop\\MyTrial\\bin\\";
private static final String JAVA_BIN_PATH = "C:\\Program Files\\Java\\jdk1.7.0_21\\bin\\";
//----------------------------Compile and Run the file
public static void compileAndRun(String className, String source) {
File sourceFile = new File(FOLDER+className+".java");
File classFile = new File(FOLDER+className+".class");
try {
if ( compile(sourceFile,source) ) {
run(classFile);
}
} catch (Exception e) {System.err.println("Error: " + e.getMessage());}
}
//---------------------------------Compile
public static boolean compile(File filename, String classsource)
throws IOException {
// Create file
FileWriter fstream = null;
try {
fstream = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fstream);
out.write(classsource);
out.flush();
} finally {
if (fstream != null) {
try {
fstream.close();
} catch (IOException e) {
// silent
}
}
}
Process processCompile = Runtime.getRuntime().exec( new String[] { JAVA_BIN_PATH + "javac.exe", "-classpath", "./weka.jar", FOLDER + "Out1.java" } );
return printResult("Compile", processCompile)==0;
}
//------------------run
public static void run(File file) throws IOException {
System.out.println("Run java program: " + file);
Process processRun = Runtime.getRuntime().exec(new String[] { JAVA_BIN_PATH + "javaw.exe", "Out1" },null, file.getParentFile());
printResult("Run", processRun);
}
private static String getClassName(File file) {
String name = file.getName();
return name.substring(0,name.indexOf('.'));
}
//-------------------------Print Result
private static int printResult(String prog, final Process process) {
new Thread() {
public void run() {
try {
InputStream stream = process.getErrorStream();
try {
int read;
while ((read = stream.read()) != -1) {
System.err.print((char) read);
}
System.err.println();
} finally {
try {
stream.close();
} catch (IOException e) {
// silent
}
}
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
new Thread() {
public void run() {
try {
InputStream stream = process.getInputStream();
try {
int read;
while ((read = stream.read()) != -1) {
System.out.print((char) read);
}
System.out.println();
} finally {
try {
stream.close();
} catch (IOException e) {
// silent
}
}
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
try {
process.waitFor();
return process.exitValue();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;}
}
//*************************************************************************************************************************************************************
//****************************************************************------*Fitness*------***********************************************************************
//*************************************************************************************************************************************************************
/**
* Compare a string to the word. Each symbol not matching increases the fitness by 1.
* Max fitness is max(length of the word, phenotype).
* @param p Compared phenotype
* @return Number of missmatches
*/
public double evaluateString(Phenotype p) throws IOException{
//----------------------Deleting the Fitness file content value of the individual-------------------------------------------
PrintWriter writer = new PrintWriter("c:\\myobj.obj");
writer.print("");
writer.close();
//--------------------------------------------------------------------------------------------------------------------------
//----------------------Creating the whole complete java Program-------------------------------------------
System.out.println("----***------"+p+"-----***------\n");
System.out.println("----***------Start Compiling File----***------\n");
String className = "Out1";
StringBuilder classSource=new StringBuilder();
//classSource.append("package out1;");
classSource.append("import java.io.BufferedReader; import java.io.FileReader; import java.io.FileOutputStream;import java.io.ObjectOutputStream; import java.io.IOException; import java.util.Random; import java.awt.BorderLayout;import java.io.BufferedReader;import java.io.BufferedWriter;import weka.core.converters.ArffLoader;import weka.core.*;import weka.classifiers.*;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Random;import weka.classifiers.evaluation.ThresholdCurve;import weka.classifiers.rules.JRip;import weka.classifiers.rules.OneR;import weka.classifiers.rules.Ridor;import weka.classifiers.rules.Rule;import weka.classifiers.trees.J48;import weka.gui.visualize.PlotData2D;import weka.gui.visualize.ThresholdVisualizePanel;");
classSource.append(" public class Out1 {");
classSource.append(" public static void main(String[] args) throws IOException , Exception {");
classSource.append("BufferedReader breader= null;breader=new BufferedReader(new FileReader(\"C:/Users/Phd/Downloads/vowel.arff \"));Instances train1 = new Instances(breader);train1.setClassIndex(train1.numAttributes() - 1);");
classSource.append(" breader.close();");
classSource.append(" Ridor cls = new Ridor();cls.buildClassifier(train1); Evaluation eval =new Evaluation(train1);eval.crossValidateModel(cls, train1, 10, new Random(1));");
classSource.append(p);
classSource.append(" System.out.println(\"[----------Printing Fitness----------]\"+ y); ");
classSource.append(" File f = new File( \"c:\\\\Users\\\\pc\\\\Desktop\\\\myobj.obj\"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f)); out.writeDouble(y); out.flush(); out.close();");
classSource.append("}");
classSource.append("}");
//------------------------------------------------------------------------------------------------------------
//----------------------Compiling and Runing the individual---------------------------------------------------
compileAndRun(className, classSource.toString());
System.out.println("----***------Finish Compiling -----***------\n");
//------------------------------------------------------------------------------------------------------------
//----------------------reading the fitness value of the individual-------------------------------------------
ObjectInputStream restore = new ObjectInputStream(new FileInputStream("c:\\Users\\Phd\\Desktop\\myobj.obj"));
int ftnss = restore.readInt();
restore.close();
//------------------------------------------------------------------------------------------------------------
return ftnss;
}
public void getFitness(Individual i) {
try{ i.getFitness().setDouble(this.evaluateString(i.getPhenotype()));
} catch(IOException ex){ }
}
public boolean canCache()
{ return true;
}
} |
Partager