Reputation: 45
I am trying to do the following in NetLogo:
Basically, I would like to ask the old generation of turtles to die after 10 ticks and create new generation of 100 turtles breed A and 100 turtles breed B. How could this be repeated automatically every 10 ticks for a number of ticks (e.g. 3000)?
Your help is much appreciated.
Here is part of the code:
breed [ honest-citizen honest-citizens ]
breed [ potential-offender potential-offenders ]
…
to setup
clear-all
reset-ticks
ask patches [ set pcolor black ]
set-default-shape honest-citizen "circle"
create-honest-citizen initial-number-honest-citizens ;;create honest citizens then initialise their variables
[
set color white
set size 0.2
set income income-from-work
setxy random-xcor random-ycor
set birth-tick ticks
]
set-default-shape potential-offender "circle"
create-potential-offender initial-number-potential-offenders
[
set color red + 1
set size 0.2
;; set income a random number between two extremes using formula
;; random (max-extreme - min-extreme + 1) + min-extreme
set income random ((income-from-work + alfa-constant) - (income-from-work - alfa-constant) + 1) + (income-from-work - alfa-constant)
setxy random-xcor random-ycor
set birth-tick ticks
]
set-default-shape government-agent "circle"
create-government-agent initial-number-law-enforcement-agents
[
set color yellow + 1
set size 0.2
setxy random-xcor random-ycor
]
end
to go
if not any? turtles [ stop ]
if ticks > 3000 [ stop ]
ask government-agent [
count-number-of-workers
count-number-of-criminals
calculate-tax-per-worker
calculate-probability-of-conviction
move
]
ask potential-offender [
move
set net-income income
;; if decide to become criminal then commit crime, else work legally and pay tax
decide-commit-crime
death ;; occurs when arrested and after 10 periods of time
]
ask honest-citizen [
move
set net-income income-from-work
pay-tax
death ;; after 10 periods of time
]
tick
end
…
to move
rt random 50
rt random 50
fd 1
end
to death
if ticks - birth-tick > 10 [
die
]
end
Upvotes: 1
Views: 1647
Reputation: 14972
When you want something to happen "every n ticks", you can usually use mod
. Here is a complete example:
breed [ as a ]
breed [ bs b ]
globals [
; those would be sliders in your model
number-of-as
number-of-bs
]
to setup
clear-all
set number-of-as 100
set number-of-bs 100
create-as number-of-as
create-bs number-of-bs
reset-ticks
end
to go
if ticks > 0 and ticks mod 10 = 0 [ ; <--- this is the important line
ask as [ die ]
ask bs [ die ]
create-as number-of-as
create-bs number-of-bs
]
ask as [ move ]
ask bs [ move ]
tick
end
to move
rt random 50
lt random 50
fd 1
end
I have added a ticks > 0
clause so that turtles don't get killed/regenerated on the first tick, but you tweak this as you like.
Upvotes: 2