Skip to content

Instantly share code, notes, and snippets.

View kmaher9's full-sized avatar
🥑
Focusing

Kieran Maher kmaher9

🥑
Focusing
View GitHub Profile
var population
var lifespan = 200
var visualLifespan
var count = 0
var target
function setup() {
createCanvas(400, 300)
population = new Population()
@kmaher9
kmaher9 / select.js
Last active September 9, 2018 01:32
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.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++) {
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()
function dna() {
this.genes = []
for (var i = 0; i < lifespan; i++) {
this.genes[i] = p5.Vector.random2D()
this.genes[i].setMag(0.1)
}
}
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++) {
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
}
@kmaher9
kmaher9 / main.html
Last active September 8, 2018 23:59
<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>
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
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
}