From 6769419bea279935c4a3a84616d45ee7d4a6345c Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 22 Apr 2014 08:48:11 +0100 Subject: Chromosome parser is buggy, need to fix that. --- src/jcgp/JCGP.java | 6 +-- src/jcgp/backend/parser/ChromosomeParser.java | 60 +++++++++++++++++++--- src/jcgp/backend/population/Node.java | 8 ++- src/jcgp/backend/population/Population.java | 2 +- src/jcgp/backend/resources/Resources.java | 10 ++++ src/jcgp/gui/settings/SettingsPane.java | 13 +++-- .../settings/parameters/GUIDoubleParameter.java | 1 - src/jcgp/gui/settings/parameters/GUIParameter.java | 1 - 8 files changed, 83 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index f949944..2a6552c 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -267,12 +267,12 @@ public class JCGP { } } - public void loadChromosome(File file) { - ChromosomeParser.parse(file, population.getChromosome(0), resources); + public void loadChromosome(File file, int chromosomeIndex) { + ChromosomeParser.parse(file, population.getChromosome(chromosomeIndex), resources); } public void saveChromosome(File file, int chromosomeIndex) { - ChromosomeParser.save(file, population.getChromosome(chromosomeIndex)); + ChromosomeParser.save(file, population.getChromosome(chromosomeIndex), resources); } public boolean isFinished() { 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 diff --git a/src/jcgp/gui/settings/SettingsPane.java b/src/jcgp/gui/settings/SettingsPane.java index 4050a89..302e96d 100644 --- a/src/jcgp/gui/settings/SettingsPane.java +++ b/src/jcgp/gui/settings/SettingsPane.java @@ -326,12 +326,12 @@ public class SettingsPane extends AnchorPane { @Override public void handle(ActionEvent event) { FileChooser fc = new FileChooser(); - fc.setTitle("Open .chr file..."); + fc.setTitle("Load .chr file..."); fc.getExtensionFilters().add(new ExtensionFilter("CGP chromosome files", "*.chr")); fc.getExtensionFilters().add(new ExtensionFilter("All files", "*.*")); File chrFile = fc.showOpenDialog(gui.getStage()); if (chrFile != null) { - gui.getExperiment().loadChromosome(chrFile); + gui.getExperiment().loadChromosome(chrFile, 0); gui.reDraw(); } } @@ -339,7 +339,14 @@ public class SettingsPane extends AnchorPane { saveChromosome.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { - + FileChooser fc = new FileChooser(); + fc.setTitle("Save .chr file..."); + fc.getExtensionFilters().add(new ExtensionFilter("CGP chromosome files", "*.chr")); + fc.getExtensionFilters().add(new ExtensionFilter("All files", "*.*")); + File chrFile = fc.showSaveDialog(gui.getStage()); + if (chrFile != null) { + gui.getExperiment().saveChromosome(chrFile, 0); + } } }); diff --git a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java index 9a6ec4a..5331364 100644 --- a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java @@ -100,7 +100,6 @@ public class GUIDoubleParameter extends GUIParameter { @Override public void refreshValue() { - System.out.println("setting text to: " + parameter.get().doubleValue()); textField.setText(decimalFormat.format(parameter.get().doubleValue())); } } diff --git a/src/jcgp/gui/settings/parameters/GUIParameter.java b/src/jcgp/gui/settings/parameters/GUIParameter.java index 7dd7231..c6ac2e6 100644 --- a/src/jcgp/gui/settings/parameters/GUIParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIParameter.java @@ -85,7 +85,6 @@ public abstract class GUIParameter extends HBox { // the tooltip is the hover-over label containing status information, when appropriate tooltip = new Tooltip(); - tooltip.setStyle("-fx-background-color: white; -fx-border-color: black;"); tooltip.setSkin(null); valueControl = makeControl(); -- cgit v1.2.3