From 04b35ccdad6e18701ede823e333118b0b22907c2 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 30 Mar 2014 21:07:37 +0100 Subject: Running into some issues with running the CGP loop in the background with bindings. --- src/jcgp/GUI.java | 103 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 21 deletions(-) (limited to 'src/jcgp/GUI.java') diff --git a/src/jcgp/GUI.java b/src/jcgp/GUI.java index a893494..98545db 100644 --- a/src/jcgp/GUI.java +++ b/src/jcgp/GUI.java @@ -1,10 +1,8 @@ package jcgp; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; - import javafx.application.Application; +import javafx.concurrent.Service; +import javafx.concurrent.Task; import javafx.scene.Scene; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; @@ -12,9 +10,9 @@ import javafx.scene.control.TabPane.TabClosingPolicy; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import jcgp.JCGP.Resources; +import jcgp.gui.ChromosomePane; import jcgp.gui.GUIConsole; -import jcgp.gui.SettingsPane; -import jcgp.gui.population.ChromosomePane; +import jcgp.gui.settings.SettingsPane; public class GUI extends Application { @@ -35,10 +33,9 @@ public class GUI extends Application { /* Sizes and distances */ - public static final double RESIZE_MARGIN = 5.0; - public static final double SETTINGS_WIDTH = 190; + public static final double SETTINGS_WIDTH = 200; public static final double CONSOLE_HEIGHT = 100; public static final double WRAP_WIDTH = 90; @@ -52,18 +49,14 @@ public class GUI extends Application { private ChromosomePane[] chromosomes; private TabPane mainPane; - private static GUIConsole console = new GUIConsole(); + private GUIConsole console = new GUIConsole(); private SettingsPane settings; - public static final PrintStream out = new PrintStream(new OutputStream() { - @Override - public void write(int b) throws IOException { - console.getTextArea().appendText(String.valueOf((char) b)); - } - }); - + private Service cgpService; + + private static boolean evolving = false; + public static void main(String[] args) { - cgp = new JCGP(); resources = cgp.getResources(); @@ -73,6 +66,28 @@ public class GUI extends Application { @Override public void start(Stage primaryStage) throws Exception { + + cgpService = new Service () { + + @Override + protected Task createTask() { + Task t = new Task() { + @Override + protected Object call() throws Exception { + while (true) { + if (isCancelled()) { + return null; + } else { + cgp.nextGeneration(); + } + } + } + }; + + return t; + } + }; + /* * Instantiate the various GUI elements here. * @@ -81,16 +96,16 @@ public class GUI extends Application { mainPane = new TabPane(); mainPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE); - chromosomes = new ChromosomePane[(int) cgp.getResources().get("popSize")]; + chromosomes = new ChromosomePane[cgp.getResources().getInt("popSize")]; Tab tab; for (int i = 0; i < chromosomes.length; i++) { - chromosomes[i] = new ChromosomePane(cgp.getPopulation().getChromosome(i)); + chromosomes[i] = new ChromosomePane(cgp.getPopulation().getChromosome(i), resources); tab = new Tab("Chr " + i); tab.setContent(chromosomes[i]); mainPane.getTabs().add(tab); } - settings = new SettingsPane(cgp); + settings = new SettingsPane(cgp, this); //mainPane.setPrefHeight(500); @@ -102,7 +117,7 @@ public class GUI extends Application { window.setCenter(leftPane); window.setRight(settings); - + //primaryStage.setMinHeight(600); //primaryStage.setMinWidth(800); @@ -111,5 +126,51 @@ public class GUI extends Application { primaryStage.setScene(new Scene(window)); primaryStage.show(); } + + public void updateNodeGrids() { + for (int i = 0; i < chromosomes.length; i++) { + chromosomes[i].update(); + } + } + + public void playPause() { + if (!evolving) { + setEvolving(true); + cgpService.restart(); + } else { + cgpService.cancel(); + setEvolving(false); + } + } + + public void step() { + if (!evolving) { + Thread t = new Thread(new Task() { + @Override + protected Object call() throws Exception { + cgp.nextGeneration(); + setEvolving(false); + return null; + } + }); + setEvolving(true); + t.start(); + } + } + + private void setEvolving(boolean value) { + evolving = value; + settings.toggleSettings(value); + if (value) { + settings.bindParameters(); + } else { + updateNodeGrids(); + settings.unbindParameters(); + } + } + + public boolean isEvolving() { + return evolving; + } } -- cgit v1.2.3