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

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

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

Turtles hatch when crossing a line

I want my turtles to hatch (= make one more turtle) when they cross a specific line. I have tried the command ifelse?, and I can get it to work on a simple model, when my turtles randomly wanders: If they move to a patch on the left side (xcor < 0) they die, if they make a move to a patch with xcor > 0 they hatch 1.
But I want the proces to be linked to witch patch they come from. If they stand on a patch with xcor < 0 and moves to another patch with xcor < 0 they shall die - but if they change xcor from negative to positive - they should multiply (= hatch 1).
My problem is: Is it possible to write a code-string that "remembers" the turtles position one tick before and use it to either let the turtle die og multiply?
{
to setup
clear-all
create-turtles 20
ask turtles [set size 2
set heading random 45 ;; turtle is now facing northeast
setxy random-xcor random-ycor
set color white
set shape "person"]
end
to go
ask turtles
[ rt random 360 ; turns in a random direction
fd 4 ;; all turtles move forward one step
rt random 180 ;; ...and turn a random amount
fd 4
lt random 180
]
ask turtles
[ifelse pxcor > 0
[hatch random 2]
[die]]
end }
You can use a turtle variable to give each turtle a memory.
turtles-own
[
old-xcor
]
Just before the turtle moves, assign ‘xcor‘ to that variable.
To move-turtle
Set old-xcor xcor
< Your movement code >
if old-xcor < 0
[ Ifelse xcor < 0
[ Die ]
[ Hatch 1 ]
]
End

NetLogo: creation of lattice/grid resources world without using turtles?

I would like to create a "gridded" world of resources, in specific distance from the central patch and keep distances equal between these patches. Due to calculation demand, I prefer not to use turtles to create this patchy world. I expect to create something like this:
Equally, I would like to define distance between patches as a slider tool. I was wandering to use turtle lattice walk and then turn patches to different color, but is there any way how to do that without turtles ? Thanks for any suggestions !
My not totally working exemple:
to setup
clear-all
ask patches [set pcolor green]
foreach [5 10 15] [
repeat 9 [
make-red-patch ?
]
]
reset-ticks
end
to make-red-patch [dist]
crt 1 [
fd dist
rt 90
while [pcolor = red] [
bk dist
rt 90
fd 2 * dist
]
set pcolor red
die
]
end
I am not exactly sure what you need, first you mentioned you don't want to use turtles and in your own answer you have problem with the patch without a turtle.
There might be another way to approach this question:
to setup
clear-all
ask patches with [pxcor mod Grid = 0 and pycor mod Grid = 0] [set pcolor red]
end
And these are examples with different Grid size:
After more detailed search I found my answer here: http://netlogo-users.18673.x6.nabble.com/Setting-up-agents-in-a-grid-formation-td4864083.html
They consider to distribute turtles, not patches and then attribute patches turtles' qualities.
Here is the code:
to setup
clear-all
create-turtles 1
[ let $n 0 ; actual number of turtles on this patch
let $s 0 ; current number of turtles on a side of square pattern - 1
set heading 0
ask patch-here [set pcolor red]
repeat 16 ; number of needed turtles
[ hatch 1 jump Grid ; make one turtle and move
set $n $n + 1 ; increment count of curent side
ask patch-here [set pcolor red]
if $n > $s ; if side finished...
[
right 90 set $n 0 ; turn and reset count
ask patch-here [set pcolor red]
; if headed up or down, increment side count
if heading mod 180 = 0 [ set $s $s + 1
]
]
]
die
]
end
which produce:
I still don't know how to deal with 1 patch without turtle (bottom right corner), but this exemple helped me a lot ! :)

What would i need for this code to make turtles spawn slower, but not have to hold down the mouse to kill a turtle

Im playing whack a mole, and here is my code thus far
globals [
score
three-sec-spawn
]
turtles-own [
ttl]
extensions [bitmap]
To setup
ca
reset-ticks
import-drawing "whackamole.jpg"
import-pcolors-rgb "whackamole.jpg"
set-default-shape turtles "turtle"
ask turtles [
set size 7
set color red
set ttl 100]
reset-ticks
ask patch 18 2 [set plabel
"DIRECTIONS : To kill a bug, click on it. If you misclick, the bug stays there."]
ask patch 18 -1 [set plabel
"If there are 5 bugs on the screen, or if you dont kill it fast enough, you lose"]
set score 0
end
To play
if ticks > 65000 - (difficulty * 10000) [user-message "GAMEOVER" stop]
if (count turtles with [color = red]) = 10 [user-message "GAMEOVER" stop]
ask turtles [
set size 7
set color red]
ask patch 18 2 [set plabel ""]
ask patch 18 -1 [set plabel ""]
ask one-of patches with [pcolor = [42 13 9]] [sprout 1]
ask turtles [
set size 7
set color red
set ttl ttl - .5]
wait .5
if mouse-down? [
ask turtles with [distancexy mouse-xcor mouse-ycor < 1.5]
[set score score + 1
die]
reset-ticks
]
tick
end
because of the wait .5, turtles spawn too fast. But if i make the wait time any longer, id have to hold down the mouse button in order to "whack" the turtle
In your case I would not use the "wait." primitive at all, because it creates a gap of time where no actions (for example mouse clicks) can be tracked.
Instead I would use a time counter to delay the spawning and to create turtles on every x time units. Generally I would use the tick counter itself. For example, if you would like to spawn 1 turtle on every 100th tick:
if (ticks mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
In your example, the tick counter gets reset on mouse clicks. In that case you should define an additional time-counter for the spawning which does not get reset at anytime and gets increased by 1 on every timestep (analougous to the ticks but without resetting it back to 0 on mouse clicks):
if (your-counter mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
I hope this helps..?!?

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