netlogo runtime error 'turtles on' - simulation

while running the following code
if any? (turtles-on patch-ahead q)
[
some commands
]
where q is a number variable
there is a run time error saying: turtles-on expected the input to be agent or agent set but got nothing.
what can be wrong.

It could be that there is no patch-ahead q because the turtle is at the edge of world, facing towards the outside, and your would does not wrap around. The turtle has reached the end of the world and is facing the abyss.
Try setting the world so that it wraps around on both the x and y axis.

let p patch-ahead q
if p != nobody and any? turtles-on p
[ some commands ]
works, as does
if can-move? q and any? turtles-on patch-ahead q
[ some commands ]

Related

how to keep a minimum distance of 1 patch between itself and another agent when moving in netlogo

If the social_distancing variable is set to true the turtle must keep a minimum distance of 1 patch between itself and another agent when moving (i.e. check to see if another agent is in front before moving forward, a strategy to avoid collisions).
to social-distancing
if social_distancing = true[
ask turtles [
if any? other turtles-on patch-ahead 1
[
fd 1
]
]
]
end
I am new to netlogo and I have no idea if what I did here is the right way. please help me out.
That could work but you need "not" as in
if not any? other turtles-on patch-ahead 1
Also, you might consider rotating the heading of stuck turtles or that logic will result in a log-jam of turtles in little clusters all unable to move.
I didn't verify my syntax but here's the idea:
ifelse not any? other turtles-on patch-ahead 1
[
fd 1
]
[
set heading ( heading + random 90 )
]

error: WITH expected this input to be an agentset, but got a patch instead

I've been trying since yesterday to code the following: I have a world with 1 turtle in each patch (sprout command). I would like the turtle to check the 8 neighbors from the patch it is in. I did it like this: let neighWithVeg count neighbors of patch-here with [veg-value > 0.2]. But the error appears: WITH expected this input to be an agentset, but got a patch instead
Can someone help me?
patches-own [
veg-value
]
to setup
clear-all
reset-ticks
ask patches [
sprout 1
]
end
to go
ask turtles [
neighboring
]
end
to neighboring
let neighWithVeg count neighbors of patch-here of [veg-value > 0.2] ;; HERE THE ERROR
ifelse neighWithVeg = 0
[
move
]
[
move-to one-of neighbors of patch-here with [veg-value > 0.2]
]
end
Thanks in advance
For future questions, please only show the relevant code - probably the procedure with the error and the procedure that calls it. Also, NetLogo tells you which line generated the error.
I am guessing this is the line generating the error:
let neighWithVeg count neighbors of patch-here with [veg-value > 0.2]
You don't actually need of patch-here because the code is taking the perspective of the relevant patch anyway. But if you did, then you need [ ] for of, so it would be [neighbors] of patch-here.
But I think what you want is:
let neighWithVeg count neighbors with [veg-value > 0.2]

Can I modify NetLogo commands with variables?

To start with example:
to make-new-car [freq x y head ]
if (random-float 100 < freq) and not any? turtles-on patch x y [
create-cars 1 [
setxy x y
set heading head
set color one-of base-colors
]
]
end
yet I want to have more breeds for cars - not just one car breed. I also want to keep it simple and insted of doing this(first function is the same as one above):
to make-new-car [freq x y head ]
if (random-float 100 < freq) and not any? turtles-on patch x y [
create-cars 1 [
setxy x y
set heading head
set color one-of base-colors
]
]
end
to make-new-carSE [freq x y head ]
if (random-float 100 < freq) and not any? turtles-on patch x y [
create-carsSE 1 [
setxy x y
set heading head
set color one-of base-colors
]
]
end
and being redundant by repeating the same procedure just with different breed name I wan to do something like this(put breed-name as an argument and use it with create- command):
to make-new-car [freq x y head breed-name]
if (random-float 100 < freq) and not any? turtles-on patch x y [
create-breed-name 1 [
setxy x y
set heading head
set color one-of base-colors
]
]
end
However Netlogo is complaining that create-breed-name is not defined. Any ideas ?
Easiest way to do this is to do create-turtles then set breed breed-name. Here's an example.
breed [ testers tester ]
to make-turtles [ breed-name ]
create-turtles 1 [ set breed breed-name ]
end
to setup
make-turtles testers
end
You can also do probably do something with run after constructing an appropriate string, but I think the above is more straightforward.
Go with Jen's answer. It is, by far, the most straightforward way to accomplish what you need.
Just for the sake of it, however, here is one way to do it with run:
to make-new-car [freq x y head breed-name]
let commands [ ->
setxy x y
set heading head
set color one-of base-colors
]
if (random-float 100 < freq) and not any? turtles-on patch x y [
run (word "create-" breed-name " 1 [ run commands ]")
]
end
Notice that I put the commands that should be executed by the newly created turtle inside an anonymous procedure (using NetLogo 6.0.1 syntax) and then run that inside the string passed to run. You could also put everything in one big string, but then you would loose compiler checking, syntax highlighting, speed, etc.
But, anyway, don't do any of this. Use Jen's method.

Line of sight NetLogo

How do I implement a line of sight in NetLogo whereby
A turtle calculates/counts all turtles between it and the a given patch on the line of sight.
Thanks.
Let's say that each turtle has a turtles-own called target, which is the given patch, you can ask each turtle to face the target patch and count all the turtles between it and the given patch:
ask turtles [
face target
let n 1
let c 0
while [patch-ahead (n - 1) != target][
ask patch-ahead n [
if any? turtles-here [set c (c + 1)]
]
set n (n + 1)
]
print (word who " has " c " turtles on the line of sight")
]
A while loop doesn't look very clean in NetLogo but it works.
See Line of Sight Example, in the Code Examples section of NetLogo's Models Library.
Very similar to Dr_stein
To-report count-turtles-on-line-to[target]
Let c 0
Let d distance target
Face target
While d > 0
[
Let c c + count turtles-on patch-ahead d
Let d d - 1
]
Report c
End
I would suggest using the patches-ahead reporter from this answer:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
You can use it like this:
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
ask turtles [
let target one-of patches
face target
show (word
count other turtles-on patches-ahead distance target 0.1
" turtles between me and " target)
]
end

move turtles and create a crowd to target

to setup
ca
reset-ticks
ask patches [
set inside? (abs pycor < 10 and abs pxcor < 10)
set exit? false
ask patch 11 0 [ set pcolor lime set exit? true]
]
repeat initial-population [ ; start condition turtles with any other turtles on neighbors
ask one-of patches with [
inside? and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
sprout 1 [
set color blue
set size 1
]]]
end
to go
tick
define-neighbors-radius-2
move
end
to define-neighbors-radius-2
ask turtles [
set neighbors-ahead2 patches at-points [[2 1] [2 0] [2 -1]]
set neighbors-for-y-up2 patches at-points [[2 0] [2 -1] [1 -2] [0 -2] [-1 -2]] with [inside?]
set neighbors-for-y-down2 patches at-points [[-1 2] [0 2] [1 2] [2 1] [2 0]] with [inside?]
]
end
to move
;; my intent to move turtles to exit without their neighbors are occupied by other turtles, ;;that is the 8 patches around turtles are empty until exit?
ask turtles[
ifelse inside? [
if ycor = 0 [ ;strategy to turtles with in front exit
ifelse exit? [
set heading 90
fd .5
]
[
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-ahead2) [
fd .5
]
]
]
if ycor > 0 [ ; strategy to turtles occupied "bottom-side" of inside?
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-up2) [
fd .5
]
]
if ycor < 0 [ ; strategy to turtles occupied "down-side" of inside?
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-down2) [
fd .5
]
]
]
[
set heading 90
fd .5
]
]
end
I try to move turtles to exit but not all turtles move, why?
also, turtles must go out with ycor = 0, that is obliques direction don't allow because neighbors will occupied patches aren't inside!
Can't public this question because "looks like my post is mostly code", so talk about of my life:
seriously my intent is to create a crowd in front of exit and set some rules to delay the turtles flow exit, for this I need the neighbors empty to show interaction between agents.
(also accept some suggest to set this interaction) but at the moment turtles reach exit!
thanks
One problem with this code is that have you three ifs where it appears you only want one of them to run each time, but it's possible for more than one of them to trigger. You have:
if ycor = 0 [ ...commands #1... ]
if ycor > 0 [ ...commands #2... ]
if ycor < 0 [ ...commands #3... ]
but the turtle's ycor might be changed by commands #1 or #2. So it's possible that both #1 and #2 might run, or both #2 and #3, and perhaps other combinations as well. I assume you intended that each turtle should only move once per tick, so I recommend you rewrite this as:
ifelse ycor = 0
[ ...commands #1... ]
[ ifelse ycor > 0
[ ...commands #2... ]
[ ...commands #3... ] ]
Something else that should be fixed in this code:
reset-ticks should go at the end of setup, not near the start. It tells NetLogo setup is finished.
tick should go at the end of go, not near the start. It tells NetLogo a tick has finished.
I don't know if either of these problems I've pointed out are actually causing the unintended behavior you're seeing. Perhaps the bug isn't obvious just from reading over the code. In that case, you have two possible courses of action ahead of you:
1) Back up. Throw away this broken code and go back to the last version of your model that contained only code that have you have verified to work correctly. Then try again, but this time, don't add so much new code all at once. Attempt to make a very small improvement to the code, and get that working, before moving on to the next small improvement. And so on. If at any point you get stuck, come here, show your code, and ask a specific question about it. That question should be much easier to answer than your current question. Questions of the form “Here is a big mass of code that doesn't work, help!" are very difficult to answer.
2) Press on by investigating your questions yourself. You write, “not all turtles move, why?” Perhaps someone can answer this just by reading your code; I can't (unless my first guess above is correct). In order to figure it out, I would have to run the code myself and do experiments with it. I'd do things like try running it with just a single turtle and see if that case fails; add print statements to the code, showing each the turtle's coordinates and motions, so I could try and figure out which turtle is going wrong, and exactly under what conditions; and so forth. It's like detective work, or like doing chemistry experiments in the lab.