NetLogo turtles leaving a trail that fades with time - netlogo

I have turtles moving across the view, and I'd like to be able to follow where they go by making them leave a trail behind them, as though they were emitting smoke as they went. Of course, I could use the turtle pen (pen-down), but since there are many turtles, the view rapidly gets filled with old trails. The solution could be trails that last only for a few ticks before they dissipate. But I don't know how to achieve that.
To be more specific:
1) Is there a technique for making the line drawn following a pen-down command gradually fade away over the period of some ticks?
2) If not, is there a way of removing the line drawn using the pen a few ticks after it was drawn?
3) If not, is there some other technique that would have a similar visual effect?

There is no way to fade the trails in the drawing layer over time. If you want trails that fade, you'll need to represent the trails using turtles instead.
Here's sample code for having "head" turtles that trail ten-turtle "tails" behind them:
breed [heads head]
breed [tails tail]
tails-own [age]
to setup
clear-all
set-default-shape tails "line"
create-heads 5
reset-ticks
end
to go
ask tails [
set age age + 1
if age = 10 [ die ]
]
ask heads [
hatch-tails 1
fd 1
rt random 10
lt random 10
]
tick
end
I'm just killing off the old trails outright, but you could also add code that fades their color over time. (An example of a model that does that is the Fire model, in the Earth Science section of the NetLogo Models Library.)

Here's a version based on the same principle as the one by #SethTisue, but the tails fade away:
globals [ tail-fade-rate ]
breed [heads head] ; turtles that move at random
breed [tails tail] ; segments of tail that follow the path of the head
to setup
clear-all ;; assume that the patches are black
set-default-shape tails "line"
set tail-fade-rate 0.3 ;; this would be better set by a slider on the interface
create-heads 5
reset-ticks
end
to go
ask tails [
set color color - tail-fade-rate ;; make tail color darker
if color mod 10 < 1 [ die ] ;; die if we are almost at black
]
ask heads [
hatch-tails 1
fd 1
rt random 10
lt random 10
]
tick
end

Here's another approach but without using additional turtles. I include it for variety sake - I would recommend going with Seth's approach first.
In this approach, each turtle keeps a fixed length list of previous locations and headings and stamps out the last position. There's some unwanted artifacts with this approach and is not as flexible as using additional turtles, but I think it uses less memory which may help on larger models.
turtles-own [tail]
to setup
ca
crt 5 [set tail n-values 10 [(list xcor ycor heading)] ]
end
to go
ask turtles [
rt random 90 - 45 fd 1
stamp
; put current position and heading on head of tail
set tail fput (list xcor ycor heading) but-last tail
; move to end of tail and stamp the pcolor there
let temp-color color
setxy (item 0 last tail) (item 1 last tail)
set heading (item 2 last tail)
set color pcolor set size 1.5 stamp
; move back to head of tail and restore color, size and heading
setxy (item 0 first tail) (item 1 first tail)
set heading item 2 first tail
set size 1 set color temp-color
]
end

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.

Coloring a route in Netlogo

I want some of my turtles to leave at trace of the steps they make. I want them to change the color of the patches they pass by on their move. Similar to what the "pen-down" command does - just with the effect, that the patches change color. It is esay for me to change the color of the patch, the turtle reach - but I want all the patches colored - like if you were walking on a lawn, and the grass under you momentaneouesly turns red :-) Nut just the steps with every tick, but the continous route.
Is there a way? I would be glad to se a small coded example. Thanks very much - this homepage does a great job.
Magellancruiser
Here is one basic solution. The key is realizing that instead of having the turtle do something like forward (random 10) + 1 you can instead have it go forward 1 a number of times equal to (random 10) + 1. Because the distances are based on path sizes (1 = 1 patch across), if you color the patches as you go forward 1 at a time, you should "draw" the color on the patch.
turtles-own [ my-color ]
to setup
clear-all
create-turtles 10 [
set my-color color ; the turtles will have random colors, store them to use later
set color white ; but they're easier to see if they're white
]
reset-ticks
end
to go
ask one-of turtles [
left (random 50) - 25 ; wiggle a bit to not just go in a straight line
let d (random 10) + 1 ; the turtle will move 1 to 10 steps
repeat d [
forward 1
set pcolor my-color ; the turtle can directly set the patch's pcolor variable to its own
]
]
tick
end
You can either use setup and go from the command center or add buttons for them.

you cant use tick in a turtle context because tick is observer only!! NETLOGO

If i add if to my go command i get the error message
you cant use tick in a turtle context because tick is observer only
here is my go commands. search eat go-home and den are all defined in my commands.
energy is also defined as a global variable that turtles own
to go
if ticks = day-length [set day day + 1 create-next-day]
ask adults [search eat]
if energy < 20000 [ask adults [go-home den]]
tick
end
if i take out the line
if energy < 20000 [ask adults [go-home den]]
it runs perfectly, but i need that line or an equivalent. please help
Commands
;;-------------------------------------------------------------;;
;;------------------- ADULTS COMMANDS--------------------------;;
;;-------------------------------------------------------------;;
;; Need to add a private variable (wolves own) for wolves [state] and then need to code 4 states 1. Den 2. Search 3. Eat 4. Return
;; need to code all 4 states
;; Need to correctly allocate energy and the state of decline
To den ;when wolf is full
set energy energy - .04
end
to search ;when wolf is hungry
set energy energy - .07
fd v-wolf
if random 600 = 1 ;; frequency of turn
[ ifelse random 2 = 0 ;; 50:50 chance of left or right
[ rt 15 ] ;; could add some variation to this with random-normal 45 5
[ lt 15 ]] ;; so that it samples from a dist with mean 45 SD 5
;; check if it can see a prey/food item
;; here i think we probably pick one of several possible prey
;; that are detectable randomly using the one-of command.
;; We should probably select the nearest one instead ** The turtles are getting
;; caught between two prey species and dying because they cant choose which one **
if any? prey in-radius smell [set heading towards one-of prey in-radius smell]
if energy < 0 [die]
end
To eat ;to kill prey and eat it
let kill one-of prey-here in-radius smell
;need to code in a variable for success too
if kill != nobody
[ask kill [ die ]
set energy energy + 10000]
end
to go-home ;to head home after they've eaten and den until they need to feed again
if energy > 30000 [set target-patch min-one-of (patches with [pcolor = white]) [distance myself]]
face target-patch
fd v-wolf
set energy energy - 1
end
if energy < 20000 [ask adults [go-home den]] will be a problem in go if energy is (as it appears) a turtle variable. This will make the context of the procedure a turtle context, not an observer context.
Edit:
For example, if energy is a turtle variable, perhaps you meant
ask adults [if (energy < 20000) [go-home den]]
First, you need to build your code much more gradually. You have multiple parts of your code that don't work and that you don't understand. Try adding the smallest amount you can and make sure that works before adding anything else. You have three different questions at the moment, with errors in different parts of your code.
On the context issue in Alan's answer, think about it this way: the variable 'energy' belongs to turtles. This means that if you have 10 turtles, you have 10 variables named 'energy', one for each turtle. Which one are you checking whether it is <20000?
What you probably want is to check it for EACH turtle individually and get the turtle to do the required action if it passes the test. So it must be inside ask turtles [], and that changes from the observer to the turtle context (what model entity is doing the thing).
to go
if ticks = day-length
[ set day day + 1
create-next-day
]
ask adults
[ search
eat
if energy < 20000
[ go-home
den
]
]
tick
end
I have also cleaned up your formatting. This is not a requirement, NetLogo is happy to deal with spaces wherever you put them. However, as your code gets longer and more complicated, it will be much easier for you to debug if you follow some basic practices (1) each call to a procedure, each command etc on a separate line (2) bracket [] and indent so that you can see the code block that the brackets enclose.

NetLogo: Combine and form a new turtle

I am currently learning NetLogo and I need help. In my model I have same sized 10 turtles which moves randomly. When 2 or more turtles are on the same patch they will combine and form a new turtle with the double size. In this manner, the main rule is max. 5 turtles can combine to each other. And this formation will continue until the there will be 2 turtles (with each contain 5 turtles) remain.
I had created turtles and made them move randomly, but I could not managed to combine them. Can you show me a way to do this? Any help appreciated. Regards.
EDIT: I tried the "in-radius" command unsuccessfully. 5-5 distribution of the turtles (as you can can see from the code, they represent H2O molecules) is vital for the system definition and any other distributions are not allowed in the model.
In detail, when randomly moving 2 H2O molecules meet on the same patch, they will combine to form a new molecule (2H2O). The main rule is as previously mentioned, max. 5 molecules can combine which ends with forming 5H2O. Since, initially there are 10H2O molecules in the system, there will be 2 5H2O molecules at the end.
The code I tried to implement is as follows,
breed [h2o-molecules h2o]
to setup
clear-all
reset-ticks
create-h2o-molecules h2o-num [
set color 105
set sIze .5
set shape "circle"
setxy random-xcor random-ycor
set pen-mode "up"
]
end
to setup-patches
ask patches [set pcolor 0]
show count turtles
end
to set-label
ask patches [
ifelse count turtles-here > 0
[set plabel count turtles-here]
[set plabel ""]
]
end
to move-h2o-molecules
ask h2o-molecules [
let dice random 1000
let change (dice - 1)
forward 2
set HEADING (HEADING + change * 2)
]
end
to go
setup-patches
move-h2o-molecules
ask turtles [rt random 1
fd 0.3]
set-label
tick
end
Thanks for your time and patience. Regards,
Using turtles-here
You don't need to ask patches for turtles-here (as you did to set patches labels). The function runs as well if called by a turtle (and is more efficient when there are more patches than turtles). But take care to use other turtles-here if you don't want to include the calling turtle.
Combine procedure
If you declare
a turtle variable after your breed declaration:
h2o-molecules-own [
turtles-inside
]
(set the variable value inside your create-h2o-molecules)
and your combination limit max-inside as a global variable (use slider widget with 5 as default value)
then the combine procedure can look like:
to combine ;; turtle procedure
; take one turtle from the same patch as a target
; which has turtles-inside low enough to combine with
let target one-of other h2o-molecules-here with
[turtles-inside <= max-inside - [turtles-inside] of myself]
if target != nobody
[
set turtles-inside turtles-inside +
[turtles-inside] of target ;; increase turtles-inside
ask target [ die ] ;; kill the target
set size sqrt turtles-inside ;; increase size
]
end
Stop
You can stop the simulation by
if not any? h2o-molecules with [turtles-inside < max-inside] [ stop ]
Comment
The condition used to select the target turtle is using turtles-here, other and the maximum constraint which is compared to the sum of turtles inside the target and turtles inside the calling turtle (using myself function).