NetLogo:请乌龟从补丁集

时间:2017-03-20 15:25:02

标签: netlogo

我正试图抓住NetLogo,需要一些帮助。我有海龟选择基于哪个最有价值的补丁。价值取决于利益与选择补丁的成本,因此我需要海龟做一些数学运算。基本上,乌龟需要:

  1. 确定一组可用补丁(尚未拥有的补丁)。
  2. 确定每个可用补丁的好处。每个补丁都有0-1的优势。为了确定对我的好处,乌龟应该计算可用补丁的平均收益加上该补丁在半径2内的邻近补丁(以移动的窗口方式识别一组高效益补丁)。
  3. 确定每个可用修补程序的成本。现在,这是补丁与乌龟的距离。 (费用对我将在此模型的未来版本中包含其他注意事项。)
  4. 计算哪个补丁对我来说是最有价值的,对我来说是有益的。
  5. 这是有效的(左右我认为 - 这就是我认为的那样吗?):

    patches-own [
        benefit
        owner ]
    
    to go 
        ask turtles [pick-patch]
    end
    
    to pick-patch
        move-to highest-value
    end
    
    to-report highest-value 
        let available-destinations patches with [owner = 0]  ;; I think this accomplishes step 1.
        report max-one-of available-destinations [(mean [benefit] of patches in-radius 2) / (distance myself + 1)]  ;; and I believe(?) this accomplishes step 2-4. (Note, "distance myself + 1" seems required since can't divide by 0.)        
    end
    

    但我想分出利益和费用部分:

    to-report highest-value 
        let available-destinations patches with [owner = 0]  
        let benefit-to-me ...??  ;; code to assess "(mean [benefit] of patches in-radius 2)" for the patch-set of available-destinations?
        let cost-to-me ...??  ;; and code to assess "(distance myself + 1)" of available destinations?
        report max-one-of available-destinations (benefit-to-me / cost-to-me) ...??  ;; code to perform calculations based on benefit-to-me and cost-to-me?          
    end
    

    如何完成此代码以达到预期的效果?我猜测可能有多种方法来处理这种类型的编码。鉴于海龟将重复发现数千次“最高价值”,哪种选择可能最快?提前谢谢!

    尝试修改代码:(接下来是Luke的回答。)

    patches-own [
        benefit
        owner ]
    
    to setup
        ask patches [ set owner nobody ]
    end
    
    to go 
        ask turtles [pick-patch]
        tick
    end
    
    to pick-patch                      ;;<-----noticing turtle will change _destination midway to traveling there...why?
        let _destination highest-value
        ifelse _destination != nobody [
        ask _destination [set pcolor red]  ;; added this as a check, and yes, red patch switches around before the turtle gets there sometimes.
        face _destination forward 1  
          if patch-here = _destination
            [ claim-patch _destination ]
        ]
        [stop] ;; if there is no _destination
    end
    
    to claim-patch [_patch]
         ask _patch [set owner myself]
         ;; and etc., turtle does several more things to claim patch, e.g., turn patch color of turtle
    end
    
    ;;;; --reporters for calculations:-- 
    
    to-report highest-value 
         let available-destinations patches with [owner = nobody]  
         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 2
    end
    
    to-report cost-to-me
         report distance myself
    end
    

1 个答案:

答案 0 :(得分:1)

似乎在您的“最高价值”记者中使用distance myself可能会出现问题 - 除非引用的两个代理之间的距离实际为0,否则您不应该得到除零错误,因此看起来你的可用补丁包括了ask龟正在使用的补丁。如果你使用乌龟的who来指定所有权,请记住,几乎总有一个turtle 0周围的人将拥有“所有者= 0”的所有补丁。通过将补丁设置为“所有者”变量为-1或无人(例如,在初始设置中)来避免这种情况。

如果补丁的好处不是基于乌龟本身,你可以通过削减报告的distance部分让你的记者只是该补丁的“移动窗口价值”来简化一点。 。这样,乌龟可以查询补丁的移动窗口值除以距离。快速举例:

patches-own [
    benefit
    owner ]


to setup
  ca
  reset-ticks

  ask patches [ 
    set owner nobody 
    set benefit random-float 1 
    set pcolor benefit + 54
  ]

  crt 5 [ setxy (random 30 - 15)(random 30 - 15)
    ask patch-here [ 
      set owner [who] of myself
      set pcolor [color] of myself
    ]
  ]

end


to go 
    ask turtles [
      pick-patch
    ]
    tick
end


to-report moving-window-value 
  report mean [benefit] of patches in-radius 2  
end


to pick-patch

  let available-destinations patches with [ owner = nobody ]
  let best-patch max-one-of available-destinations [ moving-window-value / (distance myself) ]
  if best-patch != nobody [
    move-to best-patch 
    ask patch-here [ 
      set owner [who] of myself
      set pcolor [color] of myself
    ]
  ]
end

至于分离成本和收益 - 你可以,但如果成本只是距离,你不需要计算它,因为它已经是原始的。除非您使用其他梯度,如高程,地形类型等,否则您可以在不存储距离成本的情况下离开。