[44] | 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";
|
---|
[48] | 33 | private static final String P_DEBUG = "debug";
|
---|
[44] | 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;
|
---|
[48] | 41 | private boolean debug;
|
---|
[44] | 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 | }
|
---|
[48] | 80 |
|
---|
| 81 | debug = state.parameters.getBoolean(null, def.push(P_DEBUG), false);
|
---|
[44] | 82 | }
|
---|
| 83 |
|
---|
| 84 | private String executeCommand(String command) {
|
---|
| 85 | String line;
|
---|
| 86 | String result = "";
|
---|
| 87 | try {
|
---|
| 88 | File f = new File(directoryPath);
|
---|
| 89 | Process p = Runtime.getRuntime().exec(directoryPath + command, null, f);
|
---|
| 90 | BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
---|
| 91 | while ((line = input.readLine()) != null) {
|
---|
| 92 | result += (line + '\n');
|
---|
| 93 | }
|
---|
| 94 | input.close();
|
---|
| 95 | } catch (Exception ex) {
|
---|
| 96 | ex.printStackTrace();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[48] | 99 | if (debug) {
|
---|
| 100 | System.out.println("Executing command : " + command);
|
---|
| 101 | System.out.println("Result : " + result);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[44] | 104 | return result;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | private void saveToFile(String filePath, String contents) {
|
---|
| 108 | try {
|
---|
| 109 | BufferedWriter output = new BufferedWriter(new FileWriter(filePath));
|
---|
| 110 | output.write(contents);
|
---|
| 111 | output.close();
|
---|
| 112 | } catch (Exception ex) {
|
---|
| 113 | ex.printStackTrace();
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private String readFromFile(String filePath) {
|
---|
| 118 | String line;
|
---|
| 119 | String result = "";
|
---|
| 120 | try {
|
---|
| 121 | BufferedReader input = new BufferedReader(new FileReader(filePath));
|
---|
| 122 | while ((line = input.readLine()) != null) {
|
---|
| 123 | result += (line + '\n');
|
---|
| 124 | }
|
---|
| 125 | input.close();
|
---|
| 126 | } catch (Exception ex) {
|
---|
| 127 | ex.printStackTrace();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return result;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | public float evaluateGenotype(String genotype, String fileName) {
|
---|
| 134 | String fileContents = String.format(GENOTYPE_DESC, genotype);
|
---|
| 135 | String filePath = workingDirectory + fileName;
|
---|
| 136 |
|
---|
| 137 | saveToFile(filePath, fileContents);
|
---|
| 138 | executeCommand(String.format(EVAL_CMD, executableCommand, experimentDefinition,
|
---|
| 139 | settingsFile, filePath));
|
---|
| 140 | String evaluation = readFromFile(scriptsOutputPath);
|
---|
| 141 |
|
---|
| 142 | return Float.parseFloat(evaluation.split("\t")[1]);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public Pair<? extends InteractionResult> coevolutionaryEvaluate(String candidate, String test,
|
---|
| 146 | String fileName) {
|
---|
| 147 | float candidateResult = evaluateGenotype(candidate, fileName);
|
---|
| 148 | float testResult = evaluateGenotype(test, fileName);
|
---|
| 149 |
|
---|
| 150 | return new Pair<RealValuedResult>(new RealValuedResult(candidateResult),
|
---|
| 151 | new RealValuedResult(testResult));
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public String mutateGenotype(String genotype) {
|
---|
| 155 | String filePath = workingDirectory + TEMPORARY_FILE_NAME;
|
---|
| 156 | saveToFile(filePath, genotype);
|
---|
| 157 | return executeCommand(String.format(MUTATE_CMD, executableCommand, filePath));
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | public String crossoverGenotypes(String genotype1, String genotype2) {
|
---|
| 161 | String filePath1 = workingDirectory + TEMPORARY_FILE_NAME;
|
---|
| 162 | String filePath2 = workingDirectory + "_" + TEMPORARY_FILE_NAME;
|
---|
| 163 | saveToFile(filePath1, genotype1);
|
---|
| 164 | saveToFile(filePath2, genotype2);
|
---|
| 165 | return executeCommand(String.format(XOVER_CMD, executableCommand, filePath1, filePath2));
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public String getNewGenotype() {
|
---|
| 169 | return executeCommand(String.format(NEW_CMD, executableCommand));
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /*
|
---|
| 173 | * Sample usage :
|
---|
| 174 | */
|
---|
| 175 | /*
|
---|
| 176 | * public static void main(String[] args) { FramsticksUtils utils =
|
---|
| 177 | * FramsticksUtils.getInstance(); System.out.println(utils.evaluateGenotype("X", "halo1.gen"));
|
---|
| 178 | * System.out.println(utils.mutateGenotype("X"));
|
---|
| 179 | * System.out.println(utils.crossoverGenotypes("AX", "MX"));
|
---|
| 180 | * System.out.println(utils.getNewGenotype()); }
|
---|
| 181 | */
|
---|
| 182 | }
|
---|