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

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

Related

Different types of turtles in a lattice grid

In Netlogo there are some default models, including the lattice grid.
I am wondering how to add three types/breeds (40%, 40%, 20%) of turtles connected to each others only at the beginning, then, when tick > 0, some new links can be created between turtles of different type.
In the models library in Netlogo, I found the code for a regular lattice:
to setup-square
clear-all
ask patches [ sprout 1 ]
ask turtles [ create-links-with turtles-on neighbors4 ]
reset-ticks
end
Instead of turtles, I should consider the three types (types1, types2, types3) in order to have a lattice with:
40% of turtles as breed type1 (red);
40% of turtles as breed type2 (blue);
20% of turtles as breed type3 (white).
Initially, all the turtles above are connected to other turtles of the same type. Links between turtles may change through time, so links between turtles of different breeds can exist when tick>0.
I would do as follows (instead of ask turtles [...]):
ask types1 [ create-links-with types1-on neighbors4 ]
ask types2 [ create-links-with types2-on neighbors4 ]
...
but the problem is that I have no control on the proportion of turtles for each type. Also, a condition on the breed of the neighbors of a turtle should be considered as well.
How can I set the proportion of these types and make connections only between turtles on the same type, then mix links?
There are several ways to do this, some of them more exact than others. It is especially important to realise that you can change a turtles breed after it has been created, simply by asking it to set breed <breed>.
In the first example, I give all turtles type1 as they are created, and then change a predefined number of them to another type. Keep in mind how you get that number as rounding errors can have an effect here (see further down for the counts I had). It is important that you give them all a breed at first, as this version will not work with ask n-of <percentage> turtles [set breed <breed>]. That is because the agentset turtles will always contain all turtles, even those who you have given a different breed.
breed [types1 type1]
breed [types2 type2]
breed [types3 type3]
to setup-shape
set-default-shape types1 "default"
set-default-shape types2 "x"
set-default-shape types3 "square"
end
to setup-1
ca
setup-shape
ask patches [sprout-types1 1 ]
let total count turtles
ask n-of (total * 0.4) types1 [set breed types2]
ask n-of (total * 0.2) types1 [set breed types3]
end
A second version that works with a more sochastic approach is to let each newly created turtle choose random number, and give it a breed depending on the number. Keep in mind that the proportions will vary more, as they are determined by chance rather than exact numbers.
to setup-2
ca
setup-shape
ask patches [ sprout 1 [
let number random-float 1.0
show number
(ifelse number < 0.4 [set breed types1]
number < 0.8 [set breed types2]
[set breed types3])
]
]
end
The counts I had of the different versions for each type. Notice that in version 1, type1 and type2 have a different count because I let all turtles start with the breed types1.
to count-types
;standard world of 33x33
show count types1 ; version 1: 437 , version 2: 454
show count types2 ; version 1: 435 , version 2: 432
show count types3 ; version 1: 217 , version 2: 203
end

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

Comparing patches

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.

How to stop the turtles for some time and kill both turtles if it is in some radius in netlogo

How to stop / kill two turtles if they come in some radius? Rest of the turtles should not stop moving.
Asking all turtles to check their environment
Here's an example that might be helpful as a first attempt. Since the aim is to signal to a large set of turtles when to continue walking and die, you will need to create a procedure that asks all turtles to check their environment.
Naive Implementation:
to global-step
ask turtles [ ifelse any? other turtles in-radius 3 [ die ] [ fd 1 ] ]
end
The naive implementation works well to remove turtles, but not all turtles are removed, since turtles would disappear with each turtle's check.
Improvements with multiple agents
One way to improve this is through adding a turtles-own'd variable for marking the turtles as within a certain radius of a turtle.
Improved Version:
to global-step
ask turtles [
ifelse any? other turtles in-radius 3
[ set turtle-variable true ]
[ set turtle-variable false ] ]
ask turtles [ ifelse turtle-variable [ die ] [ fd 1 ] ]
end

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