I need some help. My issue is the following
I want to solve the following formula
sum (Zi - Zj)^2 where Zi is a constant for an individual i and Zj is the value for a generic individual j that is within a neighborood with radius = 1 of the individual i.
Therefore, I want a sum of the square of the distance between a constant value and the value of Z for each individual within that radius.
Sorry for the absence of my code, but I have no idea about how to approach this issue
I will write an example
Zi = 1
The neighborhood of the individual i is composed of 2 agents, let say a and b where Za = 3 and Zb = 5
I want the following result
(1-3)^2 + (1-5)^2 = 20
Thanks
I think you want to do the sum of squared differences between a constant and a list of numbers, where the list of numbers is the value of Z for several turtles. If this is correct, then the following is a complete model that does what you want.
turtles-own [ varZ ]
to setup
clear-all
create-turtles 40
[ setxy random-xcor random-ycor
set varZ random 10
set color blue
]
testme
end
to testme
ask one-of turtles
[ set color red
let friends other turtles in-radius 4
ask friends [ set color yellow ]
type "my varZ is: " print varZ
type "sum of squared differences is: " print sum-sq-diff varZ [varZ] of friends
]
end
to-report sum-sq-diff [#constant #listvals]
report reduce + (map [ thisval -> (thisval - #constant) ^ 2 ] #listvals)
end
The procedure sum-sq-diff takes two inputs: a constant and a list of values. It calculates the squared sum of differences between the constant and each value in the list. The map does the square of differences and creates a list of those values, then the reduce sums across the list. You can test is by simply typing sum-sq-diff 1 [ 2 3 4 ] in the command centre and you will get back 14 (which is (2-1)^2 + (3-1)^2 + (4-1)^2).
The rest of the code is an example of how to use this procedure in the context I think you want, pulling out the turtles within some radius and using their variable values as the list.
Related
I setup two different breeds with the breed command:
breed [breeds1 breed1]
breed [breeds2 breed2]
and in go, I ask to a random turtle to execute an action command, like this:
to go
ask one-of turtles [
action
]
end
where action is defined as
to action
ifelse (breed = breeds1)
[
set q random-float 1
set c q
set potential_1 (1 + d) * (1 - c)^(d)
]
[
set c random-float 1
set potential_2 (1 + (1 / d))*(1 - c)^(1 / d)
]
end
For breeds1, q has value in [0,1] and c takes its value.
For breeds2, q has value equal to 0 and c takes random values in [0,1].
Both breeds have d=3 (fixed value).
c,q, and both potentials are global variables.
What I need to do is to plot the two potentials/distributions.
I used plotxy to plot the distributions in the plot code box:
[![enter image description here][1]][1]
What I would like are the following two distributions in the same plot. Plot 1 shows the distribution for breeds1, plot 2 for breeds2.
If I consider potential_1 and potential_2 as turtles-own (the first one for breeds1, the second one for breeds2) I receive the message that I can't use potential_1 in an observer context, because potential_1 s turtle-only. Same for potential_2.
If I consider q and c as turtles-own as following:
breeds1-own
[ q
c
potential_1
]
breeds2-own
[
q
c
potential_2
]
I receive the following error:
You can't use c in an observer context, because c is turtle-only
So my question is: how could I plot the two distributions?
I hope you can help me.
Okay, I can't answer your question using your code as I can't work out the logic of the ask one-of. So, what I have done instead is created a complete model that does the plotting in the hope that will help you work out what's wrong with your code.
Try this:
globals [d]
turtles-own
[ potential
group
c
]
to setup
clear-all
set d 3
create-turtles 100
[ set group one-of ["type1" "type2"]
action
]
reset-ticks
end
to action
ifelse (group = "type1")
[ let q random-float 1
set c q
set potential (1 + d) * (1 - c)^(d)
]
[ set c random-float 1
set potential (1 + (1 / d))*(1 - c)^(1 / d)
]
end
Then you will need the following as the pens in your plot. You will also need to change the plot settings for each pen to 'point' rather than 'line'.
ask turtles with [group = "type1"] [plotxy c potential]
ask turtles with [group = "type2"] [plotxy c potential]
What I have done is create 100 turtles in two groups, with the values of the variables 'c' and 'potential' calculated for each group using your code. But it's all done in a single pass - each turtle does its calculation and then control moves to the next turtle as they are created. Then I have each pen in the plot just plot the values from one group.
The error message you were getting "You can't use c in an observer context, because c is turtle-only" means that you tried to use the variable c without letting NetLogo know which turtle's value of 'c' you wanted to use.
I would like to plot the sum of all turtles holding values in a range from 2 to 4. How can I achieve that?
I only get the sum of turtles holding a value of 4 with plot count turtles with [value = 4], however, I would need something like plot count turtles with [2 <= value => 4].
How can I achieve this?
My MWE is:
breed [ turtles ]
turtles-own [ value ]
to setup
clear-all
create-turtles 100
[
set value random 4
]
reset-ticks
end
to go
ask turtles [
rt random 360
fd 1
]
tick
end
NetLogo doesn't support the 2 <= value => 4 syntax. You need to write both conditions separately:
plot count turtles with [ value >= 2 and value <= 4 ]
By the way, if you wanted to plot the sum of the values instead of the count of turtles with values in that interval, you could write:
plot sum [ value ] of turtles with [ value >= 2 and value <= 4 ]
I have a very simple model of 50 turtles moving away from a central point. I would like to be able to extract the spatial coordinates (xcor, ycor) of a subset of them every nth tick in behaviour space. Hope you can help!
The modulo operator mod is probably the simplest way to do this. It outputs the remainder from a division operation, so you can just use a logical flag such that the coordinates are only extracted when ticks divided by n is equal to 0. For example:
to setup
ca
crt 10
reset-ticks
end
to go
; set up lists for example output
let tlist []
let xlist []
let ylist []
ask turtles [
rt random 60 - 30
fd 1
]
tick
; If ticks is not zero, and the remainder of
; the number of ticks / 3 is zero, extract
; some info about the turtles and print it.
if ticks > 0 and ticks mod 3 = 0 [
ask turtles with [ xcor > 0 ] [
set tlist lput self tlist
set xlist lput xcor xlist
set ylist lput ycor ylist
]
print tlist
print xlist
print ylist
]
end
Run this several times and you'll see that on tick 3 (and 6, 9, 12, etc), the lists are printed out. Note that where you have your tick increment will affect when this output is actually extracted- in the example above, tick happens at the end of the go procedure but before the if statement is evaluated.
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.