Bounce routine with random walk in Netlogo 3D - netlogo

I'm having trouble coding a bounce routine with a random walk in Netlogo. I want to have agents do a random walk in a unidirectional current (the code for this bit is not correct, but is a functional placeholder). I also need to prevent agents from entering a seabed feature, delineated by an RGB pcolor. Here's my effort:
to move-resources
ask resources [
right random 45
left random 45
tilt-up random 45
tilt-down random 45
fd 1
;;; simulated current in one direction:
set heading 90
set pitch 0
set roll 0
fd 1
while [ any? patches in-radius 1 with [ pcolor = [218 160 62]] ] [
let nearest-patch min-one-of (patches with [pcolor = [218 160 62]])[distance
myself] ;;; find the closest sandy patch
face nearest-patch ;; face that patch
set heading heading - 180 ;; face away from that patch
fd 1 ;; move away from that patch
]
]
end

Apologies for missing a trivial mistake, but my problem was simply the use of:
set heading heading - 180
When in 3D I should've used (for a horizontally-oriented seabed):
set pitch pitch - 180

Related

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

How to go back to the middle of the road after turn in Netlogo?

My code is about 2 car in one road, where both cars will change their heading (turn to left) if they meet head-to-head, and go back to the center of the road (which is pycor = 0) and turtles can't walk outside the road,
My problem is I don't know how to make the turtles move back to the center, I tried to use cohesion from flocking, but I don't know the right way. Here is my code:
globals
[initialHead too-close-distance]
turtles-own
[speed
top-speed]
to setup
clear-all
ask patches [setup-road]
setup-cars
reset-ticks
end
to go
move
end
to setup-road
if pycor < 2 and pycor > -2 [ set pcolor white
]
end
to setup-cars
create-turtles 1
[
setxy -15 0
set color red
set size 1.2
set initialHead 90
set speed 0.5
set top-speed 0.5 + random-float 0.5
]
create-turtles 1
[
setxy 15 -0
set color blue
set size 1.2
set initialHead 270
set speed 0.5
set top-speed 0.5 + random-float 0.5
]
end
to move
ask turtles
[
speed-up-car
avoid
forward speed]
end
to avoid
slow-down-car
let visibility (patches in-cone 7 50)
let center pycor = 0
let too-near one-of other turtles-on visibility
ifelse too-near != nobody
[turn-away ([heading] of too-near) max-separate-turn
[;need to turn back to center]
[ fd speed]
end
to turn-away [new-heading max-turn]
turn-at-most (subtract-headings heading new-heading) max-turn
end
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
to speed-up-car
set speed (speed + acceleration)
if speed > top-speed [ set speed top-speed ]
end
to slow-down-car
set speed (speed - deceleration)
if speed < 0 [ set speed deceleration ]
end
Here's some basic logic that I think will work for passing for the case of 2 cars heading opposite directions. The logic gets much more complex if you have many cars.
I'll leave it to you to write NetLogo code.
Basically, you need a state-transition plans which keeps track of what state each car is in, so it can decide what to do next and what state that will put it in.
Suppose you have states ["driving", "turning-away", "passing", "turning-back" ], and you know if you have conflicting traffic. You also know your y-position so you know how far off the center you are.
You need to figure out logically what all the possible and relevant transitions are and the rules that determine which one to take given what state you are currently in.
It would be interesting (!) to model the states as nodes and the transitions as links, but let's leave that for another time.
Basically you will end up with a long list of if-else clauses, which you can evaluate to decide what the next-state will be, and then after everything is evaluated, you can do what it takes to move to that state. Rather than building a full dynamic model of motion, let's simplify to say that passing involves turning to 10 degrees from the original heading for a bit, then by 30 degrees for a while, then by 10 degrees for a while longer, then back to 0 degrees when happily off the road. Returning to the center-line is just the reverse process. This would probably look ok to a user.
Here's some pseudocode:
if driving and there's no conflict, keep driving.
if driving [
if there's a conflict
[ set next-state "turning-away"
if off-road by short distance [set offset-angle 10]
if off-road by medium distance [ set offset-angle 30]
if off-road by large distance [ set offset-angle 10]
if off-road entirely [ set offset-angle 0 set state "passing"]
]
otherwise [ set next-state "driving"]
and move forward based on speed and heading.
if in the middle of turning away the conflict goes away, i don't know what you want to do. Probably completing move to the passing location is simplest to code.
if passing and there's still a conflict, keep on passing
if passing and there's no conflict anymore, reverse the moves based on distance off-road to get back on the road.
if in the middle of turning back there is a new conflict, i don't know what you want to do. Probably change to turning-away and pick a heading based on distance from the centerline as above.
Anyway, let me know what you think of that approach. I don't see how to make it much simpler.

How to move turtles back to the patches that it come from

I have built the road shapefile which already intersected with patches. I want the turtle to move on the road that I had assigned.
to go
ask turtles
[
step
avoid-wall
]
tick
end
to-report coinflip?
report random 2 = 0
end
to avoid-wall
if [pcolor] of patch-ahead 3 = black [set heading heading - 180]
end
to step
ifelse coinflip? [rt random 60] [lt random 60]
fd random 3
end
Actually, I intend to let the turtle move out of the assigned patches. But I still want them to come back in the next tick or just the few tick later. Some turtles come back and some do not (according to my code). How should I fix the code?
I have tried to use this code in Turtles moving in a pattern (Netlogo) but it does not work (I think that it does not work because my patches are a narrow road, not an area.)

Netlogo 'towards' behaviour

For a pursuit-evasion assignment I need to use the NetLogo command 'towards', but it doesn't seem to work, or I don't understand it.
What I think it should do: give the angle between the heading of the turtle and the line connecting the turtle with the target.
Here's the code for a simple model I made just to show the problem.
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end
And here's a video of the behaviour. https://youtu.be/MUBiAypppc4 (I couldn't find a way to remove the audio without just replacing it using YouTube's current editing system, I'm sorry; you'll have to mute the audio yourself)
Examples of expected behaviour:
from 0:14 to 0:19, I would expect the number to gradually decrease, not increase
at about 0:38, I would expect the number to be 0, not somewhere around 300
between 0:38 and 0:42, I would expect the number to decrease or increase consistently, without those two sudden jumps
Is there a problem somewhere, or does 'towards' mean something different than I thought?
So turtle 0 is moving and turtle 1 is reporting the direction to turtle 0. I think towards is working fine but you have forgotten about the world settings. For example, in the 14-19s part, the shortest path from 0 to 1 is down and left (about 220 heading), but that shortest path is with the world wrapped. Your turtles can move off one side and come in on the other (as you can see turtle 1 doing).
NetLogo measures distances and directions taking into account the wrapping configuration. It knows that the shortest path to get from turtle 0 to turtle 1 goes off the side and comes in the other, and reports the direction that the turtle would have to move to follow that path.
Create a link and you can see this. Revised code:
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
create-link-with turtle 0
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end

How to add paths showing turtle movement in Netlogo

I'm trying to make a model that will tick a stipulated number of times and will move a maximum distance each time. I've gotten that done, but I can't figure out how to make the model show where the turtle has been using paths connecting the different locations the turtle went to.
I don't even really know if this is possible...? I've googled, searched stack overflow and looked through the Netlogo dictionary and looked through the somewhat helpful textbook I have on hand and can't find anything. I'm sure it's simple, but I just have no idea how to go about it. My current code:
to setup
clear-all
;; create turtles on random patches.
ask patch 118 28 [
sprout 1 [
set color one-of [green]
set size 10
]
]
reset-ticks
end
to go
ask turtles [
rt random-float 360
lt random-float 360
fd random-float 15
]
if ticks = 28 [return]
tick
end
to return
ask turtles [
setxy 118 28
]
end