View Javadoc
1   package net.bmahe.genetics4j.neat.spec;
2   
3   import java.util.List;
4   import java.util.Objects;
5   
6   /**
7    * Node layout with explicitly declared, ordered input and output IDs.
8    *
9    * <p>The constructor defensively copies both lists. IDs must be unique, non-negative, disjoint between inputs and
10   * outputs, and outside the configured hidden-node range.
11   */
12  public class ExplicitNeatNodeLayout implements NeatNodeLayout {
13  
14  	private final List<Integer> inputNodeIds;
15  	private final List<Integer> outputNodeIds;
16  	private final long hiddenNodeIdStartInclusive;
17  	private final long hiddenNodeIdEndExclusive;
18  
19  	/**
20  	 * Creates an explicit node layout.
21  	 *
22  	 * @param _inputNodeIds               input IDs in vector order
23  	 * @param _outputNodeIds              output IDs in vector order
24  	 * @param _hiddenNodeIdStartInclusive inclusive hidden-node range start
25  	 * @param _hiddenNodeIdEndExclusive   exclusive hidden-node range end
26  	 * @throws NullPointerException     if either external-ID list or one of its elements is {@code null}
27  	 * @throws IllegalArgumentException if the external IDs or hidden-node range are invalid
28  	 */
29  	public ExplicitNeatNodeLayout(final List<Integer> _inputNodeIds,
30  			final List<Integer> _outputNodeIds,
31  			final long _hiddenNodeIdStartInclusive,
32  			final long _hiddenNodeIdEndExclusive) {
33  		inputNodeIds = List.copyOf(_inputNodeIds);
34  		outputNodeIds = List.copyOf(_outputNodeIds);
35  		NeatNodeLayoutValidation
36  				.validate(inputNodeIds, outputNodeIds, _hiddenNodeIdStartInclusive, _hiddenNodeIdEndExclusive);
37  		hiddenNodeIdStartInclusive = _hiddenNodeIdStartInclusive;
38  		hiddenNodeIdEndExclusive = _hiddenNodeIdEndExclusive;
39  	}
40  
41  	@Override
42  	public List<Integer> inputNodeIds() {
43  		return inputNodeIds;
44  	}
45  
46  	@Override
47  	public List<Integer> outputNodeIds() {
48  		return outputNodeIds;
49  	}
50  
51  	@Override
52  	public long hiddenNodeIdStartInclusive() {
53  		return hiddenNodeIdStartInclusive;
54  	}
55  
56  	@Override
57  	public long hiddenNodeIdEndExclusive() {
58  		return hiddenNodeIdEndExclusive;
59  	}
60  
61  	@Override
62  	public int hashCode() {
63  		return Objects.hash(hiddenNodeIdEndExclusive, hiddenNodeIdStartInclusive, inputNodeIds, outputNodeIds);
64  	}
65  
66  	@Override
67  	public boolean equals(final Object obj) {
68  		if (this == obj) {
69  			return true;
70  		}
71  		if (obj instanceof ExplicitNeatNodeLayout other) {
72  			return hiddenNodeIdEndExclusive == other.hiddenNodeIdEndExclusive
73  					&& hiddenNodeIdStartInclusive == other.hiddenNodeIdStartInclusive
74  					&& inputNodeIds.equals(other.inputNodeIds) && outputNodeIds.equals(other.outputNodeIds);
75  		}
76  		return false;
77  	}
78  
79  	@Override
80  	public String toString() {
81  		return "ExplicitNeatNodeLayout [inputNodeIds=" + inputNodeIds + ", outputNodeIds=" + outputNodeIds
82  				+ ", hiddenNodeIdStartInclusive=" + hiddenNodeIdStartInclusive + ", hiddenNodeIdEndExclusive="
83  				+ hiddenNodeIdEndExclusive + "]";
84  	}
85  }