diff options
Diffstat (limited to 'src/jcgp/backend/parsers')
-rw-r--r-- | src/jcgp/backend/parsers/ChromosomeParser.java | 56 | ||||
-rw-r--r-- | src/jcgp/backend/parsers/FunctionParser.java | 37 |
2 files changed, 71 insertions, 22 deletions
diff --git a/src/jcgp/backend/parsers/ChromosomeParser.java b/src/jcgp/backend/parsers/ChromosomeParser.java index 838ec48..6106dbd 100644 --- a/src/jcgp/backend/parsers/ChromosomeParser.java +++ b/src/jcgp/backend/parsers/ChromosomeParser.java @@ -29,9 +29,9 @@ public abstract class ChromosomeParser { * is not viable to implement these defensive measures with the chromosome format used * by CGP. * - * @param file the .chr file to parse from - * @param chromosome the chromosome to configure - * @param resources the experiment resources + * @param file the .chr file to parse from. + * @param chromosome the chromosome to configure. + * @param resources the experiment resources. */ public static void parse(File file, Chromosome chromosome, Resources resources) { /* @@ -134,8 +134,8 @@ public abstract class ChromosomeParser { * The file is written in the standard .chr format and can * be read by the original CGP implementation. * - * @param file the file to write to - * @param chromosome the chromosome to save + * @param file the file to write to. + * @param chromosome the chromosome to save. * @param resources a reference to the experiment's resources. */ public static void save(File file, Chromosome chromosome, Resources resources) { @@ -187,4 +187,50 @@ public abstract class ChromosomeParser { resources.println("[Parser] Chromosome saved successfully"); } + + /** + * Writes a chromosome to the console in .chr format. Note + * that, if using a GUI console, that console must be flushed for the + * output to appear. + * + * @param chromosome the chromosome to save. + * @param resources a reference to the experiment's resources. + */ + public static void print(Chromosome chromosome, Resources resources) { + + // for all nodes, columns first + for (int c = 0; c < resources.columns(); c++) { + for (int r = 0; r < resources.rows(); r++) { + for (int i = 0; i < resources.arity(); i++) { + // print the connections, separated by spaces + Connection conn = chromosome.getNode(r, c).getConnection(i); + if (conn instanceof Input) { + resources.print(" " + ((Input) conn).getIndex()); + } else if (conn instanceof Node) { + resources.print(" " + (((((Node) conn).getColumn() + 1) * resources.inputs()) + ((Node) conn).getRow())); + } else { + resources.println("[Parser] Error: could not handle " + conn.getClass() + " as a subclass of Connection"); + } + } + // print the function numbers + resources.print(" " + resources.getFunctionIndex(chromosome.getNode(r, c).getFunction())); + // node is done, print tab + resources.print("\t"); + } + } + // nodes are done, print two tabs to separate from output + resources.print("\t\t"); + for (int o = 0; o < resources.outputs(); o ++) { + Connection source = chromosome.getOutput(o).getSource(); + if (source instanceof Input) { + resources.print(" " + ((Input) source).getIndex()); + } else if (source instanceof Node) { + resources.print(" " + (((((Node) source).getColumn() + 1) * resources.inputs()) + ((Node) source).getRow())); + } else { + resources.println("[Parser] Error: could not handle " + source.getClass() + " as a subclass of Connection"); + } + } + + resources.println(""); + } } diff --git a/src/jcgp/backend/parsers/FunctionParser.java b/src/jcgp/backend/parsers/FunctionParser.java index 806099e..081c08a 100644 --- a/src/jcgp/backend/parsers/FunctionParser.java +++ b/src/jcgp/backend/parsers/FunctionParser.java @@ -42,11 +42,11 @@ public abstract class FunctionParser { resources.println("[Parser] Error: could not find " + file.getAbsolutePath()); return; } - + Scanner in = new Scanner(fr); boolean excessFunctions = false; resources.println("[Parser] Parsing file: " + file.getAbsolutePath() + "..."); - + /* * The encoding used in .par files is quite simple, so regex matches are used to extract * the values. @@ -71,29 +71,32 @@ public abstract class FunctionParser { */ while (in.hasNextLine()) { String line = in.nextLine(); - if (line.substring(line.length() - 1).matches("[0-9]")) { - String[] splitString = line.split("[^0-9]+"); - int functionIndex = Integer.parseInt(splitString[splitString.length - 1]); - - if (functionIndex < functionSet.getTotalFunctionCount()) { - if (Integer.parseInt(splitString[0]) != 0) { - functionSet.enableFunction(functionIndex); - resources.println("[Parser] Enabled function: " + functionSet.getFunction(functionIndex)); - } else if (Integer.parseInt(splitString[0]) == 0) { - functionSet.disableFunction(functionIndex); - resources.println("[Parser] Disabled function: " + functionSet.getFunction(functionIndex)); + if (!line.isEmpty()) { + if (line.substring(line.length() - 1).matches("[0-9]")) { + String[] splitString = line.split("[^0-9]+"); + int functionIndex = Integer.parseInt(splitString[splitString.length - 1]); + + if (functionIndex < functionSet.getTotalFunctionCount()) { + if (Integer.parseInt(splitString[0]) != 0) { + functionSet.enableFunction(functionIndex); + resources.println("[Parser] Enabled function: " + functionSet.getFunction(functionIndex)); + } else if (Integer.parseInt(splitString[0]) == 0) { + functionSet.disableFunction(functionIndex); + resources.println("[Parser] Disabled function: " + functionSet.getFunction(functionIndex)); + } + } else { + excessFunctions = true; } - } else { - excessFunctions = true; } } + } - + // warn the user function index went out of bounds if (excessFunctions) { resources.println("[Parser] Warning: the parameter file contained more functions than the current function set"); } - + in.close(); resources.println("[Parser] Finished parsing functions"); } |