2017-02-13 12 views
1

JavaScriptでP5.JSライブラリを使用して遺伝的アルゴリズムシミュレーションを試みていますが、いくつかの問題が発生しています。ここで私はこれまで持っているものです。JS - エラー: "未定義のプロパティを読み取ることができません"

//var popS = 2; 
var popu; 
//var balls = []; 
var target; 

function setup() { 
    createCanvas(800, 300); 
    popu = new Population(); 
    target = createVector(width - 15, height/2); 
} 

function draw() { 
    background(50); 
    popu.run(); 

    var ballsC = 0; 
    for (var a = 0; a < popu.balls.length; a++) { 
    if (popu.balls[a].done == true){ 
     ballsC++; 
    } 
    } 
    if (ballsC >= popu.popS) { 
    //popu = new Population(); 
    popu.evaluate(); 
    //popu.selection(); 
    } 

    fill(255, 0, 30); 
    noStroke(); 
    ellipse(target.x, target.y, 20, 20); 
} 

function DNA() { 
    this.genes = []; 
    this.changes = 7;//random(2, 50); 

    for (var a = 0; a < this.changes; a++) { 
    this.genes[a] = random(0, 15); 
    } 

    this.crossover = function (pB) { 
    var newdna = new DNA(); 
    var mid = floor(random(0, this.genes.length)); 
    for (var a = 0; a < this.genes.length; a++) { 
     if (a < mid) { 
     newdna.genes[a] = this.genes[a]; 
     }else { 
     newdna.genes[a] = pB.genes[a]; 
     } 
    } 
    return newdna; 
    } 
} 

function Population() { 
    this.balls = []; 
    this.popS = 50; 
    this.maxfit = 0; 
    this.matingpool = []; 

    for (var a = 0; a < this.popS; a++) { 
    this.balls[a] = new Ball(); 
    } 

    this.evaluate = function() { 
    for (var a = 0; a < this.balls.length; a++) { 
     this.balls[a].calcF(); 
     if (this.balls[a].fitness > this.maxfit) { 
     this.maxfit = this.balls[a].fitness; 
     } 
    } 
    this.matingpool = []; 
    for (var b = 0; b < this.balls.length; b++) { 
     var n = this.balls[b].fitness * 100; 
     for (var c = 0; c < n; c++) { 
     this.matingpool.push(this.balls[c]); 
     } 
    } 

    this.selection(); 
    } 

    this.selection = function() { 
    var newBalls = []; 
    for (var a = 0; a < this.balls.length; a++) { 
     var parentA = this.matingpool[floor(random(0, this.matingpool.length))]; 
     var parentB = this.matingpool[floor(random(0, this.matingpool.length))]; 
     var child = parentA.dna.crossover(parentB.dna); 
     newBalls[a] = new Ball(child); 
    } 
    this.balls = newBalls; 
    } 

    this.run = function() { 
    for (var a = 0; a < this.balls.length; a++) { 
     this.balls[a].update(); 
     this.balls[a].checkCol(); 
     this.balls[a].show(); 
    } 
    } 
} 

function Ball(dna) { 
    this.pos = createVector(10, height/2); 
    this.speed = createVector(2, 2.5); 
    this.mul = -1; 
    this.time = 0; 
    this.a = 0; 
    if (dna) { 
    this.dna = dna; 
    } else { 
    this.dna = new DNA(); 
    } 
    this.done = false; 
    this.fitness = 0; 
    this.reached; 

    this.update = function() { 
    if (this.done == false) { 
     if (this.time >= this.dna.genes[this.a]) { 
     this.a++; 
     this.time = 0; 
     this.mul *= -1; 
     } 
     this.speed.set(2, 2.5 * this.mul); 
     this.pos.add(this.speed); 
    } 
    } 

    this.show = function() { 
    this.time += 0.1; 
    fill(255, 70); 
    noStroke(); 
    ellipse(this.pos.x, this.pos.y, 10, 10); 
    } 

    this.checkCol = function() { 
    if (this.pos.y > height || this.pos.y < 0 || this.pos.x > width) { 
     //print("col"); 
     this.done = true; 
    } 
    if (dist(this.pos.x, this.pos.y, target.x, target.y) <= (10/2) + (20/2)) { 
     //print("done!"); 
     this.done = true; 
     this.reached = true; 
    } 
    } 

    this.calcF = function() { 
    var a = dist(this.pos.x, this.pos.y, target.x, target.y); 
    var b = this.dna.genes.length; 
    var c = 0; 
    if (this.reached){ 
     c = 1; 
    } 

    this.fitness = map(map(a, 0, width, 1, 0) + map(b, 2, 50, 1, 0) + c, 0, 3, 0, 1); 
    } 
} 

これは、コードの最も重要な部分である:

var popu; 

function setup() { 
    createCanvas(800, 300); 
    popu = new Population(); 
} 

function draw() { 
    background(50); 
    //popu = new Population(); 
    popu.evaluate(); 
    //popu.selection(); 
} 

function DNA() { 
    this.genes = []; 
    this.changes = 7; //random(2, 50); 

    for (var a = 0; a < this.changes; a++) { 
    this.genes[a] = random(0, 15); 
    } 

    this.crossover = function(pB) { 
    var newdna = new DNA(); 
    var mid = floor(random(0, this.genes.length)); 
    for (var a = 0; a < this.genes.length; a++) { 
     if (a < mid) { 
     newdna.genes[a] = this.genes[a]; 
     } else { 
     newdna.genes[a] = pB.genes[a]; 
     } 
    } 
    return newdna; 
    } 
} 

function Population() { 
    this.balls = []; 
    this.popS = 50; 
    this.maxfit = 0; 
    this.matingpool = []; 

    for (var a = 0; a < this.popS; a++) { 
    this.balls[a] = new Ball(); 
    } 

    this.evaluate = function() { 
    this.matingpool = []; 
    for (var b = 0; b < this.balls.length; b++) { 
     var n = this.balls[b].fitness * 100; 
     for (var c = 0; c < n; c++) { 
     this.matingpool.push(this.balls[c]); 
     } 
    } 

    this.selection(); 
    } 

    this.selection = function() { 
    var newBalls = []; 
    for (var a = 0; a < this.balls.length; a++) { 
     var parentA = this.matingpool[floor(random(0, this.matingpool.length))]; 
     var parentB = this.matingpool[floor(random(0, this.matingpool.length))]; 
     var child = parentA.dna.crossover(parentB.dna); 
     newBalls[a] = new Ball(child); 
    } 
    this.balls = newBalls; 
    } 
} 

function Ball(dna) { 
    this.pos = createVector(10, height/2); 
    this.speed = createVector(2, 2.5); 
    this.mul = -1; 
    this.time = 0; 
    this.a = 0; 
    if (dna) { 
    this.dna = dna; 
    } else { 
    this.dna = new DNA(); 
    } 
    this.done = false; 
    this.fitness = 0; 
    this.reached; 

} 

だから、ここまでなるたび:

this.selection = function() { 
    var newBalls = []; 
    for (var a = 0; a < this.balls.length; a++) { 
     var parentA = random(this.matingpool); 
     var parentB = random(this.matingpool); 
     var child = parentA.dna.crossover(parentB.dna); 
     newBalls[a] = new Ball(child); 
    } 
    this.balls = newBalls; 
    } 

私が手エラー: "未定義のプロパティ 'dna'を読み取ることができません、なぜ地球上で起こっているのですか?私がクロムでデバッガを使用すると、matingpoolに2000要素があることがはっきり分かりますが、ランダムなものを取得しようとすると "undefined"が返されます。

var parentA = random(this.matingpool); 
var parentB = random(this.matingpool); 

奇妙なことは、parentBは動作しますが、parentA dosn'tです。

enter image description here

すべてのヘルプははるかに高く評価されます。全体のコードはここに実行している:http://codepen.io/felipe_mare/pen/bgOYMN

それが助け場合、私は時々エラーが出る:「プロパティを読み取ることができませんが 『0』未定義の」ではなく、将来的にはライン138

this.update = function() { 
    if (this.done == false) { 
     //line 138 
     if (this.time >= this.dna.genes[this.a]) { 
     //line 138 
     this.a++; 
     this.time = 0; 
     this.mul *= -1; 
     } 
     this.speed.set(2, 2.5 * this.mul); 
     this.pos.add(this.speed); 
    } 
    } 
+0

あなたの 'random()'はどのように見えますか? – JohanP

+0

@JohanP [tag:p5.js]タグに注目してください。 P5.jsライブラリは 'random()'関数を提供します。 –

+0

"line 138"はどれですか? – Li357

答えて

3

で、試してみてくださいくださいください。質問をMCVEに絞り込んでください。私はこれがデバッグするには複雑な問題であると理解していますが、の最小値(20行以下)の例に絞り込むと、より良い運を得られます。ほとんどの場合、MCVEを作成する過程でエラーが発生します。

しかし、あなたはここでmatingpool配列、作成するときに、あなたの問題は、実際には次のとおりです。

this.matingpool = []; 
for (var b = 0; b < this.balls.length; b++) { 
    var n = this.balls[b].fitness * 100; 
    for (var c = 0; c < n; c++) { 
    this.matingpool.push(this.balls[c]); 
    } 
} 

を、私はこのように、インナーforループ内のprint文を追加する場合:

this.matingpool = []; 
for (var b = 0; b < this.balls.length; b++) { 
    var n = this.balls[b].fitness * 100; 
    for (var c = 0; c < n; c++) { 
    console.log("pushing: " + this.balls[c]); 
    this.matingpool.push(this.balls[c]); 
    } 
} 

その後、私は見ますあなたはundefinedをアレイに一気に押し込んでいると言っています:

(1178) pushing: [object Object] 
(3) pushing: undefined 
(482) pushing: [object Object] 
(3) pushing: undefined 
(216) pushing: [object Object] 

次に、この配列からランダムに選択するので、コード内のランダムな場所にエラーが表示されます。

なぜこれが起こっているのか理解するためにこれをさらにデバッグする必要があります。配列の長さではなく、フィットネスに基づいてループしているのが変だと思われますが、念のため。いずれにせよ、これが正しい軌道に乗ることを願っています。

+0

配列に与えると、ランダムな要素が返されます – Pepe

+0

@Pepe Oh wow。決してそれを知らなかった。きちんとした私は掘り起こし、私の答えを編集します。 –

+0

私も 'this.matingpool [floor(random(0、this.matingpool.length)];'を試していますが、それでも仕事はしません。 – Pepe

関連する問題