From 260f1baaab10ab9b1db67ab587bc36adcb34494e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 8 Apr 2014 20:04:12 +0100 Subject: GUIParameters all refactored and commented. --- .../backend/resources/ModifiableResources.java | 193 ++++++++++++++- src/jcgp/backend/resources/Resources.java | 260 +++++++++++++-------- .../resources/parameters/BooleanParameter.java | 27 +-- .../resources/parameters/DoubleParameter.java | 25 +- .../resources/parameters/IntegerParameter.java | 26 +-- .../backend/resources/parameters/Parameter.java | 22 +- 6 files changed, 371 insertions(+), 182 deletions(-) (limited to 'src/jcgp/backend/resources') diff --git a/src/jcgp/backend/resources/ModifiableResources.java b/src/jcgp/backend/resources/ModifiableResources.java index a221f73..689f846 100644 --- a/src/jcgp/backend/resources/ModifiableResources.java +++ b/src/jcgp/backend/resources/ModifiableResources.java @@ -1,8 +1,6 @@ package jcgp.backend.resources; import jcgp.backend.function.FunctionSet; -import jcgp.backend.resources.parameters.BooleanParameter; -import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.IntegerParameter; /** @@ -21,19 +19,191 @@ public class ModifiableResources extends Resources { super(); } - public void set(String key, Object value) { - if (parameters.get(key) instanceof IntegerParameter) { - ((IntegerParameter) parameters.get(key)).set(((Integer) value).intValue()); - } else if (parameters.get(key) instanceof DoubleParameter) { - ((DoubleParameter) parameters.get(key)).set(((Double) value).doubleValue()); - } else if (parameters.get(key) instanceof BooleanParameter) { - ((BooleanParameter) parameters.get(key)).set(((Boolean) value).booleanValue()); - } + /** + * @param rows the rows to set + */ + public void setRows(int rows) { + this.rows.set(rows); + } + + /** + * @param columns the columns to set + */ + public void setColumns(int columns) { + this.columns.set(columns); + } + + /** + * @param inputs the inputs to set + */ + public void setInputs(int inputs) { + this.inputs.set(inputs); + } + + /** + * @param outputs the outputs to set + */ + public void setOutputs(int outputs) { + this.outputs.set(outputs); + } + + /** + * @param populationSize the populationSize to set + */ + public void setPopulationSize(int populationSize) { + this.populationSize.set(populationSize); + } + + /** + * @param levelsBack the levelsBack to set + */ + public void setLevelsBack(int levelsBack) { + this.levelsBack.set(levelsBack); + } + + /** + * @param currentGeneration the currentGeneration to set + */ + public void setCurrentGeneration(int currentGeneration) { + this.currentGeneration.set(currentGeneration); + } + + /** + * @param generations the generations to set + */ + public void setGenerations(int generations) { + this.generations.set(generations); + } + + /** + * @param currentRun the currentRun to set + */ + public void setCurrentRun(int currentRun) { + this.currentRun.set(currentRun); + } + + /** + * @param runs the runs to set + */ + public void setRuns(int runs) { + this.runs.set(runs); + } + + /** + * @param arity the arity to set + */ + public void setArity(int arity) { + this.arity.set(arity); + } + + /** + * @param seed the seed to set + */ + public void setSeed(int seed) { + this.seed.set(seed); + } + + /** + * @param report the report to set + */ + public void setReport(int report) { + this.report.set(report); + } + + /** + * @return the rows + */ + public IntegerParameter getRowsParameter() { + return rows; + } + + /** + * @return the columns + */ + public IntegerParameter getColumnsParameter() { + return columns; + } + + /** + * @return the inputs + */ + public IntegerParameter getInputsParameter() { + return inputs; + } + + /** + * @return the outputs + */ + public IntegerParameter getOutputsParameter() { + return outputs; + } + + /** + * @return the populationSize + */ + public IntegerParameter getPopulationSizeParameter() { + return populationSize; + } + + /** + * @return the levelsBack + */ + public IntegerParameter getLevelsBackParameter() { + return levelsBack; + } + + /** + * @return the currentGeneration + */ + public IntegerParameter getCurrentGenerationParameter() { + return currentGeneration; + } + + /** + * @return the generations + */ + public IntegerParameter getGenerationsParameter() { + return generations; + } + + /** + * @return the currentRun + */ + public IntegerParameter getCurrentRunParameter() { + return currentRun; + } + + /** + * @return the runs + */ + public IntegerParameter getRunsParameter() { + return runs; + } + + /** + * @return the arity + */ + public IntegerParameter getArityParameter() { + return arity; + } + + /** + * @return the seed + */ + public IntegerParameter getSeedParameter() { + return seed; + } + + /** + * @return the report + */ + public IntegerParameter getReportParameter() { + return report; } public void setFunctionSet(FunctionSet functionSet) { this.functionSet = functionSet; - set("arity", functionSet.getMaxArity()); + setArity(functionSet.getMaxArity()); } public void setConsole(Console console) { @@ -56,5 +226,4 @@ public class ModifiableResources extends Resources { console.print(s); } } - } diff --git a/src/jcgp/backend/resources/Resources.java b/src/jcgp/backend/resources/Resources.java index 13e0c51..c83fa35 100644 --- a/src/jcgp/backend/resources/Resources.java +++ b/src/jcgp/backend/resources/Resources.java @@ -1,16 +1,12 @@ package jcgp.backend.resources; -import java.util.HashMap; import java.util.Random; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import jcgp.backend.function.Function; import jcgp.backend.function.FunctionSet; -import jcgp.backend.resources.parameters.BooleanParameter; -import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.IntegerParameter; -import jcgp.backend.resources.parameters.Parameter; import jcgp.backend.resources.parameters.ParameterStatus; /** @@ -23,12 +19,12 @@ import jcgp.backend.resources.parameters.ParameterStatus; * */ public class Resources { - protected HashMap parameters = new HashMap(); + protected IntegerParameter rows, columns, inputs, outputs, populationSize, + levelsBack, currentGeneration, generations, currentRun, runs, + arity, seed, report; protected Random numberGenerator = new Random(); - protected FunctionSet functionSet; - // GUI console protected Console console; @@ -36,175 +32,234 @@ public class Resources { createBaseParameters(); } - public int getInt(String key) { - if (parameters.get(key) instanceof IntegerParameter) { - return ((IntegerParameter) parameters.get(key)).get(); - } else if (parameters.get(key) instanceof DoubleParameter) { - return (int) ((DoubleParameter) parameters.get(key)).get(); - } else { - throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int."); - } + /** + * @return the rows + */ + public int rows() { + return rows.get(); } - - public double getDouble(String key) { - if (parameters.get(key) instanceof IntegerParameter) { - return ((IntegerParameter) parameters.get(key)).get(); - } else if (parameters.get(key) instanceof DoubleParameter) { - return ((DoubleParameter) parameters.get(key)).get(); - } else { - throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to double."); - } + + /** + * @return the columns + */ + public int columns() { + return columns.get(); } - - public boolean getBoolean(String key) { - if (parameters.get(key) instanceof BooleanParameter) { - return ((BooleanParameter) parameters.get(key)).get(); - } else { - throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int."); - } + + /** + * @return the inputs + */ + public int inputs() { + return inputs.get(); } - - public Parameter getParameter(String key) { - return parameters.get(key); + + /** + * @return the outputs + */ + public int outputs() { + return outputs.get(); } - - public boolean contains(String key) { - return parameters.containsKey(key); + + /** + * @return the populationSize + */ + public int populationSize() { + return populationSize.get(); } - + + /** + * @return the levelsBack + */ + public int levelsBack() { + return levelsBack.get(); + } + + /** + * @return the nodes + */ + public int nodes() { + return columns.get() * rows.get(); + } + + /** + * @return the currentGeneration + */ + public int currentGeneration() { + return currentGeneration.get(); + } + + /** + * @return the generations + */ + public int generations() { + return generations.get(); + } + + /** + * @return the currentRun + */ + public int currentRun() { + return currentRun.get(); + } + + /** + * @return the runs + */ + public int runs() { + return runs.get(); + } + + /** + * @return the arity + */ + public int arity() { + return arity.get(); + } + + /** + * @return the seed + */ + public int seed() { + return seed.get(); + } + + /** + * @return the report + */ + public int report() { + return report.get(); + } + private void createBaseParameters() { - parameters.put("rows", new IntegerParameter(8, "Rows", false, true) { + rows = new IntegerParameter(5, "Rows", false, true) { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 row."); } else { status = ParameterStatus.VALID; } } - }); - parameters.put("columns", new IntegerParameter(9, "Columns", false, true) { + }; + + columns = new IntegerParameter(5, "Columns", false, true) { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 column."); } else { status = ParameterStatus.VALID; } } - }); - parameters.put("inputs", new IntegerParameter(3, "Inputs", false, true) { + }; + + inputs = new IntegerParameter(3, "Inputs", false, true) { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 input."); } else { status = ParameterStatus.VALID; } } - }); - parameters.put("outputs", new IntegerParameter(3, "Outputs", false, true) { + }; + + outputs = new IntegerParameter(3, "Outputs", false, true) { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 output."); } else { status = ParameterStatus.VALID; } } - }); - parameters.put("popSize", new IntegerParameter(5, "Population", false, true) { + }; + + populationSize = new IntegerParameter(5, "Population", false, true) { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Population size must be at least 1."); } else { status = ParameterStatus.VALID; } } - }); - parameters.put("levelsBack", new IntegerParameter(2, "Levels back", false, true) { + }; + + levelsBack = new IntegerParameter(2, "Levels back", false, true) { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Levels back must be at least 1."); - } else if (newValue > getInt("columns")) { + } else if (newValue.intValue() > columns.get()) { status = ParameterStatus.INVALID; status.setDetails("Levels back must be less than or equal to the number of columns."); } else { status = ParameterStatus.VALID; } } - }); - - IntegerParameter nodes = new IntegerParameter(1, "Nodes", true, false) { - @Override - public void validate(int newValue) { - // blank - } }; - nodes.valueProperty().bind(((IntegerParameter) parameters.get("rows")).valueProperty().multiply(((IntegerParameter) parameters.get("columns")).valueProperty())); - parameters.put("nodes", nodes); - - parameters.put("generations", new IntegerParameter(1000000, "Generations") { + + generations = new IntegerParameter(1000000, "Generations") { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Number of generations must be greater than 0."); - } else if (newValue < getInt("currentGen")) { + } else if (newValue.intValue() < currentGeneration.get()) { status = ParameterStatus.WARNING_RESET; status.setDetails("Setting generations to less than the current generation will cause the experiment to restart."); } else { status = ParameterStatus.VALID; } } - }); - parameters.put("currentGen", new IntegerParameter(1, "Generation", true, false) { + }; + + currentGeneration = new IntegerParameter(1, "Generation", true, false) { @Override - public void validate(int newValue) { + public void validate(Number newValue) { // blank } - }); + }; - parameters.put("runs", new IntegerParameter(5, "Runs") { + runs = new IntegerParameter(5, "Runs") { @Override - public void validate(int newValue) { - if (newValue <= 0) { + public void validate(Number newValue) { + if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("Number of runs must be greater than 0."); - } else if (newValue < getInt("currentRun")) { + } else if (newValue.intValue() < currentRun.get()) { status = ParameterStatus.WARNING_RESET; status.setDetails("Setting runs to less than the current run will cause the experiment to restart."); } else { status = ParameterStatus.VALID; } } - }); - parameters.put("currentRun", new IntegerParameter(1, "Run", true, false) { + }; + + currentRun = new IntegerParameter(1, "Run", true, false) { @Override - public void validate(int newValue) { + public void validate(Number newValue) { // blank } - }); + }; - //parameters.put("arity", new IntegerParameter(functionSet.getMaxArity(), "Max arity", true, false) { - parameters.put("arity", new IntegerParameter(0, "Max arity", true, false) { + arity = new IntegerParameter(0, "Max arity", true, false) { @Override - public void validate(int newValue) { + public void validate(Number newValue) { // blank } - }); + }; - IntegerParameter seed = new IntegerParameter(1234, "Seed", false, true) { + seed = new IntegerParameter(1234, "Seed", false, true) { @Override - public void validate(int newValue) { + public void validate(Number newValue) { status = ParameterStatus.VALID; } }; @@ -217,19 +272,18 @@ public class Resources { } }); numberGenerator.setSeed(seed.get()); - parameters.put("seed", seed); - parameters.put("report", new IntegerParameter(1, "Report", false, false) { + report = new IntegerParameter(1, "Report", false, false) { @Override - public void validate(int newValue) { - if (newValue > getInt("generations")) { + public void validate(Number newValue) { + if (newValue.intValue() > generations.get()) { status = ParameterStatus.WARNING; status.setDetails("No reports will be printed."); } else { status = ParameterStatus.VALID; } } - }); + }; } /* @@ -271,8 +325,8 @@ public class Resources { * These are affected by parameter report */ public void reportln(String s) { - if (getInt("report") > 0) { - if (getInt("currentGen") % getInt("report") == 0) { + if (report.get() > 0) { + if (currentGeneration.get() % report.get() == 0) { System.out.println(s); if (console != null) { console.println(s); @@ -282,8 +336,8 @@ public class Resources { } public void report(String s) { - if (getInt("report") > 0) { - if (getInt("currentGen") % getInt("report") == 0) { + if (report.get() > 0) { + if (currentGeneration.get() % report.get() == 0) { System.out.print(s); if (console != null) { console.print(s); diff --git a/src/jcgp/backend/resources/parameters/BooleanParameter.java b/src/jcgp/backend/resources/parameters/BooleanParameter.java index cd17649..cc74a64 100644 --- a/src/jcgp/backend/resources/parameters/BooleanParameter.java +++ b/src/jcgp/backend/resources/parameters/BooleanParameter.java @@ -2,13 +2,11 @@ package jcgp.backend.resources.parameters; import javafx.beans.property.SimpleBooleanProperty; -public abstract class BooleanParameter extends Parameter { - - private SimpleBooleanProperty value; - +public abstract class BooleanParameter extends Parameter { + public BooleanParameter(boolean value, String name, boolean monitor, boolean critical) { super(name, monitor, critical); - this.value = new SimpleBooleanProperty(value); + this.valueProperty = new SimpleBooleanProperty(value); } /** @@ -20,23 +18,6 @@ public abstract class BooleanParameter extends Parameter { */ public BooleanParameter(boolean value, String name) { super(name, false, false); - this.value = new SimpleBooleanProperty(value); - } - - public boolean get() { - return value.get(); - } - - public void set(boolean newValue) { - if (!value.isBound()) { - value.set(newValue); - } - } - - public abstract void validate(boolean newValue); - - @Override - public SimpleBooleanProperty valueProperty() { - return value; + this.valueProperty = new SimpleBooleanProperty(value); } } diff --git a/src/jcgp/backend/resources/parameters/DoubleParameter.java b/src/jcgp/backend/resources/parameters/DoubleParameter.java index 8464c83..b109446 100644 --- a/src/jcgp/backend/resources/parameters/DoubleParameter.java +++ b/src/jcgp/backend/resources/parameters/DoubleParameter.java @@ -2,35 +2,20 @@ package jcgp.backend.resources.parameters; import javafx.beans.property.SimpleDoubleProperty; -public abstract class DoubleParameter extends Parameter { - - protected SimpleDoubleProperty value; +public abstract class DoubleParameter extends Parameter { public DoubleParameter(double value, String name, boolean monitor, boolean critical) { super(name, monitor, critical); - this.value = new SimpleDoubleProperty(value); + this.valueProperty = new SimpleDoubleProperty(value); } public DoubleParameter(double value, String name) { super(name, false, false); - this.value = new SimpleDoubleProperty(value); + this.valueProperty = new SimpleDoubleProperty(value); } - public double get() { - return value.get(); - } - - public void set(double newValue) { - if (!value.isBound()) { - value.set(newValue); - } - } - @Override - public SimpleDoubleProperty valueProperty() { - return value; + public Double get() { + return super.get().doubleValue(); } - - public abstract void validate(double newValue); - } diff --git a/src/jcgp/backend/resources/parameters/IntegerParameter.java b/src/jcgp/backend/resources/parameters/IntegerParameter.java index 2a7b2a7..7cf68bd 100644 --- a/src/jcgp/backend/resources/parameters/IntegerParameter.java +++ b/src/jcgp/backend/resources/parameters/IntegerParameter.java @@ -2,36 +2,20 @@ package jcgp.backend.resources.parameters; import javafx.beans.property.SimpleIntegerProperty; -public abstract class IntegerParameter extends Parameter { - - private SimpleIntegerProperty value; +public abstract class IntegerParameter extends Parameter { public IntegerParameter(int value, String name, boolean monitor, boolean critical) { super(name, monitor, critical); - this.value = new SimpleIntegerProperty(value); + this.valueProperty = new SimpleIntegerProperty(value); } public IntegerParameter(int value, String name) { super(name, false, false); - this.value = new SimpleIntegerProperty(value); + this.valueProperty = new SimpleIntegerProperty(value); } - public int get() { - return value.get(); - } - - public void set(int newValue) { - if (!value.isBound()) { - validate(newValue); - value.set(newValue); - } - } - @Override - public SimpleIntegerProperty valueProperty() { - return value; + public Integer get() { + return super.get().intValue(); } - - public abstract void validate(int newValue); - } diff --git a/src/jcgp/backend/resources/parameters/Parameter.java b/src/jcgp/backend/resources/parameters/Parameter.java index 7e12ff8..3990ae6 100644 --- a/src/jcgp/backend/resources/parameters/Parameter.java +++ b/src/jcgp/backend/resources/parameters/Parameter.java @@ -1,8 +1,9 @@ package jcgp.backend.resources.parameters; import javafx.beans.property.Property; +import javafx.beans.property.ReadOnlyProperty; -public abstract class Parameter { +public abstract class Parameter { protected boolean monitor, critical, reset = false; @@ -10,6 +11,8 @@ public abstract class Parameter { protected String name; + protected Property valueProperty; + public Parameter(String name, boolean monitor, boolean critical) { this.name = name; this.monitor = monitor; @@ -36,6 +39,19 @@ public abstract class Parameter { return status; } - public abstract Property valueProperty(); - + public ReadOnlyProperty valueProperty() { + return valueProperty; + } + + public T get() { + return valueProperty.getValue(); + } + + public void set(T newValue) { + if (!valueProperty.isBound()) { + valueProperty.setValue(newValue); + } + } + + public abstract void validate(T newValue); } -- cgit v1.2.3