我有一个要解决的问题。可以说有两种类型的代理(兔子和猎人)。我希望猎人找到离他们最近的兔子。但是如果两个猎人要找到同一只兔子(即距离猎人103最近的兔子,而猎人105最靠近兔子99)。我希望其中一位猎人找到下一个最近的兔子。因此,每个猎人都需要检查其他猎人所连接的兔子,并且如果有猎人与同一只兔子连接,请查找下一个最近的兔子。任何想法如何解决这个问题。谢谢
breed [hunters hunter]
breed [rabbits rabbit]
hunters-own [rab-in-sight]
to setup
clear-all
create-hunters 20
create-rabbits 100
ask hunters [
set color red
setxy random-xcor random-ycor
]
ask rabbits [
set color white
setxy random-xcor random-ycor
]
end
to connect
ask hunters [
set rab-in-sight min-one-of rabbits in-radius 5 [distance myself]
]
end
答案 0 :(得分:3)
您可以使用指示兔子是否已成为目标的变量,也可以使用links
完成相同的操作。
查看您的setup
的修改版,其中猎人将其rab-in-sight
设置为nobody
:
breed [hunters hunter]
breed [rabbits rabbit]
hunters-own [rab-in-sight]
to setup
clear-all
create-hunters 20
create-rabbits 100
ask hunters [
set color red
setxy random-xcor random-ycor
set rab-in-sight nobody
]
ask rabbits [
set color white
setxy random-xcor random-ycor
]
reset-ticks
end
然后,使用if
或ifelse
语句根据当前是否在猎兔来控制猎人的行为:
to go
ask hunters [
connect
]
tick
end
to connect
; If a hunter is not already targetting a rabbit
ifelse rab-in-sight = nobody [
fd 1
; Choose a target rabbit that does not already have a link with other hunters
set rab-in-sight min-one-of ( rabbits in-radius 5 with [
not any? my-links ] ) [distance myself]
; If that rabbit exists, create a link with it so no other hunters will
; target the same rabbit
if rab-in-sight != nobody [
create-link-with rab-in-sight
]
] [
; If you're targetting a rabbit, hunt it
face rab-in-sight
ifelse distance rab-in-sight > 1 [
fd 1
] [
move-to rab-in-sight
ask rab-in-sight [
die
]
set rab-in-sight nobody
]
]
end
标志或信号量变量的方法类似-稍作修改的setup
现在将rab-in-sight
设置为nobody
并给rabbits
一个targeted?
布尔变量,即设置为false
:
breed [hunters hunter]
breed [rabbits rabbit]
hunters-own [rab-in-sight]
rabbits-own [ targeted? ]
to setup
clear-all
create-hunters 20
create-rabbits 100
ask hunters [
set color red
setxy random-xcor random-ycor
set rab-in-sight nobody
]
ask rabbits [
set color white
setxy random-xcor random-ycor
set targeted? false
]
reset-ticks
end
现在,当猎人将一只兔子作为目标时,该兔子将其targeted?
变量更改为true
,以便其他猎人也“不知道”也不再将它作为目标:
to go
ask hunters [
connect
]
tick
end
to connect
; If a hunter is not already targetting a rabbit
ifelse rab-in-sight = nobody [
fd 1
; Choose a target rabbit that is not currently being targeted
set rab-in-sight min-one-of ( rabbits in-radius 5 with [
not targeted? ] ) [distance myself]
; If that rabbit exists, have it set targetted? to true so
; no other hunters will target the same rabbit
if rab-in-sight != nobody [
ask rab-in-sight [
set targeted? true
]
]
] [
; If you're targetting a rabbit, hunt it
face rab-in-sight
ifelse distance rab-in-sight > 1 [
fd 1
] [
move-to rab-in-sight
ask rab-in-sight [
die
]
set rab-in-sight nobody
]
]
end
答案 1 :(得分:1)
在这种情况下,我建议为兔子创建一个单独的代理集,例如rabbits-not-hunted
。猎人选择了该组附近的一只兔子,而选定的兔子则从该组中移除。因此,下一个猎人无法再选择这只兔子作为目标并选择下一个“自由”猎物。请检查以下代码:
breed [hunters hunter]
breed [rabbits rabbit]
hunters-own [rab-in-sight]
to setup
clear-all
create-hunters 20
create-rabbits 100
ask hunters [
set color red
setxy random-xcor random-ycor
]
ask rabbits [
set color white
setxy random-xcor random-ycor
]
end
to connect
;; a seperate agentset of all rabbits which are not hunted yet. At first, all rabbits can be hunted, therefore it coincides with all rabbits
let rabbits-not-hunted rabbits
ask hunters [
;; the prospective prey is taken from the set of rabbits-not-hunted yet
let prospective-prey turtle-set min-one-of rabbits-not-hunted in-radius 5 [distance myself]
set rab-in-sight prospective-prey
;; updates rabbits-not-hunted agentset by removeing the prey selected from the agentset
set rabbits-not-hunted rabbits-not-hunted with [ not member? self prospective-prey ]
]
end
请注意,如果在猎人半径内只有一只兔子而该兔子已成为另一位猎人的目标,则该代码可能会产生错误。考虑到您代码中的猎物比率,这种情况不太可能发生,不过我想对此给出一个合理的警告。