We are using NetLogo for a simple infection. As a modell we use an imported gephi file. For our purpose we have to start the infection from the same turtle (meaning the one with the same specific label) for several times. In our code we have tried using the "who" number to make sure of that, but as soon as we setup, this number changes. So my question is:
Is there the possibility to instead of the who number use the turtle's label in the code?
So far we have been using this code
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "jk.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "jk1.graphml"
end
to spread
if (count turtles with [informed? = true] > .9 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.02)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you so much!
As King-Ink said in a comment, you can get a turtle with a particular label by doing one-of turtles with [ label = "some label" ]
Related
I have to make turtles go faster when being on a green patch, and when they are on a blue patch the speed should go down. I added part of the code that I tried, but it doesn't work. Could someone please help me?
Thanks in advance!!
turtles-own [speed]
to go
ask turtles [
left 60
right random 60
forward 1
if any? neighbors with [pcolor = green]
[
set speed speed + 1
]
if any? neighbors with [pcolor = blue]
[
set speed speed + 0.1
]
]
reset-ticks
I think what user2901351 was getting at was that you are using neighbors in your example code. If you look at the dictionary entry for neighbors, you'll see that it references the 8 patches around the current patch. If you instead want the turtle to check the patch it's currently on, you can use the patch-here primitive or, as a shortcut, just ask the turtle to check a patch-owned variable directly. Below is a toy example that shows an example of this at work- more details in the comments.
turtles-own [speed]
to setup
ca
crt 5 [ set speed 1 ]
ask n-of 20 patches [
ifelse random-float 1 < 0.5
[ set pcolor green ]
[ set pcolor blue ]
]
reset-ticks
end
to go
ask turtles [
; Since 'pcolor' is a patch variable, if the turtle queries pcolor
; it will check the patch's variable directly.
if pcolor = green [ set speed speed + 0.5 ]
if pcolor = blue [ set speed speed - 0.5 ]
; Make sure that the turtles are moving forward by their speed value,
; rather than the hard-coded value of "1" in the example code. Also,
; I've included a check here so the turtles don't either stop moving
; or start moving backwards.
if speed < 1 [ set speed 1 ]
rt random-float 60 - 30
fd speed
show speed
]
tick
end
How to create two different random color groups in NetLogo 6.1.1?
I am trying to create two different groups from the group of 250 turtles. The starting situation is that all 250 turtles are gray and then they will turn to yellow and pink groups one by one.
This code of mine makes all turtles in the beginning to be gray and then they will all turn to pink. I do not want this but I want two randomly made groups where pink turtles is typically either larger or smaller group of turtles than the yellow group of turtles in the end of the code run.
I just started to code with NetLogo 6.1.1. Thank you for understanding and all help and have a nice day.
[
time
person
]
turtles-own [ x ]
to setup
clear-all
reset-ticks
set time 0
create-turtles 250
[
setxy random-xcor random-ycor
]
ask turtles
[
set shape "person"
set size 1
set color gray
]
end
to go
ask turtles
[
show random 2 = x
if x = 1 [set color yellow]
if x = 0 [set color pink]
]
end ```
I don't see any place where the values for the turtle variable x are set, so they will always have the default value of 0. In NetLogo = is used to check equality, not for assignment, so show random 2 = x is just going to print true or false depending on if random 2 is 0 or not (in case you thought that was assignment). You'd want something like this:
to go
ask turtles
[
set x random 2
if x = 1 [set color yellow]
if x = 0 [set color pink]
]
end
Or you could move the set x random 2 to the setup procedure if you just want to set the value once to use later in go.
I realize that my question is just slight modification of questions:
Adding patch clusters in a landscape
How to create cluster patches that do not overlap between them
and
To build patch clusters at large spatial scales
but I can't modify them to satisfied my needs.
I need to produce a patchy forest landscape. Each setup, the total area of "green" patches has to be same (20%, 10%... of total count patches) and the size of one blob should be same. Thus: blob_size = area / number_blobs
I suggest that
to create-forests
clear-all
ask n-of 1 patches [ set pcolor green ]
repeat 6 [
ask one-of patches with [pcolor = green ] [
ask one-of neighbors4 with [pcolor = black] [
set pcolor green ]
]
]
end
should be the answer, as by n-of 1 (number_blobs) patches I create number of blobs needed, and blob_size is constrained by repeat 6 (blob_size). However, in my simple example I have an error ASK expected input to be an agent or agentset but got NOBODY instead. apparently because one-of patches with [pcolor = green] has not black neighbors.
Please, how can I include the condition ask one-of patches with [pcolor = green ] and with min-one of neighbors4 with [pcolor = black] in my code?
Or what is the different way to do this? I need to keep my total area of green patches same, and patches size +- same too, the best would be if they will not overlap. Thank you a lot!
to create-forests
clear-all
ask n-of 1 patches [ set pcolor green ]
repeat 6 [
ask one-of patches with [pcolor = green and any? neighbors4 with [ pcolor = black ] ] [
ask one-of neighbors4 with [pcolor = black] [
set pcolor green ]
]
]
end
Modified to make-blob from: Creating a random shape (blob) of a given area in NetLogo
my blobs still can overlap but at least total "area" is the same for every run
to make-blob
let total_area 500 ; how patches I want to turn green
repeat 5 [ ; number of blobs I want to have
let blob-maker nobody
crt 1 [ set blob-maker self
setxy random-xcor random-ycor] ; set random position of "blob-makers"
repeat 10 [ ; size of one blob (number of patches of the same color close one to another)
ask blob-maker [
ask min-one-of patches with [ pcolor = black ] [ distance myself ] [ set pcolor green ]
rt random 360
fd 1
]
]
ask blob-maker [ die ]
]
end
resulting in
I have two procedures for turtles but I need a random number of turtles to do one of the procedures and another number of random turtles to do the other procedure in equivalent parts.
So suppose i have a population of 40 turtles and I have the following procedures:
to move
ask turtles [
rt random-float 90 - random-float 90
fd 1
]
end
to bounce
ask turtles
[
if [pcolor] of patch-at dx 400 = white [
set heading (- heading)
]
if [pcolor] of patch-at 400 dy = white [
set heading (180 - heading)
]
]
end
What I want to do is to have 20 random turtles do the procedure "to bounce" and the other 20 random turtles do the procedure "to move". Or the 40 turtles will do any of the 2 procedures randomly but without any turtle doing the 2 processes at once.
I know this is kind of difficult but I think it can be done. Any suggestions?
This is the easiest way I can think of:
let bouncers n-of 20 turtles
ask turtles [
ifelse member? self bouncers
[ bounce ]
[ move ]
]
There's lots of interpretations of the phrase random number of turtles. Seth's method allows you to control exactly how many turtles do each of the two procedures. That is, a fixed number of random turtles.
If you want it always to be approximately, but not necessarily equal to, some proportion of turtles (0.4 in the example below), so each turtle randomly decides with a given probability, then you can do:
ask turtles [
ifelse-value random-float 1 < 0.4
[ bounce ]
[ move ]
]
If you want the number of turtles to be random, then you can change the first line of Seth's code to:
let bouncers n-of (1 + random count turtles) turtles
Note: none of this code is tested so please comment (or edit) if it throws an error.
Hello i have the following code:
to go
move
play-papelvstije
play-papelvsrock
play-tijevsrock
play-tijevspapel
play-rockvspapel
play-rockvstije
end
to play-rockvspapel
ask turtles with [color = red]
[
let nearby other turtles in-radius 1
if any? nearby with [color = green]
[
set color green
]
]
end
to play-papelvstije
ask turtles with [color = green]
[
let nearby other turtles in-radius 1
if any? nearby with [color = blue]
[
set color blue
]
]
end
to play-tijevsrock
ask turtles with [color = blue]
[
let nearby other turtles in-radius 1
if any? nearby with [color = red]
[
set color red
]
]
end
to play-rockvstije
ask turtles with [color = red]
[
let nearby other turtles in-radius 1
if any? nearby with [color = blue]
[
set color red
]
]
end
to play-papelvsrock
ask turtles with [color = green]
[
let nearby other turtles in-radius 1
if any? nearby with [color = red]
[
set color green
]
]
end
to play-tijevspapel
ask turtles with [color = blue]
[
let nearby other turtles in-radius 1
if any? nearby with [color = green]
[
set color blue
]
]
end
So as you can see, i run this procedures play-papelvstije play-papelvsrock play-tijevsrock, play-tijevspapel, play-rockvspapel, play-rockvstije in this exactly order, so when run the simulation i get an slant in my results, because the first command to run is the one that have an increase population in the end, so what i want to do is finding a way to run this procedures but with one just command. I have tried with "foreach" and "map" commands, however i have not gotten results. Anny suggestions?
one solution can be to not directly ask blue turtles but for all turtles ... something like
to setup
clear-all
create-turtles 100 [
set color red
setxy random-pxcor random-pycor
]
ask n-of 10 turtles [
set color green
]
ask n-of 10 turtles with [color = red][
set color blue
]
reset-ticks
end
to go
move
changeColor
tick
end
to move
ask turtles [
rt random-float 90
lt random-float 90
fd 1
]
end
to changeColor
ask turtles [
let mycolor color
let nearby other turtles in-radius 1
if mycolor = blue [
if any? nearby with [color = green]
[
set color blue
]
if any? nearby with [color = red]
[
set color red
]
]
if mycolor = green [
if any? nearby with [color = red]
[
set color green
]
if any? nearby with [color = blue]
[
set color blue
]
]
if mycolor = red [
if any? nearby with [color = red][
if any? nearby with [color = blue]
[
set color red
]
if any? nearby with [color = green]
[
set color green
]
]
]
]
end