Comparing patches - netlogo

I want to compare the patches in a certain radius regarding the amount of a certain class of agents on them. The agents should move to the patch where the most agents (in this case humans) are. If they are already on the patch with the most humans then they must not move. I coded it and the humans group but most of them don't stay and run around in lines (one behind the other). Would be great if anyone of you could have a quick look at my code. Thanks
if Strategy = "Gathering-Simple" [
if ((count(humans-on max-one-of patches in-radius rad [count(humans-here)] )) ) >= count(humans-here) [
if count(humans-on patches in-radius rad) - count(humans-here) > 0 [
face max-one-of patches in-radius rad [count(humans-here)]
fd 1
]]
]

This is a complete working example that uses your code. Is this displaying the behaviour you mean? It does have turtles chasing each other.
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch >= count turtles-here ; comment 1
[ if count turtles-on patches in-radius rad > count turtles-here ; comment 2
[ face target-patch
forward 1
]
]
]
tick
end
If so, have a look at the two lines I have comments on.
Comment 1: The >= means that, even if the turtles are already on the highest count patch, this condition will be satisfied because count turtles-here will be equal to the count of the turtles on the highest count patch (this patch).
Comment 2: This line means that as long as there are any turtles on any patch within the radius but not on the particular patch where the asking turtle is located, then the turtle will move forward.
If you want to only have turtles move if not on a maximum count patch, try this instead:
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch > count turtles-here
[ face target-patch
forward 1
]
]
tick
end
I took out the = in the comment 1 line and removed the second condition entirely so now the turtles move if their current patch has fewer (strictly, not <=) turtles than the patch they've spotted.

I agree with the previous post, but have some additional information.
If you want to move entirely to the target patch on each iteration, instead of moving just one step towards the target patch, in the above answer you could replace the code which produces one step of motion
[ face target-patch
forward 1
]
with
[
move-to target-patch
]
I confirmed by experimentation that the results of the two methods of moving will produce similar but somewhat different results.

Related

Is there a way to kill a certain number of turtles within a radius of a patch with a certain color in Netlogo?

I am very new to Netlogo and I am trying to simulate culling of turtles within a certain region based on the characteristics of the patch. Within my model landscape, I have one single patch that is orange. I want 50% of turtles within 5 pixels of that patch to die. I would also like this process to continue every year after the first year of the simulation (this part I think I have). Can anyone help me?
if d = 10 [
if year >= 2 [
let ring nobody
set ring patches in-radius 5 patches with [pcolor = orange]
ask turtles-on ring [die]
]
]
in-radius reports agents that are within a certain distance from the caller, so the orange patch must be the one calling in-radius.
The code in your example is not reproducible, so I'll make an arbitrary example:
to setup
clear-all
ask one-of patches [
set pcolor orange
]
create-turtles 100 [
setxy random-xcor random-ycor
]
end
to go
ask patches with [pcolor = orange] [
ask turtles-on patches in-radius 5 [
if (random 2 < 1) [die]
]
]
end
Note that in-radius reports agents within a certain distance, where the unit of measure of distance is a patch`s side.
Identifying patches within a certain distance and then asking all turtles on those patches to die (as in the example above, which follows the approach in your question) has a different effect than asking all turtles within a certain distance to die.
Which of the two approaches fits your case is something that you have to decide. Below, the code that would address turtles directly:
to go
ask patches with [pcolor = orange] [
ask turtles in-radius 5 [
if (random 2 < 1) [die]
]
]
end

How to create a given number of turtles based on a distance criterion

We're creating code and we're having trouble doing the following:
We would like to create a specific number of turtles (for example 100 turtles) based on the following criteria:
distance between agents must be greater than or equal to 2
We've already tried:
to setup
ask n-of 100 patches [
if not any? turtles in-radius 2 [
sprout-turtles 1 [ setup-turtles ] ]
]
]
end
to setup-turtles
set energy 0
set size 1
set color orange
end
it didn't work, because less than 100 agents are being born even though the world holds the required amount of agents, which in this case is 100
Does anyone have any suggestions on how we can resolve this issue?
Thanks in advance
The approach you outline is running into issues because you are asking patches to sprout turtles if they meet some condition. Because the patches are operating in a random sequential order, you are selecting some patches that, by the time it is their turn to act, no longer fulfill the condition to sprout since other nearby patches have already sprouted a nearby turtle.
One option is to use a while loop to keep trying to sprout turtles until the desired number is reached:
to setup
ca
while [ count turtles < 100 ] [
ask one-of patches with [ not any? turtles in-radius 2 ] [
sprout 1
]
]
reset-ticks
end
Be careful with while loops- if you do not code such that the while condition can eventually become false, your model will run forever stuck in the loop.
Another option that will give a more explicit error if it fails would be to just create your number of turtles, then have them move to a space that fulfills the condition:
to setup-2
ca
crt 100 [
move-to one-of patches with [ not any? turtles in-radius 2 ]
]
reset-ticks
end

How to limit number of turtles in a patch in NetLogo

I want to limit number of turtles per patch. I thought if I restrict movement of turtles as per the (1) and (2) conditions it will limit number of turtles per patch but whatever code I tried for this till now did not worked.
Let's suppose there are five turtles on patch Y and five is the limit.
1) to ask turtles standing at front on patch X (refer figure) to stop moving till there are five turtles on patch Y (refer figure).
2) to ask turtles standing at front on patch Y to move forward to patch z (refer figure) if patch z has less than five(5) turtles on it else stop.
At last I am using following simple code
let turtles-ahead other turtles in-cone speed 90
let turtle-ahead min-one-of turtles-ahead [distance myself]
ifelse turtle-ahead != nobody
[
set speed [speed] of turtle-ahead
slow-down
]
[speed-up]
This code simply ask turtles to move one-behind-another pattern or queue but it does not help me to limit number of turtles per patch whatever limit may be 4,5,6,7, 8... I have sprouted turtles within "go" procedures (1 turtle per patch, as per my need). The turtles are sprouted on a defined set of patches not in the whole world. So slowly number of turtles starts increasing and move around the world and after certain amount of ticks they are ask to exit out of the defined area and they die. Now at times it shows 10,11,.... 37 or above turtles on certain patches and this I want to stop actually.
I have checked one-turtles-per-patch, other code examples and many other helps from internet but no results.
For any other idea or help I would be obliged. Please help me.
I think you want to have turtles assess the count of turtles-here of the patch to which they are trying to move. Consider this simple example:
to setup
ca
ask n-of 15 patches with [ pycor = 0 ] [
sprout 3 [
set heading 90
]
]
reset-ticks
end
to go
ask turtles [
if ( count [turtles-here] of patch-ahead 1 ) < 5 and xcor < 16 [
fd 1
]
]
print [count turtles-here] of patches with [ any? turtles-here ]
tick
end
On each tick, the turtles with an xcor of less than 16 (just to set a stop for this example) all check the patch-ahead 1 for the count of turtles on that patch. If the count is less than 5, the turtle moves to that patch. Otherwise, the turtle does nothing.

define neighbor turtles using in-radius or distance

I would like to define neighbors using in-radius or distance. My solution so far is:
turtles-own [
my-neighbors
num-neighbors]
to setup
ca
crt 100 [
move-to one-of patches with [ not any? turtles-here ]
set my-neighbors (other turtles) in-radius 3
set num-neighbors count my-neighbors
]
end
The problem with this is that most of the turtles have between 0 to 4 neighbors, but a few of them have a relatively huge number of neighbors (e.g., 34 and 65). Those turtles are located close to the center of the world.
Any ideas about what I am doing wrong?
It has to do with the timing of side effects in your program.
Suppose that the very first turtle to move moves near the center. None of the other turtles have moved yet, so they're all still on patch 0 0 and set my-neighbors (other turtles) in-radius 3 will capture them all. And even after they move elsewhere, they will still be included in the first turtle's my-neighbors agentset.
You can avoid the problem by first moving all the turtles and then calculate their neighbors:
to setup
clear-all
crt 100 [
move-to one-of patches with [ not any? turtles-here ]
]
ask turtles [
set my-neighbors (other turtles) in-radius 3
set num-neighbors count my-neighbors
]
end

NetLogo - find minimum angle between two patches in patchset

I'd like to find the set of patches that constitute the convex hull around a set of patches belonging to the turtle's territory. I was planning to use a "gift wrap" procedure to calculate the convex hull (related to below picture).
I'm first finding the territory patch with the lowest pxcor to start at. Then, I'd like to find the territory patch that has the smallest heading (i.e., angle closest to zero) from the starting patch, and so on, until I arrive back at the starting patch. But I can't seem to figure out how to calculate headings between two patches. Any suggestions would be really helpful. Here is what I have so far. Eventually I will have to make this loop through each point along the outer hull.
patches-own [ owner-animal ]
turtles-own [ territory ]
to setup
ca
create-turtles 1
[
move-to patch-at (max-pxcor / 2) (max-pycor / 2)
set territory patches in-radius (2 + random 8)
ask territory [
set owner-animal myself
set pcolor [ color ] of myself - 2
]
]
end
to find-convex-hull
ask turtles
[
let start-patch min-one-of territory [pxcor]
ask start-patch
[
let next-patch min-one-of [territory] of myself [towards self]
]
]
end
I think this does what you're looking for. The only odd thing about the code is the [towards myself - heading] of myself] in which the first myself refers to the next turtle and the second myself refers to original, because it is the myself of the myself.
Does that make sense? Let me know if you have any questions.
to setup
ca
crt 10 [setxy random-xcor random-ycor]
end
to start-connecting
ask min-one-of turtles [xcor][set heading 0 connect]
end
to connect
let the-turtle min-one-of other turtles with [[towards myself - heading] of myself > 0] [ [towards myself - heading] of myself]
if the-turtle = nobody [stop]
face the-turtle
ask the-turtle [set heading [heading] of myself]
create-link-with the-turtle
ask the-turtle [connect]
end
By the way, if you only want to do this for particular agentsets, just change which turtles you connect with in the connect procedure.