How to plot distributions of two different breeds - netlogo

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.

Related

linear combination of colors in NetLogo

I'm trying to help a student with a NetLogo homework. The gist of the homework is to load images, save the patch values to properties c1 c2 for each patch, set an alpha value 0.0 <= alpha <=1.0 and then set the patches to a linear combination of c1 and c2 under alpha.
This ought to be simple: all I want to do is code (this is pseudocode, I know this doesn't actually work) like:
to combine
ask patches [ set pcolor (c1 * alpha) + (c2 * alpha) ]
end
the problem being that c1,c2 are vectors, alpha is a scalar, and I can't figure out how to do vector multiply and vector addition in NetLogo.
Here's the code to set up a test case:
patches-own [ c1 c2]
to setup
ask patches [ set pcolor one-of [ red green blue] ]
end
to load-image
import-pcolors image
end
to set-one
ask patches [ set c1 pcolor ]
end
to set-two
ask patches [ set c2 pcolor ]
end
to return-c1
ask patches [ set pcolor c1 ]
end
I don't know what your various vectors (or lists really in NetLogo) and other numbers look like. But if the heart of the problem is that you want to multiply a 'vector' and scalar, here is a function that does so:
to-report mult-vec-num [ #vec #num ]
report map [ ii -> ii * #num] #vec
end
The map primitive is essentially an implicit foreach, with the arbitrarily named ii being used as an iterator through the provided list (named #vec) and multiplied by the provided scalar (named #num). Example use case:
show mult-vec-num [ 1 2 3 ] 5
UPDATE:
To add two lists together, again use the map primitive:
let l1 [1 2 3]
let l2 [4 5 6]
show (map + l1 l2)

Netlogo: sum within a neighborood

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.

How do you generate a histogram in NetLogo?

I have a breed called players. Each player-owns a player-strategy. There are 6 possible player-strategies. When I write
histogram [player-strategy] of players
nothing appears on my plot.
I have one plot with one pen -- and don't set either the plot or penname. plot-name returns the plot. pen-mode is bar. interval is 1.0
I don't know how to ask for the current penname. But since the plot has only one pen it shouldn't matter.
The histogram command is in both plot-update and pen-update. I've tried it in both together or one at a time. I also tried in code. Makes no difference.
What do I have to do to get a histogram to appear?
OK, here is what I think you are after.
If you create the following plot:
And write the following code:
extensions [table]
breed [players player]
players-own [player-strategy]
to setup
clear-all
create-players 100 [ set player-strategy one-of ["a" "b" "c" "d" "e"] ]
reset-ticks
end
to update-strategy-plot
set-current-plot "Player strategies"
clear-plot
let counts table:counts [ player-strategy ] of players
let strategies sort table:keys counts
let n length strategies
set-plot-x-range 0 n
let step 0.05 ; tweak this to leave no gaps
(foreach strategies range n [ [s i] ->
let y table:get counts s
let c hsb (i * 360 / n) 50 75
create-temporary-plot-pen s
set-plot-pen-mode 1 ; bar mode
set-plot-pen-color c
foreach (range 0 y step) [ _y -> plotxy i _y ]
set-plot-pen-color black
plotxy i y
set-plot-pen-color c ; to get the right color in the legend
])
end
You should get the following plot:
I believe this should answer your previous question as well.
(Note that I've put the plotting code in a code tab procedure, but you could just as well stick it in the "Plot update commands" field.)
I know it is a very old question, but in case anyone lands here on a search. The current version of NetLogo comes with example code for histogram usage: look in the file menu under 'Models Library'; see 'Code Examples'/'Histogram Example'.

Herfindahl index code in Netlogo

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

Q: How to calculate 'hub integration' / how many of your link-neighbors are also my link-neighbors?

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.