Skip to content

Instantly share code, notes, and snippets.

@esshka
Created October 26, 2023 17:49
Show Gist options
  • Save esshka/b407fb6dbc27e9505651b653521ad71b to your computer and use it in GitHub Desktop.
Save esshka/b407fb6dbc27e9505651b653521ad71b to your computer and use it in GitHub Desktop.
base fitness function for NEAT
const createFitnessFunction = (dataset, { repeat = 1, cost }) => {
return network => {
let score = 0
for (let i = 0; i < repeat; i++) {
const result = network.test(dataset, cost)
if (!result || typeof result.error === 'undefined') {
throw new Error('The test method did not return a valid result with an error property.')
}
score -= result.error
}
score -= network.topologySize * growthPenalty
score = isNaN(score) ? -Infinity : score
return score / repeat
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment