what does `forward 1` mean in netlogo? how to specify the nearest patch of a turtle in netlogo? - netlogo

when we ask a turtle to forward 2, does it mean:
on its heading direction, move to the second nearest patch
on its heading direction, move to the second nearest patch's center point
on its heading direction, move to the second nearest patch's center point which is (pxcor, pycor)
which one is correct? or are they the same answer?
How do we define the nearest patch this turtle is pointing to? Is the following understanding correct?
top patch: if heading is between -45(315) and 45 degree
right patch: if heading is between 45 and 135 degree
bottom patch: if heading is between 135 and 225 degree
left patch: if heading is between 225 and 315 degree

It means to move forward a distance of 1. The easiest way to see this is with the following code:
to setup
clear-all
create-turtles 1
[ setxy 0 0
set heading 45
forward 1
print xcor
print ycor
]
end
Try changing the heading and see what happens. Note that patches are exactly 1 unit wide and the centre of the patch is at integer values (so patch 0 0 extends from xcor -0.5 to xcor +0.5)

forward totally ignores patch boundaries and patch centers, so 1–3 are all incorrect. Patches don't enter into it; it's just trigonometry on the turtle's x and y coordinates. Specifically, forward 1 just means setxy (xcor + sin heading) (ycor + cos heading). The destination patch is simply whatever patch the new x and y coordinates happen to lie within. The destination may or may not be a patch center.
forward 1 might leave the turtle in the same patch (e.g. if the turtle is in the southwest corner of a patch facing northeast; the length of the diagonal is 1.414..., so forward 1 isn't enough to reach a new patch). Or, forward 1 might take the turtle to the nearest patch ahead, or to the second nearest patch ahead. (The latter can occur if the turtle just grazes the corner of a patch.)
A good model to play with to explore and understand all of these possibilities is Next Patch Example, in the Code Examples section of NetLogo's Models Library
As for the second part of your question, your definition is only correct if the turtle is initially standing on a patch center. For the general case, Next Patch Example answers that too. The next-patch procedure in that model is like your definition, but handles the general case.

Related

Moving downhill a patch variable while maintaining a global direction

I am building a dispersal model in a landscape with different landcover types: urban, forest, residential etc. Each one of these landcover types has a resistance-to-movement patch variable and they are found in clusters of patches:
urban - 4 + random-float 1.0
residential - 9 + random-float 1.0
forest - 1 + random-float 1.0.
I would like my turtles to move downhill the resistance values from one forest patch cluster to another. The issue I am having however, is that because the resistance values are not distributed in a gradient, the turtles will stay put if the resistance of its neighbours is the same or move backwards away from the forest patches if the resistance is lower in that direction.
Most models in the model library that use the downhill/uphill functions have gradient landscapes such as mountains, but mine are randomly distributed.
How can I get my turtles to move downhill while also maintaining a global direction towards the forest patches so that they do not move backwards?
Thanks!
That seems rather like a modelling problem than like a Netlogo problem to me.
How would you like the turtles to move, if they have visited all forest patches in that cluster?
Two ideas:
they move to the patch with the smallest resistance from all neighbor patches, that are closer to their destination, e.g. with min-one-of
face destination ; destination is a patch
; could be a global variable set in setup
let candidates (patch-set patch-right-and-ahead 30 1
patch-left-and-ahead 30 1
patch-ahead 1)
move-to min-one-of candidates [value] ; turtle moves to the candidate patch with the smallest "value"
or, if they have knowledge of the whole landscape, they could use a path search algorithm such as A* in order to minimize the sum of resistance on their path.

Netlogo: can I set the distance between turtles?

Netlogo: can I set the distance between turtles?
Hello,
I’m trying to create a model in which on each tick a turtle randomly chooses another turtle as a partner, and jumps to a specified distance of their partner (the distance that it’s given is based on a probability). It does not matter where it moves to, as long as the turtles are the specified distance apart.
I have tried to model this by creating a ‘jump-with-probabilities’ procedure, and defining distance the turtle jumps in the two ‘IID’ procedures:
to jump-with-probabilities ;; adds behaviours depending on how a random number compares with the odds.
ask turtles [
let random-fraction
random-float 1.0
if-else random-fraction <= 0.4
[ IID_10 ]
[ IID_50 ]
]
end
to IID_10
ifelse distance partner >= 10 ;; if the distance to their partner is larger than or equal to 10
[ jump (distance partner - 10) ] ;; TRUE - jump forward by the difference of distance partner & 10, so that the distance is now 10
[ jump (-1 * (10 - distance partner)) ] ;; FALSE - jump backward by the difference of distance partner & 10, so that the distance is now 10
end
to IID_50
ifelse distance partner >= 50 ;; if the distance to their partner is larger than or equal to 50
[ jump (distance partner - 50) ] ;; TRUE - jump forward by the difference of distance partner & 10, so that the distance is now 50
[ jump (-1 * (50 - distance partner)) ] ;; FALSE - jump backward by the difference of distance partner & 10, so that the distance is now 50
end
The problem with using this is that the distances between the turtles in the end are not the same as the distances that I specified. For example, Turtle 0 may jump towards Turtle 5 so that their distance is the specified 20. But, Turtle 5 will also jump towards its partner, which will change the distance between Turtle 0 and Turtle 5. I considered using ‘ask-concurrent’ instead of ask, but the problem remains, because I am telling the turtles to move a certain distance, rather than to move to a certain distance of their partner.
So my question is; is there a way that I can tell a turtle to be within a specified distance of another turtle? So that if the partner moves the turtle will move too to keep the distance at the specified length.
I thought it may be possible to use ‘move-to’ and add the specified distance somehow. Or alternatively, use ‘distance’ to set this between 2 turtles. It seems rather basic, but I have not been able to figure out how to do it!
Any help would be much appreciated!
There's possibly a better way, but I would do this by moving turtle B to where turtle A is (move-to turtleA), then giving it a random heading (set heading random 360) then moving it forward 10 (forward 10). You could also hide turtle B until you have finished moving it and then unhide it to make the visualisation neater. That sets up the relative position, then use Alan's suggestion of tie to hold the relative position.

How to control the positioning of a turtle on a patch?

In order for two way traffic to flow along a road that is the thickness of a single patch it is necessary for the position of each turtle to be to either the left or the right of the patch on which they are travelling. How can I control where a turtle resides on a patch while travelling along it so that there is enough space for two opposing flows of traffic on the same road?
You can use set xcor ... and set ycor ..., or setxy ... ..., or any other turtle motion command such as fd or bk, to position the turtle however you like. If the turtle's coordinates are both integers, it's on a patch center. If either or both aren't integers, then the turtle is in some other location in the patch.

Computing an agent on the further ends of cone of vision

Given:
The wall(grey agents) are in a constant place along the top of the
world.
The blue agents always directly below but at various
distances. But they be off to the side of the gap but nevertheless
can be rotated so that they face the gap.
That the cone of vision angle is same for all blue turtles.
In the above figures, the blue agent's cone of vision is depicted. I wish to calculate the grey wall which meet the ends of the cone of vision ,that is, one on right and one on left.Also could I somehow calculate the x-coordinate at that point. Not the grey agent's coordinate as that would be a approximation.
To Compute:
The x coordinates where the extremes of cone of vision intersect grey turtles. Or those grey turtles they intersect.
Rough Figure:
So I wish to compute x_1 and x_2 in the below figure.
One way could as suggested by #JenB to divide it into three cases and and calculate A in each case.(Primarily on left or right). Then use trigonometry. I am correct. Are there any other ways as well?
If this is a 2D problem, it is simply a case of intersecting lines.
I would avoid using multiple cases; that is very prone to errors.
You will have a line that describes your wall of turtles, and two lines that describe your FOV boundaries. You can formulate each of these three lines in parametric form as [o.x,o.y] + [v.x, v.y] * s, which is a fixed point [o.x,o.y] plus a normal vector [v.x,v.y] scaled by s.
The wall of turtles is only defined for a certain domain of 's'; let's say domain of wall.s = [0 to 0.4, and 0.6 to 1]
I would describe how to find the intersection points, but intersections of parametric 2D lines is pretty standard fare, and is better shown in a PDF, so I'll refer you to this...
http://www.ahinson.com/algorithms_general/Sections/Geometry/ParametricLineIntersection.pdf
(remember never to divide by zero)
Once you know the values of the scale parameters 'left.wall.s' and 'right.wall.s', you can tell whether the domain of the turtle wall is within the view of the player. Also you can determine the intersection points simply by plugging back into the parametric line formulas.
dwn's answer covers computing the precise point of intersection.
You said you were also interested in just finding out what patch the answer lies on. Here's code for that:
to setup
clear-all
create-turtles 1 [
set heading -30 + random 60
]
ask turtles [
;; show center of vision cone
ask boundary-patch [ set pcolor red ]
;; show edges of 20 degree vision cone
lt 10
ask boundary-patch [ set pcolor blue ]
rt 20
ask boundary-patch [ set pcolor blue ]
;; restore turtle's original heading
lt 10
]
end
;; answers the question, what patch on the top row of the
;; world is the turtle currently facing?
to-report boundary-patch ;; turtle procedure
let n 0
while [true] [
let target patch-ahead n
if target = nobody or [pycor = max-pycor] of target [
report target
]
set n n + 1
]
end
Sample result:
Of course, it would actually be computationally more efficient to compute the answer directly, via a formula. (With an optional rounding step at the end, depending on whether you want a point or a patch.) But this code shows how to do it without having to do any tricky math.
The following trigonometry approach(suggested by #JenB) works perfect:
to-report calx2 [x0 y0 x1 y1 A]
report x0 + (y1 - y0) * tan ( A + atan (x1 - x0) (y1 - y0))
end
to start
ask turtles[
set corner-1 list calx2 xcor ycor ([pxcor] of patch-goal)([pycor] of patch-goal - 0.4) (-45) ([pycor] of patch-goal - 0.4)
set corner-2 list calx2 xcor ycor ([pxcor] of patch-goal)([pycor] of patch-goal - 0.4) ( 45) ([pycor] of patch-goal - 0.4)
]
The problem just arises when the left edge goes beyond 180 and right edge go beyond 0. I didn't consider that cases. Anyways, the above code solves the problem.

Learn the direction of a moving agent

I created a turtle in NetLogo which is moving randomly and there are some obstacles. Is it possible to get its current direction? I want to get the turtle to walk back to the center when it sees an obstacle. I can calculate distance to the center, but since I don't know its direction I can't say forward or backwards, for example.
The turtle's current direction is given by the heading variable. You can both read and write to this variable in order to change the turtle's heading. You can also change it using facexy as N. Payette mentioned.
The facexy primitive will allow you to set your turtle's heading toward the origin:
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#facexy
ask turtle <who>
[If (patch-ahead = obstacle)
[
facexy origin
fd distance origin
]
]
Here obstacle and origin are the respective patches.
Building on what Jose M Vidal and N. Payette have already said.