NetLogo: how to let turtles return after one tick - netlogo

hope you can help me out! I have a NetLogo question about the following code:
breed [tourists tourist]
turtles-own [satisfaction]
to setup
clear-all
reset-ticks
end
to go
tick
ask n-of initial-number-tourists patches
[
sprout 1 [
set color white
set size 1
]
]
ask turtles
[ let nearest min-one-of turtles [distance myself]
if-else distance nearest < 0.0000001 [ set satisfaction 0 ]
[ set satisfaction 1 ]
]
ask turtles with [satisfaction = 0] [die]
end
The intention is to sprout "initial-number-tourists" at the start of each tick. Then, they calculate their 'satisfaction' based on the if there are turtles close and let the turtles that have a satisfaction of 0 die. Then, the turtles that remain (that are satisfied) will be sprouted again and their satisfaction will be calculated again.
The code is working, however, with unexpected outcomes. What happens is that the turtles are all sprouted, and they all die each tick. The next tick; they are sprouted again and all die again. Even if i set the distance threshold really low like i did in the code I provided.
Hopefully you can help me out!! Kind regards

The problem you are encountering is that the nearest turtle to each turtle is itself! Thus the distance to nearest is always zero and satisfaction will be set to zero for every turtle. What you want is the closest among the other turtles,
let nearest min-one-of other turtles [distance myself]
(Your distance threshold will mean that all turtles will have satisfaction of 1, but I assume that is what you wanted to accomplish in your testing. Since turtles are sprouted at the centers of the patches, a threshold of < 1.0 would accomplish the same thing.)
Minor point: the convention is to put tick at the end, not the beginning of the go procedure.

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

Finding the distance to the next patch ahead of a given color in Netlogo

I'm trying to model predators hunting prey through a world filled with obstacles. In an obstacle free world, the predators go on random walks until prey fall within a certain detection radius (react-D), and then move towards the prey to capture. I guess there may be several ways to handle obstacles that block line of sight, but my initial efforts are along the following lines:
ifelse any? prey in-radius react-D
[let target min-one-of prey in-radius react-D [distance myself]
face target
...move towards target if it's closer than the nearest obstacle (blue patch) ahead
]
[continue to random walk, avoiding obstacles
]
The first condition of ifelse may need another ifelse. In any case I think I just need a distance of the nearest blue patch ahead to compare with the target distance, but it's not clear to me how I can get that from patch-ahead.
It would be nicer if I could put all this into the test of the first ifelse, but something like the following doesn't allow for patches that aren't blocking line of sight to the prey:
ifelse any? prey in-radius react-D and min-one-of (patches in-radius react-D with [pcolor = blue])[distance myself] > min-one-of (prey in-radius react-D) [distance myself]
Line of sight is actually really tricky to model in NetLogo. At the moment I do it by creating a turtle (invisible, separate breed called crawler) where the agent is that is looking and have the crawler move ahead in small steps checking the colour of the patch that it is on. That goes inside a while loop and the crawler stops when it finds the wrong colour or when it gets to the target distance. the crawler has to take small steps because crossing over the corner of a patch should still block it and it's a trade-off between resolution (how small the crawler step is) against efficiency.
You can almost certainly do the same thing by creating a patch-set as the union of patches found with patch-ahead of multiple distances like 0.1, 0.2, .... and then checking any? over that patch-set
My current solution, giving patches-own [ ob ] values of 1 to all obstacle patches, then using while to detect obstacles ahead:
to detect-ahead
let dist 1
let last-patch patch-here
while [dist <= react-D] [
let p patch-ahead dist
if p != last-patch and [ob] of p = 1 [
ask p [ sprout-markers 1 [ set color yellow ] ]
set last-patch p
]
set dist dist + 1
]
end
Then it should simply be a case of comparing min-one-of the marker agent set with the target. Obviously smaller increments would be good, as JenB says.

NetLogo: calculate distance to other turtles and make them dissatisfied if another turtle is too close

I sprouted my turtles over the patches and in the 'go' I want each turtle to calculate if another turtle is too close (based on a certain threshold of X meters/patches). If another turtle is too close, i want the turtle to become 'dissatisfied'.
Movements of the turtles are not required.
What is the best way to do this? The only piece of code for the go procedure I have so far is as follows:
to go
ask n-of initial-number-tourists (patches with [ lagoon_proximity = 1])
[
sprout 1 [
set color black
set size 10
]
]
tick
end
Kind regards!
Your code is creating more turtles and doesn't have anything about checking distances so it is not clear how it is related to the problem you are trying to solve. However, the following code measure the distance to the nearest turtle and prints a message if that is less than 5.
ask turtles
[ let nearest min-one-of turtles [distance myself]
if distance nearest < 5 [ show "I feel crowded" ]
]

How to make n-of turtles move to the same place every loop?

I want to specify the number of turtles to go and back the same place every of my loop.
For example, in day1, some of turtles born in place1 and some of turtles born in place 2 and turtles its selves know the place where they want to go and come back to the same place where they born. For day2, turtles its selves still go the same place and go on like this every day.
Anyone have any ideas or suggestions?
It's a little hard to work out what you are asking but hopefully this is close and will give us a starting point to work out what you actually want. This model has a variable for each turtle to remember where it was born, and each tick some (5) of the turtles move back to their home patch. The other turtles move in a random direction.
turtles-own [myhome]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set myhome patch-here
]
reset-ticks
end
to go
let homers n-of 5 turtles
ask turtles
[ ifelse member? self homers
[ move-to myhome ]
[ set heading random 360
forward 1
]
]
tick
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.