Netlogo - how to count turtles number around a specific turtle - netlogo

I need to "do-something-special" if around yellow turtle there is at least 3 blue turtles . Is the code bellow correct?
I tried
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[do-something-special]
]
do-something-special should remove (disappear) 3 of blue turtles and current yellow one
Did I do the location of relevant turtles correctly and how do I kill them after I find them?

Hannah's answer is good but the linked example won't fully fix your problem. Since you will be using the set of close agents twice (once to count and once to potentially remove some), you should also create an agentset for efficiency reasons (you don't want to create it twice). Here is a full solution.
ask turtles with [color = yellow]
[ let near-blue turtles in-radius 1 with [color = blue]
if count near-blue >= 3
[ ask n-of 3 near-blue [die]
die
]
]
Also, if you don't care about the exactness of the radius, an alternative to turtles in-radius 1 would be turtles-on (patch-set neighbors patch-here), which is all the turtles on the neighbouring and same patches to wherever your asker turtle is sitting.

At the moment your code counts the amount of turtles that are blue in the radius of one patch around the yellow turtle. If the amount of blue turtles is bigger/equal 3 the yellow turtles die if you use the "die" command instead of "do-something-special". So it looks as follows.
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[die]
]
Maybe you can merge the code with the following example and then kill the neighbors.

Related

How to spawn turtles a certain amount of patches away from each other

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end
Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

In an «ask turtles» method: How can I read a variable of a patch in a relative angle and distance from a turtle?

I want [turtles with [shape = "sheep"]] to move either to the lefthand side or the righthand side, depending on how many [attractive (green) patches] are around a certain relative patch.
How the turtles with [shape = "sheep"] should count the patches (yellow)
The (incorrect) code looks like this:
to move-sheep
ask turtles with [shape = "sheep"] [
right random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead 60 2)^ 2)
left random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead -60 2)^ 2)
forward 1
#(some other commands…)
]
end
Thank you ^^
It would be much easier if you actually told us what the problem is. You say your code is wrong, but not how you know it's wrong. Is it reporting an error (and, if so, what is the error and which line is reporting it)? Is the moving turtle going the wrong way?
Regardless, the easiest way to approach this is to do something smaller before trying to move. If you simply count the number of green patches and print that out, you can test the code. Once you introduce movement, how will you tell if it counted correctly?
I am still not entirely sure what you are asking. But I think this code might help you diagnose your problem. It tells you what the count is around the target patches, so you can see if it is doing the correct count. Once you know the counting works, you can then modify for movement.
to testme
clear-all
ask patches [set pcolor one-of [yellow green]]
create-turtles 1 [set heading 30 set color black]
ask one-of turtles
[ ask patch-right-and-ahead 60 2 [set pcolor red]
type "Right: "
print count ([neighbors4] of patch-right-and-ahead 60 2) with [pcolor = green]
ask patch-right-and-ahead -60 2 [set pcolor red]
type "Left: "
print count ([neighbors4] of patch-right-and-ahead -60 2) with [pcolor = green]
]
end

How to ask turtles to place in a desired area, on Netlogo?

I want to create turtles, which they place in a desired area with random coordinate:
they should place in the white area and in middle of it in a line. in other words, in the top regtangle, their xcor should be random and their ycor is 10. in the right regtangle, their ycor should be random and their xcor is 10 and so on.
When you create turtles, you can give them instructions such as their location. For example:
create-turtles 1 [ set ycor 10 ]
Alternatively, you can sprout the turtles from the relevant patches and their location will already be set. For example:
ask n-of 5 patches with [pcolor > 1] [ sprout 1 ]
to place-on-color [#color]
let _patches (patches with [pcolor = #color])
ask turtles [
move-to one-of (_patches with [not any? turtles-here])
]
end
Add error checking if you may have too many turtles. (Or remove the unique occupancy constraint if you don't want it.)

How to make simulation run faster in netlogo instead of using Slider bar near to view update

Is there any code to make simulation run faster in netlogo, instead of using slider bar near to setting? What my code need to do is to simulate the crowd behavior, it's work fine if the number of turtles around 100,however when I increase the number up to 300-800 turtles, simulation take very long to finish. Each tick also take very long to count from 0 to 1 and next until all turtles die. one thing that I suspect cause slow simulation is when ask turtles to evacuate. without evacuate rule, everything went smoothly even set a maximum number of turtles. is there other way to write evacuate rule, so that it can run faster? thanks.
to go
ask turtles [wander fd 0.01]
if emergency? = true [move]
if all? turtles [ pcolor = red ] ;stops simuation
[stop]
tick
end
to wander
[ do..something]
end
to move
set time-to-evacuate time-to-evacuate + 1
ask turtles [avoid-obstacles fd 0.1]
ask turtles [follow-leader fd 0.1]
ask turtles [flock fd 0.1]
ask turtles with [pcolor != red] [evacuate fd 0.1]
ask turtles with [pcolor = red][die]
end
to evacuate
ask turtles with [color = black ]
[let beings-seen patches in-cone 10 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
ask turtles with [color = white ]
[let beings-seen patches in-cone 5 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
end
to avoid-obstacles
[do something]
end
to follow-leader
[do something]
end
to flock
[do something]
end
In your move procedure you have:
ask turtles with [pcolor != red] [ evacuate ... ]
And then in evacuate you have:
ask turtles with [color = black] [ ... ]
evacuate is already being run by all of the non-red turtles, so you've got every non-red turtle asking every black turtle to do something at every time tick.
I don't think you intended that.
I have to guess a bit at your intent, but I think if in evacuate you replace the ask with an if:
if color = black [ ... ]
that's probably closer to what you meant.

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.