NetLogo:如何确保变量保持在定义的范围内? [英] NetLogo : How to make sure a variable stays in a defined range?

查看:334
本文介绍了NetLogo:如何确保变量保持在定义的范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些变量,可以通过+ 0.1和-0.1的变化或不进行任何更改或再次随机来继承给子代理,我所做的就是这样的:(代码仅是示例)

I have a few variables which can be inherited to child agents by a variation of + 0.1 and -0.1 or without any changes, or random again, What I have done is like this: (The code is just an example)

to reproduce
  ask turtle 1
  [
  let X-Of-Mother X

  hatch 1
  [

    set X one-of (list (X-Of-Mother) (X-Of-Mother + 0.1) (X-Of-Mother - 0.1) (random-float 1))

    ]

  ]

end

目前,我必须检查像这样的子龟的X是否始终在范围内:

Currently I have to check if X of child turtle is always within range by something like this:

if X > 1 [set X X - 0.2]
if X < 0 [set X X + 0.2]

有什么更好的方法呢?

What could be a better way to do it?

如果我必须使用随机正态0.5 0.1怎么办,如何将其限制为0到1之间的值,我做了很多重复的生成此类随机数的操作,我认为随机正态的质量很好并且有我不需要检查它是否超出范围很多次.

What if I have to use random-normal 0.5 0.1 , how can I limit that to values between 0 and 1 , I have done many repetitions of generating such random numbers I think the quality of random-normal is good and there is not that many times that I need to check if it's beyond the range.

例如:

  to test 
    Let c 0
    let b 0 
    repeat 100000000
    [Set b random-normal 0.5 0.1
      if b > 1 [set C C + 1]
      If b < 0 [set C C + 1]

      ]
    print c  

  end

输出是100000000次中的* 67 * 67是我得到的最大的东西,我得到58,51,...

OUTPUT is *67 times out of 100000000 Time* 67 is biggest one I got, I got 58 , 51 , ...

推荐答案

正如您所发现的,random-normal可能有问题,因为您返回的结果实际上可以是任何数字.

As you've discovered, random-normal can be problematic because the result you get back can be literally any number.

一种可能的解决方案是将random-normal的输出限制在边界内,如Matt的回答所示.请注意,这种方法会在范围的边界处产生尖峰:

One possible solution is to clamp the output of random-normal within boundaries, as in Matt's answer. Note that this approach creates spikes at the boundaries of the range:

observer> clear-plot set-plot-pen-interval 0.01 set-plot-x-range -0.1 1.1
observer> histogram n-values 1000000 [ median (list 0 (random-normal 0.5 0.2) 1) ]

正如Marzy在问题本身中描述的那样,另一种可能的解决方案是丢弃random-normal给您的任何超出范围的结果,只是继续尝试直到获得超出范围的结果.这样可以避免边界出现尖峰:

Another possible solution, as Marzy describes in the question itself, is to discard any out-of-bounds results random-normal gives you and just keeping trying again until you get an in-bounds result. This avoids the spikes at the boundaries:

to-report random-normal-in-bounds [mid dev mmin mmax]
  let result random-normal mid dev
  if result < mmin or result > mmax
    [ report random-normal-in-bounds mid dev mmin mmax ]
  report result
end

observer> clear-plot set-plot-pen-interval 0.01 set-plot-x-range -0.1 1.1
observer> histogram n-values 1000000 [ random-normal-in-bounds 0.5 0.2 0 1 ]

另一种解决方案是问自己是否真的需要钟形曲线,或者三角形分布是否合适.您只需将对random-float的两个调用相加即可非常简单地得到三角形分布的结果:

Another solution is to ask yourself whether you really need a bell curve, or whether a triangle-shaped distribution would be just fine. You can get a triangle-shaped distribution of results very simply just by summing two calls to random-float:

observer> clear-plot set-plot-pen-interval 0.01 set-plot-x-range 0 1
observer> histogram n-values 10000000 [ 0.5 + random-float 0.5 - random-float 0.5 ]

这篇关于NetLogo:如何确保变量保持在定义的范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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