Get mean heading of neighboring turtles
to-report mean-heading [ headings ]
let mean-x mean map sin headings
let mean-y mean map cos headings
report atan mean-x mean-y
end
from that answer gets me part way there but I want is each of the headings to be weighted on size of the turtles.
something like
sum [heading * size] of turtles / sum [size] of turtles
But that would, you know, actually work.
If you accept a turtleset instead of a list of headings, then:
to-report weighted-mean-heading [turts]
let mean-x mean [size * sin heading] of turts
let mean-y mean [size * cos heading] of turts
report atan mean-x mean-y
end
Related
I have sorted all the turtles by the value of a property called point.
now I want a plot of point versus turtle number. how do I do this?
turtles-own [ point ]
to setup
ca
crt 100
reset-ticks
end
to go
repeat 100[
ask turtles[
if random 10 = 1[
set point point + 1
]
]
];;sorting
let array sort-on [point] turtles
tick
end
By "turtle number", I assume you mean the location in the list. Then replace let array sort-on [point] turtles with plotByOrder where
to plotByOrder
clear-plot
let pts sort [point] of turtles
foreach pts [[pt] -> plot pt]
end
Of course, you will need to have created a plot in the interface, and this assumes it is the current plot.
I have trouble with programming the Herfindahl index in Netlogo. I want Netlogo to calculate it. I have trouble telling the program the following formula:
https://en.wikipedia.org/wiki/Herfindahl_index#Formula
I want Netlogo to report it and plot it. Help please :-s.
Here is a complete working example. For your purposes, you just need the calc-HI procedure (and remember to call it in your go procedure) but I have given you a separate model so you can test it yourself. The way to test it is to run setup, then run go then show HI in the command centre. By inspecting the individual turtles, you can see their variable values and use a calculator to check the match for HI value.
globals [ HI ]
turtles-own [ val ]
to setup
clear-all
create-turtles 3
[ setxy random-xcor random-ycor
set val random 5
]
reset-ticks
end
to go
set HI calc-HI
tick
end
to-report calc-HI
let num count turtles
let total sum [ val ] of turtles
let herf sum [ ( val / total ) ^ 2 ] of turtles
report herf
end
For plotting, all you need to do is plot HI in the plot widget.
Assuming you have a list of market-shares using percentages. Map each market-share to the square and sum them up:
;;e.g. let market-shares (list .5 .5)
to-report calculate-herfindahl [market-shares]
report (sum (map [? * ?] market-shares))
end
If you want to calculate the shares based on actual shares. Map each share to a percentage, then apply the above formula:
;;e.g. let market-shares (list 30 40)
to-report calculate-herfindahl [market-shares]
let market-size sum market-shares
report (sum (map [(? / market-size) ^ 2] market-shares))
end
In the model I am building I need to make turtles calculate the 'hub integration' of their link-neighbors. By 'hub integration'(HI) I mean the following: HI = number-of-shared-neighbors/n-of-your-neighbors.
HI is a value that the 'turtle x' assigns to every other turtle that shares a link with her (we will call every linked turtles as 'turtle y'). The value of HI is thus the fraction of the number of nodes that are linked to both turtles x and y, with the number of nodes linked to turtle y.
I am using as references the Netlogo dictionary and the book 'An Introduction to Agent-Based Modeling' from Wilensky and Rand. Still, without the help of this community it would be really hard for me, if not impossible, to learn more advanced procedures.
EDIT 3 ---
I am greatly thankful for all the help received. I finally have a running procedure.
For the record, my final code of the 'hub integration procedure' is the following:
to find-hi
ask turtles [
foreach sort link-neighbors [
ask ? [
if count [my-links] of self > 1 and count [my-links] of myself > 1 [
let hi ( calc-HI self myself )
run-procedure ] ] ] ]
end
to-report calc-HI [ XX YY ]
let sizeX count [my-links] of XX
let sizeY count [my-links] of YY
let sizeXY count (turtle-set [link-neighbors] of XX [link-neighbors] of YY)
report (sizeX + sizeY - sizeXY) / sizeY
end
I think you want to count the number of neighbours, not list them all out.
One approach to count the number in common is to count the neighbours of X, count the neighbours of Y and count the agents who are either neighbours of X or Y. A turtle who is a neighbour of both X and Y will still only appear once in the agentset constructed, so the size of the intersection is the sum of the individual counts then subtract the size of the union.
This code expects you to nominate two turtles and reports the HI of turtle YY from the perspective of turtle XX (note that there is no checking that the two turtles have a link between them). I am not sure I have understood the calculation that you want, but you can amend as required.
to-report calc-HI [ XX YY ]
let sizeX count [my-links] of XX
let sizeY count [my-links] of YY
let sizeXY count (turtle-set [link-neighbors] of XX [link-neighbors] of YY)
report (sizeX + sizeY - sizeXY) / sizeY
end
You're using print in your reporter, try using report instead.
E.G.:
to-report who-of-neighbors
report [who] of link-neighbors
end
More info on to-report right here.
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.
I set/update each turtle's position as follows:
set xcor xcor + item 0 vector
set ycor ycor + item 0 vector
Therefore I add a vector to the current agent's coordinates.
PROBLEM:
I wish to rotate the added vector by angle x. Thus the vector "vector" should be rotated by angle x.
The angle should be taken from a Gaussian distribution with a specified deviation.
I am trying to achieve something similar to Couzin's model.
http://www.csim.scu.edu.tw/~chiang/course/ComputerGameAdvance/Collective%20Memory%20and%20Spatial%20Sorting%20in%20Animal%20Groups.pdf
Thanks in advance!
You seem to have two questions here; I'll address the one you used for the title. The matrix extension allows matrix multiplication, so you could just create a standard rotation matrix once you have the ange of rotation. But standard advice in NetLogo would be to use a more turtle-centric approach. Then you need to decide whether to use the NetLogo heading conventions (0 degrees for north, 90 degrees for east, etc.) If so you could do something like this:
to move [#dx #dy]
let %dist 0
ask patch 0 0 [set %dist distancexy #dx #dy]
facexy (xcor + #dx) (ycor + dy)
let %theta random-rotation
rt %theta
jump %dist
end
to-report random-rotation
report (random-float 360) - 180
end
Here the random rotation is not Gaussian distributed because I was not sure what you meant. Perhaps a von Mises distribution? In any case, you should clarify and ask as a separate question.
Just to emphasize Alan's point: Unless you have a good reason to use vectors, it's usually much easier and clearer to avoid them in NetLogo. If all you want to do is turn the turtle by a random amount drawn from a Gaussian distribution, you can just do:
right-turn random-normal 0 <std-dev>
where <std-dev> is your desired standard deviation. Then, you can tell the turtle to go forward by what would have been the magnitude of the vector: forward <distance>.
If you absolutely need to do a vector rotation, you can do so without the matrix extension fairly easily:
to-report rotate-vector [ vec angle ]
let x first vec
let y last vec
let mag sqrt (x * x + y * y)
let old-angle atan x y
let new-angle angle + old-angle
report (list (mag * sin new-angle) (mag * cos new-angle))
end
Remember that angles in NetLogo are flipped around 45º so that 0º is north and 90º is east; thus, sin and cos are flipped when dealing with angles.
Somewhat simple, convert vector to angle, rotate (randomize), then convert back. For good coding style and such, break into modules.
to-report rotate [ #vector #angle ]
let $dx first #vector
let $dy last #vector
let $magnitude sqrt ($dx * $dx + $dy * $dy)
set #angle #angle + atan $dx $dy
report (list $magnitude * sin #angle $magnitude * cos #angle)
end
to-report nudge-vector [ #vector #std-dev ]
report rotate #vector random-normal 0 #std-dev
end
to move-inaccurately [ #vector #std-deviation ]
set #vector nudge-vector #vector #std-deviation
setxy (xcor + first #vector) (ycor + last #vector)
end