aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/parser/ChromosomeParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/parser/ChromosomeParser.java')
-rw-r--r--src/jcgp/backend/parser/ChromosomeParser.java60
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");
}
}