本文介绍了Netlogo:使用概率分配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用组/列表中变量的概率将字符串或整数变量分配给turtle?例如,从特定的组/列表中使用一个特定的变量的概率为0.4.函数根据概率随机选择变量.之后,我需要使用相同的方法根据概率从列表中选择一个变量(字符串).在python中应该是:

How to assign a string or integer variable to turtle, using probabilities of the variables in a group/list? For example it is 0.4 probability that one specific variable is used from specific group/list. The function selects randomly the variable based on probability. I need to use the same method afterwards to choose a variable (string) from a list according to probability.In python it should be:

import random
def random_value(probability_list, values):
    r = random.random()
    index = 0
    while(r >= 0 and index < len(probability_list)):
      r -= probability_list[index]
      index += 1
    value=values[index - 1]
    value_index=index-1
    return value,value_index

我在Netlogo中尝试了如下操作(出现索引为-1的错误),但是有更好的方法吗?

I tried it in Netlogo like below (get error that index is -1) but is there a better way?

globals [random_nr probabilities some_list index]
to initialize-variables
  set some_list[]
  set probabilities[]
end
to random_pick
  set random_nr random-float 1
  set probabilities [0.1 0.2 0.4 0.3]
  set some_list ["String1" "String2" "String3" "String4"]
  set index 0
  while [(random_nr >= 0) and (length probabilities < index)] [
   set random_nr random_nr - item index probabilities
   set index index + 1 ]
  set index index - 1
end

推荐答案

是的.

NetLogo 6.0随附了 rnd扩展. (对于早期版本的NetLogo,您也可以单独下载扩展名 )

NetLogo 6.0 comes with the rnd extension bundled. (You can also download the extension separately for earlier versions of NetLogo.)

rnd扩展名提供了 rnd:weighted-one-of-list 原语,它确实可以完成您要执行的操作:

The rnd extension offers the rnd:weighted-one-of-list primitive, which does exactly what you're trying to do:

extensions [ rnd ]

to-report pick
  let probabilities [0.1 0.2 0.4 0.3]
  let some_list ["String1" "String2" "String3" "String4"]
  report first rnd:weighted-one-of-list (map list some_list probabilities) last
end

让我稍微整理一下最后一个表达式:

Let me unpack the last expression a bit:

  • (map list some_list probabilities)的作用是将两个列表压缩"在一起,以获取以下形式的对列表:[["String1" 0.1] ["String2" 0.2] ["String3" 0.4] ["String4" 0.3]].

  • The role of (map list some_list probabilities) is to "zip" the two lists together, in order to get a list of pairs of the form: [["String1" 0.1] ["String2" 0.2] ["String3" 0.4] ["String4" 0.3]].

该对列表作为第一个参数传递给rnd:weighted-one-of-list.我们将last作为rnd:weighted-one-of-list的第二个参数传递,以告知它应使用每对中的第二个作为概率.

That list of pairs is passed as the first argument to rnd:weighted-one-of-list. We pass last as the second argument of rnd:weighted-one-of-list to tell it that it should use the second item of each pair as the probability.

rnd:weighted-one-of-list随机选择一对对,然后返回整个对.但是,由于我们只对这对中的第一项感兴趣,因此我们使用first提取它.

rnd:weighted-one-of-list then picks one of the pairs at random, and returns that whole pair. But since we're only interested in the first item of the pair, we use first to extract it.

要了解该代码的工作原理,有助于了解匿名过程工作.请注意,我们如何利用简洁的语法将list传递给map以及将last传递给rnd:weighted-one-of-list.

To understand how that code works, it helps to understand how Anonymous procedures work. Note how we make use of the concise syntax for passing list to map and for passing last to rnd:weighted-one-of-list.

这篇关于Netlogo:使用概率分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:01