diff options
Diffstat (limited to 'src/jcgp/gui/population/ChromosomePane.java')
-rw-r--r-- | src/jcgp/gui/population/ChromosomePane.java | 195 |
1 files changed, 121 insertions, 74 deletions
diff --git a/src/jcgp/gui/population/ChromosomePane.java b/src/jcgp/gui/population/ChromosomePane.java index a87a054..3546011 100644 --- a/src/jcgp/gui/population/ChromosomePane.java +++ b/src/jcgp/gui/population/ChromosomePane.java @@ -6,13 +6,17 @@ 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; import jcgp.gui.GUI; +import jcgp.gui.constants.Constants; /** * This extension of {@code ScrollPane} contains a series of * nodes, inputs and outputs spread across a grid. It also contains - * all of the connection lines laid over the nodes, inputs and outputs. + * all of the connection lines overlaid over the nodes, inputs and outputs. * * * @author Eduardo Pedroni @@ -20,121 +24,164 @@ import jcgp.gui.GUI; */ public class ChromosomePane extends ScrollPane { - private GUIInput[] guiInputs; private GUINode[][] guiNodes; + private GUIInput[] guiInputs; private GUIOutput[] guiOutputs; - + private Pane content; - + + private ArrayList<Line> connectionLines; + private ArrayList<GUIOutput> relock = new ArrayList<GUIOutput>(); + + private int rows, columns; + + private Object[] testInputs; + private boolean target = false; - - public ChromosomePane(Chromosome chromosome) { + private PopulationPane parent; + + public ChromosomePane(Chromosome chromosome, GUI gui, PopulationPane parent) { super(); - - ArrayList<Line> connectionLines = new ArrayList<Line>(); - - int rows = GUI.resources.rows(); - int columns = GUI.resources.columns(); - + + final Resources resources = gui.getExperiment().getResources(); + this.parent = parent; + + rows = resources.rows(); + columns = resources.columns(); + + connectionLines = new ArrayList<Line>(); + content = new Pane(); content.setId("content pane for genes"); - - /* - * inputs - */ - guiInputs = new GUIInput[GUI.resources.inputs()]; + + // generate the GUIGenes + // inputs + guiInputs = new GUIInput[resources.inputs()]; for (int i = 0; i < guiInputs.length; i++) { - guiInputs[i] = new GUIInput(chromosome.getInput(i)); + // make the GUI elements + guiInputs[i] = new GUIInput(this, chromosome.getInput(i)); + content.getChildren().addAll(guiInputs[i]); } - // add inputs to content pane - content.getChildren().addAll(guiInputs); - - /* - * nodes - */ + // nodes guiNodes = new GUINode[rows][columns]; - for (int c = 0; c < columns; c++) { - for (int r = 0; r < rows; r++) { + double angle, xPos, yPos; + for (int r = 0; r < rows; r++) { + for (int c = 0; c < columns; c++) { // make the connection lines - Line lines[] = new Line[GUI.resources.arity()]; + Line lines[] = new Line[resources.arity()]; for (int l = 0; l < lines.length; l++) { - lines[l] = new Line(); + angle = ((((double) (l + 1)) / ((double) (lines.length + 1))) * Constants.THETA) - (Constants.THETA / 2); + xPos = (-Math.cos(angle) * Constants.NODE_RADIUS) + (((c + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); + yPos = (Math.sin(angle) * Constants.NODE_RADIUS) + ((r * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.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 element - guiNodes[r][c] = new GUINode(chromosome.getNode(r, c), lines); - // add node to content pane - content.getChildren().add(guiNodes[r][c]); + // make the GUI elements + guiNodes[r][c] = new GUINode(this, chromosome.getNode(r, c), lines, gui); } + content.getChildren().addAll(guiNodes[r]); } - - /* - * outputs - */ - guiOutputs = new GUIOutput[GUI.resources.outputs()]; + // outputs + guiOutputs = new GUIOutput[resources.outputs()]; for (int i = 0; i < guiOutputs.length; i++) { - // make the connection line - Line line = new Line(); - line.setVisible(false); + xPos = ((resources.columns() + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)); + yPos = (chromosome.getOutput(i).getIndex() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.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 element - guiOutputs[i] = new GUIOutput(chromosome.getOutput(i), line); + // make the GUI elements + guiOutputs[i] = new GUIOutput(this, chromosome.getOutput(i), line, gui); + content.getChildren().addAll(guiOutputs[i]); } - // add outputs to content pane - content.getChildren().addAll(guiOutputs); - // add lines to the pane on top of genes content.getChildren().addAll(connectionLines); - - setPrefWidth(620); + setPrefWidth(620); setContent(content); } - + + protected 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(); + } + } + protected boolean isTarget() { return target; } - + protected void setTarget(boolean newValue) { target = newValue; } - + public void updateGenes(Chromosome chr) { - for (int r = 0; r < GUI.resources.rows(); r++) { - for (int c = 0; c < GUI.resources.columns(); c++) { + for (int r = 0; r < rows; r++) { + for (int c = 0; c < columns; c++) { guiNodes[r][c].setNode(chr.getNode(r, c)); + guiNodes[r][c].updateLines(); + guiNodes[r][c].updateText(); } } for (int i = 0; i < guiOutputs.length; i++) { guiOutputs[i].setOutput(chr.getOutput(i)); + guiOutputs[i].updateLines(); + } + if (isEvaluating()) { + setInputs(testInputs); + } + } + + 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(); + } + } + + public void setInputs(Object[] values) { + testInputs = values; + for (int i = 0; i < guiInputs.length; i++) { + guiInputs[i].setValue(values[i]); } + updateValues(); } - public static boolean isAllowed(GUIMutable source, GUIConnection target) { - if (source instanceof GUINode) { - // if the source is a node, all inputs and some nodes are valid - if (target instanceof GUIInput) { - return true; - } else if (target instanceof GUINode) { - // target and source are nodes, let's look at levels back - Node t = ((GUINode) target).getNode(), s = ((GUINode) source).getNode(); - if (s.getColumn() - t.getColumn() > 0 && s.getColumn() - t.getColumn() <= GUI.resources.levelsBack()) { - return true; - } - } - return false; - } else if (source instanceof GUIOutput) { - // if the source is an output, any node or input is valid - if (target instanceof GUINode || target instanceof GUIInput) { - return true; - } else { - // this should never happen... - return false; + public void updateValues() { + for (int i = 0; i < guiInputs.length; i++) { + guiInputs[i].updateText(); + } + for (int c = 0; c < columns; c++) { + for (int r = 0; r < rows; r++) { + guiNodes[r][c].updateText(); } } - // if the source was neither node nor output, something bad is happening - throw new ClassCastException("Source was neither GUINode nor GUIOutput."); + for (int o = 0; o < guiOutputs.length; o++) { + guiOutputs[o].updateText(); + } + } + + /** + * @return the evaluating attribute. + */ + public boolean isEvaluating() { + return parent.isEvaluating(); } } |