【问题标题】:NetLogo: ask turtle to set destination and keep walking towards it until reachedNetLogo:让乌龟设置目的地并继续朝它走直到到达
【发布时间】: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


【解决方案1】:

使用while 的困难在于它会在滴答声中全程移动。既然海龟返回到起始补丁,为什么不简单地添加一个条件,即它只在起始补丁处挑选目的地?所以代码看起来像这样:

to go
   tick
   ask turtles
   [ if patch-here = start-patch [pick-patch]
   ]
end

【讨论】:

  • 同意——我的想法是家庭范围选择(我从用户的早期模型中了解到)独立于其他海龟,因此刻度对于这方面并不重要。但是,是的,这是更好的答案!
【解决方案2】:

你是对的-每次乌龟运行pick-patch,它都会通过将目标设置为highest-value的步骤。然后,它会向前移动一个并检查它是否已经到达。此时,无论它是否已经到达目的地,其他海龟(如果有的话)将有机会运行pick-patch。一旦所有其他海龟都这样做了,您的原始海龟将再次将其目的地设置为新评估的最高值。因此,由于highest-value 取决于距离,并且海龟的空间坐标会随着它的移动而变化,因此其他一些补丁可能具有来自海龟新位置的highest-value

你可以完成你所追求的一种方法是使用while,这样你的海龟就会停留在过程中,直到达到你指定的任何标准。举个很简单的例子:

to move-until
  ask turtles [
    let start-patch patch-here
    let destination patch-ahead 10
    while [ distance destination > 1 ] [
      fd 1
    ]
  ]
end

显然你必须修改它以满足你的需要,但它应该让你开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多