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
Related
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
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
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.
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.
I ask patches sprout a number of turtles equal to slider initial-population on interface, now i wont that each turtle has neighbors around empty and that each patch contain one turtle. Below my wrong-code
to setup-turtles
set-default-shape turtles "circle"
ask n-of initial-population patches with [(pcolor = white - 1) and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
sprout-normals 1 [
set color blue
set size 1
]
]
end
but no result neighbors empty foreach turtle..why?
I'm not sure that this is really what you are asking because the sentence "that each turtle has neighbors around empty and that each patch contain one turtle" is really hard to understand, but here is my guess:
You want turtles to appear only on patches where they will not have any neighbors. But it doesn't work because at the time you select your patches, every single patch fulfills the criteria (not any? other turtles-here) and (not any? turtles-on neighbors). As soon as you start sprouting turtles, that condition is not true for every patch anymore, but that fact is not taken into account by your code because you have already asked the patches to sprout turtles.
What you need to do is to check for the condition again each time you add a turtle. You can do that using repeat and then one-of instead of n-of:
repeat initial-population [
ask one-of patches with [(pcolor = white - 1) and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
sprout-normals 1 [
set color blue
set size 1
]
]
]