In-Cone仅适用于补丁中心Netlogo [英] In-Cone only works for patch centers Netlogo
问题描述
我在Netlogo中的in-cone命令遇到了一些问题.我正在尝试直接在乌龟当前位置之前确定所有补丁变量的总和/均值(即,它穿过的所有变量的总和).但是,这仅在乌龟位于补丁中心时才有效(坐标是整数而不是小数),这也意味着我只能以直角移动乌龟.我尚未在Stackoverflow或其他地方找到与同一问题有关的任何其他问题.因此,如果有人可以提供一些见识,我将不胜感激.
I'm having some issues with the in-cone command in Netlogo. I am trying to identify the sum / mean of all the patch variables directly in front of my turtles current location (ie the sum of all the variables it crosses). However, this only appears to be working when my turtle is at the center of a patch (co-ordinates are integers not decimals), which also means I can only move my turtles at right angles. I'm yet to find any other questions pertaining to the same issue on Stackoverflow or elsewhere. So if anyone could offer some insight, I'd be greatly appreciative.
下面是简单的示例代码.而且我已经注释了进行更改的位置,这导致此功能无法正常工作.
Below is the simple sample code. And I've annotated where making the changes causes this to not work.
欢呼 保罗
turtles-own [value]
patches-own [value-test]
to Set-Up
ca
reset-ticks
ask patches [if pycor > 150 [set value-test 1]]
ask patches [if pxcor > 150 [set value-test 1]]
ask patches [if value-test = 1 [set pcolor red]]
create-turtles 1
ask turtles[
;It works when the turtle is created at the origin (0 0), or at defined coordinates (but not random-xcor random-ycor)
;setxy random-xcor random-ycor
set value 0
set size 10
set color yellow]
end
to go
ask turtles[
;heading has to be 0, 90, 180, or 270.
set heading 270]
ask turtles[
let test mean [value-test] of patches in-cone 10 1
print test
print xcor
print ycor
ask patches in-cone 10 1 [set pcolor blue]
forward 10]
end
推荐答案
in-cone
不是适合该工作的工具.不幸的是,NetLogo没有一个可以直视前方的原语.但是,它确实有 patch-ahead
,其中报告了给定距离的单个补丁.我们可以用它来构建与您想要的东西类似的东西:
in-cone
is not the right tool for the job. Unfortunately, NetLogo doesn't have a primitive that looks ahead in a straight line. It does, however, have patch-ahead
, which reports a single patch at a given distance. We can use that to build something similar to what your looking for:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
乍一看,这段代码似乎令人费解,但实际上它很简单:
This code may look puzzling at first, but what it does it actually quite simple:
-
它使用
n-values
进行构建递增值列表:n-values (dist / step) [ step + ? * step ]
.例如,如果dist
是1
,而step
是0.2
,则将得到[0.2 0.4 0.6 0.8 1]
.这些值表示我们要寻找补丁的距离.
It uses
n-values
to build a list of incrementing values:n-values (dist / step) [ step + ? * step ]
. For example, ifdist
was1
andstep
was0.2
, you'd get[0.2 0.4 0.6 0.8 1]
. These values represent the distances at which we are going to be looking for a patch.
它使用 map
来调用 patch-ahead
补丁.请注意,此列表可以包含重复的补丁程序,尤其是如果step
很小时,因为例如patch-ahead 0.1
和patch-ahead 0.2
可能是同一补丁程序.
It uses map
to call patch-ahead
for each of values in the list and build a list of patches. Note that this list can contain duplicate patches, especially if step
is small, since patch-ahead 0.1
and patch-ahead 0.2
, for example, may very well be the same patch.
它使用 patch-set
转列出了正确的补丁代理集,没有重复.
It uses patch-set
to turn that list in a proper agentset of patches, without duplicates.
(您可以通过while
循环和递增计数器来实现相同的功能,但是代码会更长,更容易出错并且不太优雅.)
(You could achieve the same thing with a while
loop and an incrementing counter, but the code would be longer, more error prone, and much less elegant.)
要使用它,只需将代码中patches in-cone 10 1
的实例替换为patches-ahead 10 0.1
.
To use it, just replace instances of patches in-cone 10 1
in your code by something like patches-ahead 10 0.1
.
您会注意到,精度和速度之间需要权衡:step
越小,跳过"补丁的一角的可能性就越小,但是运行所需的时间就越长.但是,我敢肯定,您会找到适合自己的价值.
You will notice that there is a trade-off between precision and speed: the smaller step
is, the less likely it is to "skip" the corner of a patch, but the longer it will take to run. But I'm sure that you can find a value that works well for you.
这篇关于In-Cone仅适用于补丁中心Netlogo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!