diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-22 08:48:11 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-22 08:48:11 +0100 |
commit | 6769419bea279935c4a3a84616d45ee7d4a6345c (patch) | |
tree | 79a8c80865f0663e82eb9986f5b7c5f999e64aab /src/jcgp/backend/parser | |
parent | efe490fec1c7a94f004b496c7c97c82083fe44ec (diff) |
Chromosome parser is buggy, need to fix that.
Diffstat (limited to 'src/jcgp/backend/parser')
-rw-r--r-- | src/jcgp/backend/parser/ChromosomeParser.java | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/src/jcgp/backend/parser/ChromosomeParser.java b/src/jcgp/backend/parser/ChromosomeParser.java index d5af1bd..fd03b7d 100644 --- a/src/jcgp/backend/parser/ChromosomeParser.java +++ b/src/jcgp/backend/parser/ChromosomeParser.java @@ -3,10 +3,12 @@ package jcgp.backend.parser; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.PrintWriter; import java.util.Scanner; import jcgp.backend.population.Chromosome; import jcgp.backend.population.Connection; +import jcgp.backend.population.Input; import jcgp.backend.population.Node; import jcgp.backend.resources.Resources; @@ -73,15 +75,16 @@ public abstract class ChromosomeParser { Connection newConnection; Node changingNode; // for all nodes, columns first - for (int c = 0; c < resources.columns(); c ++) { - for (int r = 0; r < resources.rows(); r ++) { + for (int c = 0; c < resources.columns(); c++) { + for (int r = 0; r < resources.rows(); r++) { // store the changing node changingNode = chromosome.getNode(r, c); // for every connection - for (int i = 0; i < resources.arity(); i ++) { + for (int i = 0; i < resources.arity(); i++) { // get connection number from the .chr file gene = in.nextInt(); + System.out.println("r: " + r + " c: " + c + " i: " + i + " gene: " + gene); if (gene < resources.inputs()) { // connection was an input newConnection = chromosome.getInput(gene); @@ -113,6 +116,8 @@ public abstract class ChromosomeParser { chromosome.getOutput(o).setConnection(0, newConnection); } in.close(); + } else { + throw new IllegalArgumentException("The topology of the chromosome in " + file.getName() + " does not match that of the experiment."); } } @@ -120,12 +125,53 @@ public abstract class ChromosomeParser { * Writes a chromosome into the specified .chr file. * * - * - * @param file - * @param chromosome + * @param file the file to write to + * @param chromosome the chromosome to save */ - public static void save(File file, Chromosome chromosome) { + public static void save(File file, Chromosome chromosome, Resources resources) { + PrintWriter writer; + try { + writer = new PrintWriter(file); + } catch (FileNotFoundException e) { + // something bad happened + return; + } + + // 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) { + writer.print(" " + ((Input) conn).getIndex()); + } else if (conn instanceof Node) { + writer.print(" " + (((((Node) conn).getColumn() + 1) * resources.inputs()) + ((Node) conn).getRow())); + } else { + throw new ClassCastException("Could not handle " + conn.getClass() + " as a subclass of Connection"); + } + } + // print the function numbers + writer.print(" " + resources.getFunctionIndex(chromosome.getNode(r, c).getFunction())); + // node is done, print tab + writer.print("\t"); + } + } + // nodes are done, print two tabs to separate from output + writer.print("\t\t"); + for (int o = 0; o < resources.outputs(); o ++) { + Connection source = chromosome.getOutput(o).getSource(); + if (source instanceof Input) { + writer.print(" " + ((Input) source).getIndex()); + } else if (source instanceof Node) { + writer.print(" " + (((((Node) source).getColumn() + 1) * resources.inputs()) + ((Node) source).getRow())); + } else { + throw new ClassCastException("Could not handle " + source.getClass() + " as a subclass of Connection"); + } + } + writer.close(); + System.out.println("writer is closed"); } } |