NetLogo:要求海龟从补丁集执行计算 [英] NetLogo: ask turtle to perform calculations from patch-sets
问题描述
我正在尝试掌握 NetLogo 的窍门,需要一些帮助.我让海龟根据哪个最有价值"来挑选补丁.价值取决于选择补丁的收益与成本,所以我需要海龟做一些数学运算.基本上,海龟需要:
I'm trying to get the hang of NetLogo and am in need of some assistance. I'm having turtles pick patches based on which is most "valuable." Value is determined by benefits versus costs of selecting patches, so I need turtles to do a bit of math. Basically, a turtle needs to:
- 确定可用补丁的补丁集(尚未拥有的补丁).
- 确定每个可用补丁对我的好处.每个补丁都有 0-1 的好处.为了确定对我的好处,海龟应该计算可用补丁的平均收益加上该补丁在半径 2 内的相邻补丁(以移动窗口方式识别一组高收益补丁).
- 确定每个可用补丁的成本.现在,这是瓦片与海龟的距离.(在此模型的未来版本中,Cost-to-me 将包括其他注意事项.)
- 计算哪个补丁最有价值,作为对我的好处/对我的成本.
这行得通(或者我认为——这是在做我认为的事情吗?):
This works (or so I think--is this doing what I think it is?):
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
我如何完成这段代码才能达到预期的效果?而且我猜可能有多种方法来处理这种类型的编码.鉴于海龟会重复寻找最高值"数千次,什么选项可能运行得最快?提前致谢!
How do I complete this code to achieve the desired effect? And I'm guessing there could be multiple ways to approach this type of coding. Given that turtles will repeat finding "highest-value" thousands of times, what option is likely to be fastest to run? Thank you in advance!
尝试修改代码:(以下是 Luke 的回答的后续.)
Attempted revision to code: (Following up from Luke's answer, below.)
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
推荐答案
似乎在你的最高价值"报告器中使用 distance own
可能有问题——你不应该得到划分除非引用的两个代理之间的距离实际上为 0,否则错误为零,因此看起来您的可用补丁包括询问海龟当前所在的补丁.如果您使用海龟的who
来指定所有权,请记住几乎总是有一个turtle 0
围绕谁将拥有"所有owner = 0"的补丁.例如,在初始设置中,通过让补丁将其所有者"变量设置为 -1 或 nobody 来避免这种情况.
It seems like the use of distance myself
in your "highest-value" reporter might be problematic- you should not get a divide-by-zero error unless the distance between the two agents referenced is actually 0, so it looks like your available patches are including the patch that the asking-turtle is currently on. If you are using a turtle's who
to designate ownership, remember that there is almost always a turtle 0
around who will "own" all patches with "owner = 0". Avoid this by having patches set their "owner" variable to -1 or nobody, for example, in the initial setup.
如果补丁的好处不是基于海龟本身,您可以通过减少 distance
部分,让您的报告器只是该补丁的移动窗口值"来简化一点记者的.这样,海龟就可以查询补丁的移动窗口值除以距离.快速示例:
If benefit of a patch is not based on the turtle itself, you could simplify a bit by having your reporter be just the "moving-window value" of that patch, by cutting the distance
part of the reporter. That way, a turtle can just query the patch for its moving-window-value divided by distance. Quick example:
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
就分离成本和收益而言 - 你可以,但如果成本只是距离,你不需要计算它,因为它已经是一个原始值.除非您使用其他一些坡度,如海拔、地形类型等,否则您可能无需存储距离成本就可以逃脱.
As far as separating out the cost and benefit- you could, but if cost is just distance, you don't need to calculate it as it's already a primitive. Unless you're using some other gradient like elevation, terrain type, etc, you can probably get away without storing your distance cost.
这篇关于NetLogo:要求海龟从补丁集执行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!