Distance Between Turtles (car) - distance

Recently I've been working on netlogo especially at Traffic Basic.
I want to modify the code and make a function to calculate distance between every turtle (car) with the one ahead of them.
How can I do this?

Maybe a to-report will do what you want. If you add this procedure to Traffic Basic:
to-report distance-car-ahead
; If there are any cars within 10 patches of me
ifelse any? other turtles in-cone 10 1 [
; Report the distance to the nearest one
report distance ( min-one-of ( other turtles in-cone 10 1 ) [distance myself] )
] [
; Otherwise, report that I am in the lead
report "I am the lead car"
]
end
Now as an example, you can modify go to check that this is working, like so:
to go
;; if there is a car right ahead of you, match its speed then slow down
ask turtles [
let car-ahead one-of turtles-on patch-ahead 1
ifelse car-ahead != nobody
[ slow-down-car car-ahead ]
[ speed-up-car ] ;; otherwise, speed up
;; don't slow down below speed minimum or speed up beyond speed limit
if speed < speed-min [ set speed speed-min ]
if speed > speed-limit [ set speed speed-limit ]
fd speed
show distance-car-ahead
]
tick
end
I recommend turning the number of cars down to 3 or 4 to evaluate the print statements to make sure it's doing what you expect.

Related

Minimum distance and interference between turtles

I am doing boarding processes model with anti-Covid measures. I would like to know if any of you could help me.
I would like to enforce that the minimum distance between turtles will be 4 patches.
I would like to identify if there is an interference, that is, that a previous turtle occupies a seat that prevents a new agent from passing through.
I don't know how to insert this in my code:
to go
; stop when all the pessengers have found a seat
if not any? turtles with [seated? = false]
[stop]
set counter counter + 1
(foreach (sort turtles with [seated? = false] ) [a ->
ask a [
; check if we have reached the correct row of seats
ifelse [seat-row] of patch-at 1 -1 = assigned-seat-row
[
; only seat the passenger if he/she has stored the luggage OR if we don't take luggages into account
ifelse luggage-store-ticks = 0 or storing-luggage-takes-time = false
[
let seat patch-at 1 -1
set xcor [pxcor] of seat
set ycor assigned-seat-number
set seated? true
]
[
set luggage-store-ticks luggage-store-ticks - 1
]
]
[
let passenger-ahead one-of turtles-on patch-ahead 1
ifelse passenger-ahead != nobody
[
set speed [speed] of passenger-ahead
if xcor != airplane-door-x-cor
[fd speed]
]
[
set speed default-speed
fd speed
]
]
]
])
end
You could extend your existing patch-ahead 1 to also look at the patches 2, 3 and 4 ahead. But I think it would be easiest to use in-cone 5 10 or similar. That will look ahead in a cone shape, 10 degrees on either side of the heading and a distance of 5. So you could do something like:
let potential-blockers turtles in-cone 5 10
let blocker min-one-of potential-blockers [distance myself]
That should (not tested) find the closest turtle approximately in front and name it "blocker" so that you can do things like check if it's far enough away, match speed (see the basic traffic model in the NetLogo models library)

How to let my turtles move while checking for other turtles?

I want to let move my turtles forward, if there are no other turtles on patch ahead 1 with the same heading. the turtles slow down at some point until they don't move anymore and there are no turtles in front of them, but I don't know why.
Here is some code I have:
to movefd
ask turtles [
let car-ahead turtles-on patch-ahead 1
ifelse car-ahead with [heading = [heading] of myself] != nobody
[ slow-down-car ]
[ speed-up-car ]
if speed < speed-min [ set speed speed-min]
if speed > speed-limit [ set speed speed-limit ]
fd speed
]
end
to slow-down-car
set speed (speed - deceleration)
end
to speed-up-car
set speed speed + acceleration
end
I think (but am not sure as I can't test) that your problem is coming from the difference between agentsets and agents. The report turtles-on returns a turtleset, which can have any number of turtles. Even if it returns exactly one turtle, it returns that as a set of one turtle rather than as a turtle. On the other hand, nobody is a turtle, not a turtleset. A set can never be the same as a turtle.
Try this (note, I also changed 'car' to 'cars' as a reminder that it's a set):
ask turtles [
let cars-ahead turtles-on patch-ahead 1
ifelse any? cars-ahead with [heading = [heading] of myself]
[ slow-down-car ]
[ speed-up-car ]
...
]
end

NetLogo - turtle to go to the closest concentration of turtles

I would like a turtle to go to the closest patches with most turtles if a threshold of a given variable is met for 5 ticks.
My code is:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
ask turtle [
let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
ask turtle [ ;; once there do the following
set shape "star"
set color red
]
]
]
]
end
The issue I have is that a) I am unsure how to say the patch with >= 3 turtles closest to you at the given range of 3 (attempted code above) and b) how to say once there, change your shape.
Revised to keep a permanent variable to track whether the variable is high enough 5 times in a row.
turtles-own
[ count-tick
]
; wherever you create the turtles, you need to `set count-tick 5`
to move
ifelse var >= 9.5
[ set count-tick count-tick - 1 ]
[ set count-tick 5 ]
if count-tick = 0
[ let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
set shape "star"
set color red
]
end
First, you are already within an ask turtles code block from the procedure calling this move procedure. So you don't need the additional ask turtles. Look up ask in the NetLogo Dictionary, it iterates through the turtles, running all the code for each turtle in turn.
Second, you need count turtles rather than sum turtles as sum is to add up values.
Note that there is no error checking in this, you may have problems if there are no patches within radius of 3 that have at least 3 turtles.

Calculating turtle mortality based on distance from origin in netlogo

I am writing a procedure (Pass-Away-Space) to calculate mortality of turtles moving from the origin (start-patch) through out the world. Each turtle calculates its own mortality based on its distance from the origin (start-patch). The code I am attempting to implement for this procedure is as follows:
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles)
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
The error I am getting is expected input to be a number but got the list. I am not sure what the issue is and I was wondering if anyone could point out and rectify the problem with the code.
The problem here is your of turtles. Since an ask procedure affects one turtle at a time, each turtle in your procedure above is evaluating the [distance start-patch] of all turtles instead of just its own distance to the start patch. To clarify, check out the following setup:
globals [ start-patch ]
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
]
set start-patch patch 0 0
end
to incorrect-example
ask turtles [
print ([ distance start-patch ] of turtles)
]
end
to correct-example
ask turtles [
print distance start-patch
]
end
Compare the print output of the incorrect-example and the correct-example procedures, and you'll see that when you use [distance start-patch] of turtles you get the list of distances of all turtles. When you ask turtles to evaluate a turtles-own variable (including color, size, etc) each turtle will automatically access its own version of that variable- there's no need to specify which turtle. So, your pass-away-space might look something more like below (untested):
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * (distance start-patch) )
if chances >= 1 [
die
set dead-count dead-count + 1
]
]
end

Streamline agent behavior in NetLogo

I'm trying to model the avoidance of animal agents from human agents in NetLogo. First, I asked a single predator to avoid people using two behaviors, "wary" and "scared". This worked nicely. But then I asked the prey animals (168 individuals right now but potentially many more) to do the same and the model has slowed down to a snail's pace. As I'm pretty new to NetLogo I'm sure that there is a more efficient way to code this behavior. Any suggestions on how to streamline this process? I'm sure there is a better way to do it. Thanks!
to avoid-people ;; test if people too close to predator and prey and animals moves away if is.
ask predator [
ifelse ticks mod 24 >= 5 and ticks mod 24 < 18 [ ;makes sure the animals respond to people during the daytime
humans-near
ifelse any? wary
[ fd 0 ]
[ ]
humans-too-near
if any? scared
[run-away]
] [set wary 0 set scared 0]]
ask preys [
ifelse ticks mod 24 >= 5 and ticks mod 24 < 18 [
humans-near
ifelse any? wary
[ fd 0 ]
[ ]
humans-too-near
if any? scared
[run-away]
] [set wary 0 set scared 0]]
end
;;Humans-near and humans-too-near are functions
;;Alert-distance and flight initiation distance are sliders for the predator but are set values for prey
to humans-near ;;adds all humans in alert-distance radius of animal to an agent subset for that agent.
ask predator [
set wary humans in-radius alert-distance]
ask preys [
set wary humans in-radius 10]
end
to humans-too-near ;;adds all humans in flight-initiation-distance radius of animal to an agent subset for that agent.
ask predator [
set scared humans in-radius flight-initiation-distance]
ask preys [
set scared humans in-radius 5]
end
to run-away ;;Make animal avoid the human closest to it.
set nearest-human min-one-of scared [distance myself]
turn-away ([heading] of nearest-human) max-separate-turn
end
;;this keeps the animals inside the tropical forest and away from human settlement.
;;Max-separate-turn is a slider dictating the angle that the predator runs away from the human
to turn-away [new-heading max-turn]
turn-at-most (subtract-headings heading new-heading) max-turn
ifelse [habitat = typeTrop] of patch-ahead run-distance
[fd run-distance] [turn-away ([heading] of nearest-human) max-separate-turn]
end
to turn-at-most [turn max-turn]
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
I did not understand your code, but this is one way to do what you want, I am not sure how agents should behave if they are scared or they move with wary, but you can change these easily :
Breed [predators predator]
Breed [Humans Human]
Breed [Preys Prey]
turtles-own [
wary
scared
]
to setup
Clear-all
Create-humans 5 [Set color orange set shape "person" move-to patch random 30 random 30]
Create-Preys 5[Set color white Set shape "Sheep" move-to patch random 30 random 30]
Create-predators 5 [set color red Set shape "wolf" move-to patch random 30 random 30]
ask turtles
[set Wary false
Set Scared False
]
reset-ticks
end
to go
ask turtles
[rt random 5
fd 0.3]
avoid-people
tick
end
to avoid-people
ifelse is-day?
[
ask predators
[ if humans-near?
[
set wary true
if humans-too-near? [Set Scared true]
set label (word wary "," Scared )
]
]
Ask Preys
[ if humans-near?
[
set wary true
if humans-too-near? [Set Scared true]
set label (word wary "," Scared )
]
]
]
[; what they should do when its night time
]
end
to-report humans-too-near?
report any? humans in-radius 2
end
to-report humans-near?
report any? humans in-radius 5
end
to-report is-day?
report (ticks mod 24 >= 5 and ticks mod 24 < 18)
end
*Update:
Your problem was in having 2 ask inside each other , I am glad your model now runs faster.