Netlogo 排成一行并由空补丁分隔的Net徽标代理必须死亡
想象一下,我有一只同一品种的乌龟,它的位置如下图所示,被一块空白区域隔开Netlogo 排成一行并由空补丁分隔的Net徽标代理必须死亡,netlogo,Netlogo,想象一下,我有一只同一品种的乌龟,它的位置如下图所示,被一块空白区域隔开 1234 56 78 9 <br/> AAAA AA AA A 下面是我用来杀死turle的代码: ask virus-on patch in_xcor in_ycor [ ask link-neighbors [die] die ] 有一件事是肯定的,那就是你不需要经历在你要杀死的海龟之间建立联系的麻烦。你的主要问题是确定哪些海龟需要死亡,这可以通过一个递归过程来完成:你有一只需要死
1234 56 78 9 <br/>
AAAA AA AA A
下面是我用来杀死turle的代码:
ask virus-on patch in_xcor in_ycor [
ask link-neighbors [die]
die
]
有一件事是肯定的,那就是你不需要经历在你要杀死的海龟之间建立联系的麻烦。你的主要问题是确定哪些海龟需要死亡,这可以通过一个递归过程来完成:你有一只需要死亡的目标海龟,但你希望它让它的邻居也这么做(他们反过来也要问他们的,等等) 这个过程稍微复杂一点,因为在递归过程中杀死一只乌龟会把事情搞得一团糟,但是你可以通过使用一个海龟变种来改变它,它是一个标记需要杀死的海龟的标志,然后杀死所有的海龟。 以下代码应提供一个完整的工作示例:
turtles-own [ marked-for-death ]
to setup
clear-all
ask patch 1 0 [ sprout 1 ]
ask patch 2 0 [ sprout 1 ]
ask patch 3 0 [ sprout 1 ]
ask patch 4 0 [ sprout 1 ]
ask patch 6 0 [ sprout 1 ]
ask patch 7 0 [ sprout 1 ]
ask patch 9 0 [ sprout 1 ]
end
to go
ask turtles [ set marked-for-death false ]
ask turtles-on patch 2 0 [ mark-for-death ]
ask turtles with [ marked-for-death = true ] [ die ]
end
to mark-for-death
set marked-for-death true
ask (turtles-on neighbors) with [ marked-for-death = false ] [ mark-for-death ]
end
这个例子杀死了补丁2 0上的海龟,以及所有与之相关的海龟。你将需要调整它为自己的目的,但它应该足以让你去
编辑:
稍微优雅一点的版本,因为它不需要turtle变量(假设相同的设置过程):
turtles-own [ marked-for-death ]
to setup
clear-all
ask patch 1 0 [ sprout 1 ]
ask patch 2 0 [ sprout 1 ]
ask patch 3 0 [ sprout 1 ]
ask patch 4 0 [ sprout 1 ]
ask patch 6 0 [ sprout 1 ]
ask patch 7 0 [ sprout 1 ]
ask patch 9 0 [ sprout 1 ]
end
to go
ask turtles [ set marked-for-death false ]
ask turtles-on patch 2 0 [ mark-for-death ]
ask turtles with [ marked-for-death = true ] [ die ]
end
to mark-for-death
set marked-for-death true
ask (turtles-on neighbors) with [ marked-for-death = false ] [ mark-for-death ]
end
to go
let marked [ marked-for-death [] ] of turtles-on patch 2 0
ask turtle-set marked [ die ]
end
to-report marked-for-death [ marked ]
set marked fput self marked
ask (turtles-on neighbors)
with [ not (member? self marked) ]
[ set marked marked-for-death marked ]
report marked
end