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

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.

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.

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.

Variables in different breeds

i'm trying to make an archaeological model, in which hunters are making paintings in shelters, depending on their quality.
breed [shelters shelter]
breed [hunters hunter]
shelters-own [quality paintings]
The value of each shelter quality is set in the setup (with a slider for the actual number of shelters).
create-shelters number-shelters [set quality random 100]
The action of painting-or-not is then defined by random against the quality of each shelter:
to make-painting
ask shelters [
if any? hunters-on patch-here [
if random 100 < quality [set paintings paintings + 1]
]
]
end
Now, I would like to complexify it a bit more: the quality wouldn't be defined by the shelter itself (and thus be the same for every hunter), but by the hunters: each of them would attribute a different quality for each shelter. The action of painting-or-not would still be a test against random, but with this new variable defined by each individual hunter...
But I can't find a way to code it down properly.
Does anyone have a suggestion?
I think I got it right. Basically, I had every hunter to create a matrix filled with random numbers. The size is 33x33, so it goes all over the world (had to set it to start in a corner, or it gets negative coordinates).
create-hunters number-hunters [
set color white
set size 1
setxy random-xcor random-ycor
set hunter-matrix matrix:make-constant 33 33 random 10
]
ask n-of number-shelters patches [
sprout-shelters 1 [
set color one-of base-colors
set size 1
set xpatch xcor set ypatch ycor
]
Then, when they would reach a shelter, the value corresponding to that patch location would be extracted and used to paint-or-not.
ask patches [
ask hunters-here [set quality matrix:get hunter-matrix xpatch ypatch]
let paintings-here sum [paintings] of shelters-here
if any? hunters-here and any? shelters-here [
if random 10 < quality [
ask shelters-here [
set paintings paintings + 1]
]
]
]
I'm still not completely sure it actually does what I think it's doing, tho.

How to randomly remove block side in a grid?

Following on from How to control square size in a grid from square area?, I would like to randomly remove block side in a grid. Here my idea:
%% In a first time, I randomly select a white patch in the grid (figure below):
let random-patch one-of patches with [pcolor = white]
%% Then, I draw red patches to identify intersections between white roads (figure below):
ask patches with [ (pxcor mod (side + 1) = 0 ) and (pycor mod (side + 1) = 0 ) ] [set pcolor red]
%% Finally, I color in brown all white patches that are situated between two red patches and on the same side than the random patch. To identify these white patches, have I to calculate distance between the random patch and the nearest red patches and to color all white patches situated along these distances ?
Thanks in advance for your help.
An alternative way to think about your problem is in terms of finding clusters of white patches: you're picking a white patch at random, and you want to turn all the contiguous white patches to brown.
You can look at the "Patch Clusters Example" in the Code Examples section of NetLogo's model library to see one way to do this.
Here is how I would do it. Let's start by defining a grow-cluster reporter:
to-report grow-cluster [ current-cluster new-patch ]
let patches-to-add [
neighbors4 with [
pcolor = [ pcolor ] of myself and
not member? self current-cluster
]
] of new-patch
let new-cluster (patch-set current-cluster new-patch)
report ifelse-value any? patches-to-add
[ reduce grow-cluster fput new-cluster sort patches-to-add ]
[ new-cluster ]
end
The code may be hard to understand if you're not used to functional programming because it uses recursion within a call to reduce. Still, in a nutshell, it:
takes an existing patch cluster and a new patch to add to this cluster
looks for neighbors of that new patch that should also be added to the cluster, i.e., those that are the same color but not already part of the cluster
calls itself for these new patches to add so that their neighbors can be added to the cluster (and those neighbors' neighbors, etc.) until no new patches are found.
Once you have grow-cluster, you can use it to accomplish exactly what you want by seeding it with an empty cluster and the random patch that you selected:
to remove-random-side
let random-patch one-of patches with [pcolor = white]
let side grow-cluster no-patches random-patch
ask side [ set pcolor brown ]
end
Caveat: for this to work, world wrapping has to be disabled in your model settings.
Since you're making a uniform grid, you might consider just doing math on pxcor and pycor instead of taking the Patch Clusters Example approach. That approach is best suited to dealing with irregular shapes.
To set up your grid, you can just do:
ask patches [
set pcolor brown
let horizontal-street? pycor mod (side + 1) = 0
let vertical-street? pxcor mod (side + 1) = 0
if horizontal-street? or vertical-street?
[ set pcolor white ]
if horizontal-street? and vertical-street?
[ set pcolor red ]
]