This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NeuralNetwork { | |
constructor( | |
inputNodes, | |
hiddenNodes, | |
outputNodes, | |
learningRate, | |
bias, | |
trainingCycles, | |
normalizer | |
) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
normalize(inputArray) { | |
return inputArray.map((input) => { | |
return input / this.normalizer | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
feedforward(inputArray) { | |
inputArray = this.normalize(inputArray) | |
const input = Matrix.fromArray(inputArray) | |
const hidden = this.calculateFF( | |
input, | |
this.weightsInputToHidden, | |
this.biasForHidden | |
) | |
const output = this.calculateFF( | |
hidden, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = class Matrix { | |
static randomNormalized() { | |
let r = 0 | |
const v = 4 | |
for (let i = v; i > 0; i--) { | |
r += Math.random() | |
} | |
return r / v | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GeneticAlgorithm { | |
constructor(phraseToGuess, populationSize, mutationRate, pow) { | |
this.phraseToGuess = phraseToGuess | |
this.populationSize = populationSize | |
this.mutationRate = mutationRate | |
this.pow = pow | |
this.population = [] | |
this.foundIndex = -1 | |
this.cycle = 0 | |
this.rateSum = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mutationRate = 0.01 | |
mutate(child) { | |
const chars = child.split('') | |
for (let i = 0; i < chars.length; i++) { | |
if (Math.random() <= mutationRate) { | |
chars[i] = newChar() | |
} | |
} | |
return chars.join('') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
crossover(a, b) { | |
const midpoint = Math.floor(Math.random() * (phraseToGuess.length - 2)) + 1 | |
let crossed = '' | |
for (let i = 0; i < phraseToGuess.length; i++) { | |
if (i <= midpoint) { | |
crossed += a[i] | |
} else { | |
crossed += b[i] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
selection() { | |
let index = 0 | |
let random = Math.random() | |
while (random > 0) { | |
random = random - population[index].powRate | |
index++ | |
} | |
index-- // index++ jumped to the next phrase | |
return population[index] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DNAchanges() { | |
population.forEach((_, index) => { | |
const a = selection() | |
const b = selection() | |
let child = crossover(a.randomPhrase, b.randomPhrase) | |
child = mutate(child) | |
population[index].randomPhrase = child | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
normalization() { | |
let powSum = 0 | |
population.forEach((p, i) => { | |
powSum += population[i].powRate | |
}) | |
population.forEach((p, i) => { | |
population[i].powRate = p.powRate / powSum | |
}) | |
} |