netlogo中形成子集的限制 [英] limitation of forming subset in netlogo

查看:109
本文介绍了netlogo中形成子集的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的Netlogo社区,

Dear Netlogo community,

上周,我在同一论坛上发布了有关从netlogo中的集合中形成子集的问题.这是链接.

Last week I posted the question on the same forum about forming subset from a set in netlogo. Here is the link.

如何形成一组子集Netlogo中的数字

上述线程的问题是,如果集合包含超过21个元素,它将不会给出子集.它将Java堆空间抛出内存不足异常.我相信这是因为上述线程将所有子集存储在一个列表中,并且最终列表列表达到了其最大范围,并且将Java堆空间抛出内存不足异常.在这方面的任何帮助都是有价值的.

The problem with the above thread is that it wont give the subsets if the set contains more than 21 elements. It throws java heap space out of memory exception. I believe this is because the above thread stored all subsets in one list and eventually list of list reached to its maximum range and throws java heap space out of memory exception. Any help in this regard will be valuable.

推荐答案

包含N个元素的集合具有2 ^ N个子集.通过计算,一旦N大,您将无法对它们全部执行任何操作,但是您仍然可以从它们中随机选择.假设您的N个元素在列表中.然后,您可以选择一个随机子集:

A set with N elements has 2^N subsets. Computationally, you cannot do anything with all of them once N is big, but you can still pick from them randomly. Let's assume your N elements are in a list. Then you can pick a random subset:

to-report random-subset [#lst]
  let _result []
  foreach #lst [
    if (one-of [true false]) [
      set _result lput ? _result
    ]
  ]
  report _result
end

请注意,这等效于在[0 ..(2 ^ N-1)]中选择一个随机数,然后选择与该数字对应"的子集.

Note that this is equivalent to picking a random number in [0..(2^N-1)] and then selection the subset "corresponding" to that number.

如果您希望使用功能更强大的方法,则可以使用

If you prefer a more functional approach, you can use

to-report random-subset02 [#lst]
  report filter (task [one-of [true false]]) #lst
end

这篇关于netlogo中形成子集的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆