-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tom Hohler
committed
Apr 16, 2018
1 parent
0d3002a
commit c4bb1d8
Showing
5 changed files
with
60 additions
and
48 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,62 @@ | ||
import { Selection } from "./genetic/methods"; | ||
import { Candidate, Population } from "./genetic/model"; | ||
import { Network } from "./neural-network/network"; | ||
|
||
const fitness = (nn: Network) => () => { | ||
// console.log("FITNESS"); | ||
// const xorFF = nn.activate([0, 0])[0]; | ||
// const errFF = 1 / (1 - xorFF); | ||
// // console.log(xorFF); | ||
const xorFT = nn.activate([0, 1])[0]; | ||
// const errFT = 1 / xorFT; | ||
// // console.log(xorFT); | ||
// const xorTF = nn.activate([1, 0])[0]; | ||
// const errTF = 1 / xorFT; | ||
// // console.log(xorTF); | ||
// const xorTT = nn.activate([1, 1])[0]; | ||
// const errTT = 1 / (1 - xorFF); | ||
// // console.log(xorTT); | ||
// // console.log(1 - xorFF + xorFT + xorTF + 1 - xorTT); | ||
// console.log(errFF, errFT, errTF, errTT); | ||
// return Math.pow(1 / (errFF + errFT + errTF + errTT), 2); | ||
return xorFT; | ||
|
||
const PASSWORD = "hardcorepasswordmasterrace"; | ||
const fitness = (password: string) => (genes: string[]) => { | ||
const passwordArray = password.split(""); | ||
if (genes.length !== password.length) { | ||
throw Error("passwords length not matching"); | ||
} | ||
let score = 0; | ||
for (let i = 0; i < genes.length; i++) { | ||
if (genes[i] === passwordArray[i]) { | ||
score += 1; | ||
} | ||
} | ||
return score * 100 / password.length; | ||
}; | ||
|
||
function mutate(weights: number[]) { | ||
const weightsCopy = weights.slice(); | ||
const indexMutated = Math.floor(Math.random() * weightsCopy.length); | ||
weightsCopy[indexMutated] = Math.random() * 2 - 1; | ||
return weightsCopy; | ||
} | ||
const mutate = (password: string) => (genes: string[]) => { | ||
const genesCopy = genes.slice(); | ||
const mutatedIndex = Math.floor(Math.random() * PASSWORD.length); | ||
genesCopy[mutatedIndex] = getRandomLetter(); | ||
return genesCopy; | ||
}; | ||
|
||
// Create the networks | ||
const candidates = []; | ||
|
||
for (let i = 0; i < 20; i ++) { | ||
const nn = Network.perceptron([2, 4, 3, 1]); | ||
const candidate = new Candidate(nn.weights, { | ||
fitness: fitness(nn), | ||
mutate, | ||
mutationProbability: 1 | ||
}); | ||
for (let i = 0; i < 100; i ++) { | ||
const genes = generateRandomGenes(); | ||
const candidate = new Candidate<string>( | ||
generateRandomGenes(), | ||
{ | ||
fitness: fitness(PASSWORD), | ||
mutate: mutate(PASSWORD), | ||
mutationProbability: 0.4 | ||
} | ||
); | ||
candidates.push(candidate); | ||
} | ||
|
||
const initialPop = new Population(candidates); | ||
|
||
let pop = initialPop; | ||
|
||
for (let i = 0; i < 3; i ++) { | ||
if (true) { | ||
console.table(pop.candidates.map(it => it.fitness([]))); | ||
} | ||
for (let i = 0; i < 100; i ++) { | ||
document.getElementById("main")!.innerHTML | ||
+= ("<br/><br />" + pop.candidates[0].fitness([])); | ||
+= ("<br/><br />" + pop.candidates[0].genes + ", fitness: " + pop.candidates[0].fitness(pop.candidates[0].genes)); | ||
pop = pop.createNextGeneration(); | ||
} | ||
|
||
function generateRandomGenes() { | ||
const genes = []; | ||
for (let i = 0; i < PASSWORD.length; i++) { | ||
genes.push(getRandomLetter()); | ||
} | ||
return genes; | ||
} | ||
|
||
function getRandomLetter() { | ||
return String.fromCharCode(97 + Math.floor(Math.random() * 26)); | ||
} |
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