我正在创建一个模型,我有100只乌龟,我让它们合作起来。然后,我希望他们留在他们的伙伴关系,并转移到我制作的任何随机50黄色补丁,每个补丁只有一对。我该怎么做呢?
补丁的设置代码是:
to setup
clear-all
ask patches [ set pcolor 3 ]
ask n-of 50 patches [ set pcolor yellow ]
.
.
.
reset-ticks
end
和合作伙伴的代码是:
to find-partners
let singles turtles with [partner = nobody]
if not any? singles [ stop ]
ask singles
[ lt random 50
rt random 50
fd 1 ]
ask turtles
[
if (partner = nobody) and (any? other turtles-here with [partner = nobody])
[ set partner one-of other turtles-here with [partner = nobody]
ask partner [
set partner myself
]]]
end
答案 0 :(得分:1)
一种方法是给补丁一个patches-own
变量,如“被占用?”在setup
程序中设置为“FALSE”。为简单起见,使用turtles-own
之类的“嵌套?”所以他们可以知道他们是否在他们的“巢址”。然后当乌龟配对时,他们都可以声称一个未被占用的补丁作为他们的“巢穴”,将其设置为占用,然后前往该补丁。
因此,以下变量已在setup
中正确设置:
turtles-own [partner nested? nest-site ]
patches-own [occupied?]
您可以按原样保留find-partners
程序,并执行嵌套查找程序。例如:
to find-nest
ask turtles with [ partner != nobody and nested? = false ] [
if nest-site = nobody [
set nest-site min-one-of (patches with [pcolor = yellow and occupied? = false]) [distance myself ]
ask partner [
set nest-site [nest-site] of myself
]
ask nest-site [
set occupied? true
]
]
face nest-site
fd 1
if patch-here = nest-site [
move-to nest-site
set nested? true
]
]
end
此代码已经配对的乌龟选择最近的未占用黄色补丁,选择它作为它们的巢,然后在下面的滴答中朝向它。 Here 是整个简单版本。功能