1 | package framsticks;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.BufferedWriter;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileReader;
|
---|
7 | import java.io.FileWriter;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 |
|
---|
10 | import cecj.interaction.InteractionResult;
|
---|
11 | import cecj.interaction.RealValuedResult;
|
---|
12 | import cecj.utils.Pair;
|
---|
13 |
|
---|
14 | import ec.EvolutionState;
|
---|
15 | import ec.util.Parameter;
|
---|
16 |
|
---|
17 | public class FramsticksUtils {
|
---|
18 |
|
---|
19 | private static final String NEW_CMD = "%s \"getsimplest 1\" -q";
|
---|
20 | private static final String EVAL_CMD = "%s \"ex %s\" \"eval %s %s\" -q";
|
---|
21 | private static final String MUTATE_CMD = "%s rnd \"mutate %s\" -q";
|
---|
22 | private static final String XOVER_CMD = "%s rnd \"crossover %s %s\" -q";
|
---|
23 |
|
---|
24 | private static final String GENOTYPE_DESC = "org:\ngenotype:~\n%s~\n";
|
---|
25 | private static final String TEMPORARY_FILE_NAME = "temp.gen";
|
---|
26 |
|
---|
27 | private static final String P_DIRECTORY_PATH = "directory-path";
|
---|
28 | private static final String P_SCRIPTS_OUTPUT = "scripts-output";
|
---|
29 | private static final String P_SETTINGS = "settings-file";
|
---|
30 | private static final String P_WORKING_DIRECTORY = "working-directory";
|
---|
31 | private static final String P_EXPERIMENT_DEFINITION = "expdef";
|
---|
32 | private static final String P_EXECUTABLE_COMMAND = "executable-cmd";
|
---|
33 | private static final String P_DEBUG = "debug";
|
---|
34 |
|
---|
35 | private String directoryPath;
|
---|
36 | private String scriptsOutputPath;
|
---|
37 | private String settingsFile;
|
---|
38 | private String workingDirectory;
|
---|
39 | private String experimentDefinition;
|
---|
40 | private String executableCommand;
|
---|
41 | private boolean debug;
|
---|
42 |
|
---|
43 | private static FramsticksUtils instance;
|
---|
44 |
|
---|
45 | public synchronized static FramsticksUtils getInstance(final EvolutionState state) {
|
---|
46 | if (instance == null) {
|
---|
47 | instance = new FramsticksUtils();
|
---|
48 | instance.setup(state);
|
---|
49 | }
|
---|
50 | return instance;
|
---|
51 | }
|
---|
52 |
|
---|
53 | private void setup(final EvolutionState state) {
|
---|
54 | Parameter def = FramsticksDefaults.base();
|
---|
55 | directoryPath = state.parameters.getString(null, def.push(P_DIRECTORY_PATH));
|
---|
56 | if (directoryPath == null) {
|
---|
57 | state.output.fatal("No Framsticks directory specified", def.push(P_DIRECTORY_PATH));
|
---|
58 | }
|
---|
59 | scriptsOutputPath = state.parameters.getString(null, def.push(P_SCRIPTS_OUTPUT));
|
---|
60 | if (scriptsOutputPath == null) {
|
---|
61 | state.output.fatal("No scripts output file specified", def.push(P_SCRIPTS_OUTPUT));
|
---|
62 | }
|
---|
63 | settingsFile = state.parameters.getString(null, def.push(P_SETTINGS));
|
---|
64 | if (settingsFile == null) {
|
---|
65 | state.output.fatal("No settings file specified", def.push(P_SETTINGS));
|
---|
66 | }
|
---|
67 | workingDirectory = state.parameters.getString(null, def.push(P_WORKING_DIRECTORY));
|
---|
68 | if (workingDirectory == null) {
|
---|
69 | state.output.fatal("No working directory specified", def.push(P_WORKING_DIRECTORY));
|
---|
70 | }
|
---|
71 | experimentDefinition = state.parameters.getString(null, def.push(P_EXPERIMENT_DEFINITION));
|
---|
72 | if (experimentDefinition == null) {
|
---|
73 | state.output.fatal("No experiment definition specified", def
|
---|
74 | .push(P_EXPERIMENT_DEFINITION));
|
---|
75 | }
|
---|
76 | executableCommand = state.parameters.getString(null, def.push(P_EXECUTABLE_COMMAND));
|
---|
77 | if (executableCommand == null) {
|
---|
78 | state.output.fatal("No executable command specified", def.push(P_EXECUTABLE_COMMAND));
|
---|
79 | }
|
---|
80 |
|
---|
81 | debug = state.parameters.getBoolean(null, def.push(P_DEBUG), false);
|
---|
82 | }
|
---|
83 |
|
---|
84 | private String executeCommand(String command) {
|
---|
85 | String line;
|
---|
86 | String result = "";
|
---|
87 |
|
---|
88 | if (debug) {
|
---|
89 | System.err.println("Executing command : " + command);
|
---|
90 | }
|
---|
91 |
|
---|
92 | try {
|
---|
93 | File f = new File(directoryPath);
|
---|
94 | Process p = Runtime.getRuntime().exec(directoryPath + command, null, f);
|
---|
95 | BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
---|
96 | while ((line = input.readLine()) != null) {
|
---|
97 | result += (line + '\n');
|
---|
98 | }
|
---|
99 | input.close();
|
---|
100 | } catch (Exception ex) {
|
---|
101 | ex.printStackTrace();
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (debug) {
|
---|
105 | System.err.println("Result : " + result);
|
---|
106 | }
|
---|
107 |
|
---|
108 | return result;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void saveToFile(String filePath, String contents) {
|
---|
112 | try {
|
---|
113 | BufferedWriter output = new BufferedWriter(new FileWriter(filePath));
|
---|
114 | output.write(contents);
|
---|
115 | output.close();
|
---|
116 | } catch (Exception ex) {
|
---|
117 | ex.printStackTrace();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private String readFromFile(String filePath) {
|
---|
122 | String line;
|
---|
123 | String result = "";
|
---|
124 | try {
|
---|
125 | BufferedReader input = new BufferedReader(new FileReader(filePath));
|
---|
126 | while ((line = input.readLine()) != null) {
|
---|
127 | result += (line + '\n');
|
---|
128 | }
|
---|
129 | input.close();
|
---|
130 | } catch (Exception ex) {
|
---|
131 | ex.printStackTrace();
|
---|
132 | }
|
---|
133 |
|
---|
134 | return result;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public float evaluateGenotype(String genotype, String fileName) {
|
---|
138 | String fileContents = String.format(GENOTYPE_DESC, genotype);
|
---|
139 | String filePath = workingDirectory + fileName;
|
---|
140 |
|
---|
141 | saveToFile(filePath, fileContents);
|
---|
142 | executeCommand(String.format(EVAL_CMD, executableCommand, experimentDefinition,
|
---|
143 | settingsFile, filePath));
|
---|
144 | String evaluation = readFromFile(scriptsOutputPath);
|
---|
145 |
|
---|
146 | return Float.parseFloat(evaluation.split("\t")[1]);
|
---|
147 | }
|
---|
148 |
|
---|
149 | public Pair<? extends InteractionResult> coevolutionaryEvaluate(String candidate, String test,
|
---|
150 | String fileName) {
|
---|
151 | float candidateResult = evaluateGenotype(candidate, fileName);
|
---|
152 | float testResult = evaluateGenotype(test, fileName);
|
---|
153 |
|
---|
154 | return new Pair<RealValuedResult>(new RealValuedResult(candidateResult),
|
---|
155 | new RealValuedResult(testResult));
|
---|
156 | }
|
---|
157 |
|
---|
158 | public String mutateGenotype(String genotype) {
|
---|
159 | String filePath = workingDirectory + TEMPORARY_FILE_NAME;
|
---|
160 | saveToFile(filePath, genotype);
|
---|
161 | return executeCommand(String.format(MUTATE_CMD, executableCommand, filePath));
|
---|
162 | }
|
---|
163 |
|
---|
164 | public String crossoverGenotypes(String genotype1, String genotype2) {
|
---|
165 | String filePath1 = workingDirectory + TEMPORARY_FILE_NAME;
|
---|
166 | String filePath2 = workingDirectory + "_" + TEMPORARY_FILE_NAME;
|
---|
167 | saveToFile(filePath1, genotype1);
|
---|
168 | saveToFile(filePath2, genotype2);
|
---|
169 | return executeCommand(String.format(XOVER_CMD, executableCommand, filePath1, filePath2));
|
---|
170 | }
|
---|
171 |
|
---|
172 | public String getNewGenotype() {
|
---|
173 | return executeCommand(String.format(NEW_CMD, executableCommand));
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * Sample usage :
|
---|
178 | */
|
---|
179 | /*
|
---|
180 | * public static void main(String[] args) { FramsticksUtils utils =
|
---|
181 | * FramsticksUtils.getInstance(); System.out.println(utils.evaluateGenotype("X", "halo1.gen"));
|
---|
182 | * System.out.println(utils.mutateGenotype("X"));
|
---|
183 | * System.out.println(utils.crossoverGenotypes("AX", "MX"));
|
---|
184 | * System.out.println(utils.getNewGenotype()); }
|
---|
185 | */
|
---|
186 | }
|
---|