Class FeedForwardNetwork

java.lang.Object
net.bmahe.genetics4j.neat.FeedForwardNetwork

public class FeedForwardNetwork extends Object
Implements a feed-forward neural network for evaluating NEAT (NeuroEvolution of Augmenting Topologies) chromosomes.

FeedForwardNetwork provides a computational engine for executing neural networks evolved by the NEAT algorithm. It takes a network topology defined by connections and nodes, organizes them into computational layers, and provides efficient forward propagation for fitness evaluation. The network supports arbitrary topologies with variable numbers of hidden layers and connections.

Key features:

  • Dynamic topology: Supports arbitrary network structures evolved by NEAT
  • Layer-based evaluation: Automatically computes optimal evaluation order
  • Configurable activation: Supports any activation function for hidden and output nodes
  • Efficient propagation: Optimized forward pass through network layers

Network evaluation process:

  1. Input assignment: Input values are assigned to input nodes
  2. Layer computation: Each layer is computed in topological order
  3. Node activation: Each node applies weighted sum followed by activation function
  4. Output extraction: Output values are collected from designated output nodes

Network construction workflow:

  • Topology analysis: Network connections are analyzed to determine layer structure
  • Layer partitioning: Nodes are organized into evaluation layers using topological sorting
  • Connection mapping: Backward connections are precomputed for efficient evaluation
  • Dead node removal: Unreachable nodes are excluded from computation

Common usage patterns:


 // Create network from NEAT chromosome
 NeatChromosome chromosome = // ... obtain from evolution
 Set<Integer> inputNodes = Set.of(0, 1, 2);
 Set<Integer> outputNodes = Set.of(3, 4);
 Function<Float, Float> activation = Activations::sigmoid;
 
 FeedForwardNetwork network = new FeedForwardNetwork(
     inputNodes, outputNodes, chromosome.getConnections(), activation
 );
 
 // Evaluate network on input data
 Map<Integer, Float> inputs = Map.of(0, 1.0f, 1, 0.5f, 2, -0.3f);
 Map<Integer, Float> outputs = network.compute(inputs);
 
 // Extract specific outputs
 float output1 = outputs.get(3);
 float output2 = outputs.get(4);
 

Activation function integration:

  • Sigmoid activation: Standard logistic function for binary classification
  • Tanh activation: Hyperbolic tangent for continuous outputs
  • Linear activation: Identity function for regression problems
  • Custom functions: Any Function<Float, Float> can be used

Performance optimizations:

  • Layer precomputation: Network layers are computed once during construction
  • Connection mapping: Backward connections are precomputed for fast lookup
  • Dead node elimination: Unreachable nodes are excluded from evaluation
  • Efficient propagation: Only enabled connections participate in computation

Error handling and validation:

  • Input validation: Ensures all input nodes receive values
  • Output validation: Verifies all output nodes produce values
  • Topology validation: Validates network structure during construction
  • Connection consistency: Ensures connection endpoints reference valid nodes

Integration with NEAT evolution:

  • Chromosome evaluation: Converts NEAT chromosomes to executable networks
  • Fitness computation: Provides network output for fitness evaluation
  • Topology evolution: Supports networks with varying structure complexity
  • Innovation tracking: Works with networks containing historical innovations
See Also:
  • Field Details

    • logger

      public static final org.apache.logging.log4j.Logger logger
    • inputNodeIndices

      private final Set<Integer> inputNodeIndices
    • outputNodeIndices

      private final Set<Integer> outputNodeIndices
    • connections

      private final List<Connection> connections
    • layers

      private final List<List<Integer>> layers
    • backwardConnections

      private final Map<Integer,Set<Connection>> backwardConnections
    • activationFunction

      private final Function<Float,Float> activationFunction
  • Constructor Details

    • FeedForwardNetwork

      public FeedForwardNetwork(Set<Integer> _inputNodeIndices, Set<Integer> _outputNodeIndices, List<Connection> _connections, Function<Float,Float> _activationFunction)
      Constructs a new feed-forward network with the specified topology and activation function.

      The constructor analyzes the network topology, computes evaluation layers using topological sorting, and precomputes connection mappings for efficient forward propagation. The network is immediately ready for evaluation after construction.

      Parameters:
      _inputNodeIndices - set of input node indices
      _outputNodeIndices - set of output node indices
      _connections - list of network connections defining the topology
      _activationFunction - activation function to apply to hidden and output nodes
      Throws:
      IllegalArgumentException - if any parameter is null or empty
  • Method Details

    • compute

      public Map<Integer,Float> compute(Map<Integer,Float> inputValues)
      Computes the network output for the given input values.

      This method performs forward propagation through the network, computing node activations layer by layer in topological order. Input values are assigned to input nodes, then each subsequent layer is computed by applying weighted sums and activation functions.

      The computation process:

      1. Input values are assigned to input nodes
      2. For each layer (starting from first hidden layer):
      3. For each node in the layer:
      4. Compute weighted sum of inputs from previous layers
      5. Apply activation function to the sum
      6. Store the result for use in subsequent layers
      7. Extract and return output values from output nodes
      Parameters:
      inputValues - mapping from input node indices to their values
      Returns:
      mapping from output node indices to their computed values
      Throws:
      IllegalArgumentException - if inputValues is null, has wrong size, or missing required inputs