aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/GUI.java
blob: eaca07785af966a5aac858d2d232d0d89c062647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package jcgp.gui;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import jcgp.JCGP;
import jcgp.backend.resources.Resources;
import jcgp.gui.console.GUIConsole;
import jcgp.gui.dragresize.HorizontalDragResize;
import jcgp.gui.dragresize.VerticalDragResize;
import jcgp.gui.population.FunctionSelector;
import jcgp.gui.population.PopulationPane;
import jcgp.gui.settings.SettingsPane;

public class GUI extends Application {
	
	/* Colours */
	/** NEUTRAL_COLOUR is the colour elements should be when no interaction is occurring
	 * (no hovering, clicking, dragging or any other form of interaction). */
	public static final String NEUTRAL_COLOUR = "#FFFFFF";
	public static final String HARD_HIGHLIGHT_COLOUR = "#5496FF";
	public static final String MEDIUM_HIGHLIGHT_COLOUR = "#75BAFF";
	public static final String SOFT_HIGHLIGHT_COLOUR = "#C7DFFF";
	
	public static final String GOOD_SELECTION_COLOUR = "#38C25B";
	public static final String NEUTRAL_SELECTION_COLOUR = "#FFEF73";
	public static final String BAD_SELECTION_COLOUR = "#FF5C5C";
	
	/* Sizes and distances */
    public static final double RESIZE_MARGIN = 5.0;
    
    public static final double SETTINGS_WIDTH = 200;
    public static final double CONSOLE_HEIGHT = 100;
    
    public static final double WRAP_WIDTH = 90;
    
	public static final JCGP jcgp = new JCGP();
	public static final Resources resources = jcgp.getResources();
	
	public static final FunctionSelector functionSelector = new FunctionSelector(resources.getFunctionSet());
	
	private PopulationPane populationPane;
	
	private GUIConsole console = new GUIConsole();
	
	private SettingsPane settingsPane;
	
	private boolean evolving = false;
	
	private Object printLock = new Object();
	
	private Service<Void> cgpService = new Service<Void> () {
		@Override
		protected Task<Void> createTask() {
			Task<Void> t = new Task<Void>() {
				@Override
				protected Void call() throws Exception {
					while (!isCancelled() && !jcgp.isFinished()) {
						synchronized (printLock) {
							Platform.runLater(consoleFlush);
							jcgp.nextGeneration();
							printLock.wait();
						}
					}
					if (jcgp.isFinished()) {
						Platform.runLater(new Runnable() {
							@Override
							public void run() {
								runningMode(false);
							}
						});
					}
					return null;
				}
			};
			return t;
		}
	};
	
	private Runnable consoleFlush = new Runnable() {
		@Override
		public void run() {
			synchronized(printLock) {
				console.flush();
				printLock.notifyAll();
			}
		}
	};
	
	public static void main(String[] args) {
//		jcgp = new JCGP();
//		resources = jcgp.getResources();
//		
//		functionSelector = 
		
		launch();
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		jcgp.setConsole(console);
		
		/*
		 * Instantiate the various GUI elements here. 
		 * 
		 * 
		 */
		BorderPane leftFrame = new BorderPane();
		
		populationPane = new PopulationPane(jcgp);
		
		settingsPane = new SettingsPane(jcgp, this);
		
		HorizontalDragResize.makeDragResizable(settingsPane);
		VerticalDragResize.makeDragResizable(console);
		
		leftFrame.setCenter(populationPane);
		leftFrame.setBottom(console);
		
		BorderPane experimentLayer = new BorderPane();
		
		experimentLayer.setCenter(leftFrame);
		experimentLayer.setRight(settingsPane);
		
		primaryStage.setTitle("JCGP");
		
		Pane sceneParent = new Pane();
		experimentLayer.prefHeightProperty().bind(sceneParent.heightProperty());
		experimentLayer.prefWidthProperty().bind(sceneParent.widthProperty());
		sceneParent.getChildren().addAll(experimentLayer, functionSelector);
		
		primaryStage.setScene(new Scene(sceneParent));
		primaryStage.setMinWidth(800);
		primaryStage.setMinHeight(600);
		primaryStage.show();
	}

	public void runPause() {
		if (!jcgp.isFinished() && settingsPane.areParametersValid()) {
			if (!evolving) {
				runningMode(true);
				cgpService.restart();
			} else {
				cgpService.cancel();
				runningMode(false);
			}
		}
	}
	
	public void step() {
		if (!evolving && !jcgp.isFinished() && settingsPane.areParametersValid()) {
			if (settingsPane.isResetRequired()) {
				reset();
			}
			populationPane.unlockOutputs();
		
			jcgp.nextGeneration();
			console.flush();
			
			populationPane.updateGenes();
			populationPane.relockOutputs();
			
		}
	}
	
	public void reset() {
		if (!evolving && settingsPane.areParametersValid()) {
			settingsPane.applyParameters();
			jcgp.reset();
			populationPane.remakeTabs(jcgp.getPopulation(), jcgp.getResources());
			settingsPane.revalidateParameters();
			settingsPane.updateControls(false, jcgp.isFinished());
			console.flush();
		}
	}
	
	private void runningMode(boolean value) {
		populationPane.setDisable(value);
		settingsPane.updateControls(value, jcgp.isFinished());
				
		if (value) {
			populationPane.unlockOutputs();
			if (settingsPane.isResetRequired()) {
				reset();
			}
		} else {
			populationPane.updateGenes();
			populationPane.relockOutputs();
		}
		evolving = value;
	}
	
	public static void updateFunctionSelector() {
		functionSelector.remakeFunctions(resources.getFunctionSet());
	}
	
}