How to keep track of the coordinates of the turtles after they hit the boundaries of the world in NetLogo? - distance

I am simulating a random walk within bounded angels, and I want to calculate the mean squared displacement of the turtles in 2D in my model. The turtles all start from the center. I am not sure how to update the xy coordinates for the turtles after they hit the boundaries. I am asking the turtles to bounce back when they hit the wall and save their new xcor and ycor. Here's my code:
to go
ask turtles
[; head in a random direction in range (-theta, +theta)
ifelse theta = 0
[set heading heading + 0 ]
; choose a normally distributed random angel in range (-theta, +theta)
[set heading heading + random-normal 0 (theta)]
fd step-size
set xc xc + (step-size * dx)
set yc yc + (step-size * dy)
; if your next patch is blocked:
ifelse not can-move? 1
[ set heading heading + 180
fd 1 ]
;otherwise:
[rt random-float random-normal 0 (theta)]
set xcor xc
set ycor yc
set dist sqrt (xc * xc + yc * yc)]
]
I am getting the error which says:
"Cannot move turtle beyond the worlds edge.
error while turtle 423 running SET
called by procedure GO
called by Button 'go'". Any ideas why?

You have the grid-wrap disabled and your xcor/ycor of your agents are going past the max-pxcor/min-pxcor and max-pycor/min-pycor

Related

Understand the use of dx and dy commands in NetLogo?

I'm new to NetLogo and I'm in doubt about 2 Netlogo commands.
Could someone help me understand the dx and dy commands? I read the description in the NetLogo dictionary. But, I didn't quite understand what it does.
What's the difference between using a move with dx and dy and without?
Thanks in advance!
A simplified example taken from a book with application in NetLogo:
turtles-own
[
real_x ;; the real x coordinate dist from origin ie ignoring wrapping around world
real_y ;; the real y coordinate dist from origin ie ignoring wrapping around world
]
to setup
here
create-turtles 1
ask turtles
[
setxy random-xcor random-ycor
; set real-x xcor
; set real-y ycor
]
reset ticks
end
to go
move
tick
end
to move
;; using dy and dx
; ask turtles
; [
; set heading random-float 360
; set real_x real_x + dx
; set real_y real_y + dy
; fd 1
; ]
ask turtles
[
set heading random-float 360
fd 1
]
end
dx is the difference in the x-coordinate if the turtle move forward 1 unit at its current heading. Same thing for dy and the y-coordinate.
Mathematically: tan(heading) = dy/dx
Geometrically:

Move a turtle to the nearest unoccupied patch in Netlogo

Currently, I have a model with many parameters in it, and one of them is having male yearling deer disperse according to certain criteria. The distance each male yearling disperses is pulled from a log-normal distribution. Here is what I have so far:
to move-dispersing-maleyearlings
ask maleyearlings [
let chance-disperse random-float 1.001
if chance-disperse < .62 [ ;;average dispersal rates in Long et. al paper
let mu 7.5
let sigma 6.1
let beta ln (1 + (sigma * sigma) / (mu * mu))
let S (sqrt beta)
let M (ln mu) - (beta / 2)
let new-distance exp (random-normal M S)
while [any? other turtles-here and dispersal-distance < new-distance]
[right random 360
fd 1
set dispersal-distance dispersal-distance + 1]]]
end
So this code should have 62% of the male yearling deer disperse, and they'll disperse a distance of "new-distance". If I am understanding my while loop correctly, they will move until they reach their "new-distance" and until they land on an unoccupied patch.
But now instead what I want to do is have each male yearling deer disperse their respective "new-distance", but if they land on a patch that is occupied, I want them to then move to the nearest unoccupied patch. If the patch they land on after moving "new-distance" is unoccupied, then they would stay on that patch.
Any ideas of how to do this? Thanks for your help!
If I am understanding your request correctly, you want to replace:
while [any? other turtles-here and dispersal-distance < new-distance]
[right random 360
fd 1
set dispersal-distance dispersal-distance + 1]]]
with code that moves to the closest empty patch. Try something like this (not tested):
if any? other turtles-here
[ move-to min-one-of (patches with [not any? turtles-here]) [distance myself]

Find the presence of other turtles in a given direction upto a distance

I wish to find out whether in a given turtle's heading there is another agent present upto a given distance.
Here the Distance is "D".
Note:
Any agent present before D in the given direction should be also considered.
Even the direction doesn't coincide with the other's agent centre but just touches it ,even then that agent should be considered.
Problem:
No turtle-ahead procedure available. Combination of patch-ahead and turtles-on not applicable due to patch-size>> turtle-size.
Possible approach:
1.Represent the turtle's heading by the equation of a line.
to-report calculate-line[x y angle]
let m tan angle
let A m
let B -1
let C (- m * x + y)
report (list A B C)
end
to-report heading-to-angle [ h ]
report (90 - h) mod 360
end
let line-equ calculate-line (xcor) (ycor) (heading-to-angle heading)
2.Calculate the perpendicular distance from other turtles here, Check if there are within a range that the size of other turtles.
to-report value[A X1 Y1];;A is list of coefficents of line, x1 and y1 are coordinates of red turtle
if-else(abs((item 0 A * X1 + item 1 A * Y1 + item 2 A) / (sqrt((item 0 A ^ 2) + (item 1 A ^ 2) ))) < [size] of wall )
[ report "true"][report "false"]
end
3.To check if the red turtle is within D. One could obtain a line perpendicular to black one and compute the red turtle distance from it to check if it is less than or equal to D. But then that adds more complication.(Though one can simplify rotate the turtle by 90 left or right and get the line equation.)
This is what I meant by my comment. Run this code (as its own model). What it does is turn all the turtles on a few 'ahead' patches a different colour. I know this is not what you are trying to do, but the agentset candidates is a relatively small number of turtles. These are the only ones that you have to check whether they are on the correct path. So instead of turning them a different colour you could check the direction that they are from your initial turtle.
to setup
clear-all
set-patch-size 25
resize-world -10 10 -10 10
create-turtles 1000
[ setxy random-xcor random-ycor
set color yellow
set size 0.4
]
ask one-of turtles
[ set color red
set size 1
check-from-me 5
]
end
to check-from-me [howfar]
let counter 0
let candidates turtles-here
while [counter < howfar]
[ set counter counter + 1
set candidates (turtle-set candidates turtles-on patch-ahead counter)
]
ask candidates [set color red]
end
to-report check-wall
let return false
hatch 1[
set color black
set size ([size] of one-of walls) / 2
show (2.5 * ([size] of myself))
while [distance myself < (2.5 * ([size] of myself))]
[
fd ([size] of one-of walls) / 64
if any? walls in-radius size
[
set return true
]
show distance myself
]
]
report return
end
The above works. But still is approx. I am looking for better solution with probably less maths as one elucidated in the question.

Rotating a turtle through cone of vision

I wish to write to where I have a cone-of-vision cone angle for the turtle.
And I rotate the turtle through the cone-of-vision.
Therefore, first it's heading the start of cone-of-vision and with a increment of 0.05 degree it changes till it reaches the end of cone-of-vision
let max-head heading + (cone-angle / 2)
set heading subtract-headings heading (zero-dash / 2)
while[(subtract-headings heading max-head ) < 0]
[
set heading heading + .05
;wait 0.1
]
The above code I wrote isnt correct. But I can't find the mistake.
I think part of your difficulty is using subtract-headings. This finds the size of the shortest angle between two headings and that's not what you want for a cone of vision (which could potentially be wide). Since your code rotates clockwise, I have assumed you want to start at the most anti-clockwise point and rotate. The only difficulty then is if the heading goes through 360, so you need to separate the cone sweeping counter and the actual heading, and use mod arithmetic to convert from the counter.
globals
[ cone-angle]
to setup
clear-all
create-turtles 1
set cone-angle 25
end
to go
ask turtles
[ let max-head heading + (cone-angle / 2)
let fake-head heading - (cone-angle / 2)
while [fake-head < max-head]
[ print fake-head
set fake-head fake-head + 1
set heading fake-head mod 360
wait 0.1
]
]
end

What is the most efficient way to ask other turtle who can see the caller turtle in NetLogo?

I was wondering if anyone has a good idea to filter turtles who have the caller turtle in their vision and in their cone of vision with the least computational cost?
Now I am using something like this:
Member? Caller-Agent agents in-cone 5 100
or just who are agents who can see me?
I was wondering if anyone knows how can I check if heading of other agents in some radius X is toward the caller agent ?
So , this is the way I did it:
I used abs( towards myself - heading) < 50
I thought it might include cone with angle of 100 since it uses absolute value.
This is just a test program to see if it works:
ask turtles[set label "" set color green]
ask turtle 7 [
set color red
set label "Caller"
ask other (turtles with [distance myself <= 3 and abs( towards myself - heading) < 50 ])
[
set color yellow
]
ask other turtles [
set label (Member? turtle 7 Other turtles in-cone 3 100)
]
]
and this one cone 80 and abs( towards myself - heading) < 40 :
There is a problem with this approach:
if an agent is as same location of the caller then I will get the error that there is no heading defined to same point or something like that!
Error is : No heading is defined from a point (7,7) to that same point. error while turtle 28 running TOWARDS
Update:
[(distance myself <= 3 and distance myself > 0 and abs( subtract-headings towards myself heading ) < 60) or distance myself = 0 ]