aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/population/ChromosomePane.java
blob: 841c18afd797b0250935515389002aa244ec6044 (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
package jcgp.gui.population;

import java.util.ArrayList;

import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Input;
import jcgp.backend.population.Node;
import jcgp.backend.resources.Resources;


public class ChromosomePane extends ScrollPane {

	private GUINode[][] guiNodes;
	private GUIInput[] guiInputs;
	private GUIOutput[] guiOutputs;
	
	private Pane content;
	
	private ArrayList<Line> connectionLines;
	
	private ArrayList<GUIOutput> relock = new ArrayList<GUIOutput>();
		
	private boolean target = false;
	
	public ChromosomePane(Chromosome chromosome, Resources resources) {
		super();		
		connectionLines = new ArrayList<Line>();
				
		content = new Pane();
		content.setId("content pane for genes");
		
		// generate the GUIGenes
		// inputs
		guiInputs = new GUIInput[resources.getInt("inputs")];
		for (int i = 0; i < guiInputs.length; i++) {
			// make the GUI elements
			guiInputs[i] = new GUIInput(this, chromosome.getInput(i));
			content.getChildren().addAll(guiInputs[i]);
		}
		// nodes
		guiNodes = new GUINode[resources.getInt("rows")][resources.getInt("columns")];
		double angle, xPos, yPos;
		for (int r = 0; r < guiNodes.length; r++) {
			for (int c = 0; c < guiNodes[r].length; c++) {
				// make the connection lines
				Line lines[] = new Line[resources.getInt("arity")];
				for (int l = 0; l < lines.length; l++) {
					angle = ((((double) (l + 1)) / ((double) (lines.length + 1))) * GUIGene.THETA) - (GUIGene.THETA / 2);
					xPos = (-Math.cos(angle) * GUIGene.NODE_RADIUS) + (((c + 1) * (2 * GUIGene.NODE_RADIUS + GUIGene.SPACING)) + GUIGene.NODE_RADIUS);
					yPos = (Math.sin(angle) * GUIGene.NODE_RADIUS) + ((r * (2 * GUIGene.NODE_RADIUS + GUIGene.SPACING)) + GUIGene.NODE_RADIUS);
					
					lines[l] = new Line(xPos, yPos, xPos, yPos);
					lines[l].setMouseTransparent(true);
					lines[l].setVisible(false);
					connectionLines.add(lines[l]);
				}
				// make the GUI elements
				guiNodes[r][c] = new GUINode(this, chromosome.getNode(r, c), lines);
			}
			content.getChildren().addAll(guiNodes[r]);
		}
		// outputs
		guiOutputs = new GUIOutput[resources.getInt("outputs")];
		for (int i = 0; i < guiOutputs.length; i++) {
			xPos = ((resources.getInt("columns") + 1) * (2 * GUIGene.NODE_RADIUS + GUIGene.SPACING));
			yPos = (chromosome.getOutput(i).getIndex() * (2 * GUIGene.NODE_RADIUS + GUIGene.SPACING)) + GUIGene.NODE_RADIUS;
			// make the line
			Line line = new Line(xPos, yPos, xPos, yPos);
			line.setMouseTransparent(true);
			line.setVisible(false);
			connectionLines.add(line);
			// make the GUI elements
			guiOutputs[i] = new GUIOutput(this, chromosome.getOutput(i), line);
			content.getChildren().addAll(guiOutputs[i]);
		}

		content.getChildren().addAll(connectionLines);
		setPrefWidth(620);	
		setContent(content);
	}
	
	public GUIGene getGuiGene(Connection gene) {
		if (gene instanceof Input) {
			return guiInputs[((Input) gene).getIndex()];
		} else if (gene instanceof Node) {
			return guiNodes[((Node) gene).getRow()][((Node) gene).getColumn()];
		} else {
			// something bad happened!
			throw new ClassCastException();
		}	
	}
	
	public boolean isTarget() {
		return target;
	}
	
	public void setTarget(boolean newValue) {
		target = newValue;
	}
	
	public void updateGenes() {
		for (int r = 0; r < guiNodes.length; r++) {
			for (int c = 0; c < guiNodes[r].length; c++) {
				guiNodes[r][c].updateLines();
				guiNodes[r][c].updateFunction();
			}
		}
		for (int i = 0; i < guiOutputs.length; i++) {
			guiOutputs[i].updateLines();
		}
	}
	
	public void unlockOutputs() {
		relock.clear();
		for (int i = 0; i < guiOutputs.length; i++) {
			if (guiOutputs[i].isLocked()) {
				guiOutputs[i].unlock();
				relock.add(guiOutputs[i]);
			}	
		}
	}
	
	public void relockOutputs() {
		for (int i = 0; i < relock.size(); i++) {
			relock.get(i).lock();
		}
	}
}