开发者

strange data in array.toSource() learning genetic algorithm

I'm just starting to learn genetic algorithms and I'm essentially writting this tutorial http://lethain.com/entry/2009/jan/02/genetic-algorithms-cool-name-damn-simple/ to javascript. with a few changes which better represent my dataset.

Anyway, when I output via newPop.toSource(), I get

[[#1=[[30,22],#2=[30,85],#3=[30,76]...]]],[#1#,#2#,#3#...#15]]]

I've never seen my .toSource output look like this, I was expecting just an array with two arrays inside it

My code is

var newPop=populate(data,population,0,70);

function individual(population, min, max){
   var newIndivids=[];
   for(s i开发者_Go百科n population){
      newIndivids.push(population[s]);
     newIndivids[s][0]+=rand;
   }
   return newIndivids;

}

function populate(count,population,min,max){
    var popul=[];
    for(indiv in count){
     popul.push(individual(population,min,max));
    }
    return popul;
}

Is there something I'm doing wrong in my code which is giving me this strange array structure??


Not sure what those #1, #2, ... things are, but toSource() is gecko specific: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/toSource
My guess is that it's some kind of "reference" to the object in memory at that point, i.e. not portable output.

I suggest you use JSON.stringify instead, which will output a portable string representation of your data structure.

The JSON global object will be available in Firefox/Safari/Chrome out of the box, but if you also need it in IE you can get it here: http://www.json.org/js.html

Then to reverse this and get back an actual living object, use JSON.parse:

var data = JSON.parse(str);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜