NetLogo not creating turtles in random intervals - netlogo

I'm trying to create a procedure in NetLogo to create a turtle agent every 0-60 seconds. Using the following code and then running the procedure in a loop, it appears that the random generator is not working. The graph plot (agents to ticks) is linear.
to go
every random 60 [crt 1 [
set xcor random 20 - 10
set ycor random 20 - 10
]
]
plot count turtles
end
But if I were to do:
to go
every 2 [crt 1 [
set xcor random 20 - 10
set ycor random 20 - 10
]
]
plot count turtles
end
It seems to work as expected. Every 2 seconds a new turtle gets created.
Am I doing something wrong?

Here is an idea, don't keep regenerating a new random number every time, regenerate it after the timer is reached. Code:
globals [t]
to setup
set t random 60
end
to go
every t [
set t random 60
crt 1 [
set xcor random 20 - 10
set ycor random 20 - 10
]
]
end
I haven't tested it, but it should address the issue that Jose brought up.

Your first bit of code is doing what its supposed to, which is creating a turtle almost every instant.
This might not be intuitive but notice that the 'go' is getting called thousands of time per second (depending on your machine speed). Every time it is called it generates a new random number between 0 and 60. So, there is a really high probability that it will generate the number 0. If so, then it creates a turtle at that moment.
As an example, run this code from a 'forever' button and see what it prints out:
to go
every random 60 [
show timer ;shows how long its been since last reset-timer
reset-timer
]
end
I get:
observer: 0.016
observer: 0.0060
observer: 0.016
observer: 0
observer: 0
observer: 0
observer: 0.0050
observer: 0
observer: 0
observer: 0
on my laptop.

Related

How can I properly implement a function at the end of a counter in Netlogo?

I am writing an assembly line model, and I would like to implement a counter to hold a turtle at a specific patch (in this case, patch 3 0) for 10 ticks. Once 10 ticks have passed, I would like the turtle to keep on moving at the rate of one patch per tick and for the next turtle in line to begin its own 10 tick timer once it arrives at the specified patch.
So far, I can stop the turtles at the specified patch and run a 10 tick counter; however, I cannot seem to get the turtles to keep moving continuously after the timer is completed.
Here are the relevant parts of my code so far.
to go
move-tubs
move-drums
machine-marriage
move-machines
stay
keep-going
tick
end
to move-machines
ask wmachines [
if not any? turtles-on patch-ahead 1 and xcor < 3
[ forward 1]
]
end
to stay
ask wmachines-on patch 3 0[
ifelse counter = 0 [
set counter 10
]
[set counter counter - 1
set label counter
if counter = 0
[forward 1]
]
]
end
to keep-going
ask wmachines-on patch 4 0[
if not any? turtles-on patch-ahead 1 and xcor < 12
[ forward 1]
]
end
If your problem is that turtles leave patch 3 0 but then they do not move forward continuously beyond patch 4 0, it is simply because your keep-going procedure is only addressing turtles that are exactly on patch 4 0 (and for this reason the xcor < 12 part is completely unused).
In general, it looks very complicated and unnecessary that you are using three different procedures (i.e. one before patch 3 0, one for patch 3 0, and one for patch 4 0 but which should really be beyond patch 3 0) each of which is hard-coding some location in your model.
The whole point of having a counter is that you can generalise a waiting condition across the whole model, so your go procedure can be simplified a lot by simply asking agents that have concluded their countdown to do one thing, and those who have not concluded their countdown to do another thing.
Look at this minimal reproducible example where I have an unpredictable arrangement of stopping-patches but implement the waiting condition in a very general and simple way:
turtles-own [
counter
]
to setup
clear-all
reset-ticks
resize-world 0 30 0 4
set-patch-size 25
ask patches with [pxcor = min-pxcor] [
sprout 1 [
set heading 90
set color lime
]
]
ask n-of 15 patches with [pxcor > min-pxcor] [
set pcolor brown
]
end
to go
ask turtles [
ifelse (counter = 0)
;; If the counter equals 0:
[forward 1
if (pcolor != black) [
set counter 10
]
]
;; If the counter does not equal 0:
[set counter counter - 1]
ifelse (pcolor = black)
;; If the turtle is on a black patch:
[set label ""]
;; If the turtle is not on a black patch:
[set label counter]
]
tick
end

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

Error: Element 287 of list [0] could not be found, which is only 1 in NetLogo

I would like to quantify how many times each turtle has passed each patch in the world. Do you know how I can get this information from NetLogo? I was able to find out how many times the turtle visited the patches, but not how many times it went to each specific patch. For example: turtle 0 visited patch (0, 0) 2 times and patch (0, 1) 4 times. But,Turtle 2 visited patch (0 0) 3 times and patch (0, 1) 3 times and so on.
But, the following error appears: Element 287 of list [0] could not be found, which is only 1.
error while patch 7 22 running ITEM
called by (anonymous command: [ id -> let item id turtle-visits set turtle-visits replace-item id turtle-visits current-num-visits + 1 ])
called by procedure GO
called by 'go' button
Can someone help me?
globals [ edge-size ]
patches-own [ turtle-visits ]
to setup
ca
let num-turtles 1
set edge-size 29
resize-world 0 edge-size 0 edge-size
let pcolors []
set pcolors [ 85 95 ]
ask patches [
set turtle-visits n-values num-turtles [0]
set pcolor item (random 2) pcolors
]
reset-ticks
end
to go
ask turtles [
rt random 360
fd 1
]
end
The problem is, that you initialize the list of turtle-visits with the number of turtles per patch, i.e num-turtles:
set turtle-visits n-values num-turtles [0]
If you replace num-turtles with count turtles, since you want a value for every turtle in the world, it should work:
set turtle-visits n-values count turtles [0]

Create two rows of turtles with an equal x-cor distance

first of all, sorry for my english. I hope you can understand me.
I'm trying to create an ABM of couple bargaining. In order to accomplish this, I want to set two rows of men and women, like in the "Party" Netlogo model.
As I see, I want to have one row with men disposed with equal distances between every man, like man, (5 empty patches), man (5 positions), man... starting with one on the left in a determined position. And the same goes for woman's row.
How can I do this?
With this:
setxy random-xcor 15 ; for the man's row
setxy random-xcor 15 ; for the woman's row
I can get two rows of men and women within a fixed axis, and a random-xcor on the other axis, but i don't get equal distances between turtles.
Thank you so much for your help.
ask patches with [pxcor mod 5 = 0 and abs pycor = 1] [
sprout 1 [
set shape "person"
set color ifelse-value (pycor > 0) [blue][pink]
]
]
You could do something like this:
create-turtles num-men [ setxy who * distance 15]
create-turtles num-women [setxy (who - num-men) * distance -15]
For example,
if you have 5 turtles, their whos will be [0 1 2 3 4] and a distance of 5, thus their xcors will be [0 5 10 15 20] respectively. The only reason this would work is if the men's whos start at 0. You may need to offset their whos by how many other turtles were created before...see the women.

Hatching in NetLogo by fractional probabilities

I'm trying to hatch a certain amount of turtles according to some statistics I've complied about births in a particular country.
To save you the math there are 201 births everyday in the adult population of 3565765.
What I want to do is scale my numbers to correspond to a netlogo model with 73 adults, 19 children and 8 toddlers so one hundred agents in total. (I've scaled these numbers according to their population distribution)
The birth rate defined by the user at the interface and currently I have..
to births
ask n-of(count adults * birth-rate) adults[
hatch-toddlers 1
.....
The way I'm currently deciding the birth rate is 201(average births per day)/3565765(adult population) = 0.00005636939 x 73(my adult population in model) = 0.00411496551
when I get work through by code with this figure I get a birth rate of 0.30039248239. When I run my model no new toddlers are born. Can I assume that ask n-of will only hatch a new toddler if its over 1?
If so is there a way I can use these probabilities to slip a coin so to speak to see a turtle is hatched?
Right, n-of will discard the fractional part of any number you pass it.
I think you want random-poisson here. To demonstrate its usage, suppose I want an average of 0.1 births to occur every tick. Then:
to demo
clear-all
reset-ticks
while [ticks < 100] [
let births random-poisson 0.1
if births > 0 [
crt births
print (word births " born on tick " ticks)
]
tick
]
print (word "after 100 ticks, there are " count turtles " turtles")
end
Where a typical run ends up looking like:
observer> demo
1 born on tick 2
1 born on tick 7
1 born on tick 12
1 born on tick 28
1 born on tick 39
1 born on tick 48
1 born on tick 49
1 born on tick 52
2 born on tick 58
1 born on tick 66
after 100 ticks, there are 11 turtles
On the average, you'll get 10 turtles.