Rotating a turtle through cone of vision - netlogo

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

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:

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

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

Specifying dispersal distances for turtles in Netlogo

So for part of my model, I want my turtles (in this case, deer, specifically juvenile female deer) to "disperse" according to certain criteria. Here is what I have so far:
to move-dispersing-femjuvs
ask femjuvs [
let natal-range patches in-radius 5
let density (count femadults-on natal-range + count maleadults-on natal-
range + count femjuvs-on natal-range + count malejuvs-on natal-range +
count infants-on natal-range)
let chance-disperse 1 / (1 + exp(2.051 - (0.002605 * density)))
if (random-float 1.001 < chance-disperse) [
let mean-distance (18.703 + (0.02533 * density))
I believe this will give me the proportion of the female juveniles that will disperse, as well as the mean dispersal distance, taken from literature (I realize the if statement is incomplete, this is where I'm stuck). My question is, how do I get the "femjuvs" who are dispersing to actually move? They would move until they reach an unoccupied patch in a radius that does not overlap the "natal range". Ideally, I would want all of the distances of the femjuvs who do disperse to have a mean of "mean-distance". I have "dispersal-distance" as a "femjuvs-own" variable, but I haven't set it to any value.
Any help with any part of this problem is appreciated. Thank you!
Possibly use: "setxy" or "forward"
setxy
If the aim is to update a deer position over time, calculating its coordinates, setxy might be a good option. This isn't so much moving as it is adjusting its coordinates.
ask femjuvs [ setxy <xcor> <ycor> ] ; example
ask femjuvs [ let x-coordinate <some-long-computation>
let y-coordinate <some-longer-computation>
setxy x-coordinate y-coordinate ]
forward
To move a deer, there is a function called "forward" that can be invoked by femjuvs (turtle/breed context):
ask femjuvs [ forward <number> ]
It would move femjuvs n spaces in the direction it is facing, where is the number specified.
Possible additions: (* - relevant functions)
to move-dispersing-femjuvs
ask femjuvs [
let natal-range patches in-radius 5
let density (count femadults-on ...) ; shortened for space
* forward 1 ; move forward one
]
...
end
Also, if dispersing from a given point or coordinate is necessary, having the deer face the point and turn it 180 degrees could work.
ask femjuvs [ face <random-point> ; or facexy <xcor> <ycor>
left 180 ] ; turn the opposite direction
ask femjuvs [ forward 1 ] ; disperse
One thing to note is that if the direction is not set by the developer, NetLogo will store a direction beforehand, which may or may not be helpful.
More details in : https://ccl.northwestern.edu/netlogo/docs/dictionary.html

Move diagonally in netlogo

I want move to a specific diagonal direction in netlogo.
I can move the turtle with the fd command but I don't know what condition (if-else) put in the code
to go
ask turtle 2
[ move ]
tick
end
to move
if( (pxcor = -15) and (pycor = -15 ))
[fd 5 ]
end
Your question is still unclear. You say you "want turtle with id 2 ,first move to right and then move to up". But how far in each direction? I'll assume 5, based on your question. Here is what seems to be the best overall match to your question.
to go
ask turtle 2 [move1]
end
to move1
set heading 45
fd 5
end
But if you really want to move right and then move up
to move2
set heading 90
fd (5 / sqrt 2)
set heading 0
fd (5 / sqrt 2)
end

Trying to get factions to arrange in segments? NetLogo code advice

I've made a model that aranges factions (turtles in different colours) in a circle.
At the moment they arrange randomly, was wondering if someone could help me arrange them so, for example, red occupies the first 90 degrees, blue the next 90 degrees, etc (on setup).
Here's my code...
ask patch 0 0
[ ask patches in-radius ( max-pxcor * .9) with [ random-float 100 < density ]
[ sprout 1
[ set breed cons
set shape "circle"
set faction random factions
set heading random 360
set size 1
]
]
]
.. guessing I will have to do 360 / fractions, but not sure how to phrase it, if someone could help me out that'd be great. Thanks!
The NetLogo primitive that's the closest to what you want to do is in-cone, which reports the set of turtles that are in the "cone of vision" of another turtle. But your "pie slices" should just be relative to patch 0 0, not to another turtle! No problem: just make a temporary turtle at patch 0 0, use it to get turtles in-cone with the appropriate angle, and kill your temporary turtle.
The following procedure can be used "as is" with your code (just call it from your setup procedure after creating your turtles exactly as you were doing before):
to assign-factions
let angle 360 / factions
foreach n-values factions [?] [
ask patch 0 0 [
sprout 1 [
set heading ? * angle
ask turtles in-cone max-pxcor angle [ set faction ? + 1 ]
die
]
]
]
end
The code is pretty straightforward, except for maybe the more obscure n-values. You could replace it with a while loop if you prefer, but it's really just counting from 0 to factions.
Here is what you'd get with 5 factions: