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
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
I 'm new in Netlogo programming. I would like to make turtles with cloud shape and big size so if another turtle i.e. a person be at the same patch with the cloud to lose energy. The problem is that I can't have a turtle to be in more than one patches, netlogo "can see" that it's in only one patch.
Regardless of the size of an icon depicting a turtle, the turtle is located only at a single point (defined by the variables xcor and ycor). However, you can instead use distance to find if other turtles are close
As JenB said, the turtle only exists as a point, you'll have to come up with logic to make the clouds seem bigger than they are if you want them to be turtles.
Here is some code that demonstrates how to use size and in-radius to make the clouds breed affect the leaves breed color as they move past. It works best with shape = "circle" since then the radius of the cloud will match where the leaves are affected. You can add this code to a basic new NetLogo model to see it work:
breed [ clouds cloud ]
breed [ leaves leaf ]
to setup
clear-all
ask patches [
set pcolor blue + 2
]
create-clouds 10 [
set xcor random-xcor
set ycor random-ycor
set size 1 + random 4
set color white - 2
set shape "circle"
]
create-leaves 35 [
set xcor random-xcor
set ycor max-pycor
set shape "leaf"
set color green
set heading 180
]
end
to go
ask clouds [
ask leaves in-radius (size / 2) [
set color (color - 1)
]
]
ask leaves [
fd (1 + random 10) / 10
]
end
You can also reverse the logic a bit so it's the leaves that check if they are inside a cloud using distance. I find this option more confusing, but it might work better in your case:
to go-leaves
ask leaves [
if any? clouds with [distance myself < (size / 2)] [
set color (color - 1)
]
fd (1 + random 10) / 10
]
end
And finally, instead of using turtles to represent your large areas that turtles move through, you could use patches instead. It would simplify some things, but wouldn't work in every case.
I am currently learning NetLogo and I need help. In my model I have same sized 10 turtles which moves randomly. When 2 or more turtles are on the same patch they will combine and form a new turtle with the double size. In this manner, the main rule is max. 5 turtles can combine to each other. And this formation will continue until the there will be 2 turtles (with each contain 5 turtles) remain.
I had created turtles and made them move randomly, but I could not managed to combine them. Can you show me a way to do this? Any help appreciated. Regards.
EDIT: I tried the "in-radius" command unsuccessfully. 5-5 distribution of the turtles (as you can can see from the code, they represent H2O molecules) is vital for the system definition and any other distributions are not allowed in the model.
In detail, when randomly moving 2 H2O molecules meet on the same patch, they will combine to form a new molecule (2H2O). The main rule is as previously mentioned, max. 5 molecules can combine which ends with forming 5H2O. Since, initially there are 10H2O molecules in the system, there will be 2 5H2O molecules at the end.
The code I tried to implement is as follows,
breed [h2o-molecules h2o]
to setup
clear-all
reset-ticks
create-h2o-molecules h2o-num [
set color 105
set sIze .5
set shape "circle"
setxy random-xcor random-ycor
set pen-mode "up"
]
end
to setup-patches
ask patches [set pcolor 0]
show count turtles
end
to set-label
ask patches [
ifelse count turtles-here > 0
[set plabel count turtles-here]
[set plabel ""]
]
end
to move-h2o-molecules
ask h2o-molecules [
let dice random 1000
let change (dice - 1)
forward 2
set HEADING (HEADING + change * 2)
]
end
to go
setup-patches
move-h2o-molecules
ask turtles [rt random 1
fd 0.3]
set-label
tick
end
Thanks for your time and patience. Regards,
Using turtles-here
You don't need to ask patches for turtles-here (as you did to set patches labels). The function runs as well if called by a turtle (and is more efficient when there are more patches than turtles). But take care to use other turtles-here if you don't want to include the calling turtle.
Combine procedure
If you declare
a turtle variable after your breed declaration:
h2o-molecules-own [
turtles-inside
]
(set the variable value inside your create-h2o-molecules)
and your combination limit max-inside as a global variable (use slider widget with 5 as default value)
then the combine procedure can look like:
to combine ;; turtle procedure
; take one turtle from the same patch as a target
; which has turtles-inside low enough to combine with
let target one-of other h2o-molecules-here with
[turtles-inside <= max-inside - [turtles-inside] of myself]
if target != nobody
[
set turtles-inside turtles-inside +
[turtles-inside] of target ;; increase turtles-inside
ask target [ die ] ;; kill the target
set size sqrt turtles-inside ;; increase size
]
end
Stop
You can stop the simulation by
if not any? h2o-molecules with [turtles-inside < max-inside] [ stop ]
Comment
The condition used to select the target turtle is using turtles-here, other and the maximum constraint which is compared to the sum of turtles inside the target and turtles inside the calling turtle (using myself function).
I have created a new patch type which allows turtles to turn randomly down various paths while one a junction patch ( with pcolor 6 ). How should I modify this code so that I do not get the error "Towards expected input to be an agent but got nobody instead." The code is as follows :
if pcolor = 6 [ set heading towards one-of neighbors in-cone 1 180]
Any help would be much appreciated.
To specifically answer your question, you need to check if there are any patches that fulfill your criteria. To do that, you can do
ask a-turtle [if any? neighbors in-cone 1 180 [face one-of neighbors in-cone 1 180]]
Doing it this way, you will create the same turtle set twice (when you check if there are any, and before you face one of them), so a more optimized way of doing this is:
ask a-turtle [
let eligible-neighbors neighbors in-cone 1 180
if any? eligible-neighbors [face one-of eligible-neighbors]
]
That said, I think Alan is right that you are getting this error because you have wrapping off and your turtles are either in a corner or facing a wall. If this is the case, you need to figure out what to do in that case. If you just want them to turn around and keep going, you could use ifelse like this:
ask a-turtle [
let eligible-neighbors neighbors in-cone 1 180
ifelse any? eligible-neighbors
[face one-of eligible-neighbors] ;; face a neighboring patch if there are any
[rt 180] ;; else, turn around 180 degrees
]
try first making a temporary variable with 'let', then setting the heading towards that.
e.g.
let FaceHere one-of neighbors in-cone 1 180
face FaceHere
haven't tried that - just an idea.
by the way, you can replace
set heading towards
with
face
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.