aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population/Output.java
blob: 89ca13cfeaed0f67627cf6fdd16b73461a50bbc8 (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
package jcgp.population;

import java.util.ArrayList;

import jcgp.parameters.Parameters;

public class Output implements MutableElement {
	
	private Connection source;
	private Chromosome chromosome;
	private int index;
	
	public Output(Chromosome chromosome, int index) {
		this.chromosome = chromosome;
		this.index = index;
	}

	public Object calculate() {
		Object result = source.getValue();
		if (Parameters.getDebug()) {
			System.out.println("Output " + index + ": " + result);
		}
		return result;
	}

	@Override
	public void setConnection(Connection newConnection) {
		source = newConnection;
		chromosome.recomputeActiveNodes();
	}

	public int getIndex() {
		return index;
	}

	public Connection getSource() {
		return source;
	}

	public void getActiveNodes(ArrayList<Node> activeNodes) {
		if (source instanceof Node) {
			((Node) source).getActive(activeNodes);
		}
	}
	
	@Override
	public boolean copyOf(MutableElement m) {
		if (this != m) {
			if (m instanceof Output) {
				Output o = (Output) m;
				if (index == o.getIndex()) {
					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;
	}
}