Repeat command and use of variable by turn in Netlogo - netlogo

I am stuck with repeat command. NetLog codes' examples couldn't help anymore. I want turtles to pick the value by turn assigned through "who" number and then move a step forward. I want turtles to repeat the same task 10 times. I appreciate any help.
I run the commands error appears in as;
Can't find element 3 of the list [0 1 0], which is only of length 3.
error while turtle 0 running ITEM
called by (anonymous command: [ ?1 -> ask turtles with [ who = ?1 ] [ if item ticks turn = 1 [ fd 1 ] ] ])
called by procedure GO
called by Button 'Go'*
Here are the codes
turtles-own [turn]
To setup
ca
create-turtles 2
[move-to one-of patches
]
reset-ticks
end
to go
tick
define-turn
foreach [ 0 1]
[ ?1 ->
ask turtles with [who = ?1]
[ if (item ticks turn) = 1 [fd 1 ] ]]
end
to define-turn
Ask turtle 0 [ repeat 10 [ set turn [0 1 0] ]]
Ask turtle 1 [repeat 10 [ set turn [0 0 1] ]]
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

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]

Netlogo: How can I obtain the accumulate value in Netlogo?

I already confirmed the information on the link.
However I could not apply this.
The following is a part of the sample model program.
I wanted to accumulate the number of turtles, however I could not accumulate it with the sample program.
I probably need your advice. Thank you.
globals [num-turtles cumulative-sum average-time number-dead ]
turtles-own [count-up]
to setup
clear-all
set num-turtles 5
reset-ticks
end
to go
if count turtles < num-turtles
[ ask patch 0 0
[ sprout 1
[ set count-up 0 ]
]
]
set cumulative-sum cumulative-sum + 1 ;I would like to calculate the integral value here, but this syntax is not a cumulative value.
ask (turtles-on patch 0 0)
[
set cumulative-sum count turtles-here
]
set average-time ifelse-value (number-dead = 0)
[ 0 ][(cumulative-sum) / (number-dead)]
if (count turtles > 0) [
ask min-one-of turtles [who] [
if count-up >= 6 [
set number-dead number-dead + 1
die
]
]
]
ask (turtles-on patch 0 0)
[ set count-up count-up + 1
]
tick
end
that's much better, thanks- I can now run the code no problem. However, I still don't think I understand what you are wanting cumulative-sum to actually count. Are you just looking for the total number of turtles, including both those still alive and the ones that have died? If so, I think it's just a matter of moving your set cumulative-sum cumulative-sum + 1 line. For example:
EDIT:
Ok I think I understand now from your comment. Try this:
globals [num-turtles cumulative-sum average-time number-dead ]
turtles-own [count-up]
to setup
clear-all
set num-turtles 5
reset-ticks
end
to go
if count turtles < num-turtles [
ask patch 0 0 [
sprout 1 [
set count-up 0
]
]
]
if (count turtles > 0) [
ask min-one-of turtles [who] [
if count-up >= 6 [
set number-dead number-dead + 1
die
]
]
]
ask turtles-on patch 0 0 [
set count-up count-up + 1
]
set cumulative-sum cumulative-sum + count turtles
set average-time ifelse-value (number-dead = 0) [0] [(cumulative-sum) / (number-dead)]
tick
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

Net logo agents that are in a row and seperated by a empty patch must die

Imagine i have the same breed of turtle which is positioned like below and is separated by a empty patch.
1234 56 78 9 <br/>
AAAA AA AA A
When a random position is generated. Let say position 2 is the target, i want all the agents that are positioned at 1 2 3 4 to die as they are all supposed to be linked together. Similarly 5 and 6 will die if position 5 or 6 is selected.
I tried using links and neighbors but they don't seem to work. When i tried it, the turtles at position 4 & 3 are the only wants that dies.
Below is the code that i used to link the turtles:
if (any? virus-on neighbors)
[set create-links-with virus-on neighbors [tie]]
Below is the code that i used to kill the turle:
ask virus-on patch in_xcor in_ycor [
ask link-neighbors [die]
die
]
One thing for sure is that you don't need to go through the trouble of creating links between the turtles that you are about to kill. Your main problem is to identify which turtles need to die, and this can be accomplished with a recursive procedure: you have a target turtle that needs to die, but you want it to ask its neighbors do the same (and they, in turn, to ask theirs, etc.)
The process is made slightly more complex by the fact that killing a turtle in the middle of the recursion would mess things up, but you can get around this by using a turtle variable that serves as a flag for marking turtles that need to be killed, and then killing all of those at the end.
The following code should provide a fully working example:
turtles-own [ marked-for-death ]
to setup
clear-all
ask patch 1 0 [ sprout 1 ]
ask patch 2 0 [ sprout 1 ]
ask patch 3 0 [ sprout 1 ]
ask patch 4 0 [ sprout 1 ]
ask patch 6 0 [ sprout 1 ]
ask patch 7 0 [ sprout 1 ]
ask patch 9 0 [ sprout 1 ]
end
to go
ask turtles [ set marked-for-death false ]
ask turtles-on patch 2 0 [ mark-for-death ]
ask turtles with [ marked-for-death = true ] [ die ]
end
to mark-for-death
set marked-for-death true
ask (turtles-on neighbors) with [ marked-for-death = false ] [ mark-for-death ]
end
This example kills the turtle on patch 2 0, and all of those that are linked to it. You will need to adjust it for own purposes but it should be enough to get you going...
Edit:
Slightly more elegant version, as it does not require a turtle variable (assuming same setup procedure):
to go
let marked [ marked-for-death [] ] of turtles-on patch 2 0
ask turtle-set marked [ die ]
end
to-report marked-for-death [ marked ]
set marked fput self marked
ask (turtles-on neighbors)
with [ not (member? self marked) ]
[ set marked marked-for-death marked ]
report marked
end