diff options
Diffstat (limited to 'src/jcgp/gui/settings')
-rw-r--r-- | src/jcgp/gui/settings/SettingsPane.java | 116 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIBooleanParameter.java | 34 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIDoubleParameter.java | 39 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIIntegerParameter.java | 36 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIParameter.java | 56 |
5 files changed, 161 insertions, 120 deletions
diff --git a/src/jcgp/gui/settings/SettingsPane.java b/src/jcgp/gui/settings/SettingsPane.java index 9edcbdb..61011aa 100644 --- a/src/jcgp/gui/settings/SettingsPane.java +++ b/src/jcgp/gui/settings/SettingsPane.java @@ -5,7 +5,6 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; -import javafx.beans.property.SimpleIntegerProperty; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; @@ -22,14 +21,15 @@ import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.FontSmoothingType; import javafx.scene.text.Text; -import jcgp.GUI; import jcgp.JCGP; -import jcgp.function.FunctionSet; +import jcgp.backend.function.FunctionSet; +import jcgp.backend.modules.ea.EvolutionaryAlgorithm; +import jcgp.backend.modules.fitness.FitnessFunction; +import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.Parameter; +import jcgp.gui.GUI; import jcgp.gui.settings.parameters.*; -import jcgp.modules.ea.EvolutionaryAlgorithm; -import jcgp.modules.fitness.FitnessFunction; -import jcgp.modules.mutator.Mutator; -import jcgp.parameters.*; public class SettingsPane extends AnchorPane { @@ -84,31 +84,28 @@ public class SettingsPane extends AnchorPane { bpPane.getChildren().add(header); - final GUIIntegerParameter generation = new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("currentGen")); - final GUIIntegerParameter run = new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("currentRun")); + GUIParameter rows = GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("rows")); + parameters.add(rows); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("columns"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("inputs"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("outputs"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("levelsBack"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("rows"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("columns"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("inputs"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("outputs"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("levelsBack"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("popSize"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("generations"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("runs"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("popSize"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("generations"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("runs"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentGen"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentRun"))); - parameters.add(generation); - parameters.add(run); - - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("seed"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("report"))); - parameters.add(new GUIBooleanParameter((BooleanParameter) cgp.getResources().getParameter("verbose"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("seed"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("report"))); bpPane.getChildren().addAll(parameters); mainContainer.getChildren().add(bpPane); - generation.getTextField().textProperty().bind(((SimpleIntegerProperty) cgp.getResources().getParameter("currentGen").valueProperty()).asString()); + } private void initialiseEAParameters(final JCGP cgp) { @@ -245,13 +242,16 @@ public class SettingsPane extends AnchorPane { play.setOnAction(new EventHandler<ActionEvent>() { @Override - public void handle(ActionEvent event) { - gui.playPause(); - if (gui.isEvolving()) { + public void handle(ActionEvent event) { + if (play.getText() == "Run") { + if (applyChanges()) { + gui.resetCGP(); + } play.setText("Pause"); } else { play.setText("Run"); } + gui.playPause(); } }); @@ -260,6 +260,9 @@ public class SettingsPane extends AnchorPane { step.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { + if (applyChanges()) { + gui.resetCGP(); + } gui.step(); } }); @@ -274,8 +277,7 @@ public class SettingsPane extends AnchorPane { controls.getChildren().add(flowButtons); - mainContainer.getChildren().add(controls); - + mainContainer.getChildren().add(controls); } /** @@ -287,19 +289,10 @@ public class SettingsPane extends AnchorPane { vb.getChildren().clear(); while (it.hasNext()) { Parameter parameter = ((Map.Entry<String, Parameter>) it.next()).getValue(); - if (parameter instanceof IntegerParameter) { - GUIIntegerParameter gip = new GUIIntegerParameter((IntegerParameter) parameter); - parameters.add(gip); - vb.getChildren().add(gip); - } else if (parameter instanceof DoubleParameter) { - GUIDoubleParameter gdp = new GUIDoubleParameter((DoubleParameter) parameter); - parameters.add(gdp); - vb.getChildren().add(gdp); - } else if (parameter instanceof BooleanParameter) { - GUIBooleanParameter gbp = new GUIBooleanParameter((BooleanParameter) parameter); - parameters.add(gbp); - vb.getChildren().add(gbp); - } + GUIParameter gp = GUIParameter.create(parameter); + + parameters.add(gp); + vb.getChildren().add(gp); } } @@ -371,30 +364,29 @@ public class SettingsPane extends AnchorPane { return event.getX() < (GUI.RESIZE_MARGIN); } - public void refreshValues() { - for (GUIParameter parameter : parameters) { - parameter.refreshValue(); - } - //parent.updateNodeGrids(); - } - -// public void bindParameters() { -// for (GUIParameter parameter : parameters) { -// parameter.doBindings(); -// } -// } -// -// public void unbindParameters() { -// for (GUIParameter parameter : parameters) { -// parameter.undoBindings(); -// } -// } - - public void toggleSettings(boolean value) { + public void disableSettings(boolean value) { bpPane.setDisable(value); eaPane.setDisable(value); mutatorPane.setDisable(value); ffPane.setDisable(value); nfPane.setDisable(value); } + + /** + * Writes the GUI parameter value back to the resources parameter, + * if any changes were made. + * + * @return true if the experiment needs to be reset, false otherwise. + */ + private boolean applyChanges() { + boolean reset = false; + + for (GUIParameter parameter : parameters) { + reset |= parameter.applyChange(); + } + + return reset; + } + + } diff --git a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java index a9f6753..8acd6dd 100644 --- a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java @@ -1,11 +1,11 @@ package jcgp.gui.settings.parameters; import javafx.scene.control.CheckBox; -import jcgp.parameters.BooleanParameter; +import jcgp.backend.parameters.BooleanParameter; public class GUIBooleanParameter extends GUIParameter { - public GUIBooleanParameter(BooleanParameter parameter) { + GUIBooleanParameter(BooleanParameter parameter) { super(); this.parameter = parameter; @@ -13,22 +13,16 @@ public class GUIBooleanParameter extends GUIParameter { value = new CheckBox(parameter.getName()); ((CheckBox) value).setSelected(parameter.get()); - value.setDisable(!parameter.isEditable()); + value.setDisable(parameter.isMonitor()); - getChildren().add(value); + if (parameter.isMonitor()) { + makeLightBinding(); + System.out.println("light binding made for " + name); + } + getChildren().add(value); } -// @Override -// public void doBindings() { -// ((CheckBox) value).selectedProperty().bind(((BooleanParameter) parameter).valueProperty()); -// } -// -// @Override -// public void undoBindings() { -// ((CheckBox) value).selectedProperty().unbind(); -// } - @Override public void refreshValue() { if (!((CheckBox) value).selectedProperty().isBound()) { @@ -36,5 +30,17 @@ public class GUIBooleanParameter extends GUIParameter { } } + + @Override + public boolean applyChange() { + if (((CheckBox) value).isSelected() != ((BooleanParameter) parameter).get() && (!parameter.isMonitor())) { + // apply value + ((BooleanParameter) parameter).set(((CheckBox) value).isSelected()); + if (parameter.isCritical()) { + return true; + } + } + return false; + } } diff --git a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java index 25d5b9f..4b74c6e 100644 --- a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java @@ -4,12 +4,12 @@ import javafx.geometry.Pos; import javafx.scene.control.TextField; import javafx.scene.layout.Priority; import javafx.scene.text.Text; -import jcgp.GUI; -import jcgp.parameters.DoubleParameter; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.gui.GUI; public class GUIDoubleParameter extends GUIParameter { - public GUIDoubleParameter(DoubleParameter parameter) { + GUIDoubleParameter(DoubleParameter parameter) { super(); this.parameter = parameter; @@ -23,26 +23,29 @@ public class GUIDoubleParameter extends GUIParameter { name.setWrappingWidth(GUI.WRAP_WIDTH); - ((TextField) value).setEditable(parameter.isEditable()); + ((TextField) value).setEditable(!parameter.isMonitor()); + + if (parameter.isMonitor()) { + makeLightBinding(); + } getChildren().addAll(name, value); } -// @Override -// public void doBindings() { -// ((TextField) value).textProperty().bind(((DoubleParameter) parameter).valueProperty().asString()); -// } -// -// @Override -// public void undoBindings() { -// ((TextField) value).textProperty().unbind(); -// } - @Override public void refreshValue() { - if (!((TextField) value).textProperty().isBound()) { - ((TextField) value).setText(String.valueOf(((DoubleParameter) parameter).get())); - } + ((TextField) value).setText(String.valueOf(((DoubleParameter) parameter).get())); } - + + @Override + public boolean applyChange() { + if (Double.valueOf(((TextField) value).getText()) != ((DoubleParameter) parameter).get() && (!parameter.isMonitor())) { + // apply value + ((DoubleParameter) parameter).set(Double.valueOf(((TextField) value).getText())); + if (parameter.isCritical()) { + return true; + } + } + return false; + } } diff --git a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java index 6bcfd55..70b78a6 100644 --- a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java @@ -4,12 +4,12 @@ import javafx.geometry.Pos; import javafx.scene.control.TextField; import javafx.scene.layout.Priority; import javafx.scene.text.Text; -import jcgp.GUI; -import jcgp.parameters.IntegerParameter; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.gui.GUI; public class GUIIntegerParameter extends GUIParameter { - public GUIIntegerParameter(IntegerParameter parameter) { + GUIIntegerParameter(IntegerParameter parameter) { super(); this.parameter = parameter; @@ -23,27 +23,27 @@ public class GUIIntegerParameter extends GUIParameter { name.setWrappingWidth(GUI.WRAP_WIDTH); - ((TextField) value).setEditable(parameter.isEditable()); + ((TextField) value).setEditable(!parameter.isMonitor()); + + if (parameter.isMonitor()) { + makeLightBinding(); + } getChildren().addAll(name, value); } -// @Override -// public void doBindings() { -// ((TextField) value).textProperty().bind(((IntegerParameter) parameter).valueProperty().asString()); -// } -// -// @Override -// public void undoBindings() { -// ((TextField) value).textProperty().unbind(); -// } - - public TextField getTextField() { - return (TextField) value; - } - @Override public void refreshValue() { ((TextField) value).setText(String.valueOf(((IntegerParameter) parameter).get())); } + + @Override + public boolean applyChange() { + if (Integer.valueOf(((TextField) value).getText()) != ((IntegerParameter) parameter).get() && (!parameter.isMonitor())) { + // apply value + ((IntegerParameter) parameter).set(Integer.valueOf(((TextField) value).getText())); + return parameter.isCritical(); + } + return false; + } } diff --git a/src/jcgp/gui/settings/parameters/GUIParameter.java b/src/jcgp/gui/settings/parameters/GUIParameter.java index a2f2f9c..a783d71 100644 --- a/src/jcgp/gui/settings/parameters/GUIParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIParameter.java @@ -1,10 +1,18 @@ package jcgp.gui.settings.parameters; +import java.util.concurrent.atomic.AtomicBoolean; + +import javafx.application.Platform; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.geometry.Pos; import javafx.scene.control.Control; import javafx.scene.layout.HBox; import javafx.scene.text.Text; -import jcgp.parameters.Parameter; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.Parameter; public abstract class GUIParameter extends HBox { @@ -12,19 +20,51 @@ public abstract class GUIParameter extends HBox { protected Text name; protected Control value; - public GUIParameter() { + protected AtomicBoolean updateCheck = new AtomicBoolean(true); + + protected GUIParameter() { setAlignment(Pos.CENTER_LEFT); setSpacing(5); } - public Parameter getParameter() { - return parameter; + protected final void makeLightBinding() { + parameter.valueProperty().addListener(new ChangeListener<Object>() { + @Override + public void changed( + ObservableValue<? extends Object> observable, + Object oldValue, Object newValue) { + if (updateCheck.getAndSet(false)) { + Platform.runLater(new Runnable() { + @Override + public void run() { + updateCheck.set(true); + refreshValue(); + } + }); + } + } + }); } -// public abstract void doBindings(); -// -// public abstract void undoBindings(); - public abstract void refreshValue(); + public static GUIParameter create(Parameter parameter) { + if (parameter instanceof IntegerParameter) { + return new GUIIntegerParameter((IntegerParameter) parameter); + } else if (parameter instanceof DoubleParameter) { + return new GUIDoubleParameter((DoubleParameter) parameter); + } else if (parameter instanceof BooleanParameter) { + return new GUIBooleanParameter((BooleanParameter) parameter); + } else { + throw new ClassCastException("No GUIParameter subclass exists for argument of type " + parameter.getClass()); + } + } + + /** + * Writes the GUI parameter value back to the resources parameter, + * if any changes were made. + * + * @return true if the experiment needs to be reset, false otherwise. + */ + public abstract boolean applyChange(); } |