How to get turtle to wait at patch for 10 ticks - netlogo

I would like my turtles to
1) Stop for ten ticks if the turtle is red and comes across a red patch
2) After ten ticks I would like the turtle to continue on the look-for-food subroutine, which I already have.

An easy way to accomplish this is to use some sort of counter. Here is a full example:
turtles-own [ counter ]
to setup
clear-all
create-turtles 100 [
set counter 0
setxy random-xcor random-ycor
]
ask n-of 25 turtles [ set color red ]
ask n-of 100 patches [ set pcolor red ]
reset-ticks
end
to go
ask turtles [
ifelse counter = 0 [
look-for-food
if color = red and pcolor = red [
set counter 10
]
] [
set counter counter - 1
set label counter ; just to show what's going on
]
]
tick
end
to look-for-food
; your own look-for-food procedure is presumably different
right random 20
left random 20
forward 1
end

Related

Netlogo: Patches Disappear instantly instead of continualy

I'm trying to show deforestation versus reforestation. To do this I created a slider to show how much reforestarion and deforestation is being done. However every time at 11 ticks the whole scene gets deforestated and I don't know why.
patches-own
[reforestar
deforestar]
breed [ potreros potrero ] ; sheep is its own plural, so we use "a-sheep" as the singular
breed [ bordes borde ]
breed [ bosques bosque ]
to setup
clear-all
set-default-shape turtles "frog top"
ask patches
[ifelse pcolor = 44
[ set reforestar tiempo-sin-reforestar ]
[ set reforestar tiempo-sin-reforestar * 0.5];
]
reset-ticks
end
to go
ask patches [ reforestacion ]
ask patches [ deforestacion ]
tick
end
to reforestacion ; patch procedure
; countdown on brown patches: if you reach 0, grow some grass
if pcolor = 35 [
ifelse reforestar <= 0
[ set pcolor 44
set reforestar tiempo-sin-reforestar ]
[ set reforestar reforestar - 1 ]
]
end
to deforestacion
if pcolor = 44 [
ifelse deforestar >= 10
[ set pcolor 35
set deforestar tasa-deforestacion ]
[ set deforestar deforestar + 1 ]
]
end
The idea is that some patches of random brown (deforestacion) turn into yellow (reforestacion) but for some reason it just changes everything at once.
You're not asking a random number of patches to do something, you're asking all patches with pcolor 44 to count up to 10 with every tick and when they reach 10, they get "deforested".
If you want to ask a random number of patches to get deforested, try something like
ask n-of (random ([count patches with pcolor = 44] * deforestationRate)) patches with pcolor = 44 [set pcolor 35 set deforestar tasa-deforestacion]
Where deforestationRate would be a value from a slider from 0 to 1. What this would do is count the nuber of patches that can be deforested and then select a random number of those patches to deforest. If you only use the count itself, then every tick between 0 and 100% of the forest will get deforested, but if you add the deforestationRate slider value, it may be whatever maximum percentage you'd like. (So if you set it to 0.1 for example, then only up to 10% of the forest can get deforested each tick) You can do the same thing with reforestation too and use a different slider / value for the rate.
(Note: I haven't used NetLogo in a while so the code and parentheses may not be 100% on point, but you get the idea)

Netlogo increase turtle number by tick

I am trying to build a model where turtle move one patch per tick with random movement. I am looking for a solution to increase the number of turtle per tick based on the percentage. For ex. In the beginning there are 7 turtles, by each tick they should increase by following percentage:
10.72%
10.83%
10.93%
11.03%
11.11%
11.19%
11.27%
11.33%
11.39%
11.45%
Not sure if this is possible? If needed this can be round up to whole number.
If this is not possible, how can I increase the turtle number by 11% each tick for 10 ticks then after 12% each tick for another 10 ticks and so on?
Below is the code that I am using.
to setup
clear-all
setup-turtles
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles tourists [setxy random-xcor random-ycor ]
ask turtles [ set shape "person" set size 2 ]
end
to go
if ticks >= 130 [ stop ]
move-turtles
eat
tick
end
to move-turtles
ask turtles [ right random 360 forward 1]
end
to eat
ask turtles [ if pcolor = green [ set pcolor black ] ]
end
Thank you for your support.
Avi
I think your model is very similar to 'Rabbits Grass Weeds' in the Models Library so you might want to have a look at that to get some ideas. Focusing just on the revised question of moving to a green patch, you need with, which will restrict choices. Note that the code below will break if there are no neighbouring patches that are green.
to setup
clear-all
setup-turtles
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles 7 [setxy random-xcor random-ycor ]
ask turtles [ set shape "person" set size 2 ]
end
to go
if ticks >= 130 [ stop ]
move-turtles
increase-turtles
tick
end
to move-turtles
ask turtles [ move-to one-of neighbors with [pcolor = green] ]
end
to increase-turtles
ask one-of turtles [ hatch 1 ]
end
I used hatch in this code, which gets one turtle to create another turtle at the same place with the same colour etc. If you want to just create a new turtle in exactly the same way as your original turtles, then you want something more like this.
to setup
clear-all
make-turtles 7
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to make-turtles [ num ]
create-turtles num
[ setxy random-xcor random-ycor
set shape "person"
set size 2
]
end
to go
if ticks >= 130 [ stop ]
move-turtles
increase-turtles
tick
end
to move-turtles
ask turtles [ move-to one-of neighbors with [pcolor = green] ]
end
to increase-turtles
make-turtles 1
end
In this case, I have made a new procedure (called make-turtles) that creates as many turtles as you specify, randomly locates them etc. In setup, I call it to make 7 turtles, and then later on just make 1 each time.

Why won't my breed walk?

I am building a model population within a bioreactor, building on a basic tutorial telling how to eat, reproduce, and die, etc. While tinkering, my turtles stopped walking. I suspect it has to do with how to ask the different breeds to do things?
Edit: For some reason my contaminants aren't "wheels" either
turtles-own [ energy ]
patches-own [ nutrition ]
breed [ Xanthomonas Xanthomona ]
breed [ contaminants contaminant ]
globals
[ xanthan biomass ]
to setup
clear-all
setup-patches
set-default-shape Xanthomonas "bug"
set-default-shape contaminants "wheel"
crt num-Xanthomonas
[set color yellow
set energy 10
setxy random-xcor random-ycor
]
foul
determine-biomass
reset-ticks
;; begins defining a procedure named "setup"
;; resets the world to an initial, empty state
;; creates 100 turtles, they start out standing at the origin 0,0
;; set default shape so I don't have to tell it every time
;; A turtle's color variable is random until specified
;; setxy command with next two numbers as inputs
;; chooses random reporters for allowable x and y coordinates
End
to setup-patches
ask patches
[ set pcolor green
set nutrition 50
]
;; every patch starts with 50 nutrition, the color indicates it for us
end
to foul
set-default-shape contaminants "wheel"
if Contamination?
[ crt num-contaminants
[ set color red
setxy random-xcor random-ycor
]
compete ]
end
to go
if ticks >= 2000 [ stop ]
if count turtles > 2000 [stop]
if count turtles = 0 [stop]
feed
move-turtles
ask turtles
[eat-glucose]
ask Xanthomonas
[reproduce]
check-death
tick
end
to determine-biomass
ifelse Contamination?
[set biomass num-Xanthomonas + num-contaminants
]
[set biomass num-Xanthomonas ]
end
to move-turtles
;; says that each turtle should run the commands in the brackets
;; random doesn't include the number you give it as a possible result
;; uses a reporter, each turtle picks a random whole number between 0 and 359
;; makes the turtle move forward one step
;;specify what you're defining will lose 1 energy per step
ask Xanthomonas
[ right random 360
forward 1
set energy energy - 1 ]
end
to Feed
if Continuous-feed?
[ ask patches
[if random 100 < 3
[set pcolor green
set nutrition nutrition + 50
] ] ]
end
to eat-glucose
ask Xanthomonas
[ if pcolor = green
[ set energy energy + 10
set nutrition nutrition - 50
set pcolor gray
set biomass biomass + 1
] ]
ifelse show-energy?
[ set label energy ]
[ set label "" ]
;;ask turtles before "if"
;;if when true, and only then will the turtle run the commands inside the brackets (otherwise it skips them)
;;true/false questions, if true, will do first set of brackets
;;false means turtle runs commands in second set of bracket
;;energy (elements) will default to zero
end
to reproduce
ask Xanthomonas
[ if energy > 50
[set energy energy - 50
set xanthan xanthan + 1
hatch-Xanthomonas 1
[set biomass biomass + 1
rt random-float 360 fd 1
] ] ]
end
to check-death
ask Xanthomonas
[ if energy < 0
[ die ]
]
end
to reinoculate
ask patches [
if random 100 < 10
[ set pcolor green
]
]
end
to Contaminate
crt num-contaminants
[ set color red
setxy random-xcor random-ycor
]
end
to compete
ask contaminants
[ if energy > 50
[set energy energy - 50
set xanthan xanthan + 1
hatch-contaminants 1
[ set color red
set biomass biomass + 1
rt random-float 360 fd 1
] ] ]
end
okay, your basic problem is the crt command. This is short for create-turtles. You have two breeds here and you have defined them. However, when you create the turtles, you are not telling NetLogo which breed to create.
What this means is that you need to make a couple of minor changes to specify the breed. If you look in the dictionary, you will see the command is create-<breed>. Where you have crt num-Xanthomonas, change it to create-Xanthomonas num-Xanthomonas and similarly where you create the contaminants.

Selecting the first 20 turtles out of 1000 turtles

turtles-own [wages]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to go
move-turtles
get-employed
tick
end
to move-turtles
ask turtles [
right random 360
forward 1
]
end
to get-employed
ask turtles [
if pcolor = blue [
set color green
set wages wages + 10
]
ifelse show-wages?
[ set label wages ]
[ set label " " ]
]
end
to setup-patches
ask patches [set pcolor pink ]
patch 0 0 [ set pcolor blue ]
end
to setup-turtles
create-turtles 80
ask turtles [ setxy random-xcor random-ycor ]
ask turtles [ set color red]
end
I want to add the code to select the first 20 percent of the 80 turtles who comes in contact with the patch having color blue.
In your question is not clear how you will use the first 20% of turtles who get the blue patch so i assume you just want to store them in order to use this information later.
I would add a turtles-own called is-first-20-percent? set to false for every turtle.
Then, at the end of the go procedure, before the tick i would execute the check-20 procedure as following:
to check-20
if count turtles with [color = green] = (count turtles * 20 / 100) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end
In every moment you can retrieve the first 20% of turtles who reached the blu zone with the command:
ask turtles with [is-first-20-percent?] [ ... do something ... ]
This code is working because 20% of 80 is an integer number (16) but if you plan to modify the starting number of turtles i suggest to modify the check-20 procedure as following:
to check-20
if (count turtles with [color = green] >= (count turtles * 20 / 100)
and count turtles with [is-first-20-percent?] = 0) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end

How show few turtles at a time?

I have this basic code for setup and moving turtle.
By this time I want only few turtles to appear during setup and then when they move. Other turtles will show or will become visible.
to setup
crt 100
setxy random 19 random 80
end
to go
fd 1
end
I tried this. But I got error
to setup
clear-all
create-turtles random 10
reset-ticks
end
to go
fd 1
if count turtles < 100 [
create-turtles min list (random 10) (100 - count turtles)
]
tick
end
Your question is not that clear, if you want to be able to set visibility of turtles you should use hidden? Primitive to set visibility of turtles,
Following example shows how turtles appear when their who id is smaller than ticks, in tick 101 all turtles will be visible.
to setup
clear-all
reset-ticks
crt 100 [
set hidden? true
setxy random 19 random 80
]
end
to go
ask turtles
[
if who < ticks
[
set hidden? false
fd 1
]
]
ask patch 0 0 [set plabel ticks] ; just for your info
ask patch 1 1 [set plabel "Ticks"] ; just for your info
tick
end
After 1 tick only one turtle is visible:
And now 40 turtles are visible :
Update:
In this example, you can have a list of numbers that you want to ask turtles to set their visibility to true:
globals [ number-to-set-visible]
to setup
clear-all
reset-ticks
set number-to-set-visible [ 5 5 7 8 2 ]
crt 100 [
set hidden? true
setxy random 19 random 80
]
end
to go
if visibility-condition and length number-to-set-visible > 0
[
ask n-of item 0 number-to-set-visible turtles [
set hidden? false
]
set number-to-set-visible remove-item 0 number-to-set-visible
]
ask turtles with [not hidden? ]
[
fd 1
]
tick
end
to-report visibility-condition
report ticks mod 100 = 0 and ticks > 0
end
Marzy's answer covers how to create invisible turtles during setup, then gradually make them visible during go.
It's not clear to me if you actually need this visible/invisible distinction. Do you actually need all the turtles to exist from the beginning? Might it be OK to just create a few more turtles each tick? If so, try code like this:
to setup
clear-all
create-turtles random 10
reset-ticks
end
to go
if count turtles < 100 [
create-turtles min list (random 10) (100 - count turtles)
]
tick
end