Skip to content

Commit 4097cdf

Browse files
committed
simplifying steering example
1 parent e1de9e9 commit 4097cdf

File tree

4 files changed

+3
-80
lines changed

4 files changed

+3
-80
lines changed

examples/NeuroEvolution-steering/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>ml5.js NeuroEvolution Steering</title>
8-
<script src="p5.min.js"></script>
8+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js"></script>
99
<script src="../../dist/ml5.js"></script>
1010
</head>
1111
<body>

examples/NeuroEvolution-steering/p5.min.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/NeuroEvolution-steering/sketch.js

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,51 @@
11
let vehicles = [];
22
let timeSlider;
33
let lifetime = 0;
4-
54
let target;
65

76
function setup() {
87
createCanvas(640, 240);
9-
// Initialize a set number of vehicles
8+
ml5.tf.setBackend("cpu");
109
for (let i = 0; i < 50; i++) {
1110
vehicles[i] = new Vehicle(random(width), random(height));
1211
}
13-
ml5.tf.setBackend("cpu");
14-
1512
target = createVector(random(width), random(height));
1613
timeSlider = createSlider(1, 20, 1);
1714
}
1815

1916
function draw() {
2017
background(255);
21-
2218
for (let i = 0; i < timeSlider.value(); i++) {
2319
for (let v of vehicles) {
24-
v.edges();
2520
v.think(target);
2621
v.eat(target);
2722
v.update();
2823
}
2924
lifetime++;
3025
}
3126

32-
let best = null;
33-
let record = -1;
3427
for (let v of vehicles) {
3528
v.show();
36-
if (v.fitness > record) {
37-
record = v.fitness;
38-
best = v;
39-
}
4029
}
4130

42-
best.show();
31+
fill(0, 0, 255, 150);
4332
noStroke();
44-
fill(255, 0, 0, 100);
45-
circle(best.position.x, best.position.y, 64);
46-
fill(0);
47-
text(best.fitness, best.position.x + 12, best.position.y);
48-
fill(0, 0, 255, 100);
4933
circle(target.x, target.y, 32);
5034

51-
stroke(0, 100);
52-
strokeWeight(1);
53-
//line(target.x, target.y, best.position.x, best.position.y);
54-
5535
if (lifetime > 500) {
5636
normalizeFitness();
5737
reproduction();
5838
target = createVector(random(width), random(height));
5939
lifetime = 0;
6040
}
61-
// if (allVehiclesDead()) {
62-
// }
63-
6441
fill(0);
6542
text(lifetime, 10, 30);
6643
}
6744

68-
// function allVehiclesDead() {
69-
// for (let v of vehicles) {
70-
// if (v.alive) {
71-
// return false;
72-
// }
73-
// }
74-
// return true;
75-
// }
76-
7745
function normalizeFitness() {
78-
let recordFitness = 0;
7946
for (let v of vehicles) {
80-
if (v.fitness > recordFitness) {
81-
recordFitness = v.fitness;
82-
}
8347
v.fitness = pow(2, v.fitness);
8448
}
85-
console.log(recordFitness);
86-
8749
let sum = 0;
8850
for (let v of vehicles) {
8951
sum += v.fitness;

examples/NeuroEvolution-steering/vehicle.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class Vehicle {
2323
let d = p5.Vector.dist(this.position, food);
2424
if (d < 10) {
2525
this.fitness++;
26-
// this.position = createVector(random(width), random(height));
2726
target = createVector(random(width), random(height));
2827
}
2928
}
@@ -35,10 +34,6 @@ class Vehicle {
3534

3635
// Predicting the force to apply
3736
const outputs = this.brain.predictSync(inputs);
38-
// let x = 2 * outputs[0].value - 1;
39-
// let y = 2 * outputs[1].value - 1;
40-
// let force = createVector(x, y); //.setMag(magnitude);
41-
// force.setMag(1);
4237
let angle = outputs[0].value * TWO_PI;
4338
let magnitude = outputs[1].value;
4439
let force = p5.Vector.fromAngle(angle).setMag(magnitude);
@@ -54,32 +49,13 @@ class Vehicle {
5449
this.position.add(this.velocity);
5550
// Reset acceleration to 0 each cycle
5651
this.acceleration.mult(0);
57-
58-
// if (
59-
// this.position.x > width ||
60-
// this.position.y > height ||
61-
// this.position.x < 0 ||
62-
// this.position.y < 0
63-
// ) {
64-
// this.alive = false;
65-
// }
6652
}
6753

6854
applyForce(force) {
6955
// We could add mass here if we want A = F / M
7056
this.acceleration.add(force);
7157
}
7258

73-
// Wraparound
74-
edges() {
75-
// this.position.x = constrain(this.position.x, 0, width);
76-
// this.position.y = constrain(this.position.y, 0, height);
77-
//if (this.position.x < -this.r) this.position.x = width + this.r;
78-
//if (this.position.y < -this.r) this.position.y = height + this.r;
79-
//if (this.position.x > width + this.r) this.position.x = -this.r;
80-
//if (this.position.y > height + this.r) this.position.y = -this.r;
81-
}
82-
8359
show() {
8460
//{!1} Vehicle is a triangle pointing in the direction of velocity
8561
let angle = this.velocity.heading();
@@ -94,18 +70,5 @@ class Vehicle {
9470
vertex(-this.r * 2, this.r);
9571
endShape(CLOSE);
9672
pop();
97-
98-
//fill(0);
99-
//noStroke();
100-
//text(this.fitness, this.position.x + 12, this.position.y);
101-
102-
// let d = p5.Vector.dist(this.position, this.target);
103-
// if (d < 50) {
104-
// stroke(0, 200);
105-
// strokeWeight(1);
106-
// line(this.position.x, this.position.y, this.target.x, this.target.y);
107-
// fill(0, 255, 0, 100);
108-
// circle(this.target.x, this.target.y, 8);
109-
//}
11073
}
11174
}

0 commit comments

Comments
 (0)