问题
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.
Cheers Paul
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
回答1:
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:
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.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, sincepatch-ahead 0.1
andpatch-ahead 0.2
, for example, may very well be the same patch.It uses patch-set to turn that list in a proper agentset of patches, without duplicates.
(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.)
To use it, just replace instances of patches in-cone 10 1
in your code by something like patches-ahead 10 0.1
.
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.
回答2:
Nicolas has a much better answer solving the problem of looking in a straight line but if you simply what look at the patch directly ahead use patch-ahead 1 it works at all angles and coordinates and is much faster than in-cone.
Completely an aside but probably the reason you found this bug is because your cone was set to 1 degree wide and 10 patches long. Long narrow cones tend to break up.
来源:https://stackoverflow.com/questions/26798947/in-cone-only-works-for-patch-centers-netlogo