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 |
|
---|
34 | private String directoryPath;
|
---|
35 | private String scriptsOutputPath;
|
---|
36 | private String settingsFile;
|
---|
37 | private String workingDirectory;
|
---|
38 | private String experimentDefinition;
|
---|
39 | private String executableCommand;
|
---|
40 |
|
---|
41 | private static FramsticksUtils instance;
|
---|
42 |
|
---|
43 | public synchronized static FramsticksUtils getInstance(final EvolutionState state) {
|
---|
44 | if (instance == null) {
|
---|
45 | instance = new FramsticksUtils();
|
---|
46 | instance.setup(state);
|
---|
47 | }
|
---|
48 | return instance;
|
---|
49 | }
|
---|
50 |
|
---|
51 | private void setup(final EvolutionState state) {
|
---|
52 | Parameter def = FramsticksDefaults.base();
|
---|
53 | directoryPath = state.parameters.getString(null, def.push(P_DIRECTORY_PATH));
|
---|
54 | if (directoryPath == null) {
|
---|
55 | state.output.fatal("No Framsticks directory specified", def.push(P_DIRECTORY_PATH));
|
---|
56 | }
|
---|
57 | scriptsOutputPath = state.parameters.getString(null, def.push(P_SCRIPTS_OUTPUT));
|
---|
58 | if (scriptsOutputPath == null) {
|
---|
59 | state.output.fatal("No scripts output file specified", def.push(P_SCRIPTS_OUTPUT));
|
---|
60 | }
|
---|
61 | settingsFile = state.parameters.getString(null, def.push(P_SETTINGS));
|
---|
62 | if (settingsFile == null) {
|
---|
63 | state.output.fatal("No settings file specified", def.push(P_SETTINGS));
|
---|
64 | }
|
---|
65 | workingDirectory = state.parameters.getString(null, def.push(P_WORKING_DIRECTORY));
|
---|
66 | if (workingDirectory == null) {
|
---|
67 | state.output.fatal("No working directory specified", def.push(P_WORKING_DIRECTORY));
|
---|
68 | }
|
---|
69 | experimentDefinition = state.parameters.getString(null, def.push(P_EXPERIMENT_DEFINITION));
|
---|
70 | if (experimentDefinition == null) {
|
---|
71 | state.output.fatal("No experiment definition specified", def
|
---|
72 | .push(P_EXPERIMENT_DEFINITION));
|
---|
73 | }
|
---|
74 | executableCommand = state.parameters.getString(null, def.push(P_EXECUTABLE_COMMAND));
|
---|
75 | if (executableCommand == null) {
|
---|
76 | state.output.fatal("No executable command specified", def.push(P_EXECUTABLE_COMMAND));
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | private String executeCommand(String command) {
|
---|
81 | String line;
|
---|
82 | String result = "";
|
---|
83 | try {
|
---|
84 | File f = new File(directoryPath);
|
---|
85 | Process p = Runtime.getRuntime().exec(directoryPath + command, null, f);
|
---|
86 | BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
---|
87 | while ((line = input.readLine()) != null) {
|
---|
88 | result += (line + '\n');
|
---|
89 | }
|
---|
90 | input.close();
|
---|
91 | } catch (Exception ex) {
|
---|
92 | ex.printStackTrace();
|
---|
93 | }
|
---|
94 |
|
---|
95 | return result;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void saveToFile(String filePath, String contents) {
|
---|
99 | try {
|
---|
100 | BufferedWriter output = new BufferedWriter(new FileWriter(filePath));
|
---|
101 | output.write(contents);
|
---|
102 | output.close();
|
---|
103 | } catch (Exception ex) {
|
---|
104 | ex.printStackTrace();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | private String readFromFile(String filePath) {
|
---|
109 | String line;
|
---|
110 | String result = "";
|
---|
111 | try {
|
---|
112 | BufferedReader input = new BufferedReader(new FileReader(filePath));
|
---|
113 | while ((line = input.readLine()) != null) {
|
---|
114 | result += (line + '\n');
|
---|
115 | }
|
---|
116 | input.close();
|
---|
117 | } catch (Exception ex) {
|
---|
118 | ex.printStackTrace();
|
---|
119 | }
|
---|
120 |
|
---|
121 | return result;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public float evaluateGenotype(String genotype, String fileName) {
|
---|
125 | String fileContents = String.format(GENOTYPE_DESC, genotype);
|
---|
126 | String filePath = workingDirectory + fileName;
|
---|
127 |
|
---|
128 | saveToFile(filePath, fileContents);
|
---|
129 | executeCommand(String.format(EVAL_CMD, executableCommand, experimentDefinition,
|
---|
130 | settingsFile, filePath));
|
---|
131 | String evaluation = readFromFile(scriptsOutputPath);
|
---|
132 |
|
---|
133 | return Float.parseFloat(evaluation.split("\t")[1]);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public Pair<? extends InteractionResult> coevolutionaryEvaluate(String candidate, String test,
|
---|
137 | String fileName) {
|
---|
138 | float candidateResult = evaluateGenotype(candidate, fileName);
|
---|
139 | float testResult = evaluateGenotype(test, fileName);
|
---|
140 |
|
---|
141 | return new Pair<RealValuedResult>(new RealValuedResult(candidateResult),
|
---|
142 | new RealValuedResult(testResult));
|
---|
143 | }
|
---|
144 |
|
---|
145 | public String mutateGenotype(String genotype) {
|
---|
146 | String filePath = workingDirectory + TEMPORARY_FILE_NAME;
|
---|
147 | saveToFile(filePath, genotype);
|
---|
148 | return executeCommand(String.format(MUTATE_CMD, executableCommand, filePath));
|
---|
149 | }
|
---|
150 |
|
---|
151 | public String crossoverGenotypes(String genotype1, String genotype2) {
|
---|
152 | String filePath1 = workingDirectory + TEMPORARY_FILE_NAME;
|
---|
153 | String filePath2 = workingDirectory + "_" + TEMPORARY_FILE_NAME;
|
---|
154 | saveToFile(filePath1, genotype1);
|
---|
155 | saveToFile(filePath2, genotype2);
|
---|
156 | return executeCommand(String.format(XOVER_CMD, executableCommand, filePath1, filePath2));
|
---|
157 | }
|
---|
158 |
|
---|
159 | public String getNewGenotype() {
|
---|
160 | return executeCommand(String.format(NEW_CMD, executableCommand));
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Sample usage :
|
---|
165 | */
|
---|
166 | /*
|
---|
167 | * public static void main(String[] args) { FramsticksUtils utils =
|
---|
168 | * FramsticksUtils.getInstance(); System.out.println(utils.evaluateGenotype("X", "halo1.gen"));
|
---|
169 | * System.out.println(utils.mutateGenotype("X"));
|
---|
170 | * System.out.println(utils.crossoverGenotypes("AX", "MX"));
|
---|
171 | * System.out.println(utils.getNewGenotype()); }
|
---|
172 | */
|
---|
173 | }
|
---|