How to kill of all turtles except for one? - netlogo

Im making a race model. the function is that the turtles move across the grid horizontally starting at xcor -13 to xcor 13 at speeds that
constantly vary and when a turtle reaches xcor = 13, all of the other turtles (besides the one that crossed first) die
to Race
wait .3
fd random 5
if xcor = 13 ( this is where i want to tell all other turtles to die )
end
how do i ask all other turtles to die?
THe first answer doesnt help me, someone else please respond

You can do that by asking the winner to ask other turtles [die]
to setup
clear-all
reset-ticks
;resize-world min-pxcor max-pxcor min-pycor max-pycor
resize-world -15 20 0 3
set-patch-size 15
;set-patch-size size
create-turtles 10
[setxy -13 1 set heading 90 set shape "car" wait 0.3]
ask patch -13 2 [Set plabel "Start" set pcolor 110] ; just for visualization
ask patch 13 2 [Set plabel "END" set pcolor 110]
end
to go
ifelse count turtles > 1
[
ask turtles
[Race]
]
[stop]
tick
end
to Race
fd random 5
if xcor >= 13 [ set size 2 ask other turtles [die] ]
end
This is a sample screenshot
I really low examples, so there is another way to improve visualization of the race by having multiple lines of cars:
resize-world -15 20 0 5
set-patch-size 15
create-turtles 20
[set xcor -13 set ycor one-of [0 1 2 3 4 ] set heading 90 set shape "car" ]
ask patch -13 5 [Set plabel "Start" set pcolor 110]
ask patch 13 5 [Set plabel "END" set pcolor 110]

What you are trying to do doesn't make much sense. When posting, please make sure that the question provides a better context for answering you question.
My interpretation of your question is that the cars don't matter at all. All you want to figure out is that when one turtle crosses the finish line, you kill all the other turtles.
Doing so I would probably give each turtle a property of a name or label. Store them all in an array.
Then if the turtle crosses the finish line, remove all the turtles from the array except for
if turtle.name == turtle[i].name.
Hope that helps. Please be a little more clear next time.

You need a way to identify the living turtle and kill the other turtles. To do this, you could either write a ton of if statements but it would look horrible.
if(larry.coordinates == 13){
kill(tom);
kill(harry);
}
Your best bet would be to read how to create an array. Store it in an array. Trust me, arrays are pretty simple.

Related

NetLogo - In-Cone radius

ask turtles
[ ask patches in-cone 3 60
[ set pcolor red ] ]
The code above means to have a cone of radius 3 and angle of 60 degrees. But I want a radius in range of 5 to 10. Is there a way to accomplish this?
What you can do is set up a variable that will hold the radius's that you have asked for. Let's call this variable vision. So,
let vision
set vision 5 + (random 5)
and then you can substitute vision instead of the 3 in your code.
ask turtles in-cone vision 60 [
set pcolor red
]
Here are a couple of ways you could do it. These are adapted into the Vision Cone Example model that comes in the models library with NetLogo, so we're using slightly different distances (between 9 and 30) to make the effect visually obvious. I'd recommend method1, but I'm including method2 as it's useful if you have some alternate logic to apply to the patches that are too close.
to method1
; use `distance` to filter those that are too close
; `myself` refers to the turtle doing the asking
ask patches in-cone 30 60 with [distance myself >= 9] [
set pcolor grey
]
end
to method2
; create a local variable to store the turtles that
; will be too close. `self` refers to the patch
; being asked
let too-close patches in-cone 9 60
ask patches in-cone 30 60 with [not member? self too-close] [
set pcolor gray
]
; I could use the `too-close` agentset here and ask them
; to do something, also.
end

getting a particular colored turtle to do something on a particular colored patch in Netlogo

I am putting together a Netlogo model where students and criminals are interacting on a college campus. If criminals are close to students, a chase ensues so that when a criminal is distance < 1 the student dies. In addition to dying when criminal are to close I also want students(leader) to die during the chase when they are on a green patch. Green patches in this sense equate 'safe' areas. I have procedures set up for criminals chasing(follower) and students running away(leader), but I am at a complete block with getting the students being chased to die when coming across a green patch. One thought I had was making students being chased blue with the follower procedure:
ask students[
set no-threat criminals in-radius 25
ifelse any? no-threat[set color blue][set color black]
]
Then having blue students die when they encounter a green patch. I think I am missing something obvious to get this to work. Any nudge in the right direction would be much appreciated!
FYI, I am also relatively new to asking questions here so all apologize for any forum faux pas
here is the meat of the program I have been working on for reference
'''
to move-to-student
ask criminals[
set candidate students in-radius 25
if any? candidate[
set leader min-one-of candidate [distance myself]
if leader != nobody [ set color red]
;set follower myself
face leader
fd .3
if [pcolor] of patch-ahead 5 = 2.2 [
set heading heading - 100]
]
]
end
to run-from-criminal
ask students[
;while[distance follower > 1][
set threat criminals in-radius 25
if any? threat[
set follower min-one-of threat [distance myself]
if distance follower < 1 [ set number-dead number-dead + 1 die]; set candidate leader set threat follower ] ;students 'die' if criminal gets to close
if threat != nobody [set color blue]
face follower
rt 180 lt random 20 rt random 20 fd .2
if [pcolor] of patch-ahead 5 = 2.2 [
set heading heading - 100]
]]
end
to safe-die
ask students[
if follower = blue and if [pcolor] of patch-here = green [die]
]
end
to revert-leader
ask students[
set no-threat criminals in-radius 25
ifelse any? no-threat[set color blue][set color black]
]
end
to create-new-students
ask students[
if count students < student-count [hatch 1 setxy random-xcor random-ycor]
]
end
I figured it out. Maybe writing it down helped.
Looks like my solution was a combination of a nested if statement (something I've tried and failed at previously) and assigning the agents I want to die to an agent-set. There might be a better way to get this done but I wanted to share the solution I got...
ask students[
if any? no-threat[if [pcolor] of patch-here = green [die]
]

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.

How to spawn simultaniously turtles in netlogo

Is there a way to spawn turtles during simulation even if they died. In my simulation fish are eating plankton, so if they encounter plankton the plankton dies/gets eaten. However, when a fish can't eat plankton anymore it will die because it doesn't get energy from plankton anymore by eating it. So when all the fish have died the plankton should come back; due to migration etc. and grow immensively. I am not sure how to implement this? The create function doesn't work here, only in the setup.
to plankton-reproduce
if random-float 100 < reproduce-plankton [
set energy (energy / 2)
hatch 1 [setxy random-xcor random-ycor]
]
if count plankton < 10 [
create-plankton 20
setxy random-xcor random-ycor
]
error: you can't use create-plankton in a turtle context, because create-plankton is observer only
I think I may understand the question.
to to have a turtle create turtles use HATCH. Your code would work (if I understand it) if you used
hatch-plankton 20
instead of
create-plankton 20
Did I get it right? Turtles hatch , patches spawn and observer creates.
the turtles hatched will be identical to the hatching turtles and will all be in a lump where hatch was called. Assuming that you do not want that.
use
hatch-plankton 20 [setxy random-xcor random-ycor]
I have incorporated this in the code, but when the count of plankton is zero, there will be no plankton respawned, this is because all plankton is dead and cannot be hatched. Do you know another way to spawn plankton or respawn turtles in general during a simulation even if they die?
Beneath the code to reproduce plankton:
to plankton-reproduce
while [count plankton != 0 and count plankton < 3000]
[ if random-float 100 < reproduce-plankton
[set energy (energy / 2)
hatch-plankton 1 [setxy random-xcor random-ycor]]]
if count plankton = 0
[set energy 1
hatch-plankton 20 [setxy random-xcor random-ycor]]
end

Move turtles around a patch

I am trying to move turtle around patch 0 0 starting from random position in world. But circle keeps on growing. What am I doing wrong here?.
Code:
to setup
clear-all
create-turtles 5
ask turtles [
setxy random-xcor random-ycor
]
ask patch 0 0 [ set pcolor green ]
reset-ticks
end
to go
move-turtles
tick
end
to move-turtles
ask turtles
[
face patch 0 0
right 90
fd 0.01
set pen-size 3
pen-down
]
end
Secondly I want a turtle to move around any patch I define when it reaches with in a certain range
Your approach is to take a small step along a tangent to the circle you want, but this takes you a little bit outside the circle. You do this repeatedly, so it accumulates over time.
For a better way, see the Turtles Circling example in the NetLogo Models Library.