Basic question in NetLogo (create an equation) - netlogo

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

Related

How to plot distributions of two different breeds

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.

How can use NetLogo to simulate the outcome of a function

assuming I got a function like Y= 0.5*a+0.23*b+0.52*c+0.3
a is a continuous variable, b and c are categorical variables(like 1 or 0)
I wanna create multiple agents that can hatch the number of Y (round up to an integer) with different b and c
Honestly, I am new to Netlogo and I am not very familiar with coding.
I went through the three tutorials of the user manual, but I still have no clue to do that.
three tutorials of the user manual.
Thank you
This does what you said. But I don't think it's actually what you mean.
to testme
clear-all
let a 5
let b 10
let c 20
let Y ceiling (0.5 * a + 0.23 * b + 0.52 * c + 0.3)
print Y
create-turtles Y
[ setxy random-xcor random-ycor
]
end

Netlogo efficiency: Asking neighbouring patches to loop through table and obtain value

In my model, agents move and record a patch variable (weighted-index) for the patch they are on as well as all patches in-radius 16 into a table that is different for each turtle (representing memory). At each time step, I ask the surrounding patches of each turtle to set a patch variable (perceived-index) equal to the current patch variable (weighted-index) + the value for weighted-index that was stored in the table from the previous visit (memvalue). This is assuming that the turtle encountered a value for weighted-index and stored it in a table, returned later to that same patch (which now has a new value), and is using information for the current index amount and the remembered amount to decide whether it will move to that patch or not. My model becomes extremely slow when I add a foreach loop asking the surrounding patches to obtain the memvalue from the memory table. My world is pretty large but this code only deals with about 800 patches on the first tick and it still takes about 5 seconds per tick.
; Viewshed is a patch-set of the surrounding patches
; patch-memmap is the table containing food values from previous visits
; id is a single number used to identify patches (as opposed to using coordinates)
set viewshed (patches in-radius 16 with [AOI? = 1])
let patch-memmap memory-map
ask viewshed
[
ifelse Memory [
foreach sort viewshed [ x ->
ask x [
ifelse table:has-key? patch-memmap id
[
let memvalue table:get patch-memmap id
set perceived-index ((((weighted-index * (exp (- distance me))) * 0.7) + (memvalue * 0.3)))
]
[set perceived-index (weighted-index * (exp (- distance me)))]
]]
]
[set perceived-index (weighted-index * (exp (- distance me)))]
]
The model works but I was hoping someone might have any ideas on how to do this differently to increase efficiency. Thank you all in advance for your help!

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.

Netlogo: Return the Minimum of calculated values

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)