Netlogo: Return the Minimum of calculated values - netlogo

I am getting an "expected a constant" error when I try to return the minimum value between two calculated values.
In my model, patches own ag, glc, and pH. According to the values of these, each patch should calculate the NP-ag-glc-effect and NP-ag-pH-effect, and use which ever of these two calculated value is smaller for further calculation.
In the code below, Netlogo is giving an error at the min [NP-ag-glc-effect NP-ag-pH-effect]. Any helping in solving this problem is greatly appreciated.
Thank you,
J.J.
to calc-ag
let NP-ag-baseline random-normal .85 .17
set ag (ag + (NP-ag-baseline * (min [NP-ag-glc-effect NP-ag-pH-effect]))
end
to-report NP-ag-glc-effect
ifelse glc < .5 [ report .5 ]
[ report ((.11 * glc) + .44) ]
end
to-report NP-ag-pH-effect
ifelse pH > 6.6 [report (1.52 * pH) - 9.9]
[report ((.28 * pH) - 1.71)]
end

min expects a list to choose from:
set ag (ag + (NP-ag-baseline * (min (list NP-ag-glc-effect NP-ag-pH-effect))))
(also note a missing closing parenthesis in your code snippet)

Related

Basic question in NetLogo (create an equation)

I have a very basic question on NetLogo. However, I don't know how to solve it. I would like to run an equation and have the value of that equation used by all turtles. I made the following code:
to equation
ask turtles [
set 10 ^ ( - 0.1 + 2.0 * log Size 10 ) * 1000
]
end
Can someone help me?
Thanks
you need to assign the answer to the equation to some variable. For example (not tested):
turtles-own [the-answer]
to equation
ask turtles [
set the-answer 10 ^ ( - 0.1 + 2.0 * log Size 10 ) * 1000
]
end
The turtles-own statement sets up an attribute for each turtle so that each turtle has its own value of that attribute (and therefore different turtles can have different values because their size varies)
If the turtles all need the same value, the calculation could be done just once in order to improve runtime. Additionally consider saving that value as a global variable.
globals [the-answer]
to equation
set the-answer 10 ^ ( - 0.1 + 2.0 * log Size 10 ) * 1000
end

How to Correct Error: Can't Find Mean of List with no numbers

I'm trying to get turtles (foragers) to move based on a reporter of mean nest-scent, which should report the mean of nest-scent spread throughout the world, from a high of 200 at the nest to 120 at the opposite corners of the world. However, at the nest, the error below is triggered, and I don't understand why. Isn't a mean nest-scent a list of numbers? How do I correct this error?
Can't find the mean of a list with no numbers: [].
error while forager 7 running MEAN
called by procedure MEAN-NEST-SCENT-IN-CONE
to-report mean-nest-scent-in-cone [cone-distance angle angle-width ] ; ant procedure - reports the mean amount of nest-scent in cone
rt angle
let p patches in-cone cone-distance angle-width
;ask p [ show chemical ]
if p = nobody [report 0]
lt angle
report (mean [nest-scent] of p)
end
`````````````````````````````````
You set up 'p' as a patchset but you are testing whether it is 'nobody'. The primitive for agentsets is any?. Try replacing:
if p = nobody [report 0]
lt angle
report (mean [nest-scent] of p)
with
ifelse any? p
[ lt angle
report mean [nest-scent] of p
]
[ report 0 ]

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.

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.