Skip to content

Commit

Permalink
Add compatibility threshold adjustment.
Browse files Browse the repository at this point in the history
  • Loading branch information
We-Gold committed Aug 2, 2023
1 parent 94bb0c9 commit d82381d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ The following is the default configuration of TinyNEAT. For any changes, for exa
```js
// Closely based on parameters of the original NEAT paper
export const defaultConfig = {
initialPopulationSize: 150, // Number of networks in the population
initialPopulationSize: 50, // Number of networks in the population
targetSpecies: 10, // Desired number of species to maintain
maxGenerations: 100, // Stopping point for evolution

maximumStagnation: 15, // Maximum number of generations a species is allowed to stay the same fitness before it is removed
excessCoefficient: 1.0, // Coefficient representing how important excess genes are in measuring compatibility
disjointCoefficient: 1.0, // Coefficient for disjoint genes
weightDifferenceCoefficient: 0.4, // Coefficient for average weight difference (highly recommended for tuning)
compatibilityThreshold: 3.0, // Threshold for speciation (highly recommended for tuning)
excessCoefficient: 2.0, // Coefficient representing how important excess genes are in measuring compatibility
disjointCoefficient: 2.0, // Coefficient for disjoint genes
weightDifferenceCoefficient: 1.0, // Coefficient for average weight difference (highly recommended for tuning)
compatibilityThreshold: 6.0, // Threshold for speciation (highly recommended for tuning)
compatibilityModifier: 0.3, // Rate to change the compatibility threshold at when target species count is not met
survivalThreshold: 0.2, // Percentage of each species allowed to reproduce
mutateOnlyProbability: 0.25, // Probability that a reproduction will only result from mutation and not crossover
mateOnlyProbability: 0.2, // Probability an offspring will be created only through crossover without mutation
Expand All @@ -139,15 +141,15 @@ export const defaultConfig = {
mateByAveragingProbability: 0.4, // Probability that matching genes will be averaged during crossover
reenableConnectionProbability: 0.01, // Probability that a connection is randomly reenabled during crossover

fitnessSort: "max", // Whether a higher or a lower fitness is better
fitnessSort: "max",

largeNetworkSize: 20, // A network with this many genes is considered to be large
minimumSpeciesSize: 2, // The minimum number of offspring a species can have
minimumSpeciesSize: 1, // The minimum number of offspring a species can have

hallOfFameSize: 10, // The number of top-performing individuals to store

inputSize: 3, // The number of inputs to each neural network
outputSize: 2, // The number of outputs of each neural network
inputSize: 1, // The number of inputs to each neural network
outputSize: 1, // The number of outputs of each neural network

// Plugin for the specific type of neural network (ANN, RNN, etc)
nnPlugin: ANNPlugin(),
Expand Down
2 changes: 1 addition & 1 deletion examples/racing/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const tn = TinyNEAT({
initialPopulationSize: POPULATION_SIZE,
inputSize: 16,
outputSize: 1,
compatibilityThreshold: 2.0,
compatibilityThreshold: 6.0,
addLinkProbability: 0.1,
addNodeProbability: 0.2,
mutateWeightProbability: 0.4,
Expand Down
18 changes: 10 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ export type FitnessSort = "max" | "min"

// Closely based on parameters of the original NEAT paper
export const defaultConfig = {
initialPopulationSize: 150, // Number of networks in the population
initialPopulationSize: 50, // Number of networks in the population
targetSpecies: 10, // Desired number of species to maintain
maxGenerations: 100, // Stopping point for evolution

maximumStagnation: 15, // Maximum number of generations a species is allowed to stay the same fitness before it is removed
excessCoefficient: 1.0, // Coefficient representing how important excess genes are in measuring compatibility
disjointCoefficient: 1.0, // Coefficient for disjoint genes
weightDifferenceCoefficient: 0.4, // Coefficient for average weight difference (highly recommended for tuning)
compatibilityThreshold: 3.0, // Threshold for speciation (highly recommended for tuning)
excessCoefficient: 2.0, // Coefficient representing how important excess genes are in measuring compatibility
disjointCoefficient: 2.0, // Coefficient for disjoint genes
weightDifferenceCoefficient: 1.0, // Coefficient for average weight difference (highly recommended for tuning)
compatibilityThreshold: 6.0, // Threshold for speciation (highly recommended for tuning)
compatibilityModifier: 0.3, // Rate to change the compatibility threshold at when target species count is not met
survivalThreshold: 0.2, // Percentage of each species allowed to reproduce
mutateOnlyProbability: 0.25, // Probability that a reproduction will only result from mutation and not crossover
mateOnlyProbability: 0.2, // Probability an offspring will be created only through crossover without mutation
Expand All @@ -27,12 +29,12 @@ export const defaultConfig = {
fitnessSort: <FitnessSort>"max",

largeNetworkSize: 20, // A network with this many genes is considered to be large
minimumSpeciesSize: 2, // The minimum number of offspring a species can have
minimumSpeciesSize: 1, // The minimum number of offspring a species can have

hallOfFameSize: 10, // The number of top-performing individuals to store

inputSize: 3, // The number of inputs to each neural network
outputSize: 2, // The number of outputs of each neural network
inputSize: 1, // The number of inputs to each neural network
outputSize: 1, // The number of outputs of each neural network

// Plugin for the specific type of neural network (ANN, RNN, etc)
nnPlugin: ANNPlugin(),
Expand Down
16 changes: 16 additions & 0 deletions src/evolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const evolvePopulation = (
// Separate the current population into species
speciatePopulation(population, species, config)

// Adjust the compatibility threshold based on the number of species
adjustCompatibilityThreshold(species, config)

// Adjust the fitness of each organism to normalize based on species size
calculateAdjustedFitnesses(species)

Expand Down Expand Up @@ -160,3 +163,16 @@ const allocateOffspring = (population: Genome[], species: Genome[][]) => {
Math.round((averageFitness / totalAverageFitness) * population.length),
)
}

const adjustCompatibilityThreshold = (species: Genome[][], config: Config) => {
// Modify the compatibility threshold to dynamically control the number of species
if (species.length < config.targetSpecies) {
config.compatibilityThreshold -= config.compatibilityModifier
} else if (species.length > config.targetSpecies) {
config.compatibilityThreshold += config.compatibilityModifier
}

// Prevent the compatibility threshold from getting too small
if (config.compatibilityThreshold < config.compatibilityModifier)
config.compatibilityThreshold = config.compatibilityModifier
}

0 comments on commit d82381d

Please sign in to comment.