突变后火箭队的方向并没有改变

时间:2017-08-18 08:11:05

标签: javascript machine-learning

我遇到一些机器学习问题。在第一代火箭没有发生变异并且只是遵循相同的路径之后。我尝试了不同的方法来解决这个问题,但是你们都看不到它们。我在openprocessing.org中写了这个,但也尝试在本地做。我没有看到问题,认为它是在Rocket构造函数中。

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);
  for (var i = 0; i < 10; i++) {
    rockets.push(new Rocket());
  }
}

function draw() {
  background(100);
  ellipse(width / 2, height / 2, 40, 40);
  ellipse(width - 10, height / 2, 40, 40);
  for (i = 0; i < rockets.length; i++) {
    rockets[i].show();
    //console.log(rockets[i].pos.y);
    if (!rockets[i].killed) {
      rockets[i].applyForce(); //it is a fittness counter inside
    }
  }
  //console.log(rockets);
  if (rockets[0].counter >= rockets[0].route.length) {
    findBest();
    respawn();
  }
}

function respawn() {
  var news = mutate(br);
  for (var i = 0; i < 10; i++) {
    rockets.push(new Rocket());
    //console.log(rockets);
    rockets[i].route = news[i];
  }
}

function findBest() {
  for (var i = 0; i < 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 = [];
  for (var i = 0; i < 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) {
    for (var i = 0; i < 20; i++) {
      this.route.push(p5.Vector.random2D());
      this.route[i].setMag(20);
      //console.log("!A");
      // console.log(i);
    }
  } else {
    // this is not used 
    this.route = a;
    // for(var i = 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>

1 个答案:

答案 0 :(得分:1)

好的代码。你的mutate函数似乎有一个错误:

function mutate(arr) {
  var news = [];
  for (var i = 0; i < 10; i++) {
    var tempArr = arr;
    tempArr[floor(random(arr.length))] = p5.Vector.random2D().setMag(30);
    news.push(tempArr);
  }
  return news;
}

使用时

var tempArr = arr;

在javascript中你没有得到数组的副本。两个数组都将指向相同的引用。当你改变一个时,原来也会发生变异。这样最终火箭队的所有路线都会变成相同的路线。

尝试......

var tempArr = arr.slice();

slice()操作克隆数组并返回对新数组的引用。

同一功能中也有一个小错字。我认为应该是......

tempArr[floor(random(arr.length))] = p5.Vector.random2D().setMag(30);