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 | |
| } |
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
| // load each line of the training data into a new index of an array | |
| var trainingFile = fs.readFileSync("data.csv").toString().split("\n") | |
| // iterate over each line of the training data, adding it to a new array for training later | |
| for (var i = 0; i < trainingFile.length; i++) { | |
| var entry = trainingFile[i] | |
| var values = entry.split(",") | |
| var points = [parseFloat(values[0]), parseFloat(values[1]), parseFloat(values[2]), parseFloat(values[3])] | |
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
| // console log the output of each iteration | |
| net.train(trainingData, { | |
| log: true | |
| }) | |
| // test data - actually virginica | |
| var output = net.run([6.2,3.4,5.1,2.6]) | |
| console.log(output) |
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 | |
| } |
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
| <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
| 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
| 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 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
| 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() |