Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

list - Netlogo: Assign variable using probabilities

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

is there a better way?

Yes there is.

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

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:

  • 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]].

  • 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 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...