I set/update each turtle's position as follows:
set xcor xcor + item 0 vector
set ycor ycor + item 0 vector
Therefore I add a vector to the current agent's coordinates.
PROBLEM:
I wish to rotate the added vector by angle x. Thus the vector "vector" should be rotated by angle x.
The angle should be taken from a Gaussian distribution with a specified deviation.
I am trying to achieve something similar to Couzin's model.
http://www.csim.scu.edu.tw/~chiang/course/ComputerGameAdvance/Collective%20Memory%20and%20Spatial%20Sorting%20in%20Animal%20Groups.pdf
Thanks in advance!
You seem to have two questions here; I'll address the one you used for the title. The matrix extension allows matrix multiplication, so you could just create a standard rotation matrix once you have the ange of rotation. But standard advice in NetLogo would be to use a more turtle-centric approach. Then you need to decide whether to use the NetLogo heading conventions (0 degrees for north, 90 degrees for east, etc.) If so you could do something like this:
to move [#dx #dy]
let %dist 0
ask patch 0 0 [set %dist distancexy #dx #dy]
facexy (xcor + #dx) (ycor + dy)
let %theta random-rotation
rt %theta
jump %dist
end
to-report random-rotation
report (random-float 360) - 180
end
Here the random rotation is not Gaussian distributed because I was not sure what you meant. Perhaps a von Mises distribution? In any case, you should clarify and ask as a separate question.
Just to emphasize Alan's point: Unless you have a good reason to use vectors, it's usually much easier and clearer to avoid them in NetLogo. If all you want to do is turn the turtle by a random amount drawn from a Gaussian distribution, you can just do:
right-turn random-normal 0 <std-dev>
where <std-dev> is your desired standard deviation. Then, you can tell the turtle to go forward by what would have been the magnitude of the vector: forward <distance>.
If you absolutely need to do a vector rotation, you can do so without the matrix extension fairly easily:
to-report rotate-vector [ vec angle ]
let x first vec
let y last vec
let mag sqrt (x * x + y * y)
let old-angle atan x y
let new-angle angle + old-angle
report (list (mag * sin new-angle) (mag * cos new-angle))
end
Remember that angles in NetLogo are flipped around 45º so that 0º is north and 90º is east; thus, sin and cos are flipped when dealing with angles.
Somewhat simple, convert vector to angle, rotate (randomize), then convert back. For good coding style and such, break into modules.
to-report rotate [ #vector #angle ]
let $dx first #vector
let $dy last #vector
let $magnitude sqrt ($dx * $dx + $dy * $dy)
set #angle #angle + atan $dx $dy
report (list $magnitude * sin #angle $magnitude * cos #angle)
end
to-report nudge-vector [ #vector #std-dev ]
report rotate #vector random-normal 0 #std-dev
end
to move-inaccurately [ #vector #std-deviation ]
set #vector nudge-vector #vector #std-deviation
setxy (xcor + first #vector) (ycor + last #vector)
end
Related
I'm working on a model in NetLogo where I would like to report the mean and standard deviation of a set of turtle dispersal directions (bearings from 0-360 degrees) in each year of the simulation. Of course there's no default command in NetLogo for these circular statistics, so I'll need to write out the calculations by hand. I'm wondering if anyone has worked out a similar custom function in NetLogo before?
I've come across this set of code for calculating the mean:
to-report mean-of-headings [headings]
let x-mean mean map [sin ?] headings
let y-mean mean map [cos ?] headings
if x-mean = 0 and y-mean = 0 [ report random 360 ]
report atan x-mean y-mean
end
But I'm not positive if that will do for the mean, and haven't seen code for the SD. My thought was to translate this R code into NetLogo language, and create a reporter similar to the one above?
Tester <-c(340, 360, 20) # list of bearings
sine = sum(sin(Tester * pi/180)) # sin of each angle, convert to radian first
cosine = sum(cos(Tester * pi/180)) # cos of each angle, convert to radian first
Tester_mean = (atan2(sine, cosine) * 180/pi) %% 360
mu = (Tester - Tester_mean + 180) %% 360 - 180 # Difference of each angle from mean
Tester_sd = sqrt(sum(mu^2)/(length(Tester) - 1)) # Standard Deviation
Tester_mean # mean bearing
Tester_sd # sd bearing
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
I have an ellipse shape. (created having vertical direction)
I want to place several such shapes on the circle contour
They should be placed having same distance one from another
;x,y - circle center coordinates
;r - radius
to draw-ellipses [x y r elNum ]
if elNum > 0 [
let theta 0
let delta 360 / elNum
loop[
if elNum = 0 [ stop ]
crt 1 [
setxy (x + r * cos theta) (y + r * sin theta)
set shape "ellipse"
set heading 90 + theta
set size 7
]
set theta (theta + delta)
set elNum (elNum - 1)
]
]
end
The ellipse shapes ate placed correctly but their direction is wrong. I want the ellipses to have radial direction. I.e to be the continues of the radius to the point, where shape is placed. but directions are not good.
Only for 0 and 180 degrees the directions are ok.
Some advices what am I missing?
Do I understand correctly that you want your turtles to face away from the radius? If that's true, try replacing your heading line with:
set heading atan xcor ycor
to do it in a way that is consistent with your angle math. Alternatively, you can use facexy, and replace the heading line instead with something like:
facexy x y
rt 180
Get mean heading of neighboring turtles
to-report mean-heading [ headings ]
let mean-x mean map sin headings
let mean-y mean map cos headings
report atan mean-x mean-y
end
from that answer gets me part way there but I want is each of the headings to be weighted on size of the turtles.
something like
sum [heading * size] of turtles / sum [size] of turtles
But that would, you know, actually work.
If you accept a turtleset instead of a list of headings, then:
to-report weighted-mean-heading [turts]
let mean-x mean [size * sin heading] of turts
let mean-y mean [size * cos heading] of turts
report atan mean-x mean-y
end
I have angle A in netlogo. Which is the turtle's heading?
I wish to convert to it to unit vector.
How should I do it given the turtle's axis are different from that normal?(Heading 0 corresponds to looking forward).
Assuming that by "unit vector" you want a x component and a y component, the dx and dy primitives do this (see dictionary). So for example:
observer> crt 1 [ set heading 30 print (list dx dy) ]
[0.49999999999999994 0.8660254037844387]
Note that dx is just sin heading and dy is just cos heading.