目前我在鼠标停机时运行程序。但没有一点能够移动。他们的立场不会像我希望的那样发生变异。但我发现我的代码没有任何问题。如果你愿意,我想要一些第二意见和/或提示/改进。
function setup() {
createCanvas(windowWidth,windowHeight);
background(255);
network = new network();
for (var i = 0; i < network.populationSize; i++) {
network.population.push(new object(random(width),random(height)));
}
}
function draw() {
background(255);
network.show();
if (mouseIsPressed) {
network.generate();
}
}
function network() {
this.populationSize = 100;
this.population = [];
this.genePool = [];
}
network.prototype.generate = function() {
this.genePool = [];
this.genePool = this.population.map(function(curr) {
curr.mutate();
curr.calculateFitness();
curr.genePoolAdd();
return curr;
})
this.population = [];
for (var i = 0; i < this.populationSize; i++) {
this.population.push(random(this.genePool));
}
}
network.prototype.show = function() {
stroke(0);
strokeWeight(5);
for (var i = 0; i < this.population.length; i++) {
point(this.population[i].dna[0],this.population[i].dna[1]);
}
}
function object(x,y) {
this.dna = [x,y];
this.mutation = 18;
this.fitness = this.calculateFitness();
}
object.prototype.mutate = function() {
for (var i; i < this.dna.length; i++) {
this.dna[i] += random(-this.mutation,this.mutation);
}
}
object.prototype.calculateFitness = function() {
var max = dist(0,0,width/2,height/2);
return max-dist(this.dna[0],this.dna[1],width/2,height/2);
}
object.prototype.genePoolAdd = function() {
for(var i = 0; i < pow(this.fitness); i++) {
genePool.push(new object(this.dna[0],this.dna[1]));
}
}