Skip to content

Commit

Permalink
Modify NEAT connection mutation helper to stay within -1 to 1 range
Browse files Browse the repository at this point in the history
  • Loading branch information
christianechevarria committed Jan 11, 2020
1 parent 6a9d651 commit 5c9a71a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/architecture/population.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,21 @@ const Population = function(options) {

// Helper function, mutates connection weights
const mutateConn = function(conn) {
// 90% chance slight change | 10% chance new random value, between -1 and 1
conn.weight = (random < .9) ? conn.weight + Math.random() : Math.random() * (Math.floor(Math.random()*2) == 1) ? 1 : -1;

const number = () => Math.random() * (Math.floor(Math.random()*2) == 1) ? 1 : -1;

// Ensure weight stays in [-1, 1] range
const clamp = function(number) {
const large = (number > 1);
const small = (number < -1);

if(large) return 1;
if(small) return -1;
else return number;
}

// 90% chance slight change, +-0.5 at most | 10% chance new random value, between -1 and 1
conn.weight = (random < .9) ? clamp(conn.weight + number() / 2) : number();

return conn
}
Expand Down

0 comments on commit 5c9a71a

Please sign in to comment.