How to set new turtles direction from average of 2 angles in netlogo - 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

Related

Move a turtle to the nearest unoccupied patch in Netlogo

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]

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

Netlogo model is extremely slow and has a delay between each tick

The model that i have created is very slow. I am a beginner in Netlogo and not sure if my code is inefficient or my system configuration is the problem.
I have a 35 x 35 grid and most of my operations are patch based i.e. each patch has to find the closest and farthest turtle as well as their distance and perform additional operations based on that. In addition at every tick based on an if-else logic where the patches that don't meet the condition turn white with a gradient using the scale-color function while those patches that meet the condition have to take the color of the closest turtle.
I can post the code but don't want to spam, please advise. Thanks.
Following piece of code seems to be problem.
to update-support
ask patches [
set closest-party min-one-of parties [distance myself]
set closest-party-dist [distance myself] of closest-party
set farthest-party max-one-of parties [distance myself]
set farthest-party-dist [distance myself] of farthest-party
set f 0
set h 0
set f ( -1 / ( [my-old-size] of closest-party / sum[my-old-size] of parties )) * (closest-party-dist ^ 2)
set h ( [my-old-size] of farthest-party / sum[my-old-size] of parties ) * (farthest-party-dist ^ 2)
set b-c (f + h)
ifelse (b-c <= threshold)
[ set votes-with-benefit 0 set pcolor scale-color white citizen-share 0 max-citizen-share ]
[ set votes-with-benefit citizens set pcolor scale-color ([color] of closest-party) citizen-share 0 max-citizen-share]
]
ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
;set my-benefit mean[b] of patches with [closest-party = myself]
set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
]
set largest-party max-one-of parties [my-size]
end
The number of parties is dynamic - it can range between 2 & 10. The update-support module is called both in the set & go. Below is that code:
to setup
clear-all
setup-citizens
setup-parties
update-support
setup-plot
reset-ticks
end
to go
ask parties [ adapt set my-old-size my-size ]
update-support
plot-voter-support
plot-voter-turnout
tick
end
Regards
Yuvaraj
First simple fix, if you are using a patchset multiple times, create it once and then call it. So:
ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
]
becomes (and easier to read, and ensures consistency if you change the condition later)
ask parties
[ let my-patches patches with [closest-party = myself]
set my-size sum [votes-with-benefit] of my-patches
set my-benefit-chen mean[b-c] of my-patches
]
In a similar vein, you have sum[my-old-size] of parties twice in the code (calculating f and h), and you actually calculate it over and over again because you ask each patch to run this code. You should calculate it once and then use it:
to update-support
let old-total sum [my-old-size] of parties
ask patches
[ ...
set f ( -1 / ( [my-old-size] of closest-party / old-total )) * (closest-party-dist ^ 2)
set h ( [my-old-size] of farthest-party / old-total ) * (farthest-party-dist ^ 2)
set b-c (f + h)
...
]
...
end
How much improvement these changes make will depend mostly on the number of parties. Please try them and see if it's solved the problem.

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

Line of sight NetLogo

How do I implement a line of sight in NetLogo whereby
A turtle calculates/counts all turtles between it and the a given patch on the line of sight.
Thanks.
Let's say that each turtle has a turtles-own called target, which is the given patch, you can ask each turtle to face the target patch and count all the turtles between it and the given patch:
ask turtles [
face target
let n 1
let c 0
while [patch-ahead (n - 1) != target][
ask patch-ahead n [
if any? turtles-here [set c (c + 1)]
]
set n (n + 1)
]
print (word who " has " c " turtles on the line of sight")
]
A while loop doesn't look very clean in NetLogo but it works.
See Line of Sight Example, in the Code Examples section of NetLogo's Models Library.
Very similar to Dr_stein
To-report count-turtles-on-line-to[target]
Let c 0
Let d distance target
Face target
While d > 0
[
Let c c + count turtles-on patch-ahead d
Let d d - 1
]
Report c
End
I would suggest using the patches-ahead reporter from this answer:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
You can use it like this:
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
ask turtles [
let target one-of patches
face target
show (word
count other turtles-on patches-ahead distance target 0.1
" turtles between me and " target)
]
end