简体   繁体   English

array.toSource()学习遗传算法中的奇怪数据

[英]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. 我刚刚开始学习遗传算法,实际上是将本教程http://lethain.com/entry/2009/jan/02/genetic-algorithms-cool-name-damn-simple/写成javascript。 with a few changes which better represent my dataset. 进行一些更改以更好地代表我的数据集。

Anyway, when I output via newPop.toSource(), I get 无论如何,当我通过newPop.toSource()输出时,我得到

[[#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 我从未见过我的.toSource输出看起来像这样,我期望里面只有两个数组

My code is 我的代码是

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

function individual(population, min, max){
   var newIndivids=[];
   for(s in 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 不确定#1, #2, ...是什么,但是toSource()是壁虎特定的: 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, ie not portable output. 我的猜测是,这是对内存中对象的某种“引用”,即不是可移植的输出。

I suggest you use JSON.stringify instead, which will output a portable string representation of your data structure. 我建议您改用JSON.stringify ,它将输出数据结构的可移植字符串表示形式。

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 JSON全局对象可以在Firefox / Safari / Chrome中直接使用,但是如果您在IE中也需要它,可以在这里获取: http : //www.json.org/js.html

Then to reverse this and get back an actual living object, use JSON.parse : 然后将其反转并返回实际的活动对象,请使用JSON.parse

var data = JSON.parse(str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM