I'm trying to learn the basics of NetLogo.
I have created a breed called zombies. I can only control my zombies.
Now I want my zombies to ask the neighbor-patch with the coordinates (0 0) to tell me the amount of zombies on the patch (0 0).
If the count is < 76, I want to move there.
What I have tried so far is:
`
let counter 0
ask neighbors [if patch-here = patch 0 0 [
set counter count [zombies-here]
]
]
ifelse counter < 76[
facexy 0 0
fd 1
set moved 1
] [
ifelse [pxcor] of patch-here > 0 [
facexy 0 2] [
facexy 0 -2
]
fd 1
set moved 1
`
any help is really appreciated!
greetings
raf
Related
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
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
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
In my model I have agents sprout at random throughout the environment. I'd like to to have a density gradient of these agents.
Is there a neater way to do it than running something like this for different radii?:
ask patch 0 0 [ask n-of 20 turtles in-radius 20 [die]]
Thanks
You could do something along those lines:
to setup
clear-all
let max-distance max [ distancexy 0 0 ] of patches
ask patches [
if random-float 1.0 > (distancexy 0 0 / max-distance) [
sprout 1
]
]
end
Many variants are possible. The key is to use a combination of random-float and distancexy 0 0 to get the density you want.
im doing an evacuation model which will compare the time to take if ull take the nearest exit or go with the crowd. What i have done so far is to simulate the" nearest exit"
to go
ask turtles [sideway]
end
to sideway
if pxcor < 0 [ set heading 270 ]
if pxcor > 0 [ set heading 90 ]
end
this is just apseudocode. now my question is that how to count the number of turtles in pxcor >0 and pxcore<0?
example
if pxcor > 0
counter ++;
count turtles with [ pxcor > 0 ]
turtles is just all the turtles. turtles with [ pxcor > 0 ] creates a new set that contains just the turtles from turtles for which pxcor > 0 is true. count just counts the number of agents in a set.