Class NeatChromosomeSpec

java.lang.Object
net.bmahe.genetics4j.neat.spec.NeatChromosomeSpec
All Implemented Interfaces:
ChromosomeSpec
Direct Known Subclasses:
ImmutableNeatChromosomeSpec

@Immutable public abstract class NeatChromosomeSpec extends Object implements ChromosomeSpec
Specification for NEAT (NeuroEvolution of Augmenting Topologies) neural network chromosomes.

A specification defines the stable identities and vector order of the network's input and output nodes, the namespace available to hidden nodes, and the bounds applied to connection weights. Chromosome factories and genetic operators use the same NeatNodeLayout throughout an evolutionary run so structural mutations do not relabel existing nodes.

Contiguous layouts preserve the traditional NEAT numbering scheme. Inputs start at zero, outputs immediately follow the inputs, and hidden-node IDs start after the outputs:

NeatChromosomeSpec xorSpec = NeatChromosomeSpec.of(
		2, // input IDs [0, 1]
		1, // output ID [2]
		-1.0f, // minimum connection weight
		1.0f); // maximum connection weight

Explicit layouts support stable, ordered, non-contiguous external IDs and a dedicated hidden-node range:

NeatNodeLayout layout = NeatNodeLayout.explicit(
		List.of(10, 1_000, 1_001), // ordered input IDs
		List.of(102_000), // ordered output IDs
		1_000_000, // hidden range start, inclusive
		2_000_000); // hidden range end, exclusive

NeatChromosomeSpec sparseSpec = new NeatChromosomeSpec.Builder().nodeLayout(layout)
		.minWeightValue(-2.0f)
		.maxWeightValue(2.0f)
		.build();

Declaration order is significant: it defines how input and output vectors map to node IDs. Node IDs themselves are opaque identities and need not be contiguous. A valid layout contains at least one input and one output, uses unique non-negative external IDs, keeps input and output IDs disjoint, and reserves a non-empty hidden-node range that does not overlap external IDs.

The minimum connection weight must be strictly less than the maximum. Factories use these bounds when creating initial connections, and mutation operators use them when changing connection weights.

See Also:
  • Constructor Details

    • NeatChromosomeSpec

      public NeatChromosomeSpec()
  • Method Details

    • nodeLayout

      @Parameter public abstract NeatNodeLayout nodeLayout()
      Returns the stable external node identities and hidden-node namespace for chromosomes created from this specification.
      Returns:
      the node layout
    • minWeightValue

      @Parameter public abstract float minWeightValue()
      Returns the minimum allowed connection weight.
      Returns:
      the inclusive lower bound for connection weights
    • maxWeightValue

      @Parameter public abstract float maxWeightValue()
      Returns the maximum allowed connection weight.
      Returns:
      the inclusive upper bound for connection weights
    • numInputs

      public int numInputs()
      Returns the number of input nodes declared by nodeLayout().
      Returns:
      the positive number of input nodes
    • numOutputs

      public int numOutputs()
      Returns the number of output nodes declared by nodeLayout().
      Returns:
      the positive number of output nodes
    • check

      @Check protected void check()
    • of

      public static NeatChromosomeSpec of(NeatNodeLayout nodeLayout, float minWeightValue, float maxWeightValue)
      Creates a specification using the supplied node layout and connection-weight bounds.
      Parameters:
      nodeLayout - stable external IDs and hidden-node namespace
      minWeightValue - minimum allowed connection weight
      maxWeightValue - maximum allowed connection weight
      Returns:
      a validated immutable chromosome specification
      Throws:
      NullPointerException - if nodeLayout is null
      IllegalArgumentException - if minWeightValue is not less than maxWeightValue
    • of

      public static NeatChromosomeSpec of(int numInputs, int numOutputs, float minWeightValue, float maxWeightValue)
      Creates a specification using the traditional contiguous node layout.

      Input IDs are [0, numInputs), output IDs are [numInputs, numInputs + numOutputs), and the hidden-node namespace starts at numInputs + numOutputs.

      Parameters:
      numInputs - number of input nodes; must be positive
      numOutputs - number of output nodes; must be positive
      minWeightValue - minimum allowed connection weight
      maxWeightValue - maximum allowed connection weight
      Returns:
      a validated immutable chromosome specification
      Throws:
      IllegalArgumentException - if either node count is not positive or the weight bounds are not ordered