This file contains hidden or 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
var population | |
var lifespan = 200 | |
var visualLifespan | |
var count = 0 | |
var target | |
function setup() { | |
createCanvas(400, 300) | |
population = new Population() |
This file contains hidden or 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.selection = function() { | |
var newSticks = []; | |
for (var i = 0; i < this.sticks.length; i++) { | |
var parentA = random(this.pool).dna; | |
var parentB = random(this.pool).dna; | |
var child = parentA.crossover(parentB); | |
//child.mutation(); |
This file contains hidden or 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.evaluate = function() { | |
var maxFitness = 0 | |
for(var i = 0; i < this.populationSize; i++) { | |
this.sticks[i].calculateFitness() // the distance from the target relative to current pos | |
if (this.sticks[i].fitness > maxFitness) { | |
maxFitness = this.sticks[i].fitness | |
} | |
} | |
for(var i = 0; i < this.populationSize; i++) { |
This file contains hidden or 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
var population | |
var lifespan = 200 // this will be used to determine how long a stick lives for, and how many genes it has | |
var visualLifespan // helper to draw to screen | |
var count = 0 // incrementally measures lifespan | |
var target // my circle, that is a circle and represents nothing else metaphorically | |
function setup() { | |
createCanvas(800, 600) | |
population = new Population() |
This file contains hidden or 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
function dna() { | |
this.genes = [] | |
for (var i = 0; i < lifespan; i++) { | |
this.genes[i] = p5.Vector.random2D() | |
this.genes[i].setMag(0.1) | |
} | |
} |
This file contains hidden or 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
function Population() { | |
this.sticks = [] | |
this.populationSize = 25 | |
for(var i = 0; i < this.populationSize; i++) { | |
this.sticks[i] = new Stick() | |
} | |
this.run = function() { | |
for(var i = 0; i < this.populationSize; i++) { |
This file contains hidden or 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
function Stick() { | |
this.pos = createVector(width/2, height) // the position of the stick | |
this.vel = createVector() // the velocity of the stick | |
this.acc = createVector() // the acceleration rate of the stick | |
this.dna = new dna() // one strand of DNA to last the lifespan of the stick | |
this.applyForce = function(force) { | |
this.acc.add(force) // this allows for movement | |
} |
This file contains hidden or 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
<body style="background-color: #d9d9d9"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.js"></script> | |
<script src="./main.js"></script> | |
<script src="./stick.js"></script> | |
<script src="./population.js"></script> | |
<script src="./dna.js"></script> | |
</body> |
This file contains hidden or 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
5.1 | 3.5 | 1.4 | 0.2 | |
---|---|---|---|---|
4.9 | 3.0 | 1.4 | 0.2 | |
4.7 | 3.2 | 1.3 | 0.2 | |
4.6 | 3.1 | 1.5 | 0.2 | |
5.0 | 3.6 | 1.4 | 0.2 | |
5.4 | 3.9 | 1.7 | 0.4 | |
4.6 | 3.4 | 1.4 | 0.3 | |
5.0 | 3.4 | 1.5 | 0.2 | |
4.4 | 2.9 | 1.4 | 0.2 | |
4.9 | 3.1 | 1.5 | 0.1 |
This file contains hidden or 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
var brain = require('brain.js') | |
var fs = require('fs') | |
// configuration to be used in the brain | |
const config = { | |
binaryThresh: 0.5, // arbitary value | |
hiddenLayers: [3], // the size of the hidden layers in the network | |
activation: 'sigmoid' // activation function | |
} |