Check Neighbors Attribute - netlogo

How can I reference the neighbor turtle in the following statement?
if count(turtles-on patch-ahead -1) with [(target-speed) > (target-speed)] > 0
I would like to compare the speed of the neighbor turtle to that of self.

instead of count agentset > 0, try if any? agentset
You want to use [target-speed] of myself
So, to put it together.
if any? turtles-on patch-ahead -1 with [target-speed > [target-speed] of myself]

Related

Is there a way to create a modification of Breed-On that includes specific characteristics?

I want my forager turtles to follow a path laid down by leader turtles, and this path consists of a turtle breed called trail-markers. The forager turtles also lay down trail-markers (to reinforce the trail), so I would like them to check for leader trail-markers on some patch/patches. I know Netlogo includes breeds-on to check for the presence of members of a breed, but I need the foragers to check for blue leader trail-markers. Is there a way to do this?
Here is what I had in mind:
if count (trail-markers with [color = blue]) on patch-ahead 1 > 0 [fd 1]
But I got, error:nothing named on has been defined, which makes sense, since the Netlogo primitive is breeds-on, so I modified it to:
if any? (trail-markers with [color = blue])-on patch-ahead 1 [fd 1]
However, I simply received: error:nothing named -on has been defined instead.
See the code below. I think you just want to use trail-markers-on, but you can simplify by using any? a bit. And you might want to have the custom reporter so you can just check for blue-markers-on if you find that simpler.
breed [ trail-markers trail-marker ]
to example
; I think this is what you want
if count ((trail-markers-on patch-ahead 1) with [color = blue]) > 0 [ forward 1 ]
; this is slightly simpler, using `any?` instead of `count ... > 0`
if any? (trail-markers-on patch-ahead 1) with [color = blue] [ forward 1 ]
; you could also write a custom reporter if you'll need to get this info a lot
if any? blue-markers-on patch-ahead 1 [ forward 1 ]
end
to-report blue-markers-on [p]
report (trail-markers-on p) with [ color = blue ]
end

How to report value of neighbor in Netlogo?

I am trying to create a list to be collected with the behaviorspace that reports the color of a turtle's neighbor.
to-report north-color1
set north-color []
foreach sort turtles [the-turtle -> set north-color lput [color] of neighbors4 north-color]
report north-color
end
the "foreach" line is to ensure that the order of the list follows turtle 0, turtle 1, turtle 2, etc in sequential order. However, I want my list to output the color of their neighbor above like [10, 20, 10, 20, 30...] How can I achieve this?
This is a complete model. Just change the print north-color-map to print north-color to try out the foreach version.
to testme
clear-all
ask patches [if random-float 1 < 0.8 [sprout 1]]
print north-color-map
end
to-report north-color
let outlist []
foreach sort-on [who] turtles
[ this-turtle -> ifelse any? turtles-on [patch-at 0 1] of this-turtle
[ set outlist lput [color] of one-of turtles-on [patch-at 0 1] of this-turtle outlist ]
[ set outlist lput "none" outlist ]
]
report outlist
end
to-report north-color-map
report map
[ this-turtle -> ifelse-value any? turtles-on [patch-at 0 1] of this-turtle
[ [color] of one-of turtles-on [patch-at 0 1] of this-turtle ]
[ "none" ]
]
sort-on [who] turtles
end
The foreach version is probably easier to understand. It follows fairly closely what you were trying to do - start with an empty list and then run through a list of all the turtles in who order (the sort-on [who] turtles creates that list) and calculates the colour of the turtle on the north patch. Finding north is done with patch-at 0 1 but you also have to say north of what - hence [patch-at 0 1] of this-turtle. And the one-of is to select one turtle from the set of all turtles on that patch - NetLogo can't tell that it will always have one or none so you would get an error essentially saying 'I don't know which turtle to find the colour of'.
The second version uses map. It does exactly the same but applies the function to all the members of a list without explicitly constructing the loop. The code is a little simpler because you don't need the empty list and the various lput statements. You also need to present everything as a reporter rather than a command. But map can be a little trickier to get your head around.

Netlogo: How to select a turtle with the lowest ID for each stagnant turtle in each specified patch in the world?

I have a model that keeps turtles in several patches each. I would like to select a turtle with the lowest ID for each stagnant turtle in each specified patch in the world.
For example, the answer is as follows. At patch coordinates (1, 0), five turtles stayed. And the ID of the turtle with the smallest ID was ID = 5 within the patch coordinates (1, 0).
The following is a sample program. But this program is not intended. Is there something good syntax instead of the syntax "min-one-of turtles [who]"? I want your advice. Thank you.
ask (turtles-on patch 1 0) [
ask min-one-of turtles [who] [
set flag-1 TRUE
]
I'm not sure what you mean by 'program is not intended'. Your problem is that you ask each of the turtles on patch 1 0 to identify the minimum who over all turtles. What you want is:
let targets (turtles-on patch 1 0)
[ ask min-one-of targets [who]
[ set flag-1 TRUE
]
]
If the only thing you are going to do with the turtles on that patch is to select the lowest who, you don't need to set up the agentset explicitly. Instead:
ask min-one-of (turtles-on patch 1 0) [who]
[ set flag-1 TRUE
]

NetLogo in-radius

i have this code and it's not clear to me what is it doing:
patches-own [ field ]
let a max-one-of patches in-radius b [field]
ifelse ([field] of a > 0.1) and ([field] of a < 0.5)
[
;; do something
]
[
;; do something else
]
Thanks,
Marco
This is apparently code to be run by a turtle or patch, it isn't apparent which.
patches in-radius b is an agentset of the circle of patches, of radius b, around the calling agent. max-one-of ... [field] finds the patch in that agentset that has the largest value for field. That patch is then stored in the new local variable a. (A better name than a might have been winner or peak or best-patch.)
[field] of a is then that maximum value of field, the same one that max-one-of found. The ifelse checks to see if that value is in a certain range or not, and does something different, depending.
Does the code inside the ifelse make any further use of a? If it does, cool. If it doesn't, well, the code could be more easily and simply written as:
let m max [field] of patches in-radius b
ifelse m > 0.1 and m < 0.5
[
;; do something
]
[
;; do something else
]
perhaps seeing it in this form will help making the meaning clear.

How to use the primitive "patch-right-and-ahead" when the world wrapping is disabled in the model settings?

I have a problem with the primitive "patch-right-and-ahead". When the world wrapping is disabled in the model settings, I obtain the following error message:
OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
when I do :
if [pcolor] of patch-here = brown [
if [pcolor] of (patch-right-and-ahead 90 1) = brown [
move-to patch-right-and-ahead 90 1 ] ]
Thanks in advance for your help.
When a primitive that normally returns an agent can't find an appropriate agent to return, it will return nobody. Here, there is no patch that's right and ahead of the current agent (er, since the angle is 90, I guess just right of). Thus, it's returning nobody. You need to check to make sure it's not nobody before using of on it:
if [pcolor] of patch-here = brown [
let target-patch patch-right-and-ahead 90 1
if target-patch != nobody and [pcolor] of target-patch = brown [
move-to target-patch
]
]