我不确定您是否仍然想让它们聚集在目标点周围,或者只是移动到目标点上方。这是一种可以得到你想要的东西的方法。它定义了一个目标点和一组安全补丁。目标点下方的海龟试图通过目标点到达安全区域。 cmets中的更多详细信息/解释
设置:
globals [ goal-spot safe-patches ]
to setup
ca
; Set the wall
ask patches with [ pycor = max-pycor ] [ set pcolor blue ]
; Set the goal-patch
set goal-spot patch 0 0
ask goal-spot [ set pcolor green ]
; Set up a triangular safe-patches zone
let check ( [pycor] of goal-spot - [pxcor] of goal-spot ) - 1
let gpx [pxcor] of goal-spot
set safe-patches patches with [
pycor > [pycor] of goal-spot and
pycor < max-pycor and
( ( pxcor < gpx and pycor + pxcor > check ) or
( pxcor >= gpx and pycor - pxcor > check ) ) ]
; Make some turtles
let n-turtles 255
if n-turtles > count safe-patches [
set n-turtles count safe-patches
]
crt n-turtles [
set shape "person"
set color green
move-to one-of patches with [ pycor < [pycor] of goal-spot and pycor < max-pycor]
if pycor <= [pycor] of goal-spot [
set color red
]
]
ask turtles [pd]
reset-ticks
end
运动:
to go
; Any turtles that are not on the safe spot, or share a patch
ask turtles with [not member? patch-here safe-patches or any? other turtles-here] [
let target nobody
; If below the goal spot, target it. If not, target a free safe patch
ifelse pycor < [pycor] of goal-spot [
set target goal-spot
] [
set target min-one-of ( safe-patches with [
not any? other turtles-here ] ) [distance myself
]
]
; Face target, move to it.
if target != nobody [
face target
move-to patch-ahead 1
]
]
; Stop when all turtles are safe and on their own patch
if not any? turtles with [
not member? patch-here safe-patches or
any? other turtles-here
] [
stop
]
tick
end
希望这能让你开始!
行为示例:
编辑
根据您的评论-是的,您可以这样做。当然有很多方法可以解决这个问题,但实际实现会根据您的实际模型和意图等而改变。例如,这里有一个使用两个安全区域的修改:
globals [ goal-spots safe-patches ]
to setup
ca
; Set the walls
ask ( patch-set
patches with [ pycor = max-pycor ]
patches with [ pxcor = max-pxcor ]
)
[
set pcolor blue
]
let possible-safe patches with [ pcolor = black ]
; Set up two triangular safe-patches
crt 1 [
set heading 0
fd 7
set goal-spots patch-here
set safe-patches possible-safe in-cone 20 90
set heading 90
setxy 7 0
set goal-spots ( patch-set goal-spots patch-here )
set safe-patches (
patch-set
safe-patches
possible-safe in-cone 20 90 )
die
]
ask safe-patches [
set pcolor green - 4
]
; Make some turtles
let n-turtles 100
if n-turtles > count safe-patches [
set n-turtles count safe-patches
]
crt n-turtles [
set shape "person"
set color green
move-to one-of patches with [ pycor < 0 and pxcor < 0 ]
set color red
]
ask turtles [pd]
reset-ticks
end
to go
; Any turtles that are not on the safe spot, or share a patch
ask turtles with [not member? patch-here safe-patches or any? other turtles-here] [
let target nobody
; If below the goal spot, target it. If not, target a free safe patch
ifelse not member? patch-here safe-patches [
set target min-one-of goal-spots [ distance myself ]
] [
set target min-one-of ( safe-patches with [
not any? other turtles-here ] ) [distance myself
]
]
; Face target, move to it.
if target != nobody [
face target
move-to patch-ahead 1
]
]
; Stop when all turtles are safe and on their own patch
if not any? turtles with [
not member? patch-here safe-patches or
any? other turtles-here
] [
stop
]
tick
end
请注意,这只是一种存在明显问题的方法的简单示例(例如,如果您在一块土地上有太多海龟,即使在满了)。我想说,如果你能以某种方式定义你的两个安全区域然后遇到问题,那么值得发布一个新问题来弄清楚如何解决你遇到的任何新问题。