我需要在装有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
您可以很容易地看到,我对如何实现代码有很多疑问。如何根据袋子的大小(或颜色)从袋子中选择一项?
您能帮我解决吗?
这里是一个完整的模型,可以将人和球创建为乌龟代理,并根据其大小选择30个球作为权重。然后,它将为选择最多球的人打开检查窗口。
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
一些注意事项:
rnd:weighted-one-of bag-of-balls [size ^ 2]
之类的功能。