Commit 5362c1f8 authored by Pablo R. Mier's avatar Pablo R. Mier

add custom mean to settings.json

parent ff84b5ac
......@@ -5,8 +5,10 @@
var invadersApp = invadersApp || {};
invadersApp.evolution = {
evolve: function(pool, genes, mutation){
if (mutation === undefined) { mutation = 0.1 };
evolve: function(pool, genes, mutation, effect){
// Load from settings
if (mutation === undefined) { mutation = 0.1 }
if (effect === undefined) { effect = 0.5 }
var offspring = [];
pool.forEach(function (pair) {
var p1 = pair[0];
......@@ -22,7 +24,10 @@ invadersApp.evolution = {
children[gen] = Math.random() * (max - min) + min;
if (Math.random() < mutation) {
// Mutate this gene
children[gen] = Math.random() * (genes[gen].max - genes[gen].min) + genes[gen].min;
var value = chance.pickone([-1.0, 1.0]) * Math.random() * (genes[gen].max - genes[gen].min) * effect;
children[gen] = children[gen] + value;
if (children[gen] < genes[gen].min) children[gen] = genes[gen].min;
if (children[gen] > genes[gen].max) children[gen] = genes[gen].max;
}
}
}
......
......@@ -14,8 +14,13 @@ invadersApp.Invader = function (ctx, genes, x, y) {
this.genes = genes || function () {
var settings = ctx.settings;
var genes = {};
for(var gene in settings.genes){
genes[gene] = game.rnd.realInRange(settings.genes[gene].min, settings.genes[gene].max);
for(var gen in settings.genes){
var min = settings.genes[gen].min;
var max = settings.genes[gen].max;
//genes[gen] = chance.normal({mean: ((max-min)/2)+min, dev: (max-min)/6});
genes[gen] = chance.normal({mean: settings.genes[gen].mean, dev: (max-min)/6});
if (genes[gen] < min) genes[gen] = min;
if (genes[gen] > max) genes[gen] = max;
}
return genes;
}();
......@@ -38,7 +43,7 @@ invadersApp.Invader = function (ctx, genes, x, y) {
// Create a shield
this.shieldGraphics = this.game.make.graphics(0,0);
// Add the invader to the game (move this outside this class?)
game.add.existing(this);
};
......
......@@ -3,27 +3,33 @@
"genes": {
"scale": {
"min": 1,
"max": 7
"max": 7,
"mean": 4
},
"alpha": {
"min": 5,
"max": 255
"max": 255,
"mean": 200
},
"xvelocity": {
"min": 0,
"max": 300
"max": 300,
"mean": 30
},
"yvelocity": {
"min": 0,
"max": 300
"max": 300,
"mean": 30
},
"x_prob_change_dir": {
"min": 0.00,
"max": 0.10
"max": 0.10,
"mean": 0.01
},
"y_prob_change_dir": {
"min": 0.00,
"max": 0.10
"max": 0.10,
"mean": 0.01
}
},
"mutation_probability": 0.1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment