aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/resources
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-05-01 13:05:27 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-05-01 13:05:27 +0100
commit36f4393bcc9e55afa2334baa33e603ce839741a1 (patch)
treed9a1d55d0d3553193a3fc11a92f11515762d202f /src/jcgp/backend/resources
parent4c8de2402f2878cde7587c7f3bbf4ffaea86efd4 (diff)
Did more commenting, implemented reflection and statistics
Diffstat (limited to 'src/jcgp/backend/resources')
-rw-r--r--src/jcgp/backend/resources/Console.java31
-rw-r--r--src/jcgp/backend/resources/ModifiableResources.java143
-rw-r--r--src/jcgp/backend/resources/Resources.java161
-rw-r--r--src/jcgp/backend/resources/parameters/BooleanParameter.java23
-rw-r--r--src/jcgp/backend/resources/parameters/DoubleParameter.java21
-rw-r--r--src/jcgp/backend/resources/parameters/IntegerParameter.java21
-rw-r--r--src/jcgp/backend/resources/parameters/Parameter.java57
-rw-r--r--src/jcgp/backend/resources/parameters/ParameterStatus.java16
8 files changed, 230 insertions, 243 deletions
diff --git a/src/jcgp/backend/resources/Console.java b/src/jcgp/backend/resources/Console.java
index 6bbd5ba..2900afe 100644
--- a/src/jcgp/backend/resources/Console.java
+++ b/src/jcgp/backend/resources/Console.java
@@ -1,11 +1,42 @@
package jcgp.backend.resources;
+/**
+ * Defines the basic model for a console.
+ * <br><br>
+ * This interface will typically be implemented by a GUI class
+ * and GUI packages such as JavaFX are usually single-threaded.
+ * If the CGP experiment is running on a side thread (which would
+ * be the case so as not to block the entire GUI), updating a GUI
+ * element such as the console from a different thread would lead
+ * to concurrency problems. For this reason, this console is
+ * intended to buffer printed messages and only output them to the
+ * actual GUI control when {@code flush()} is called (which is
+ * guaranteed to be done in a thread-safe way by the library).
+ *
+ * @author Eduardo Pedroni
+ *
+ */
public interface Console {
+ /**
+ * Prints a string and automatically adds a line break at the end.
+ *
+ * @param s the string to print.
+ */
public void println(String s);
+ /**
+ * Prints a string without line break at the end (unless the string
+ * itself specifies one).
+ *
+ * @param s the string to print.
+ */
public void print(String s);
+ /**
+ * Outputs all buffered messages to the console. Only necessary
+ * if concurrent accesses must be avoided.
+ */
public void flush();
}
diff --git a/src/jcgp/backend/resources/ModifiableResources.java b/src/jcgp/backend/resources/ModifiableResources.java
index 53dc815..3dab2aa 100644
--- a/src/jcgp/backend/resources/ModifiableResources.java
+++ b/src/jcgp/backend/resources/ModifiableResources.java
@@ -3,8 +3,9 @@ package jcgp.backend.resources;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import jcgp.backend.function.FunctionSet;
-import jcgp.backend.resources.parameters.IntegerParameter;
-import jcgp.backend.resources.parameters.ParameterStatus;
+import jcgp.backend.parameters.IntegerParameter;
+import jcgp.backend.parameters.ParameterStatus;
+import jcgp.backend.parameters.monitors.IntegerMonitor;
/**
*
@@ -18,205 +19,236 @@ import jcgp.backend.resources.parameters.ParameterStatus;
*/
public class ModifiableResources extends Resources {
+ /**
+ * Creates an instance of this class and initialises
+ * all base parameters to default values. See
+ * {@code createBaseParameters} for the exact parameter
+ * initialisation.
+ */
public ModifiableResources() {
createBaseParameters();
}
- public void setValues(String filePath) {
-
- }
-
/**
- * @param rows the rows to set
+ * @param rows the number of rows to set.
*/
public void setRows(int rows) {
this.rows.set(rows);
}
/**
- * @param columns the columns to set
+ * @param columns the number of columns to set.
*/
public void setColumns(int columns) {
this.columns.set(columns);
}
/**
- * @param inputs the inputs to set
+ * @param inputs the number of inputs to set.
*/
public void setInputs(int inputs) {
this.inputs.set(inputs);
}
/**
- * @param outputs the outputs to set
+ * @param outputs the number of outputs to set.
*/
public void setOutputs(int outputs) {
this.outputs.set(outputs);
}
/**
- * @param populationSize the populationSize to set
+ * @param populationSize the population size to set.
*/
public void setPopulationSize(int populationSize) {
this.populationSize.set(populationSize);
}
/**
- * @param levelsBack the levelsBack to set
+ * @param levelsBack the levels back to set.
*/
public void setLevelsBack(int levelsBack) {
this.levelsBack.set(levelsBack);
}
/**
- * @param currentGeneration the currentGeneration to set
+ * @param currentGeneration the current generation to set.
*/
public void setCurrentGeneration(int currentGeneration) {
this.currentGeneration.set(currentGeneration);
}
+
+ /**
+ * Adds 1 to the current generation.
+ */
+ public void incrementGeneration() {
+ this.currentGeneration.set(currentGeneration.get() + 1);
+ }
/**
- * @param generations the generations to set
+ * @param generations the total generations to set.
*/
public void setGenerations(int generations) {
this.generations.set(generations);
}
-
+
/**
- * @param currentRun the currentRun to set
+ * @param currentRun the current run to set.
*/
public void setCurrentRun(int currentRun) {
this.currentRun.set(currentRun);
}
/**
- * @param runs the runs to set
+ * Adds 1 to the current generation.
+ */
+ public void incrementRun() {
+ currentRun.set(currentRun.get() + 1);
+ }
+
+ /**
+ * @param runs the total runs to set.
*/
public void setRuns(int runs) {
this.runs.set(runs);
}
/**
- * @param arity the arity to set
+ * This is called automatically by the experiment when the arity changes.
+ *
+ * @param arity the arity to set.
*/
public void setArity(int arity) {
this.arity.set(arity);
}
/**
- * @param seed the seed to set
+ * @param seed the seed to set.
*/
public void setSeed(int seed) {
this.seed.set(seed);
}
/**
- * @param report the report to set
+ * @param report the report interval to set.
*/
public void setReportInterval(int report) {
this.reportInterval.set(report);
}
/**
- * @return the rows
+ * @return the rows parameter.
*/
public IntegerParameter getRowsParameter() {
return rows;
}
/**
- * @return the columns
+ * @return the columns parameter.
*/
public IntegerParameter getColumnsParameter() {
return columns;
}
/**
- * @return the inputs
+ * @return the inputs parameter.
*/
public IntegerParameter getInputsParameter() {
return inputs;
}
/**
- * @return the outputs
+ * @return the outputs parameter.
*/
public IntegerParameter getOutputsParameter() {
return outputs;
}
/**
- * @return the populationSize
+ * @return the population size parameter.
*/
public IntegerParameter getPopulationSizeParameter() {
return populationSize;
}
/**
- * @return the levelsBack
+ * @return the levels back parameter.
*/
public IntegerParameter getLevelsBackParameter() {
return levelsBack;
}
/**
- * @return the currentGeneration
+ * @return the current generation parameter.
*/
public IntegerParameter getCurrentGenerationParameter() {
return currentGeneration;
}
/**
- * @return the generations
+ * @return the total generations parameter.
*/
public IntegerParameter getGenerationsParameter() {
return generations;
}
/**
- * @return the currentRun
+ * @return the current run parameter.
*/
public IntegerParameter getCurrentRunParameter() {
return currentRun;
}
/**
- * @return the runs
+ * @return the total runs parameter.
*/
public IntegerParameter getRunsParameter() {
return runs;
}
/**
- * @return the arity
+ * @return the arity parameter.
*/
public IntegerParameter getArityParameter() {
return arity;
}
/**
- * @return the seed
+ * @return the seed parameter.
*/
public IntegerParameter getSeedParameter() {
return seed;
}
/**
- * @return the report
+ * @return the report interval parameter.
*/
public IntegerParameter getReportIntervalParameter() {
return reportInterval;
}
+ /**
+ * Update the current function set.
+ *
+ * @param functionSet the new function set.
+ */
public void setFunctionSet(FunctionSet functionSet) {
this.functionSet = functionSet;
setArity(functionSet.getMaxArity());
}
+ /**
+ * This can be set to null if no extra console is desired.
+ *
+ * @param console the extra console for the experiment to use.
+ */
public void setConsole(Console console) {
this.console = console;
}
+ /**
+ * For internal use only, this initialises all base parameters to default values.
+ */
private void createBaseParameters() {
rows = new IntegerParameter(5, "Rows", false, true) {
@Override
@@ -242,29 +274,9 @@ public class ModifiableResources extends Resources {
}
};
- inputs = new IntegerParameter(3, "Inputs", true, false) {
- @Override
- 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;
- }
- }
- };
+ inputs = new IntegerMonitor(3, "Inputs");
- outputs = new IntegerParameter(3, "Outputs", true, false) {
- @Override
- 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;
- }
- }
- };
+ outputs = new IntegerMonitor(3, "Outputs");
populationSize = new IntegerParameter(5, "Population", false, true) {
@Override
@@ -308,12 +320,7 @@ public class ModifiableResources extends Resources {
}
};
- currentGeneration = new IntegerParameter(1, "Generation", true, false) {
- @Override
- public void validate(Number newValue) {
- // blank
- }
- };
+ currentGeneration = new IntegerMonitor(1, "Generation");
runs = new IntegerParameter(5, "Runs") {
@Override
@@ -330,19 +337,9 @@ public class ModifiableResources extends Resources {
}
};
- currentRun = new IntegerParameter(1, "Run", true, false) {
- @Override
- public void validate(Number newValue) {
- // blank
- }
- };
+ currentRun = new IntegerMonitor(1, "Run");
- arity = new IntegerParameter(0, "Max arity", true, false) {
- @Override
- public void validate(Number newValue) {
- // blank
- }
- };
+ arity = new IntegerMonitor(0, "Max arity");
seed = new IntegerParameter(1234, "Seed", false, true) {
@Override
diff --git a/src/jcgp/backend/resources/Resources.java b/src/jcgp/backend/resources/Resources.java
index e1438f1..1f79627 100644
--- a/src/jcgp/backend/resources/Resources.java
+++ b/src/jcgp/backend/resources/Resources.java
@@ -4,13 +4,24 @@ import java.util.Random;
import jcgp.backend.function.Function;
import jcgp.backend.function.FunctionSet;
-import jcgp.backend.resources.parameters.IntegerParameter;
+import jcgp.backend.parameters.IntegerParameter;
/**
*
* The resources class encapsulates all of the resources based on which the program operates.
* Each instance of JCGP contains a single instance of Resources, which gets passed to the selected
* modules as the program executes.
+ * <br><br>
+ * The experiment's {@code Resources} object is passed to modules as the program operates, and
+ * the actual parameter values can be obtained using getter methods. Note that, for code brevity,
+ * this class's getters do not start with the word "get". For instance, to get the number of rows,
+ * one would use {@code rows()} instead of {@code getRows()}.
+ * <br><br>
+ * In addition to parameters, this class also offers utility methods. Any necessary random numbers
+ * should be obtained using {@code getRandomInt()} and {@code getRandomDouble()}. Functions from the
+ * selected function set can be obtained through this class as well. Finally, printing to the console
+ * should be done via the resources using the report and print methods, so that these prints also
+ * get sent to the GUI console (if one is present).
*
* @author Eduardo Pedroni
*
@@ -22,102 +33,102 @@ public class Resources {
protected Random numberGenerator = new Random();
protected FunctionSet functionSet;
- // GUI console
+
protected Console console;
/**
- * @return the rows
+ * @return the number of rows.
*/
public int rows() {
return rows.get();
}
/**
- * @return the columns
+ * @return the number of columns.
*/
public int columns() {
return columns.get();
}
/**
- * @return the inputs
+ * @return the number of inputs.
*/
public int inputs() {
return inputs.get();
}
/**
- * @return the outputs
+ * @return the number of outputs.
*/
public int outputs() {
return outputs.get();
}
/**
- * @return the populationSize
+ * @return the population size.
*/
public int populationSize() {
return populationSize.get();
}
/**
- * @return the levelsBack
+ * @return the levels back value.
*/
public int levelsBack() {
return levelsBack.get();
}
/**
- * @return the nodes
+ * @return the total number of nodes.
*/
public int nodes() {
return columns.get() * rows.get();
}
/**
- * @return the currentGeneration
+ * @return the current generation.
*/
public int currentGeneration() {
return currentGeneration.get();
}
/**
- * @return the generations
+ * @return the total number of generations.
*/
public int generations() {
return generations.get();
}
/**
- * @return the currentRun
+ * @return the current run.
*/
public int currentRun() {
return currentRun.get();
}
/**
- * @return the runs
+ * @return the total number of runs.
*/
public int runs() {
return runs.get();
}
/**
- * @return the arity
+ * @return the maximum arity out of the function set.
*/
public int arity() {
return arity.get();
}
/**
- * @return the seed
+ * @return the random seed being used.
*/
public int seed() {
return seed.get();
}
/**
- * @return the report interval
+ * @return the report interval.
*/
public int reportInterval() {
return reportInterval.get();
@@ -126,14 +137,37 @@ public class Resources {
/*
* Utility functions
*/
+ /**
+ * Gets the next random integer using the experiment's random
+ * number generator. The integer returned will be between 0 (inclusive)
+ * and limit (exclusive).
+ *
+ * @param limit the limit value.
+ * @return a random integer between 0 and limit.
+ */
public int getRandomInt(int limit) {
return numberGenerator.nextInt(limit);
}
+ /**
+ * Gets the next random double using the experiment's random
+ * number generator. The double returned will be between 0 (inclusive)
+ * and limit (exclusive).
+ *
+ * @param limit the limit value.
+ * @return a random double between 0 and limit.
+ */
public double getRandomDouble(int limit) {
return numberGenerator.nextDouble() * limit;
}
+ /**
+ * Gets the next random integer using the experiment's random
+ * number generator. The integer returned will be between 0 (inclusive)
+ * and 1 (exclusive).
+ *
+ * @return a random integer between 0 and 1.
+ */
public double getRandomDouble() {
return numberGenerator.nextDouble();
}
@@ -141,53 +175,94 @@ public class Resources {
/*
* FunctionSet functions
*/
+ /**
+ * Gets a random allowed function from the problem function set.
+ * This function uses {@code getRandomInt()} to choose the random
+ * function.
+ *
+ * @return a random allowed function.
+ */
public Function getRandomFunction() {
Function f = functionSet.getAllowedFunction(numberGenerator.nextInt(functionSet.getAllowedFunctionCount()));
return f;
}
+ /**
+ * Gets the indexed function out of the
+ * complete set of functions.
+ *
+ * @param index the function to return.
+ * @return the indexed function.
+ */
public Function getFunction(int index) {
- return functionSet.getAllowedFunction(index);
+ return functionSet.getFunction(index);
}
/**
- * @return the functionSet
+ * @return the problem's function set.
*/
public FunctionSet getFunctionSet() {
return functionSet;
}
+ /**
+ * Returns the index of a specified function. If the function is not found,
+ * -1 is returned.
+ *
+ * @param function the function with unknown index.
+ * @return the index of the function, or -1 if it was not found.
+ */
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;
+ // not found, default to -1
+ return -1;
}
/*
* Console functionality
* These are affected by parameter report interval
*/
- public void reportln(String s) {
+ /**
+ * Prints a message to the consoles taking into account the
+ * report interval parameter. If no reports are allowed in
+ * the current generation, this does nothing.
+ * <br>
+ * This method automatically appends a line break to the message
+ * being printed.
+ *
+ * @param message the message to print.
+ */
+ public void reportln(String message) {
if (reportInterval.get() > 0) {
if (currentGeneration.get() % reportInterval.get() == 0) {
- System.out.println(s);
+ System.out.println(message);
if (console != null) {
- console.println(s);
+ console.println(message);
}
}
}
}
- public void report(String s) {
+ /**
+ * Prints a message to the consoles taking into account the
+ * report interval parameter. If no reports are allowed in
+ * the current generation, this does nothing.
+ * <br>
+ * This method does not append a line break to the message
+ * being printed.
+ *
+ * @param message the message to print.
+ */
+ public void report(String message) {
if (reportInterval.get() > 0) {
if (currentGeneration.get() % reportInterval.get() == 0) {
- System.out.print(s);
+ System.out.print(message);
if (console != null) {
- console.print(s);
+ console.print(message);
}
}
}
@@ -197,17 +272,39 @@ public class Resources {
* Console functionality
* These are not affected by parameter report interval
*/
- public void println(String s) {
- System.out.println(s);
+ /**
+ * Prints a message to the consoles ignoring
+ * report interval. In other words, messages printed
+ * using this method will always appear (though the
+ * GUI console will still need to be flushed).
+ * <br>
+ * This method automatically appends a line break to the message
+ * being printed.
+ *
+ * @param message the message to print.
+ */
+ public void println(String message) {
+ System.out.println(message);
if (console != null) {
- console.println(s);
+ console.println(message);
}
}
- public void print(String s) {
- System.out.print(s);
+ /**
+ * Prints a message to the consoles ignoring
+ * report interval. In other words, messages printed
+ * using this method will always appear (though the
+ * GUI console will still need to be flushed).
+ * <br>
+ * This method does not append a line break to the message
+ * being printed.
+ *
+ * @param message the message to print.
+ */
+ public void print(String message) {
+ System.out.print(message);
if (console != null) {
- console.print(s);
+ console.print(message);
}
}
} \ No newline at end of file
diff --git a/src/jcgp/backend/resources/parameters/BooleanParameter.java b/src/jcgp/backend/resources/parameters/BooleanParameter.java
deleted file mode 100644
index cc74a64..0000000
--- a/src/jcgp/backend/resources/parameters/BooleanParameter.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package jcgp.backend.resources.parameters;
-
-import javafx.beans.property.SimpleBooleanProperty;
-
-public abstract class BooleanParameter extends Parameter<Boolean> {
-
- public BooleanParameter(boolean value, String name, boolean monitor, boolean critical) {
- super(name, monitor, critical);
- this.valueProperty = new SimpleBooleanProperty(value);
- }
-
- /**
- * Simple BooleanParameter constructor,
- *
- *
- * @param value
- * @param name
- */
- public BooleanParameter(boolean value, String name) {
- super(name, false, false);
- this.valueProperty = new SimpleBooleanProperty(value);
- }
-}
diff --git a/src/jcgp/backend/resources/parameters/DoubleParameter.java b/src/jcgp/backend/resources/parameters/DoubleParameter.java
deleted file mode 100644
index b109446..0000000
--- a/src/jcgp/backend/resources/parameters/DoubleParameter.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package jcgp.backend.resources.parameters;
-
-import javafx.beans.property.SimpleDoubleProperty;
-
-public abstract class DoubleParameter extends Parameter<Number> {
-
- public DoubleParameter(double value, String name, boolean monitor, boolean critical) {
- super(name, monitor, critical);
- this.valueProperty = new SimpleDoubleProperty(value);
- }
-
- public DoubleParameter(double value, String name) {
- super(name, false, false);
- this.valueProperty = new SimpleDoubleProperty(value);
- }
-
- @Override
- public Double get() {
- return super.get().doubleValue();
- }
-}
diff --git a/src/jcgp/backend/resources/parameters/IntegerParameter.java b/src/jcgp/backend/resources/parameters/IntegerParameter.java
deleted file mode 100644
index 7cf68bd..0000000
--- a/src/jcgp/backend/resources/parameters/IntegerParameter.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package jcgp.backend.resources.parameters;
-
-import javafx.beans.property.SimpleIntegerProperty;
-
-public abstract class IntegerParameter extends Parameter<Number> {
-
- public IntegerParameter(int value, String name, boolean monitor, boolean critical) {
- super(name, monitor, critical);
- this.valueProperty = new SimpleIntegerProperty(value);
- }
-
- public IntegerParameter(int value, String name) {
- super(name, false, false);
- this.valueProperty = new SimpleIntegerProperty(value);
- }
-
- @Override
- public Integer get() {
- return super.get().intValue();
- }
-}
diff --git a/src/jcgp/backend/resources/parameters/Parameter.java b/src/jcgp/backend/resources/parameters/Parameter.java
deleted file mode 100644
index 3990ae6..0000000
--- a/src/jcgp/backend/resources/parameters/Parameter.java
+++ /dev/null
@@ -1,57 +0,0 @@
-package jcgp.backend.resources.parameters;
-
-import javafx.beans.property.Property;
-import javafx.beans.property.ReadOnlyProperty;
-
-public abstract class Parameter<T> {
-
- protected boolean monitor, critical, reset = false;
-
- protected ParameterStatus status = ParameterStatus.VALID;
-
- protected String name;
-
- protected Property<T> valueProperty;
-
- public Parameter(String name, boolean monitor, boolean critical) {
- this.name = name;
- this.monitor = monitor;
- this.critical = critical;
- }
-
- public boolean isMonitor() {
- return monitor;
- }
-
- public boolean isCritical() {
- return critical;
- }
-
- public boolean requiresReset() {
- return critical || reset;
- }
-
- public String getName() {
- return name;
- }
-
- public ParameterStatus getStatus() {
- return status;
- }
-
- public ReadOnlyProperty<T> 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);
-}
diff --git a/src/jcgp/backend/resources/parameters/ParameterStatus.java b/src/jcgp/backend/resources/parameters/ParameterStatus.java
deleted file mode 100644
index 11da4c2..0000000
--- a/src/jcgp/backend/resources/parameters/ParameterStatus.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package jcgp.backend.resources.parameters;
-
-public enum ParameterStatus {
- INVALID, WARNING, WARNING_RESET, VALID;
-
- private String details;
-
- public void setDetails(String details) {
- this.details = details;
- }
-
- public String getDetails() {
- return details;
- }
-
-}