I am making an ai that will learn by its self to get to a specific point in the best possible way.
I am doing this using atom and a life server to google.
The error says that life is undefined but when I look at my code then is life first set to 0 and then every frame it increments by one.
Also: I'm using a library called p5.js
Can someone please help me. I'm getting a little bit depressed about this project. This is the code:
function setup() {
createCanvas(400, 300);
population = new Population();
// countP = createP();
dna = new Dna();
}
function draw() {
background(0);
population.run();
// lifeP.html(count);
// count++;
}
//ROCKET OBJECT
function Rocket() {
let pos = createVector(width / 2, height);
let vel = createVector();
let acc = createVector();
this.applyForce = function(force) {
acc.add(force);
}
this.update = function() {
let index = 0;
this.applyForce(dna.genes[index]);
index++;
vel.add(acc);
pos.add(vel);
acc.mult(0);
}
this.show = function() {
push();
translate(pos.x, pos.y);
rectMode(CENTER);
rotate(vel.heading());
fill(255, 150);
noStroke();
rect(0, 0, 50, 10);
pop();
}
}
//POPULATION OBJECT
function Population() {
let rockets = [];
let popsize = 25
for (let i = 0; i < popsize; i++) {
rockets[i] = new Rocket();
}
this.run = function() {
for (let i = 0; i < popsize; i++) {
rockets[i].update();
rockets[i].show();
}
}
}
//DNA OBJECT
function Dna() {
let genes = [];
for (let i = 0; i < lifespan; i++) {
genes[i] = p5.Vector.random2D();
}
}
This is the error: Uncaught TypeError:
Cannot read property '0' of undefined
at Rocket.update (sketch.js:34)
at Population.run (sketch.js:64)
at draw (sketch.js:16)
at p5.redraw (p5.js:51956)
at p5.<anonymous> (p5.js:46250)
at p5.<anonymous> (p5.js:46152)
at new p5 (p5.js:46434)
at _globalInit (p5.js:48404)
Rocket.update @ sketch.js:34
Population.run @ sketch.js:64
draw @ sketch.js:16
p5.redraw @ p5.js:51956
(anonymous) @ p5.js:46250
(anonymous) @ p5.js:46152
p5 @ p5.js:46434
_globalInit @ p5.js:48404
load (async)
26.../core/core @ p5.js:48413
s @ p5.js:2
(anonymous) @ p5.js:2
13../color/creating_reading @ p5.js:42088
s @ p5.js:2
e @ p5.js:2
(anonymous) @ p5.js:2
(anonymous) @ p5.js:2
(anonymous) @ p5.js:2