【发布时间】:2017-03-23 19:20:13
【问题描述】:
目标:我试图让乌龟选择一个目的地,然后继续朝它走去,直到到达目的地。那时,乌龟回到原来的补丁并选择另一个目的地,走向它,重复。
问题:当海龟走向它时,选定的目的地有时会发生变化。我需要一些方法来告诉乌龟保持原来的目的地,直到它到达它。
详情:这是我的相关代码。海龟正在建造领地。他们有一个领土中心点(“起点补丁”),他们可以从该中心点选择步行到并声称的目的地。目的地基于具有“最高价值”的补丁,其中价值应该是补丁的收益(“对我的收益”)除以距起始补丁的距离(“对我的成本”)。然而,我认为海龟在行走时会不断地重新评估我的成本。他们不应该这样做 - 必须在站在起始补丁上时评估最高值。
我该如何解决这个问题,以便乌龟站在起始补丁上时评估最高值,设置目的地,然后朝着它移动直到到达?
patches-own
[
owner ;; once part of a territory, owner becomes the turtle.
benefit ;; i.e., food available in a patch; used to assess "highest-value" to the turtle.
]
turtles-own
[
start-patch ;; the territory center; turtle returns here after reaching destination.
destination ;; the patch turtle wants to claim for its territory.
territory ;; the patches the turtle owns.
]
to go
tick
ask turtles
[
pick-patch
]
end
to pick-patch
set destination highest-value ;; calculated in reporters, below.
ifelse destination != nobody [
ask destination [set pcolor red] ;; reveals that destination changes occasionally before original destination is reached.
travel]
[give-up] ;; (will reposition start-patch to a new site if no destinations available.)
end
to travel
face destination forward 1 ;; **should** keep original destination, but it doesn't.
if patch-here = destination
[update-territory
move-to start-patch ] ;; return to the start-patch, and should only NOW assess new destination.
end
to update-territory
set owner self ;; and so on....
end
;;;---Reporters for highest-value:---
to-report highest-value ;; this appears to be changing while turtle moves...how fix this?
let available-destinations edge-patches
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 1 ;; i.e., moving window to find high-benefit cluster
end
to-report cost-to-me
report distance myself
end
to-report edge-patches
report (patch-set [neighbors4] of territory) with [owner = nobody]
end
(注意:而不是“forward 1”,我意识到我可以只使用“move-to”。但是,我最终会设置障碍物,并且海龟需要走向目的地检查障碍物。)
更新:我认为这个问题可以在“我的成本”报告者中解决?我尝试进行此更改:
to-report cost-to-me
report distance [start-patch] of myself
end
这应该完成我所追求的吗?它会消除“我自己的距离”部分,使这个成本保持不变。我的另一个想法是“pick-patch”或“travel”可能需要类似于“ifelse patch-here != destination [forward 1...]”的东西,但这似乎也不起作用.
我尝试了下面推荐的“while”循环想法(谢谢!),这似乎引入了一系列新的奇怪行为。如果我走那条路,我不确定如何编码。像这样的东西不起作用(他们只是停止移动):
to travel
while [distance destination > 1]
[face destination forward 1]
if patch-here = destination
[update-territory
move-to start-patch ]
end
我是新手;提前感谢您的帮助!
第二次更新:我认为我在上一次更新中所做的更改(报告我自己的距离 [start-patch])解决了我的部分问题(假设行有意义吗?),但留下了一个问题。如果具有最高值的补丁之间存在平局,乌龟仍然会在中途将目的地切换到其选定的补丁。所以它仍然回到了设置乌龟并保持目的地直到到达的原始问题。有想法该怎么解决这个吗?
【问题讨论】:
-
听起来你的海龟仍然在每一步检查最高价值的补丁。因此,您需要一种方法让它们只设置一次目标(如@JenB 所说),这就是 while 循环所做的 - 海龟在到达目标之前不会“退出”while 循环。但是,如果您更喜欢使用
if,只需打破您的程序 - 就像@JenB 的回答一样。
标签: netlogo