Netlogo 如何将两只海龟放在同一位置?
我正试图用一种不同的方法来模拟网络徽标库中的用餐哲学家。我试图创造一种情况,在一个圆圈里有20个哲学家,每个哲学家前面都有一个“叉子”。哲学家要么吃饭,要么思考,要么挨饿。他们只能用两个叉子吃饭,吃完后,他们放下叉子思考,直到饿了为止。我试图让饥饿哲学家范围内的两个叉子移动到各自的哲学家,但我不知道如何做。 以下是我目前的代码:Netlogo 如何将两只海龟放在同一位置?,netlogo,Netlogo,我正试图用一种不同的方法来模拟网络徽标库中的用餐哲学家。我试图创造一种情况,在一个圆圈里有20个哲学家,每个哲学家前面都有一个“叉子”。哲学家要么吃饭,要么思考,要么挨饿。他们只能用两个叉子吃饭,吃完后,他们放下叉子思考,直到饿了为止。我试图让饥饿哲学家范围内的两个叉子移动到各自的哲学家,但我不知道如何做。 以下是我目前的代码: breed [philosophers philosopher] breed [forks fork] philosophers-own [think
breed [philosophers philosopher]
breed [forks fork]
philosophers-own [thinking eating hungry]
globals [x y]
;eating = green
;thinking = blue
;hungry = red
to setup
ca
cro num-philosophers [set breed philosophers
fd 10 set shape "person-1"
set color blue
ask philosophers [
set hungry hungry = false
set thinking thinking = true
set eating eating = false]
set size 3]
cro num-philosophers [set breed forks fd 8
set heading heading + 180 / num-philosophers
fd -1
lt 180
set shape "fork"
set color grey
set size 2.5
]
reset-timer
end
to go
move
end
to move
every .1 [
ask philosophers with [who mod 2 = 0] [set color red
set hungry hungry = true
set thinking thinking = false
set eating eating = false]
ask philosophers with [hungry = true] [
;this following line with in-radius was my attempt to move the forks but it doesn't work
ask [forks in-radius 4] of philosophers with [hungry = true] [setxy x y]
ask fork 21 [setxy x y]
set y [ycor] of one-of philosophers with [hungry = true]
set x [xcor] of one-of philosophers with [hungry = true]
]]
end
任何关于如何解决此问题的建议都将不胜感激!谢谢大家! 第一个问题是您的行,如
set hunger=false
。在NetLogo中,可以指定一个不带等号的变量值。假设您想将名为'hungry'的变量设置为false
,那么您的代码应该是set hungry false
。此外,按照惯例,NetLogo布尔变量名在末尾使用问号(以提醒您它们是布尔的),因此最好将设置为空?false
并相应地更改语句
这将导致部分错误,因为Hunger的值被测试为true或false,但您没有指定true或false。因此,if
语句将始终为false
其次,因为你基本上是从叉子的角度来做移动,所以最好是询问叉子
,而不是询问哲学家
。也许是这样的:
ask forks
[ let targets (philosophers in-radius 4) with [hungry?]
if any? targets
move-to target with-min [distance myself]
]
此代码未经测试。最基本的方法是用叉子检查距离4以内是否有饥饿的哲学家。如果有,叉移动到最近的位置。在NetLogo字典中查找移动到
。即使这不是你想要的答案,也可能是你想要的原始答案。你不需要从一只海龟那里得到xcor和ycor并将它们传递给另一只海龟,你只需移动到海龟(或者面对海龟,然后向前移动一点)
最后,我建议您更加循序渐进地构建代码。例如,如果哲学家离叉子的距离在4以内,可以将其变成红色。然后你就可以担心搬家了
在另一个问题上,您实际上不太可能希望使用每个
。仅当您希望每个时间步都具有实时性(例如秒数)时,才可以执行此操作。相反,您应该用勾选来增加时钟。您的模型将运行得更快,因为它将受到所需处理量的限制,而不是在现实世界中跟踪时间