SharpNEAT Documentation

repository·main·Indexed 19 days ago

https://github.com/colgreen/sharpneat

A C# implementation of the Neuroevolution of Augmenting Topologies (NEAT) algorithm targeting .NET 9. SharpNEAT provides a modular framework for evolving neural network structures and weights using mutation, recombination, and selection. It includes core abstractions for Genomes and Evolutionary Algorithms, as well as an EfficacySampler for tasks such as generative function regression. The library supports hardware acceleration for neural networks and activation functions, and provides detailed configuration for species management, complexity regulation, and mutation probabilities.

Tokens
11K
Snippets
34
Records
37
Agent score
64%

What's inside SharpNEAT

  1. Overview of SharpNEAT

    main

    SharpNEAT is a full implementation of the Neuroevolution of Augmenting Topologies (NEAT) algorithm written in C# and targeting .NET 9. It is designed as a modular framework for evolving neural networks by optimizing both their structure (nodes and connections) and their connection weights.

    Unlike backpropagation, which optimizes weights for a fixed structure, SharpNEAT uses evolutionary mechanisms—mutation, recombination, and selection—to search for optimal network topologies to solve specific objective functions (problem tasks).

  2. Core abstractions in SharpNEAT

    main

    SharpNEAT is built on a modular architecture that allows for experimentation with different evolutionary strategies. The two primary abstractions you will interact with are:

    • Genome: The genetic representation or encoding of the neural network.
    • Evolutionary Algorithm: The implementation of mutation, recombination, and selection strategies used to evolve the population.
  3. Optimize SharpNEAT performance via runtime configuration

    main

    To ensure optimal performance when running SharpNEAT, consider the following runtime and environment configurations:

    • Enable Server GC: Ensure Server Garbage Collection (GC) is enabled in your .NET runtime configuration to handle high-throughput workloads more efficiently.
    • Tiered Compilation: Be aware that .NET Core 3.0 and later versions use tiered compilation by default. This can affect how code is optimized over time.
    • OS Considerations: If you observe significant performance drops on Windows 11 (which may be related to OS updates or Spectre/Meltdown mitigations), it is recommended to run efficacy sampling and performance benchmarks on Linux to ensure more consistent results.
  4. Configure SharpNEAT experiment settings

    main

    SharpNEAT experiments are configured via JSON files. The configuration defines the neural network topology, evolution parameters, and the evaluation scheme. Key top-level settings include:

    • name: A descriptive name for the experiment.
    • description: A detailed description of the task.
    • isAcyclic: Boolean indicating if the network topology must be acyclic.
    • cyclesPerActivation: Number of cycles allowed per activation.
    • activationFnName: The name of the activation function to use (e.g., LeakyReLU).
    • populationSize: Total number of individuals in the population.
    • initialInterconnectionsProportion: The proportion of initial connections in the network.
    • connectionWeightScale: Scaling factor for connection weights.
    • degreeOfParallelism: Number of parallel processes for evolution.
    • enableHardwareAcceleratedNeuralNets: Enables hardware acceleration for the network.
    • enableHardwareAcceleratedActivationFunctions: Enables hardware acceleration for activation functions.
    {
      "name": "Generative Function Regression - Beat sine wave",
      "description": "Generative Function Regression: sin(x) + sin(x * 1.2) evaluated over interval [0, 16*PI]",
      "isAcyclic": false,
      "cyclesPerActivation": 1,
      "activationFnName": "LeakyReLU",
      "populationSize": 600,
      "initialInterconnectionsProportion": 0.05,
      "connectionWeightScale": 5.0,
      "degreeOfParallelism": 4,
      "enableHardwareAcceleratedNeuralNets": false,
      "enableHardwareAcceleratedActivationFunctions": false
    }
  5. Configure complexity regulation strategy

    main

    The complexityRegulationStrategy object manages the trade-off between network performance and structural complexity.

    Available keys:

    • strategyName: The name of the strategy to use (e.g., relative).
    • relativeComplexityCeiling: The maximum allowed complexity threshold for the relative strategy.
    • minSimplifcationGenerations: The minimum number of generations required before simplification logic is applied.
    "complexityRegulationStrategy": {
        "strategyName": "relative",
        "relativeComplexityCeiling": 10,
        "minSimplifcationGenerations": 10
    }
  6. Configure the Efficacy Sampler for Generative Function Regression

    main

    The EfficacySampler uses JSON configuration files to define the parameters for evolving neural networks to approximate specific functions. This configuration controls the evolution algorithm, asexual reproduction probabilities, recombination settings, population dynamics, and the specific evaluation scheme (e.g., the target function and sampling resolution).

    {
      "name": "Generative Function Regression - Beat sine wave",
      "description": "Generative Function Regression: sin(x) + sin(x * 1.2) evaluated over interval [0, 16*PI]",
      "isAcyclic": false,
      "cyclesPerActivation": 1,
      "activationFnName": "LeakyReLU",
      "evolutionAlgorithm": {
        "speciesCount": 20,
        "elitismProportion": 0.5,
        "selectionProportion": 0.5,
        "offspringAsexualProportion": 0.5,
        "offspringRecombinationProportion": 0.5,
        "interspeciesMatingProportion": 0.01
      },
      "asexualReproduction": {
        "connectionWeightMutationProbability": 0.94,
        "addNodeMutationProbability": 0.01,
        "addConnectionMutationProbability": 0.025,
        "deleteConnectionMutationProbability": 0.025
      },
      "recombination": {
        "secondaryParentGeneProbability": 0.1
      },
      "populationSize": 600,
      "initialInterconnectionsProportion": 0.05,
      "connectionWeightScale": 5.0,
      "complexityRegulationStrategy": {
        "strategyName": "relative",
        "relativeComplexityCeiling": 10,
        "minSimplifcationGenerations": 10
      },
      "degreeOfParallelism": 8,
      "enableHardwareAcceleratedNeuralNets": false,
      "enableHardwareAcceleratedActivationFunctions": false,
      "customEvaluationSchemeConfig": {
        "functionId": "BeatSinewave",
        "sampleIntervalMin": 0,
        "sampleIntervalMax": 50.265,
        "sampleResolution": 160,
        "gradientMseWeight": 0.9
      }
    }
  7. Configure the evolution algorithm parameters

    main

    The evolutionAlgorithm object controls how species are managed and how individuals are selected for reproduction:

    • speciesCount: The number of distinct species in the population.
    • elitismProportion: The proportion of the best individuals preserved directly to the next generation.
    • selectionProportion: The proportion of the population selected for reproduction.
    • offspringAsexualProportion: The proportion of offspring produced via asexual reproduction.
    • offspringRecombinationProportion: The proportion of offspring produced via recombination (sexual reproduction).
    • interspeciesMatingProportion: The probability of mating occurring between different species.
    "evolutionAlgorithm": {
        "speciesCount": 10,
        "elitismProportion": 0.2,
        "selectionProportion": 0.9,
        "offspringAsexualProportion": 0.5,
        "offspringRecombinationProportion": 0.5,
        "interspeciesMatingProportion": 0.01
    }
  8. Configure asexual reproduction and recombination

    main

    Mutation and recombination behaviors are defined in two separate objects:

    asexualReproduction:

    • connectionWeightMutationProbability: Probability of mutating a connection weight.
    • addNodeMutationProbability: Probability of adding a new node.
    • addConnectionMutationProbability: Probability of adding a new connection.
    • deleteConnectionMutationProbability: Probability of deleting an existing connection.

    recombination:

    • secondaryParentGeneProbability: Probability of incorporating genes from a secondary parent during recombination.
    "asexualReproduction": {
        "connectionWeightMutationProbability": 0.94,
        "addNodeMutationProbability": 0.01,
        "addConnectionMutationProbability": 0.025,
        "deleteConnectionMutationProbability": 0.025
    },
    "recombination": {
        "secondaryParentGeneProbability": 0.1
    }
  9. Configure the evolution algorithm settings

    main

    The evolutionAlgorithm object controls how the population evolves through species management and selection. Settings include:

    • speciesCount: The number of distinct species to maintain.
    • elitismProportion: The proportion of the best individuals preserved directly to the next generation.
    • selectionProportion: The proportion of individuals selected for reproduction.
    • offspringAsexualProportion: The proportion of new individuals created via asexual reproduction.
    • offspringRecombinationProportion: The proportion of new individuals created via recombination (mating).
    • interspeciesMatingProportion: The probability of mating occurring between different species.
    {
      "evolutionAlgorithm": {
        "speciesCount": 10,
        "elitismProportion": 0.2,
        "selectionProportion": 0.2,
        "offspringAsexualProportion": 0.5,
        "offspringRecombinationProportion": 0.5,
        "interspeciesMatingProportion": 0.01
      }
    }
  10. Configure the Prey Capture experiment

    main

    The prey-capture.config.json file defines the parameters for a specific neural network evolution experiment. It controls the network topology, hardware acceleration, evolution algorithm settings, mutation probabilities, complexity regulation, and the specific evaluation environment for the 'Prey Capture' task.

    {
      "name": "Prey Capture",
      "isAcyclic": false,
      "cyclesPerActivation": 1,
      "activationFnName": "LeakyReLU",
      "populationSize": 225,
      "initialInterconnectionsProportion": 0.05,
      "connectionWeightScale": 5.0,
      "degreeOfParallelism": 4,
      "enableHardwareAcceleratedNeuralNets": false,
      "enableHardwareAcceleratedActivationFunctions": false,
    
    "evolutionAlgorithm": {
        "speciesCount": 15,
        "elitismProportion": 0.66,
        "selectionProportion": 0.66,
        "offspringAsexualProportion": 0.5,
        "offspringRecombinationProportion": 0.5,
        "interspeciesMatingProportion": 0.01
      },
      "asexualReproduction": {
        "connectionWeightMutationProbability": 0.94,
        "addNodeMutationProbability": 0.01,
        "addConnectionMutationProbability": 0.025,
        "deleteConnectionMutationProbability": 0.025
      },
      "recombination": {
        "secondaryParentGeneProbability": 0.1
      },
      "complexityRegulationStrategy": {
        "strategyName": "relative",
        "relativeComplexityCeiling": 30,
        "minSimplifcationGenerations": 10
      },
      "customEvaluationSchemeConfig": {
        "preyInitMoves": 4,
        "preySpeed": 1.0,
        "sensorRange": 4.0,
        "maxTimesteps": 70,
        "trialsPerEvaluation": 100
      }
    }
  11. Configure custom evaluation schemes

    main

    The customEvaluationSchemeConfig object defines how the fitness of a neural network is calculated for specific tasks, such as function regression.

    • functionId: The identifier for the target function (e.g., Sin).
    • sampleIntervalMin: The start of the evaluation interval.
    • sampleIntervalMax: The end of the evaluation interval.
    • sampleResolution: The number of samples used for evaluation.
    • gradientMseWeight: The weight applied to the Mean Squared Error (MSE) of the gradient during evaluation.
    "customEvaluationSchemeConfig": {
        "functionId": "Sin",
        "sampleIntervalMin": 0,
        "sampleIntervalMax": 25.13274123,
        "sampleResolution": 80,
        "gradientMseWeight": 0.9
    }
  12. Configure the SharpNEAT experiment settings

    main

    The experiment configuration JSON defines the neural network architecture, evolution parameters, and hardware acceleration settings for a SharpNEAT run. Key top-level settings include:

    • name: A descriptive name for the experiment.
    • isAcyclic: Boolean indicating if the network structure must be acyclic.
    • cyclesPerActivation: Number of cycles allowed per activation.
    • activationFnName: The name of the activation function to use (e.g., LogisticSteep).
    • populationSize: Total number of individuals in the population.
    • initialInterconnectionsProportion: The initial density of connections.
    • connectionWeightScale: Scaling factor for connection weights.
    • degreeOfParallelism: Number of parallel processes for evolution.
    • enableHardwareAcceleratedNeuralNets: Enables hardware acceleration for the neural network computations.
    • enableHardwareAcceleratedActivationFunctions: Enables hardware acceleration for activation functions.
    {
      "name": "Cart-Pole Balancing (single pole)",
      "isAcyclic": false,
      "cyclesPerActivation": 1,
      "activationFnName": "LogisticSteep",
      "populationSize": 150,
      "initialInterconnectionsProportion": 0.05,
      "connectionWeightScale": 5.0,
      "degreeOfParallelism": 4,
      "enableHardwareAcceleratedNeuralNets": false,
      "enableHardwareAcceleratedActivationFunctions": false
    }