从 Netlogo 中的列表中选择一个项目

Selecting an item from a list in Netlogo

我需要在包含 20 个具有属性 c (color)s (size) 的元素的包中挑选一个对象。颜色和大小都是数字(例如 c= {red = 256, black = 0, ... } = {256, 0, ...})。 在Python中我会在numpy库中使用random.choice,我在网上发现Netlogo中对应的函数是扩展名rnd。 努力寻找可能的解决方案,我做到了

已编辑:

breed[people person]
people-own 
  [
   ball
   size
   color 
   bag
  ]    
to setup
      create-people 5
  [ set color gray
    setxy random-xcor random-ycor
  ]
  ask people[
  set bag [ ] ; 0 items
  ]
end

创建球:

to create-balls
  set color random 300 ; color
  set size random-float 5 ; size
  let this-ball self
  ask one-of people [ ; ask one of people to put the ball created into the bag
      set bag fput this-ball bag ; add ball to the bag
  ]
end

下面的代码应该包括绘图的部分:

to draw
ask one-of people [
 rnd:weighted-one-of bag [ ] ; I do not know what I'd write in the brackets
]
end

如您所见,我对如何实现代码有很多疑问。 如何根据尺寸(或颜色)从包中 select 一件物品?

你能帮我解决一下吗?

这是一个完整的模型,它将人和球创建为乌龟代理,并根据大小对 30 个球进行加权。然后它会为选择最多球的人打开检查window。

extensions [rnd]

breed [people person]
people-own [ my-balls ]

breed [balls ball]
balls-own [ chosen? ]

to setup
  clear-all
  create-people 20
  [ setxy random-xcor random-ycor
    set my-balls (turtle-set nobody)
  ]
  create-balls 50
  [ hide-turtle
    set size one-of [1 2 3 4 5]
    set color one-of [red white blue yellow]
    set chosen? false
  ]
  repeat 30 [draw-ball]
  inspect max-one-of people [count my-balls]
end

to draw-ball
  ask one-of people
  [ let bag-of-balls balls with [not chosen?]
    let choice rnd:weighted-one-of bag-of-balls [size]
    ask choice [set chosen? true]
    set my-balls (turtle-set my-balls choice)
  ]
end

一些注意事项:

  1. 此代码中没有列表。有些情况下您应该使用列表。常见用途包括顺序很重要的内存(例如,您只想跟踪最近看到的 5 个其他人)或同一个代理可以出现多次的地方。而且列表命令非常强大。但是,除非您需要列表,否则您应该使用代理集。
  2. 每个人都有自己的包 'my-balls',里面装着他们 select 的球。作为设置的一部分,它被初始化为海龟列表。
  3. 我用了一个叫'chosen?'的变量,每个球都拥有这个变量来跟踪它是否还在袋子里,供下一个人选择。然后只创建球袋作为所有尚未选择的球。
  4. 加权随机选择的代码(当从代理集中选择时)只是将持有权重的变量名称作为报告者,但是如果你想要更复杂的,你可以使用一些函数,例如 rnd:weighted-one-of bag-of-balls [size ^ 2]加权方案。