javascript - Rockets are not changing direction after mutation -


i have problem machine learning stuff. after first gen rockets not mutating , follow same path. tried different ways solve can see neither of them work. wrote in openprocessing.org tried on local. don`t see problem buti think in rocket constructor function.

var rockets = [];  var bf = -1;  var br;    function setup() {    createcanvas(windowwidth, windowheight);    background(100);    nostroke();    fill(255, 50);    framerate(10);    target = createvector(width, height / 2);    (var = 0; < 10; i++) {      rockets.push(new rocket());    }  }    function draw() {    background(100);    ellipse(width / 2, height / 2, 40, 40);    ellipse(width - 10, height / 2, 40, 40);    (i = 0; < rockets.length; i++) {      rockets[i].show();      //console.log(rockets[i].pos.y);      if (!rockets[i].killed) {        rockets[i].applyforce(); //it fittness counter inside      }    }    //console.log(rockets);    if (rockets[0].counter >= rockets[0].route.length) {      findbest();      respawn();    }  }    function respawn() {    var news = mutate(br);    (var = 0; < 10; i++) {      rockets.push(new rocket());      //console.log(rockets);      rockets[i].route = news[i];    }  }    function findbest() {    (var = 0; < rockets.length; i++) {      //console.log(rockets[i].fittness);      if (rockets[i].fittness > bf) {        br = rockets[i].route;        bf = rockets[i].fittness;      }    }    rockets = [];  }    function mutate(arr) {    var news = [];    (var = 0; < 10; i++) {      var temparr = arr;      temparr[floor(random(arr.lenth))] = p5.vector.random2d().setmag(30);      news.push(temparr);    }    return news;  }    function rocket(a) {    this.pos = createvector(width / 2, height / 2);    this.route = [];    this.counter = 0;    this.fittness = 0;    this.killed = false;    //console.log(a);    if (!a) {      (var = 0; < 20; i++) {        this.route.push(p5.vector.random2d());        this.route[i].setmag(20);        //console.log("!a");        // console.log(i);      }    } else {      // not used       this.route = a;      // for(var = floor(random(a.length-1)); i<a.length; i++) {      //console.log(this.route[i]);      b = floor(random(this.route.length - 1));      console.log("before: ", this.route[b]);      this.route[b] = p5.vector.random2d().setmag(30);       //this.route[i].rotate(0.2*pow(-1, i));      console.log("after: ", this.route[b]);      //}    }      //console.log(mutate(this.route));    this.applyforce = function() {      if (this.counter == this.route.length - 1) {        this.countfittness();        //console.log(this.fittness);        this.killed = true;      }      this.pos.add(this.route[this.counter]);      this.counter++;    }    this.countfittness = function() {        this.fittness = 1 / this.pos.dist(target) * 100;      //console.log(this.fittness);    }    this.show = function() {      ellipse(this.pos.x, this.pos.y, 10, 10);      }  }
<html>    <head>    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/p5.js"></script>  </head>    </html>

nice code. seems there error in mutate function:

function mutate(arr) {   var news = [];   (var = 0; < 10; i++) {     var temparr = arr;     temparr[floor(random(arr.length))] = p5.vector.random2d().setmag(30);     news.push(temparr);   }   return news; } 

when use

var temparr = arr; 

in javascript not copy of array. both arrays point same reference. when mutate 1 original mutates. way of routes rockets turn out same.

try ...

var temparr = arr.slice(); 

the slice() operation clones array , returns reference new array.

there small typo in same function. think should ...

temparr[floor(random(arr.length))] = p5.vector.random2d().setmag(30); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -