Why color doesn't changed for the turtles? - netlogo

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.

Related

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.

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

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 ]

Why does netlogo count turtles on neighbours differently?

When I run the following two lines I get different answers. Does anybody know why? The first gives the answer I want:
ask turtles[
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = tempcol]]
ask turtles[
set nextcolor [color] of self
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = [color] of self]]
You're right in that the issue is with the use of self- from the dictionary entry for that primitive:
"self" is simple; it means "me". "myself" means "the agent who asked
me to do what I'm doing right now.
In short, you want myselfin the second example. Currently, your second example is saying something like, "turtles, show the count of neighbor turtles whose color is the color of themselves" where you really want to say "turtles, show the count of neighbor turtles whose color is the color of myself." For a potentially clearer example, check out this setup:
to setup
ca
crt 10 [
set color red
setxy random-xcor random-ycor
]
ask n-of 3 turtles [
set color blue
]
reset-ticks
end
This creates 7 red turtles and 3 blue turtles. Now, if you ask one of the blue turtles to show the count of of turtles with the same color as itself, we should expect it to return a value of 3. If you run that code using self, however, the value returned is 10- because all turtles have a color that is equal to the color of themselves:
to self-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of self' example:"
show count turtles with [ color = [color] of self ]
]
end
If you run the exact same code but use myself, it returns the answer we would expect:
to myself-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of myself' example:"
show count turtles with [ color = [color] of myself ]
]
end
I would also point out that almost all of your of self statements are redundant-you should be able to take them all out (except for [color = [color] of self]] that you will be changing to a myself statement anyway) and have your code run as before.

Using a slider to get user input for turtles' color

I want to set the color of the turtles based on a variable that I can change in the interface. I am thinking of using a slider which will have values from 1-5, and that value will dictate how many colors will be used to color the turtles.
For example, if the value is two, then the turtles will be colored with two colors. I would appreciate it if I can control what colors are there to be chosen, but random colors will do fine as well.
I used to have a static, two-color setup:
set color one-of [ red blue green brown orange ]
And it was simple as that. But with the dynamic setup, I did it this way. Is there a more efficient way of doing this?
if groups = 1 [ set color red ]
if groups = 2 [ set color one-of [ red blue ] ]
if groups = 3 [ set color one-of [ red blue green ] ]
if groups = 4 [ set color one-of [ red blue green orange ] ]
if groups = 5 [ set color one-of [ red blue green orange brown ] ]
You can use sublist to extract to number of colors that you want from your list of desired colors:
let colors sublist [ red blue green orange brown ] 0 groups
ask turtles [ set color one-of colors ]
If you didn't want to always get the same colors in the same order, you could use n-of and base-colors:
let colors n-of groups base-colors