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 | |
parent | efe490fec1c7a94f004b496c7c97c82083fe44ec (diff) |
Chromosome parser is buggy, need to fix that.
Diffstat (limited to 'src')
-rw-r--r-- | src/jcgp/JCGP.java | 6 | ||||
-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 | ||||
-rw-r--r-- | src/jcgp/gui/settings/SettingsPane.java | 13 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIDoubleParameter.java | 1 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIParameter.java | 1 |
8 files changed, 83 insertions, 18 deletions
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<ActionEvent>() { @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<Number> { @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<T> 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(); |