Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several changes for smoother results and more control over the NN. #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions week10/neuroevolution-steering/nn/nn.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,25 @@ class NeuralNetwork {
}

mutate(rate) {
// This is how we adjust weights ever so slightly
function mutate(x) {
if (Math.random() < rate) {
var offset = randomGaussian() * 0.5;
// var offset = random(-0.1, 0.1);
var newx = x + offset;
return newx;
} else {
return x;
// Check if this should be mutated at all
if (Math.random() < rate) {
// This is how we adjust weights ever so slightly
function mutate(x) {
// Mutate only so much of the values
if (Math.random() < rate) {
var offset = randomGaussian() * 0.5;
// var offset = random(-0.1, 0.1);
var newx = x + offset;
return newx;
} else {
return x;
}
}
this.weights_ih.map(mutate);
this.weights_ho.map(mutate);
this.bias_h.map(mutate);
this.bias_o.map(mutate);
}
this.weights_ih.map(mutate);
this.weights_ho.map(mutate);
this.bias_h.map(mutate);
this.bias_o.map(mutate);
}



}
11 changes: 8 additions & 3 deletions week10/neuroevolution-steering/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let foodBuffer = 50;
// How many sensors does each vehicle have?
let totalSensors = 8;
// How far can each vehicle see?
let sensorLength = 150;
let sensorLength = 50;
// What's the angle in between sensors
let sensorAngle = (Math.PI * 2) / totalSensors;

Expand Down Expand Up @@ -94,14 +94,19 @@ function draw() {
if (population.length < 20) {
for (let v of population) {
// Every vehicle has a chance of cloning itself according to score
// Argument to "clone" is probability
let newVehicle = v.clone(0.1 * v.score / record);
let probability = 0.1 * v.score / record;
let newVehicle = v.clone(probability);
// If there is a child
if (newVehicle != null) {
population.push(newVehicle);
}
}
}
// Make sure we never run out of vehicles, but favor reproduction
if (population.length <= 2) {
let vehicle = new Vehicle();
population.push(vehicle);
}
}

// Draw all the food
Expand Down
18 changes: 10 additions & 8 deletions week10/neuroevolution-steering/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Vehicle {
this.velocity = createVector();
this.position = createVector(random(width), random(height));
this.r = 4;
this.maxforce = 0.1;
this.maxforce = 0.2;
this.maxspeed = 4;
this.minspeed = 0.25;
this.maxhealth = 3;
Expand All @@ -42,7 +42,8 @@ class Vehicle {
// If a brain is passed via constructor copy it
if (brain) {
this.brain = brain.copy();
this.brain.mutate(0.1);
// Mutation rate is set quite high because there is no crossover
this.brain.mutate(0.25);
// Otherwise make a new brain
} else {
// inputs are all the sensors plus position and velocity info
Expand All @@ -52,7 +53,7 @@ class Vehicle {
this.brain = new NeuralNetwork(inputs, 32, 2);
}

// Health keeps vehicl alive
// Health keeps vehicle alive
this.health = 1;
}

Expand Down Expand Up @@ -135,11 +136,12 @@ class Vehicle {

// Create inputs
let inputs = [];
// This is goofy but these 4 inputs are mapped to distance from edges
inputs[0] = constrain(map(this.position.x, foodBuffer, 0, 0, 1), 0, 1);
inputs[1] = constrain(map(this.position.y, foodBuffer, 0, 0, 1), 0, 1);
inputs[2] = constrain(map(this.position.x, width - foodBuffer, width, 0, 1), 0, 1);
inputs[3] = constrain(map(this.position.y, height - foodBuffer, height, 0, 1), 0, 1);
// These inputs are the location of the vehicle
inputs[0] = this.position.x / width;
inputs[1] = this.position.y / height;
// These inputs are the distance of the vehicle to east- and west borders
inputs[2] = 1 - inputs[0];
inputs[3] = 1 - inputs[1];
// These inputs are the current velocity vector
inputs[4] = this.velocity.x / this.maxspeed;
inputs[5] = this.velocity.y / this.maxspeed;
Expand Down