aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/GUI.java
blob: 7e7f385cce2f41af2fb43988dc997b4c551b919d (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
203
204
205
206
207
208
209
210
211
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.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TabPane.TabClosingPolicy;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import jcgp.JCGP;
import jcgp.JCGP.Resources;
import jcgp.gui.console.GUIConsole;
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";
	// 89AAD6
	public static final String MEDIUM_HIGHLIGHT_COLOUR = "#75BAFF";
	public static final String SOFT_HIGHLIGHT_COLOUR = "#C7DFFF";
	// BDFFC2
	public static final String GOOD_SELECTION_COLOUR = "#38C25B";
	// FBFFB8
	public static final String NEUTRAL_SELECTION_COLOUR = "#EDEB72";
	// FF9C9C
	public static final String BAD_SELECTION_COLOUR = "#F53D3D";
	
	
	/* 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;
    
	private static JCGP cgp;
	public static Resources resources;
	
	private BorderPane leftPane;
	private BorderPane window;
	
	private ChromosomePane[] chromosomes;
	private TabPane chromosomeTabs;
	
	private GUIConsole console = new GUIConsole();
	private SettingsPane settings;
	
	private Service<Void> cgpService;
	
	private boolean evolving = false;
	
	private Object printLock = new Object();
	
	private Runnable consoleFlush = new Runnable() {
		@Override
		public void run() {
			synchronized(printLock) {
				console.flush();
				printLock.notifyAll();
			}
		}
	};
	
	public static void main(String[] args) {
		cgp = new JCGP();
		resources = cgp.getResources();		
		
		launch();
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		resources.setConsole(console);
		
		cgpService = new Service<Void> () {
			@Override
			protected Task<Void> createTask() {
				
				Task<Void> t = new Task<Void>() {
					@Override
					protected Void call() throws Exception {
						while (!isCancelled()) {
							Platform.runLater(consoleFlush);
							synchronized (printLock) {
								cgp.nextGeneration();
								printLock.wait();
							}
						}
						return null;
					}
				};
				return t;
			}
		};
		
		/*
		 * Instantiate the various GUI elements here. 
		 * 
		 * 
		 */
		
		leftPane = new BorderPane();
		
		chromosomeTabs = new TabPane();
		chromosomeTabs.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
		makeChromosomeTabPane();
		
		settings = new SettingsPane(cgp, this);
		
		leftPane.setCenter(chromosomeTabs);
		leftPane.setBottom(console);
		
		window = new BorderPane();
		
		window.setCenter(leftPane);
		window.setRight(settings);
		
		primaryStage.setTitle("JCGP");
		
		primaryStage.setScene(new Scene(window));
		primaryStage.show();
	}

	/**
	 * 
	 */
	private void makeChromosomeTabPane() {
		chromosomeTabs.getTabs().clear();
		
		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), resources);
			tab = new Tab("Chr " + i);
			tab.setContent(chromosomes[i]);
			chromosomeTabs.getTabs().add(tab);
		}
	}
	
	private void updateNodeGrids() {
		for (int i = 0; i < chromosomes.length; i++) {
			chromosomes[i].update();
		}
	}
	
	private void unlockOutputs() {
		for (int i = 0; i < chromosomes.length; i++) {
			chromosomes[i].unlockOutputs();
		}
	}
	
	private void relockOutputs() {
		for (int i = 0; i < chromosomes.length; i++) {
			chromosomes[i].relockOutputs();
		}
	}
	
	public void disableChromosomePanes(boolean value) {
		chromosomeTabs.setDisable(value);
	}

	public void playPause() {
		if (!evolving) {
			settings.disableSettings(true);
			disableChromosomePanes(true);
			unlockOutputs();
			evolving = true;
			cgpService.restart();
		} else {
			settings.disableSettings(false);
			disableChromosomePanes(false);
			updateNodeGrids();
			evolving = false;
			cgpService.cancel();
			relockOutputs();
		}
	}
	
	public void step() {
		if (!evolving) {
			Thread t = new Thread(new Task<Void>() {
				@Override
				protected Void call() throws Exception {
					unlockOutputs();
					cgp.nextGeneration();
					console.flush();
					updateNodeGrids();
					relockOutputs();
					return null;
				}
			});
			t.start();
		}
	}
	
	public void resetCGP() {
		cgp.remakePopulation();
		makeChromosomeTabPane();
		
		resources.set("currentGen", 1);
		resources.set("currentRun", 1);
	}
}