How show few turtles at a time? - simulation

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

Related

Problems with ticks in NetLogo

I'm trying to understand and see if it's possible to change the following:
I have a code that has 2 iterations. with the configuration exactly as it is. By clicking the setup-all button and then clicking the go once button 4 times. Call the second iteration. But, this second iteration starts at tick 1 and not at tick zero. Why does it happen? Is there a way to solve this?
globals [ iteration ]
patches-own [ scale-patch ]
to setup-world
clearMethod
random-seed 1
ifelse iteration = 0
[setup-layers]
[setup-layers-2]
setup-turtles
reset-ticks
end
to clearMethod
clear-ticks
clear-turtles
end
to setup-all
clear-all
random-seed 1
ifelse iteration = 0
[setup-layers]
[setup-layers-2]
setup-turtles
reset-ticks
end
to setup-layers
ask patches [
set scale-patch random 10
set pcolor scale-color blue scale-patch -8 12 ]
end
to setup-layers-2
ask patches [
set scale-patch random 10
set pcolor scale-color green scale-patch -8 12 ]
end
to setup-turtles
crt 1 [ set color black ]
end
to go
moveproc
let n count turtles
if n = 0
[
ifelse iteration = 0
[
set iteration 1
setup-world
]
[
stop
]
]
tick
end
to moveproc
ask turtles [
right random 360
fd 1
if ticks >= 3
[
die
]
]
end
Thanks in advance
The moment when you change iteration is within the go procedure (i.e. set iteration 1 setup-world). However, the go procedure also ends with tick. This means that when you change iteration NetLogo will first perform all the new setup things, which include reset-ticks (bringing ticks to 0), and then perform tick (bringing ticks to 1).
If you don't like this to happen and if you need to maintain this structure (i.e. go performing setup things), you can rearrange the go procedure so that tick happens before you check the condition for the change of iteration:
to go
moveproc
tick
let n count turtles
if n = 0 [
ifelse iteration = 0
[set iteration 1
setup-world]
[stop]
]
end
PS: the one you provided is a great minimal reproducible example

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)

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.

How to get turtle to wait at patch for 10 ticks

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

Leader Selection Form Swarm of Agents NetLogo

I want to implement a scenario that all agents in NetLogo simulation should report the number of agent in their neighbour upto 3 patches in radius. And then top 3 of them having largest number of agents in its radius should 'set is-leader? true ' . As i am using ' turtles-own [ is-leader? ] ' .
to setup
ca
ask n-of 30 patches [sprout 1 [
set size .8
]
]
end
to go
fd 0.5
lt random 20
choose-leader
end
to choose-leader
end
try like this:
turtles have variable "is-leader?" set to false.
turtles at each tick move in the random way you decided, then set their "is-leader?" variable to false
the procedure choose-leader is executed. It chooses the 3 turtles with the higher number of neighbors in radius 3 and set their "is-leader?" to true.
code:
turtles-own[
is-leader?
]
to setup
ca
ask n-of 30 patches [sprout 1 [set size .8 set is-leader? false]]
end
to go
ask turtles[
fd 0.5
lt random 20
set is-leader? false
]
choose-leader
end
to choose-leader
ask max-n-of 3 turtles [count turtles in-radius 3] [set is-leader? true]
end