Direct to move turtle to a specific spot - netlogo

I have enclosed turtles in between walls and obstacles and want them to move to a specific goal spot. Each tick I forward turtle only a specific amount. How do I implement this in terms to variating the turtle heading?
Explaining more
In the above figure:
1. Consider all turtles inside the box at the start
2. You want turtles to reach goal spot above,(where turtles present currently in figure)
3. You have obstacles that is, walls in between which 1 opening that is the rectangle in the figure with the gap. Turtles have to pass this gap only.
How I tried
I make turtles face the goal spot using facexy and bounce back after colliding with wall but in doing so the turtles on extreme left and right of box keep colliding with wall since after each collision they again have the direction from facexy
Please help. Thanks in advance.

An easy fix (according to what I read about making the turtles bounce).
goal-xcor is the x-coordinate of goal.
goal-ycor is the y-coordinate
of goal.
random-number is a random number between 1 and 10(?).
`If COLLISION: `
if they're in the left make them facexy goal-xcor + random-number goal-ycor
if they're in the right make them facexy goal-xcor - random-number goal-ycor
If there is not any collision it means they're in the right way and they should keep going the goal direction.
It's important that you facexy goal-xcor goal-ycor every time you make them turn around. To be more clear: Pseudo-code:
1 setup-turtles-inside-box
loop:
2 ask all turtles to face goal.
3 ask all turtles to go forward.
4 if collision
5 if turtle-in-left
6 ask turtle to face goal-xcor + random-number goal-ycor
else
ask turtle to face goal-xcor - random-number goal-ycor
7 loop until all turtles are in goal.
Let us know of any doubt!
Here is another approach:
http://rur-ple.sourceforge.net/en/random.htm

Related

Can turtles move the lateral direction (left or right) in NetLogo?

My simulated turtles can move forward and backward.
In addition to it, I want to move turtles towards left or right directions with their heading direction keeping.
However, I don't know how codes acheves it.
Please tell me any codes to program it in NetLogo.
To make a turtle move left one square, have it do:
lt 90
fd 1
rt 90

turtles are not moving through the door

Here is my code. I have walls in the colors grey and brown, which turtles should avoid when moving. I have to move my turtles when Eat = 1 toward the door with the color green and enter the room if Eat =0 turtle has to move in black patches only.
I have an issue as turtles are not moving through the door and also when facing the wall they stop there instead of
to go
ask students [
pd
set heading 90
forward 1
if (eat = 1)
[
if (([pcolor] of patch-left-and-ahead 90 1 = green) or ([pcolor] of patch-left-and-ahead 90 2 = green) or
([pcolor] of patch-left-and-ahead 90 3 = green) or ([pcolor] of patch-left-and-ahead 90 4 = green) or
([pcolor] of patch-left-and-ahead 90 5 = green) and pycor <= -10 )
[
set heading towards one-of patches with [pcolor = green]
forward 1
]
if ([pcolor] of patch-ahead 1 = green)
[
set heading 0
forward 3
]
if (pycor >= -9 and [pcolor] of patch-ahead 1 = black)
[
set heading 270
forward 1
]
]
while [ any? patches in-radius 1 with [ pcolor = grey ] or any? patches in-radius 1 with [ pcolor = brown - 2]] [
bk 1
lt (random 180) - 90
fd 1
]
]
tick
end
Your code looks quite chaotic to me so it is hard to give immediate pointers. Instead, let me describe what happens at each part:
let new-patch patch-ahead 2
You define new-patch as the patch in the direction your student is currently facing
if ([pcolor] of patch-here = black ) [
ifelse (eat = 0)
[set heading random 360
forward 1]
[
set heading random 360
forward 1
set heading towards one-of patches with [pcolor = green]
forward 5
]
When a turtle is standing on a black patch, it has two options, depending on whether or not it has any eat. If it has no eat, it faces a random direction and takes a step forward. If it has eat, it also faces a random direction and takes a step forward. After that it chooses a random door and moves 5 steps towards it.
The immediate problems I see with this part are:
What is the purpose of turning in a completely random direction and stepping forward?
Since both the turtles with eat and those without eat have the random walk it, it should not be inside the ifelse blocks.
The random step could make your turtle step onto a grey or brown patch, which I assume you don't want
The students with eat != 1 move towards a random green patch but this green patch they choose can be a different one each tick, causing them to move back and forward between two doors. I suggest either having them pick one door as destination and storing it as a turtle variable, always moving towards that one until they reach it (set my-door one-of patches with [pcolor = green]) or having them move towards the closest door (set heading towards min-one-of (patches with [pcolor = green]) [distance myself])
Here as well, your turtle could walk onto a wall if it is in the direct line towards your door
Onto the next part
if ([pcolor] of new-patch = grey or [pcolor] of new-patch = brown - 2 ) [
right 180
forward 1
]
Here you have the turtle turn around and take a step in the other direction, if the patch that was 2 in front of them at the start was grey or brown.
This part of the code is executed regardless of whether or not the first part was executed, meaning that if the turtle stood on a black patch before, it first chose new-patch as the patch that was 2 in front of it, then it did some random turning and a step forward, and now it checks what color the patch they initially defined as new-patch had, even if they are now looking in a completely opposite direction. This could actually make them turn back towards the patch you are trying to avoid
I'm just wondering, why have new-patch be the patch that is 2 steps ahead and not 1 step ahead?
And finally
if ( pxcor >= -9 and [pcolor] of patch-here = grey or [pcolor] of patch-here = brown - 2)[
left 180
forward 1
]
Here you have a turtle turn around if the patch they are standing on is grey or brown. Not necessarily a bad way of solving them walking into walls but it does not solve all of those instances and furthermove provides problems once you are stuck in those walls
Let's assume you are standing on a grey patch while facing the door. This means that there is generally grey in front and grey behind. Since the student is not standing on black, there is no random walk. The patch 2 ahead is grey so you turn around and take a step. Now you are standing on another grey patch so you turn back and take a step, ending right where you started this tick.
Of course for this to happen you first need to actually end up in the wall. For that we have the forward 5 from earlier to blame. A turtle that is close to the wall and moves 5 steps toward a door can do so through part of a wall. It gets deep enough for both of your 180° turns not to be able to get them out.
Instead of all this turning around, I suggest you add in a local variable called destination let destination patch-ahead 1, let it check whether or not that patch is a wall and only start randomly turning if it is a wall. Then set destination patch-ahead 1 and check again. Only let it move towards destination after all the checking is done.
In general, I suggest you let the turtles put their pen down to more accurately trace their movement ask turtles [pd], reduce the number of turtles and see what happens from step to step
As to your final question of why they don't move through the door: well, in the code there is no command that tells them what to do if the patch they are standing on is green so once they step onto the green they will simply stop. (or if they manage to move through the door, they will turn back towards it (or another door) and with the random walks added in, they will eventually end up either in a wall or on top of the door, both of which make them stop)
Last but certainly not least: start a bit smaller. Use just one or a few turtles, a short wall and a single door. Make sure your movement works on that before scaling up to all those different rooms
I hope this points you in the right direction and will be glad to further elaborate if you have questions

What is the difference between turtle size, patch size and frame rate in netlogo?

I want to know the Relationship between turtle size and patch size in Netlogo. Also I want to make those sizes in a way such that any turtle moves from one patch to another at every tick.
Patch size is measured in pixels. It is configurable in the "Model Settings" dialog.
You can think of turtle size as measured in "patches". A turtle of size 1 should appear to be the size of one patch. (Note, however, that turtle size is a "display only" property. Conceptually, turtles are just points. They don't really occupy space in the NetLogo world even if they appear to do so.)
Changing the xcor or the ycor of a turtle by 1 should move it by exactly one patch. If you want them to move diagonally, things are a bit more messy. You might be better off targeting destination patches directly, e.g.:
; move to the patch north-east of current position:
ask one-of turtles [ move-to patch-at 1 1 ]
; move to an adjacent patch at random:
ask one-of turtles [ move-to one-of neighbors ]
Those are just examples, of course. The code to use will depend on what exactly you are trying to do. If you tell us more in a separate question, we might be able to help you.

what does `forward 1` mean in netlogo? how to specify the nearest patch of a turtle in 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.

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.