How to create more than 1 turtles on patchset such that no two turtles have the same center?
Number of turtles to create defined as density.
Therefore, I require more agents per patch.
Eluciadtion: There a a set of patches in shape of box in which I wish to fill agents. Equivalent to distributing agents in a room.
This answer Distribute turtles on patches would create turtles outside the box as well.
Thanks.
Assuming patchset means all the patches.
Crt number
[Setxy random-xcor random-ycor]
Gives you a number of turtles uniformly distributed on the patches with a very small chance of having the same center. The birthday problem with floating points.
Or this if you want n turtles uniformly distributed on any set of patches P.
Repeat n [ask one-of p
[
Sprout 1
setxy (pxcor + random-float 1 - .5) (pycor + random-float 1 -.5)
]
]
Pick a random member of your set and put a turtle on a random part of that patch
If density is literally the number of turtles to create and my-patches is your patchset:
ask n-of density my-patches [ sprout 1 ]
If density is the fraction of patches that should have turtles on them (e.g. density = 0.5 would mean half the patches should have turtles):
ask n-of (density * count my-patches) my-patches [ sprout 1 ]
If density should be treated probabilistically (e.g. density = 0.5 would mean each patch has a 50% chance of having a turtle):
ask patches with [ random-float 1 < density ] [ sprout 1 ]
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
The objective of my submodel is to simulate how wolf agents avoid patches that have human densities above the wolves' tolerance threshold. When running my model, the sprout command is not generating the number of human agents in the urban patch like I would expect it to. The code for creating humans in the urban patch is:
ask patches [ if self = urban-patches [sprout-humans initial-number-humans]]
Here is the image of my Interface Tab:NetLogo space
The grey is coded as my urban-patches, brown is grass-patches, and green is forest-patches. Why are my human agents not appearing in the grey (urban) patches with the number of agents reflecting the initial-number-humans?
Here is the code for creating the human agents:code
I have specified the xy coordinates of the human agent to be located within the urban (grey) patch, but only one human agent appears when I run the model. How do I correctly code the initial-number-humans to connect with the sprout command?
As I think you have now discovered, the easiest way to distribute a set number of turtles randomly across a group of patches is to use create-turtles rather than sprout. This is because sprout creates the specified number of turtles on each patch that is sprouting them so you need to juggle the total to be created and the number of patches. But that option is useful if you want to achieve even distribution rather than random locations. Here is code that does both.
globals [urban-patches n-humans]
to setup
clear-all
set urban-patches n-of 20 patches
ask urban-patches [set pcolor gray]
set n-humans 100
make-humans-sprout
make-humans-create
end
to make-humans-sprout
ask urban-patches
[ sprout n-humans / count urban-patches
[ set color red
set xcor xcor - 0.5 + random-float 1
set ycor ycor - 0.5 + random-float 1
]
]
end
to make-humans-create
create-turtles n-humans
[ set color blue
move-to one-of urban-patches
set xcor xcor - 0.5 + random-float 1
set ycor ycor - 0.5 + random-float 1
]
end
Note that the adjustments to xcor and ycor are because sprout and move-to always place the turtle in the centre of the patch and there is no primitive for placing in a random place on a particular patch.
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.
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