package jcgp.backend.population; import java.util.ArrayList; /** * This is a chromosome output. Outputs are a special * type of mutable element with a single connection. It * returns the value of its single connection, but it * may not be connected to - it terminates a chromosome * active connection path. * * @author Eduardo Pedroni * */ public class Output implements Mutable { private Connection source; private Chromosome chromosome; private int index; /** * Makes a new instance of {@code Output} with the * specified arguments. * * @param chromosome the chromosome this output belongs to. * @param index the output index. */ public Output(Chromosome chromosome, int index) { this.chromosome = chromosome; this.index = index; } /** * @return the value of the output's source. */ public Object calculate() { return source.getValue(); } public int getIndex() { return index; } public Connection getSource() { return source; } public void getActiveNodes(ArrayList activeNodes) { if (source instanceof Node) { ((Node) source).getActive(activeNodes); } } /** * When mutating an output, the index parameter * is simply ignored and the output source is * set. * * @see jcgp.backend.population.Mutable#setConnection(int, jcgp.backend.population.Connection) */ @Override public void setConnection(int index, Connection newConnection) { source = newConnection; // trigger active path recomputation chromosome.recomputeActiveNodes(); } @Override public boolean copyOf(Mutable m) { // both cannot be the same instance if (this != m) { // element must be instance of output if (m instanceof Output) { Output o = (Output) m; // index must be the same if (index == o.getIndex()) { // source must be the same if (source != o.getSource()) { if (source instanceof Input && o.getSource() instanceof Input) { if (((Input) source).getIndex() == ((Input) o.getSource()).getIndex()) { return true; } } else if (source instanceof Node && o.getSource() instanceof Node) { if (((Node) source).getRow() == ((Node) o.getSource()).getRow() && ((Node) source).getColumn() == ((Node) o.getSource()).getColumn()) { return true; } } } } } } return false; } @Override public String toString() { return "Output " + index; } }