diff options
Diffstat (limited to 'src/jcgp/backend')
-rw-r--r-- | src/jcgp/backend/parser/ChromosomeParser.java | 60 | ||||
-rw-r--r-- | src/jcgp/backend/population/Node.java | 8 | ||||
-rw-r--r-- | src/jcgp/backend/population/Population.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/resources/Resources.java | 10 |
4 files changed, 70 insertions, 10 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"); } } diff --git a/src/jcgp/backend/population/Node.java b/src/jcgp/backend/population/Node.java index 6a558a4..b716ff8 100644 --- a/src/jcgp/backend/population/Node.java +++ b/src/jcgp/backend/population/Node.java @@ -31,8 +31,12 @@ public class Node extends Gene implements MutableElement, Connection { @Override public void setConnection(int index, Connection newConnection) { - connections[index] = newConnection; - chromosome.recomputeActiveNodes(); + if (newConnection instanceof Node) { + if (((Node) newConnection).getColumn() < column) { + connections[index] = newConnection; + chromosome.recomputeActiveNodes(); + } + } } public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException { diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java index 1bbdc54..b6dd055 100644 --- a/src/jcgp/backend/population/Population.java +++ b/src/jcgp/backend/population/Population.java @@ -4,7 +4,7 @@ import jcgp.backend.resources.Resources; public class Population { - private Chromosome[] chromosomes; + private final Chromosome[] chromosomes; private final Resources resources; private int fittest = 0; diff --git a/src/jcgp/backend/resources/Resources.java b/src/jcgp/backend/resources/Resources.java index 151d536..6eb75cd 100644 --- a/src/jcgp/backend/resources/Resources.java +++ b/src/jcgp/backend/resources/Resources.java @@ -320,6 +320,16 @@ public class Resources { return functionSet; } + public int getFunctionIndex(Function function) { + for (int i = 0; i < functionSet.getTotalFunctionCount(); i++) { + if (function == functionSet.getFunction(i)) { + return i; + } + } + // not found, default to 0 + return 0; + } + /* * Console functionality * These are affected by parameter report |