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

发布于 2024-10-19 23:21:01 字数 887 浏览 4 评论 0 原文

我刚刚开始学习遗传算法,我本质上是在写本教程 http://lethain.com/entry/2009/jan/02/genic-algorithms-cool-name-damn-simple/ 到 JavaScript。有一些更改可以更好地代表我的数据集。

不管怎样,当我通过 newPop.toSource() 输出时,我发现

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

我从来没有见过我的 .toSource 输出看起来像这样,我期待的只是一个里面有两个数组的数组

我的代码是

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;
}

我在哪里做错了什么我的代码给了我这个奇怪的数组结构?

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 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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-10-26 23:21:01

不确定这些 #1, #2, ... 是什么,但是 toSource() 是 gecko 特定的:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/toSource
我的猜测是,这是对当时内存中对象的某种“引用”,即不是可移植的输出。

我建议您改用 JSON.stringify,它将输出数据结构的可移植字符串表示形式。

JSON 全局对象将在 Firefox/Safari/Chrome 中开箱即用,但如果您在 IE 中也需要它,您可以在此处获取:http://www.json.org/js.html

然后要反转它并取回实际的活动对象,请使用 JSON.parse

var data = JSON.parse(str);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文