Move a turtle to the nearest unoccupied patch in Netlogo - distance

Currently, I have a model with many parameters in it, and one of them is having male yearling deer disperse according to certain criteria. The distance each male yearling disperses is pulled from a log-normal distribution. Here is what I have so far:
to move-dispersing-maleyearlings
ask maleyearlings [
let chance-disperse random-float 1.001
if chance-disperse < .62 [ ;;average dispersal rates in Long et. al paper
let mu 7.5
let sigma 6.1
let beta ln (1 + (sigma * sigma) / (mu * mu))
let S (sqrt beta)
let M (ln mu) - (beta / 2)
let new-distance exp (random-normal M S)
while [any? other turtles-here and dispersal-distance < new-distance]
[right random 360
fd 1
set dispersal-distance dispersal-distance + 1]]]
end
So this code should have 62% of the male yearling deer disperse, and they'll disperse a distance of "new-distance". If I am understanding my while loop correctly, they will move until they reach their "new-distance" and until they land on an unoccupied patch.
But now instead what I want to do is have each male yearling deer disperse their respective "new-distance", but if they land on a patch that is occupied, I want them to then move to the nearest unoccupied patch. If the patch they land on after moving "new-distance" is unoccupied, then they would stay on that patch.
Any ideas of how to do this? Thanks for your help!

If I am understanding your request correctly, you want to replace:
while [any? other turtles-here and dispersal-distance < new-distance]
[right random 360
fd 1
set dispersal-distance dispersal-distance + 1]]]
with code that moves to the closest empty patch. Try something like this (not tested):
if any? other turtles-here
[ move-to min-one-of (patches with [not any? turtles-here]) [distance myself]

Related

face to minimum heading difference

How can I face or rotate or set the heading of an agent (turtle for example) to that an element of a patch-set which needs minimum rotation.
So the agent has a initial heading, and we have a patch-set (for example 5 patches in a cone), and I would like to face the agent that one which is at minimum in angle difference. I don't want to use patch-ahead because it can be one patch backward if that is the only one.
I tried some combinations with these commands:
min-one-of towards myself self heading subtract-headings towardsxy face - 180
Thank you in advance.
Are you having trouble getting the heading differences with subtract headings? Then you can try this:
to-report abs-hdiff [#t #p]
let _current [heading] of #t
let _new [towards #p] of #t
report abs (subtract-headings _current _new)
end
For example:
to test
ca
ask n-of 5 patches [set pcolor red]
let _patches (patches with [pcolor = red])
crt 1
ask turtle 0 [
hatch 1 [pen-down fd 10 die] ;just to see old heading
face min-one-of _patches [abs-hdiff myself self]
]
end

Keeping agents in visual range while moving

I have a model whereby the agents are moving around looking for resources. The individuals move according to:
to move
set energy energy -1
fd v
if random 300 = 1 [
ifelse random 2 = 0
[rt 45][lt 45]]
end
What I would like them to do in addition to this is keep in visual range of each other or at least keep a certain group size in range.
In my head this would take the form of ifelse count turtles in-radius vision < 5 [move-towards-group][move]
I've tried a few things but the agents end up moving towards each other or losing contact while they enact some other routine causing an error.
I've looked at the flocking model as well but that's a bit too over parameterised for what I'm trying to achieve.
Hope you can help.
EDIT: Based on #JenB's advice I've come up with the following working model. But the agents here tend to swarm and can, on occasion, break off where they get stuck around a group < 5
to setup
clear-all
reset-ticks
crt 20
end
to go
ask turtles [move
group]
tick
end
to move
fd 0.01
if random 333 = 1 [
ifelse random 2 = 0
[rt 45][lt 45]]
end
to group
let midx mean [xcor] of other turtles in-radius 10
let midy mean [ycor] of other turtles in-radius 10
ifelse count other turtles in-radius 5 < 5 [facexy midx midy set color red][move set color blue]
end

NetLogo: How to pull coordinates from neighboring patches based on patch-variable

I have limited programming experience (mechanical engineering student, so a bit of matlab and labview experience) and am very new to NetLogo, so I apologize in advance if this question is pretty basic or my code is of poor quality.
I need to have my turtles move to 1 of 2 possible neighboring patches based on a given probability function. The two patches that I need to input to the probability function are the two neighboring patches with the lowest nest-scent value. I have been able to pull the two lowest nest-scent values, but I cannot figure out how to actually figure out which patches those are, and how to put those coordinates into an ifelse statement to move the turtle to one of them based on the aformentioned probability function. I have the following code that is obviously not working:
to move
set farthest-patch sort-by < [nest-scent] of neighbors
let a1x pxcor of item 0 farthest-patch
let a1y pycor of item 0 farthest-patch
let a2x pxcor of item 1 farthest-patch
let a2y pycor of item 1 farthest-patch
let a1 item 0 farthest-patch
let a2 item 1 farthest-patch
let x (((a1 + a2) / 100 ) - 1)
let probability-move 0.5 * (1 + ((exp(x) - exp( - x)) / (exp(x) + exp( - x))))
ifelse random-float 1 < probability-move
[set to-move 1]
[set to-move 0]
let a1-probability (a1 / (a1 + a2))
ifelse random-float 1 < a1-probability
[set destination [a1x a1y]]
[set destination [a2x a2y]]
ifelse count turtles-here >= 20
[set full 1]
[set full 0]
if [a1x a21] = full
[set destination [a2x a2y]]
if [a2x a2y] = full
[set destination [a1x a1y]]
if [a2x a2y] and [a1x a1y] = full
[set to-move 0]
ifelse to-move = 1
[move-to destination]
[stop]
end
Basically what I have (tried) to do here is sort a farthest-patches list by increasing nest-scent, and I have pulled the two lowest nest-scent values in order to input those values into my probability functions (both for whether or not to move, and if they are to move which of the two patches to select). I am not sure how to properly pull the patch coordinates of the patches that the a1 and a2 values were taken from.
Thanks for any help,
Brad
okay, you are making life way more complicated than it needs to be. You can select the two patches (or turtles) with the smallest values of a variable with min-n-of. Look it up in the dictionary to get the details.
Having found the two candidates, the best option is to use the rnd extension for choosing the destination because it has a primitive for random selection by weight. Finally, since you are using a function of your variable as the weight (rather than the variable value itself), you need a way to construct that weight. The best option is to separate it out - you could also have a second variable with the weight value, but that just proliferates variables.
Here is a complete working model. Please copy the whole thing into a new instance of NetLogo and try and understand how it works, rather than just copy the relevant bits into your code because min-n-of, using agentsets and passing variables to procedures are important aspects of NetLogo that you need to know about. I have also set up colouring etc so you can see the choices it makes.
extensions [rnd]
patches-own [ nest-scent ]
to setup
clear-all
create-turtles 1 [ set color red ]
ask patches
[ set nest-scent random 100
set plabel nest-scent
]
reset-ticks
end
to go
ask one-of turtles [ move ]
tick
end
to move
set pcolor blue
let targets min-n-of 2 neighbors [ nest-scent ]
let destination rnd:weighted-one-of targets [ calc-weight nest-scent ]
move-to destination
end
to-report calc-weight [ XX ]
let weight 0.5 * (1 + ((exp(XX) - exp( - XX)) / (exp(XX) + exp( - XX))))
report weight
end

Find the presence of other turtles in a given direction upto a distance

I wish to find out whether in a given turtle's heading there is another agent present upto a given distance.
Here the Distance is "D".
Note:
Any agent present before D in the given direction should be also considered.
Even the direction doesn't coincide with the other's agent centre but just touches it ,even then that agent should be considered.
Problem:
No turtle-ahead procedure available. Combination of patch-ahead and turtles-on not applicable due to patch-size>> turtle-size.
Possible approach:
1.Represent the turtle's heading by the equation of a line.
to-report calculate-line[x y angle]
let m tan angle
let A m
let B -1
let C (- m * x + y)
report (list A B C)
end
to-report heading-to-angle [ h ]
report (90 - h) mod 360
end
let line-equ calculate-line (xcor) (ycor) (heading-to-angle heading)
2.Calculate the perpendicular distance from other turtles here, Check if there are within a range that the size of other turtles.
to-report value[A X1 Y1];;A is list of coefficents of line, x1 and y1 are coordinates of red turtle
if-else(abs((item 0 A * X1 + item 1 A * Y1 + item 2 A) / (sqrt((item 0 A ^ 2) + (item 1 A ^ 2) ))) < [size] of wall )
[ report "true"][report "false"]
end
3.To check if the red turtle is within D. One could obtain a line perpendicular to black one and compute the red turtle distance from it to check if it is less than or equal to D. But then that adds more complication.(Though one can simplify rotate the turtle by 90 left or right and get the line equation.)
This is what I meant by my comment. Run this code (as its own model). What it does is turn all the turtles on a few 'ahead' patches a different colour. I know this is not what you are trying to do, but the agentset candidates is a relatively small number of turtles. These are the only ones that you have to check whether they are on the correct path. So instead of turning them a different colour you could check the direction that they are from your initial turtle.
to setup
clear-all
set-patch-size 25
resize-world -10 10 -10 10
create-turtles 1000
[ setxy random-xcor random-ycor
set color yellow
set size 0.4
]
ask one-of turtles
[ set color red
set size 1
check-from-me 5
]
end
to check-from-me [howfar]
let counter 0
let candidates turtles-here
while [counter < howfar]
[ set counter counter + 1
set candidates (turtle-set candidates turtles-on patch-ahead counter)
]
ask candidates [set color red]
end
to-report check-wall
let return false
hatch 1[
set color black
set size ([size] of one-of walls) / 2
show (2.5 * ([size] of myself))
while [distance myself < (2.5 * ([size] of myself))]
[
fd ([size] of one-of walls) / 64
if any? walls in-radius size
[
set return true
]
show distance myself
]
]
report return
end
The above works. But still is approx. I am looking for better solution with probably less maths as one elucidated in the question.

How to set new turtles direction from average of 2 angles in netlogo

I need help on how write this formula using Netlogo. (Refer to figure per attached)
I'm trying set new heading for the agent by calculate the average of 2 angles between leader's heading and agent's heading. New heading for the agent should be change to 27. Below is the code that i use, but it does not work the way I want:
to change-heading
let nearby-leaders turtles with [leader? ]
if any? nearby-leaders in-radius vision
[turn-towards average-heading-towards-leaders max-cohere-turn ]
end
to-report average-heading-towards-leaders
let nearby-leaders turtles with [leader? ]
report average-heading ([heading] of one-of nearby-leaders ) heading
end
to-report average-heading [a b]
report (a - (subtract-headings a b) / 2) mod 360
end
Really need advice on this, thanks for the help.
It sidesteps the trig but this works
to-report average-heading[a b]
Let h (a - (subtract-headings a b) / 2) mod 360
Let s (a + b) / 2
Ifelse h < s [report h][report s]
]
end
In your code
to-report average-heading-towards-leaders
report average-heading ([heading] of one-of nearby-leaders turtles with [leader? ] ) heading
end
Mark II