1 | package framsticks; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.BufferedWriter; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileNotFoundException; |
---|
7 | import java.io.FileReader; |
---|
8 | import java.io.FileWriter; |
---|
9 | import java.io.IOException; |
---|
10 | import java.io.InputStreamReader; |
---|
11 | |
---|
12 | import cecj.interaction.InteractionResult; |
---|
13 | import cecj.interaction.RealValuedResult; |
---|
14 | import cecj.utils.Pair; |
---|
15 | import ec.EvolutionState; |
---|
16 | import ec.util.Parameter; |
---|
17 | |
---|
18 | public class FramsticksUtils { |
---|
19 | |
---|
20 | private static final String NEW_CMD = "%s \"getsimplest 1 %s\" -q"; |
---|
21 | private static final String EVAL_CMD = "%s \"ex %s\" \"eval %s %s\" -q"; |
---|
22 | private static final String MUTATE_CMD = "%s rnd mut -q < %s"; |
---|
23 | private static final String XOVER_CMD = "%s rnd \"crossover %s %s\" -q"; |
---|
24 | |
---|
25 | private static final String GENOTYPE_DESC = "org:\ngenotype:~\n%s~\n"; |
---|
26 | private static final String TEMPORARY_FILE_NAME = "temp.gen"; |
---|
27 | |
---|
28 | private static final String P_DIRECTORY_PATH = "directory-path"; |
---|
29 | private static final String P_SCRIPTS_OUTPUT = "scripts-output"; |
---|
30 | private static final String P_SETTINGS = "settings-file"; |
---|
31 | private static final String P_WORKING_DIRECTORY = "working-directory"; |
---|
32 | private static final String P_EXPERIMENT_DEFINITION = "expdef"; |
---|
33 | private static final String P_EXECUTABLE_COMMAND = "executable-cmd"; |
---|
34 | private static final String P_DEBUG = "debug"; |
---|
35 | |
---|
36 | private String directoryPath; |
---|
37 | private String scriptsOutputPath; |
---|
38 | private String settingsFile; |
---|
39 | private String workingDirectory; |
---|
40 | private String experimentDefinition; |
---|
41 | private String executableCommand; |
---|
42 | private boolean debug; |
---|
43 | |
---|
44 | private static FramsticksUtils instance; |
---|
45 | |
---|
46 | public synchronized static FramsticksUtils getInstance(final EvolutionState state) { |
---|
47 | if (instance == null) { |
---|
48 | instance = new FramsticksUtils(); |
---|
49 | instance.setup(state); |
---|
50 | } |
---|
51 | return instance; |
---|
52 | } |
---|
53 | |
---|
54 | private void setup(final EvolutionState state) { |
---|
55 | Parameter def = FramsticksDefaults.base(); |
---|
56 | directoryPath = state.parameters.getString(null, def.push(P_DIRECTORY_PATH)); |
---|
57 | if (directoryPath == null) { |
---|
58 | state.output.fatal("No Framsticks directory specified", def.push(P_DIRECTORY_PATH)); |
---|
59 | } |
---|
60 | scriptsOutputPath = state.parameters.getString(null, def.push(P_SCRIPTS_OUTPUT)); |
---|
61 | if (scriptsOutputPath == null) { |
---|
62 | state.output.fatal("No scripts output file specified", def.push(P_SCRIPTS_OUTPUT)); |
---|
63 | } |
---|
64 | settingsFile = state.parameters.getString(null, def.push(P_SETTINGS)); |
---|
65 | if (settingsFile == null) { |
---|
66 | state.output.fatal("No settings file specified", def.push(P_SETTINGS)); |
---|
67 | } |
---|
68 | workingDirectory = state.parameters.getString(null, def.push(P_WORKING_DIRECTORY)); |
---|
69 | if (workingDirectory == null) { |
---|
70 | state.output.fatal("No working directory specified", def.push(P_WORKING_DIRECTORY)); |
---|
71 | } |
---|
72 | experimentDefinition = state.parameters.getString(null, def.push(P_EXPERIMENT_DEFINITION)); |
---|
73 | if (experimentDefinition == null) { |
---|
74 | state.output.fatal("No experiment definition specified", def |
---|
75 | .push(P_EXPERIMENT_DEFINITION)); |
---|
76 | } |
---|
77 | executableCommand = state.parameters.getString(null, def.push(P_EXECUTABLE_COMMAND)); |
---|
78 | if (executableCommand == null) { |
---|
79 | state.output.fatal("No executable command specified", def.push(P_EXECUTABLE_COMMAND)); |
---|
80 | } |
---|
81 | |
---|
82 | debug = state.parameters.getBoolean(null, def.push(P_DEBUG), false); |
---|
83 | } |
---|
84 | |
---|
85 | private String executeCommand(String command) { |
---|
86 | if (debug) { |
---|
87 | System.err.println("Executing command : " + command); |
---|
88 | } |
---|
89 | |
---|
90 | String result = new String(); |
---|
91 | try { |
---|
92 | File f = new File(directoryPath); |
---|
93 | |
---|
94 | String[] cmd; |
---|
95 | String os = System.getProperty("os.name"); |
---|
96 | if(os.contains("Linux")) |
---|
97 | { |
---|
98 | cmd = new String[] { "/bin/bash", "-c", directoryPath + command }; |
---|
99 | } |
---|
100 | else if(os.contains("Windows")) |
---|
101 | { |
---|
102 | cmd = new String[] { "cmd.exe", "/C", directoryPath + command }; |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | throw new Exception("Not supported OS"); |
---|
107 | } |
---|
108 | |
---|
109 | Process p = Runtime.getRuntime().exec(cmd, null, f); |
---|
110 | |
---|
111 | BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
---|
112 | result = readInput(input); |
---|
113 | } catch (Exception ex) { |
---|
114 | ex.printStackTrace(); |
---|
115 | } |
---|
116 | |
---|
117 | if (debug) { |
---|
118 | System.err.println("Result : " + result); |
---|
119 | } |
---|
120 | |
---|
121 | return result; |
---|
122 | } |
---|
123 | |
---|
124 | private void saveToFile(String filePath, String contents) { |
---|
125 | try { |
---|
126 | BufferedWriter output = new BufferedWriter(new FileWriter(filePath)); |
---|
127 | output.write(contents); |
---|
128 | output.close(); |
---|
129 | } catch (Exception ex) { |
---|
130 | ex.printStackTrace(); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | private String readFromFile(String filePath) { |
---|
135 | String result = new String(); |
---|
136 | try { |
---|
137 | BufferedReader input = new BufferedReader(new FileReader(filePath)); |
---|
138 | result = readInput(input); |
---|
139 | } catch (FileNotFoundException e) { |
---|
140 | e.printStackTrace(); |
---|
141 | } |
---|
142 | return result; |
---|
143 | } |
---|
144 | |
---|
145 | private String readInput(BufferedReader input) { |
---|
146 | StringBuilder result = new StringBuilder(); |
---|
147 | |
---|
148 | try { |
---|
149 | String line; |
---|
150 | while ((line = input.readLine()) != null) { |
---|
151 | result.append(line + '\n'); |
---|
152 | } |
---|
153 | input.close(); |
---|
154 | } catch (IOException ex) { |
---|
155 | ex.printStackTrace(); |
---|
156 | } |
---|
157 | |
---|
158 | // Delete last newline character |
---|
159 | if (result.length() > 0) { |
---|
160 | return result.substring(0, result.length() - 1); |
---|
161 | } else { |
---|
162 | return result.toString(); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | public float evaluateGenotype(String genotype, String fileName) { |
---|
167 | String fileContents = String.format(GENOTYPE_DESC, genotype); |
---|
168 | String filePath = workingDirectory + fileName; |
---|
169 | |
---|
170 | saveToFile(filePath, fileContents); |
---|
171 | executeCommand(String.format(EVAL_CMD, executableCommand, experimentDefinition, |
---|
172 | settingsFile, filePath)); |
---|
173 | String evaluation = readFromFile(scriptsOutputPath); |
---|
174 | |
---|
175 | return Float.parseFloat(evaluation.split("\t")[1]); |
---|
176 | } |
---|
177 | |
---|
178 | @Deprecated |
---|
179 | public Pair<? extends InteractionResult> pseudoCoevolutionaryEvaluate(String candidate, |
---|
180 | String test, String fileName) { |
---|
181 | float candidateResult = evaluateGenotype(candidate, fileName); |
---|
182 | float testResult = evaluateGenotype(test, fileName); |
---|
183 | |
---|
184 | return new Pair<RealValuedResult>(new RealValuedResult(candidateResult), |
---|
185 | new RealValuedResult(testResult)); |
---|
186 | } |
---|
187 | |
---|
188 | private static float chasingBest = 2.0f; |
---|
189 | private static float chasedBest = 2.0f; |
---|
190 | |
---|
191 | public Pair<? extends InteractionResult> coevolutionaryEvaluate(String candidate, String test, |
---|
192 | String fileName) { |
---|
193 | String fileContents = String.format(GENOTYPE_DESC, candidate) + "\n" |
---|
194 | + String.format(GENOTYPE_DESC, test); |
---|
195 | String filePath = workingDirectory + fileName; |
---|
196 | saveToFile(filePath, fileContents); |
---|
197 | |
---|
198 | executeCommand(String.format(EVAL_CMD, executableCommand, experimentDefinition, |
---|
199 | settingsFile, filePath)); |
---|
200 | String evaluation = readFromFile(scriptsOutputPath); |
---|
201 | |
---|
202 | float candidateResult = 0; |
---|
203 | float testResult = 0; |
---|
204 | |
---|
205 | try { |
---|
206 | |
---|
207 | String[] str = evaluation.split("\n"); |
---|
208 | candidateResult = Float.parseFloat(str[0].split("\t")[1]); //chasing |
---|
209 | testResult = Float.parseFloat(str[1].split("\t")[1]); //chased |
---|
210 | |
---|
211 | chasingBest = ((candidateResult < chasingBest)?(candidateResult):(chasingBest)); |
---|
212 | chasedBest = ((chasedBest < testResult )?(chasedBest ):(testResult)); |
---|
213 | |
---|
214 | System.out.printf("%7.4f %7.4f --> %7.4f %7.4f\n", candidateResult, chasingBest, testResult, chasedBest); |
---|
215 | } |
---|
216 | catch(Exception ex) { |
---|
217 | |
---|
218 | System.out.println(scriptsOutputPath + " in bad format. There must be 2 lines with results."); |
---|
219 | } |
---|
220 | |
---|
221 | return new Pair<RealValuedResult>(new RealValuedResult(candidateResult), |
---|
222 | new RealValuedResult(testResult)); |
---|
223 | } |
---|
224 | |
---|
225 | public String mutateGenotype(String genotype) { |
---|
226 | String filePath = workingDirectory + TEMPORARY_FILE_NAME; |
---|
227 | saveToFile(filePath, genotype); |
---|
228 | return executeCommand(String.format(MUTATE_CMD, executableCommand, filePath)); |
---|
229 | } |
---|
230 | |
---|
231 | public String crossoverGenotypes(String genotype1, String genotype2) { |
---|
232 | String filePath1 = workingDirectory + TEMPORARY_FILE_NAME; |
---|
233 | String filePath2 = workingDirectory + "_" + TEMPORARY_FILE_NAME; |
---|
234 | saveToFile(filePath1, genotype1); |
---|
235 | saveToFile(filePath2, genotype2); |
---|
236 | return executeCommand(String.format(XOVER_CMD, executableCommand, filePath1, filePath2)); |
---|
237 | } |
---|
238 | |
---|
239 | public String getNewGenotype(int initializationType) { |
---|
240 | return executeCommand(String.format(NEW_CMD, executableCommand, initializationType)); |
---|
241 | } |
---|
242 | |
---|
243 | /* |
---|
244 | * Sample usage : |
---|
245 | */ |
---|
246 | /* |
---|
247 | * public static void main(String[] args) { FramsticksUtils utils = |
---|
248 | * FramsticksUtils.getInstance(); System.out.println(utils.evaluateGenotype("X", "halo1.gen")); |
---|
249 | * System.out.println(utils.mutateGenotype("X")); |
---|
250 | * System.out.println(utils.crossoverGenotypes("AX", "MX")); |
---|
251 | * System.out.println(utils.getNewGenotype()); } |
---|
252 | */ |
---|
253 | } |
---|