Save the times an action has happend then do something Netlogo - netlogo

I dont have code as I dont really know how to do it. i have some patches of colore brown and at random the turn green for one tick and then they become brown again, it loops. I want to know if there is a way to ask a turtle to know how many times that patch has changed to green.
Thanks in advance.

A variable like you describe is often called a counter. In this case, you probably want it to be a patches-own variable so that each patch can individually track how many times a given event has occurred. In the example below, times-turned-green starts at zero (by default, that is the value for any variable declared in Netlogo) and the patches all update their own 'personal' times-turned-green whenever they change colour.
patches-own [ times-turned-green ]
to setup
ca
ask patches [ set pcolor brown]
reset-ticks
end
to example
repeat 100 [
ask patches [
set pcolor brown
if random-float 1 < 0.05 [
set pcolor green
set times-turned-green times-turned-green + 1
]
]
]
ask n-of 5 patches [
show times-turned-green
]
end
Output in the Command Center should look similar to:
(patch -6 14): 2
(patch -9 4): 6
(patch 2 1): 8
(patch -3 3): 4
(patch -5 12): 5

Related

NetLogo: Having a turtle remember its starting location

I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.

Netlogo 'towards' behaviour

For a pursuit-evasion assignment I need to use the NetLogo command 'towards', but it doesn't seem to work, or I don't understand it.
What I think it should do: give the angle between the heading of the turtle and the line connecting the turtle with the target.
Here's the code for a simple model I made just to show the problem.
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end
And here's a video of the behaviour. https://youtu.be/MUBiAypppc4 (I couldn't find a way to remove the audio without just replacing it using YouTube's current editing system, I'm sorry; you'll have to mute the audio yourself)
Examples of expected behaviour:
from 0:14 to 0:19, I would expect the number to gradually decrease, not increase
at about 0:38, I would expect the number to be 0, not somewhere around 300
between 0:38 and 0:42, I would expect the number to decrease or increase consistently, without those two sudden jumps
Is there a problem somewhere, or does 'towards' mean something different than I thought?
So turtle 0 is moving and turtle 1 is reporting the direction to turtle 0. I think towards is working fine but you have forgotten about the world settings. For example, in the 14-19s part, the shortest path from 0 to 1 is down and left (about 220 heading), but that shortest path is with the world wrapped. Your turtles can move off one side and come in on the other (as you can see turtle 1 doing).
NetLogo measures distances and directions taking into account the wrapping configuration. It knows that the shortest path to get from turtle 0 to turtle 1 goes off the side and comes in the other, and reports the direction that the turtle would have to move to follow that path.
Create a link and you can see this. Revised code:
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
create-link-with turtle 0
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end

Netlogo Reporter Not Reporting

I've made a animal behavior model involving "turtles" and "roads" and I want the model to report back to me when the turtle "crosses" a road. All I want is that it tells me when the turtle moves from a patch that is the grey color to the red color. I've included the code asking it to report this and the program has no issue with the code. To give me a visual representation of what I want it to report, I put a monitor on interface. But it always gives me a "0" for road crossings, even as I can see that my turtle has crossed roads. I would count it by hand, but it's impossible to tell for certain how many road crossings there are and this is for scientific publication. My code is as follows...
turtles-own [
road-crossings
]
to setup
clear-all
;; create turtles on random patches.
ask patch 6 -15 [
sprout 1 [
set color one-of [green]
set size 1
set road-crossings 0
]
]
ask turtles [
if [pcolor] of patch-here = 14.9 [
set road-crossings road-crossings + 1
]
]
reset-ticks
end
to go
ask turtles [
repeat 100 [
repeat 39 [
pen-down
rt random-float 360
lt random-float 360
fd random-float 1.375
]
setxy 6 -15
]
]
tick
end
Any help is appreciated! Thank you!
There are several potential problems with this that I can see.
First, road-crossings is a turtle variable, which is the correct thing to do if you want each turtle to remember how many times it crosses a road. If so, however, the monitor must report sum [road-crossings] of turtles to get the road crossings of all turtles.
Second, which I think is actually your problem: you have the turtle checking whether it crosses the road in the setup procedure rather than the go procedure. The setup procedure is only run at the beginning.
Third, you don't actually have any roads in your example code, but I suspect that's just a failure to create a proper example. I assume that there are patches with pcolor of 14.9 in your real code. If not, though, that would also cause your error. You can make sure by going into the command center and asking count patches with [pcolor = 14.9]

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.

How to kill of all turtles except for one?

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.