How can I change color of turtles over time in Netlogo? - simulation

In the simulation I am working on, I have red turtles.
I want them to be yellow at the beginning, then turn orange after 10 ticks, then red after 10 other ticks.
How can I do so ?
to ignite
ask fires [
if count neighbors > 0 [
ask one-of neighbors with [pcolor = white or pcolor = green ]
[
if count fires-here < 6 [
sprout-fires 3
[ set color red set size 3 ]
]
]
]
]
end

Note that you have tick in your setup procedure as provided. That needs to be in your go procedure. setup is for everything when the simulation starts, and go is what happens each time step. The command tick advances the counter for the time steps, and the reporter ticks reads the time step counter.
If you are going to change a turtle's colour based on how long it's been alive, the first thing you need to do is to have the turtle know when it is 'born', so create a variable for that and store the current value of ticks in that variable during the creation.
fires-own
[ state ; you have this already
birth-tick ; this is the new one
]
Change this:
sprout-fires 3
[ set color red set size 3 set state "live"]
to this (note that spacing does not matter to NetLogo, but helps with readability)
sprout-fires 3
[ set color red
set size 3
set state "live"
set birth-tick ticks
]
So that creates the birth time. Now, within your go procedure (which you don't show), you want all the turtles that are 10 ticks old to change colour. One way is:
ask fires with [ birth-tick = ticks - 10 ] [ set color orange ]

Related

Netlogo: resetting event every tick, but tick counter goes on

Trying to let a flood appear with every tick and make it disappear after every tick as well. Meanwhile, the tick counter should go on.
The flood appears this way:
to water_rise
ask patches [ ; saturates cell
if is-DEM < 800[
set cell-storage cell-storage + fill-rate
]
]
ask patches [
if any? neighbors4 with [ any? turtles-here ] [
set cell-storage cell-storage + fill-rate
]
]
ask patches [
if cell-storage > 0 [
if cell-storage > 5 [
set cell-storage 5
if not any? turtles-here [
sprout 1 [
set color blue
set size 10
set shape "circle"
]
]
]
set pcolor cell-storage + 82
]
]
end
Currently trying to figure out how to let the flood disappear after/or within this the tick, so that it can reoccur in the next one. I´m not aiming to reset the tick counter, it should reach 200.
Tried resetting the ticks, but only to manage resetting everything.
Any ideas ?
Thank you very much in adavance
Cheers
You can simply use the display primitive to update the view without waiting for the tick counter to advance.
I am not familiar with your whole model but here's a conceptual example that may help:
to water_rise
...
end
to go
repeat 100 [
water-rise
display
]
tick
end
This code would execute your water-rise procedure 100 times, update the view with new patch colors after each execution, and only increase the tick counter by 1 after the repeat loop is done.
Here is the link to NetLogo Dictionary entry about the display primitive:
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#display
Note: due to its design, NetLogoWeb does not support the display primitive. So, you need to use the desktop version.

How can I add real world location to the agent based model in Netlogo?

I have a set of positive COVID-19 cases by district in a state. I want to use agent based modelling to make the infected cases move around the state and count the number of infected, number of healthy and number of immune. I have a set of code which the number of population is random and not based on the location. Tried to watch youtube for the tutorial but could not find one which really helps. the code is as below. I want to insert the map of the state, number of population for each district and number of infected in Netlogo. The codes in Netlogo are as below.
turtles-own [illness]
to setup
clear-all
reset-ticks
create-turtles 200 [
setxy random-xcor random-ycor
set shape "person"
set color pink
set size 1.0
set illness 0
]
ask n-of starting-number-infected turtles [
set color yellow
set shape "X"
set size 1.0
]
end
to go
tick
move
infect
recover
lose-immunity
end
to move
ask turtles [
rt random 360
fd movement
]
end
to infect
ask turtles [
if color = yellow [
ask turtles in-radius infect-distance [
if random-float 200 < infect-chance [
if color = pink [
set color yellow
set shape "X"
]
]
]
]
]
end
to recover
ask turtles [
if color = yellow [
set illness illness + 1
if illness > infectious-period [
set color white
set shape "circle"
set size 1.0
set illness 0
]
]
]
end
to lose-immunity
ask turtles [
if color = white and waning-immunity < random-float 100 [
set color pink
set shape "person"
]
]
end
Our trout model provides an example of using the GIS extension to import a set of polygons that represent the places where agents live. In our case, the polygons are habitat "cells", and the agents are fish; but you could use the same approach to import districts and keep track of which people are in which district.
Our model does not represent where a fish is within a polygon, but only which polygon they are in. We use a separate breed of agent called "cells" that contain the variables of the polygon. Each NetLogo patch has a variable "patches-cell" that says which cell it belongs to.
The model is here: https://ecomodel.humboldt.edu/instream-7-and-insalmo-7
(look for inSTREAM7.3). Look at the procedure build-cells.
Steve R.

How to create a button on the interface that shows the values ​of a patch variable in NetLogo version 6.2?

I have a beginner question: I would like to create a button in interface to show the values of patch variable (patches-own: resource). I would like to click once to show the patch variable (resource) and to click the button again to make the patch variable disappear. And I want also do this with colors, if this variable is less than 0.2, it will be gray and not green. That is:
I have a patch-own variable that is called resource. This variable varies for example from 0 to 1. I want all patches that have the value <=0.2 to be gray and the rest green
So, I would like to create a button on the NetLogo 6.2 interface that when clicking on it would appear:
the green and gray patches (gray when the values ​​of the resources variable were <=0.2)
the resource variable values ​​of each patch (for example: patch (0,0) has a reource value of 0.2, the patch (0, 1) has a resource value of 0.9
After clicking again on the same button it would disappear
Is it possible to do that?
My code:
ask patches [
ifelse plabel = "" [
set plabel (word "(" resource ")") ;;Here, I would like to show the resource value of ;each patch
set plabel-color white
]
[
set plabel ""
]
no-display
ask patches [
;; low values
ifelse resource <= 0.2 [ set pcolor gray + 4 ] [
set pcolor green - 4 ] ;; high value
]
display
Thanks in advance
You can use a global variable that controls the state of display, and let the effect of the button adjust accordingly by using ifelse.
For example:
; The name of the button is "display-resource"
globals [
display-control
]
patches-own [
resource
]
to setup
clear-all
ask patches [
set resource precision (random-float 1) (1)
]
end
to display-resource
ifelse (display-control = 0)
[display-on]
[display-off]
end
to display-on
ask patches [
;set plabel resource
ifelse (resource <= 0.2)
[set pcolor grey]
[set pcolor green - 1]
]
set display-control 1
end
to display-off
ask patches [
;set plabel ""
set pcolor black
]
set display-control 0
end
Some things to note:
Note that I included the part about adding plabel, but I commented it out. Up to you to choose, but having every patch displaying a number as plabel will make the view very packed of symbols. This is the case even if resource (or its displayed value) goes only as far as the first decimal place - which is what set resource precision (random-float 1) (1) does.
If you still want to get more information from this display mode ("more" referring to the fact that you might give up using plabel), you can consider using scale-color which will give a more nuanced colouring.
I don't know which would be the previous state of patches' colours, so I just put set pcolor black in my case to go to the previous state.

Why color doesn't changed for the turtles?

I'm quite new to NetLogo and I want to use the below code to create a world of green and red circles, but the below code doesn't work with the color as it is only grey? any advice?
to create_turtles
ca
ask patches [ sprout 1 ]
ask turtles [ set shape "circle" set color green]
end
I just tried your code and it works just fine except it just create all green turtles (circles). If you want it red and green, I suggest you add some piece of code in the ask turtles command and may I suggest you to use the indentation style as well (usually NetLogo will do it automatically):
to create_turtles
ca
ask patches [ sprout 1 ]
ask turtles
[
set shape "circle"
set color green
let chooser random 2
ifelse chooser = 0
[ set color green ]
[ set color red ]
]
The let procedure is a local variable assigner and we let the value a random number of 0 and 1 (two numbers, hence the random 2, and primitive random always include 0 as first number).
In that example we ask the circles to randomly choose a number between 0 and 1. If it chooses 0 then it will set it color to green, otherwise red.
You can explore more about those primitives in the NetLogo Dictionary.

How to check if a patch remains a state for a certain ticks\time?

Based on the “sheep eat grass” model. The patches are changing color with the agents move. I want to check if a patch has remain a centain color for a while (such as 5 ticks). If a patch has stayed in a centain color for a centain times without any change it will turn into black.
I try to use countup but it didn’t work. I need a accumulated state. Thanks a lot
If pcolor=green[
Ifelse countup>=5[
Set pcolor black
Set countup 0]
[set countup countup+1]]
Can you give a little more detail as to what is going wrong with the code you've shown? For example, with this setup:
patches-own [ time-spent-green ]
to setup
ca
crt 3
reset-ticks
end
Something very similar to your example works fine for me:
to go
ask turtles [
rt random 61 - 30
fd 1
ask patch-here [
set pcolor green
]
]
ask patches with [ pcolor = green ] [
ifelse time-spent-green >= 5 [
set pcolor black
set time-spent-green 0
] [
set time-spent-green time-spent-green + 1
]
]
tick
end
Where the patches stay green for 5 ticks then turn back to black.