Averaging neighbors' variables to inform decision - netlogo

The proejct I'm building is a little too big to post here; I think this snippet should suffice.
breed [CMs CM]
CMs-own [flag?]
to go
set flag? random 2
check-von-neumann-neighborhood
end
to check-von-neumann-neighborhood
create-links-with CMs-on neighbors4
ifelse mean flag? of CMs-on neighbors4 >= 0.5
[set flag? 1]
[set flag? 0]
end
What I'm trying (and failing) to do here is to ask each CM to average the values of "flag?" of its Von Neumann neighbors and then take on that value as its own flag?.
I slapped the create-link-with code on there because I thought it would allow CMs to sense its neighbor's flag?, but that doesn't appear to me the problem.
The error message highlights "flag?" in "ifelse mean flag? of CMs-on neighbors4 >= 0.5" and says "OF expected this input to be a reporter block, but got anything instead."
I would really appreciate any help! Thanks in advance.

You don't need the links, NetLogo can detect the turtles on the neighbouring patches and then access the variables of those turtles. Your problem is that you have flag? of but need [flag?] of.
The error message means that you have used of without telling NetLogo what to report. You need the [ ] to mark the boundary of the reporter block, even if it's simply a variable. Reporter blocks could be complicated calculations so NetLogo needs to know where to start and end the calculations for each entity that is reporting (in this case, all the CMs on the neighbors4).

Related

How to optimize assignation process in Netlogo

I want place 1 million fish in a lake at random to do so i have this.
ask patches[ if sum [peces] of patches < 1000000 [ask one-of patches with [mallor = 1][set peces peces + 1]]]
This is taking too long, how would you suggest to make it quicker? I know it´s the sum part as it has to always check, but i dont know how else to do it
A first thing I noticed is that you have the construction:
ask patches [ if ... [ ask one-of patches [...]]]
That first ask patches is completely redundant here. It lets each patch direct another random patch to make a fish. Since you use one-of the second time around I don't expect that the speed impact is too big but it should still help
Next, I suggest counting the fish you have already placed with a single local variable, instead of using sum [peces] of patches 1000000 times. Getting variables from an agent (with of or with) takes a lot of time if you have a lot of agents, and I remember from your previous questions that your number of patches is pretty huge.
Then thirdly, you also check for which patches the mallor = 1 condition is true 1000000 times. That one can also be grouped into a local agentset
let fish-placed 0
let mallor_patches patches with [mallor = 1 ]
while [fish-placed < 1000000] [
ask one-of mallor_patches [
set peces peces + 1
set fish-placed fish-placed + 1
]
]
Disclaimer, I haven't tested any of this code in Netlogo since I don't have the bigger model to test the code in. However I expect it will run a many times faster than what you originally wrote.

Problem: The turtle variable is of type int (-1, for example), but the patch variable is a one-element list ( [-1] ) in NetLogo 6.2

I have one doubt:
Context: I have a code in which, briefly, turtles have an integer variable (energy-collected) and from that, patches update their own variable (energy-of-my-agent), as described in the code snippet below.
Problem: The turtle variable is of type int (-1, for example), but the patch variable is a one-element list ( [-1] ).
Question: Should this happen? Otherwise, how can I make the patch variable just an integer value?
ask turtles
[
set energy-collected (energy - euse)
]
ask patches
[
set energy-of-my-agent [energy-collected] of turtles-here
]
Thanks in advance
The main thing you have to consider is what of reports.
In your case turtles-here is an agentset, not a specific agent.
This is because, although you might have a single turtle on a patch, you may also have multiple turtles on a patch. Therefore turtles-here reports an agentset, even if that agentset may be made of a single turtle.
It follows that a collection of values from an agentset, obtained with of (and [energy-collected] of turtles-here is exactly that), will be a list of values - even if that list contains only one element.
Therefore I would say:
Is your model made in such a way that each patch cannot have more than one turtle at a time? Then you could do:
ask patches [
if any? turtles-here [
set energy-of-my-agent [energy-collected] of one-of turtles-here
]
]
In the code above, one-of turtles-here reports a specific agent - not an agentset anymore.
So its variable's value, obtained with of, will be stored as a single value (provided that the agent's variable is not a list itself, but that's not your case).
Can it happen that your patches have more than one turtle at a time? Then, if you're interested in the single patch holding "its" turtles' values, dealing with lists is probably necessary.
Update
I made a connection between this question and your other one suggesting that you want to use patches as elements of matrices.
Maybe this is useful to your case: if your model allows for the possibility of having more than one turtle on the same patch, you might be interested in doing something like:
ask patches [
set energy-of-my-agent sum [energy-collected] of turtles-here
]
As you can see, sum takes a list as input and reports a number. Each patch will take the sum of all the values of energy-collected by turtles standing there, or you can change the calculation using whatever you want (e.g. mean, max etc).
Actually, you can use this approach regardless: this way, even when you have a single turtle on a patch, sum (or any other function taking a lost and returning a value) will give you a single value where before you had a list of one value.

WITH expected a TRUE/FALSE block, but got a TRUE/FALSE instead?

I have the code
sum [plant-energy] of (patches-with-ash with (pycor > 0 and pxcor > 0)))
for a monitor in my model. plant-energy is a defined patch variable and patches-with-ash is a defined agentset. I'm trying to get a sum of all plant energies for the patches in patches-with-ash in the top-right half of the space, but this returns a weird error.
WITH expected this input to be a TRUE/FLASE block, but got a TRUE/FALSE instead
Any help would be much appreciated!
EDIT:
I'm just using the monitor as a test for my code. I'm trying to sum the plant energy of all patches in the agentset with xcor less than and ycor greater than a turtle (i.e. all patches of this agentset to the upper left of the turtle). I think this is the right avenue to go down but if anyone knows a better way I would appreciate that as well!
try it like this:
sum [plant-energy] of (patches-with-ash with [pycor > 0 and pxcor > 0]))
The [] basically tells NetLogo to do the test within the [] and return a true or false, which is then passed to the with

How to change a turtle's attribute if one of its links disappear?

In NetLogo: suppose the model has
a turtle (0) of breed A with undirected links with 3 turtles (1, 2 and 3) of breed B;
the turtle 0 has an attribute named "number-of-links" that equals 3.
Now, let one of the 3 neighbors of 0 dies..
How can I program turtle 0 to change its number-of-links automatically to 2?
If all you want is a way of keeping track of the number links, use count my-links instead of a custom variable.
In general, the least bug prone way of having a value update when the number of links changes is to compute that value when you need it. For number of links, this is simply count my-links. For more complicated things, wrap them in a reporter:
to-report energy-of-neighbors
report sum [ energy ] of link-neighbors
end
If this doesn't work for whatever reason (agents need to react to a link disappearing or you're seeing a serious, measurable performance hit from calculating on the fly), you'll have to make the updates yourself when the number of links change. The best way to do this is to encapsulate the behavior in a command:
to update-on-link-change [ link-being-removed ] ;; turtle procedure
; update stuff
end
and then encapsulate the things that can cause the number of links to change (such as turtle death) in commands as well:
to linked-agent-death ;; turtle procedure
ask links [
ask other-end [ update-on-link-change myself ]
]
die
end

NetLogo AgentSet substraction

I got the error message:
MEMBER? expected input to be a string or list or agentset but got the number 0 instead.
during running following NetLogo Code:
to find-flockmates ;; turtle reporter
;; latch on to the nearby birds
set flockmates-infront other birds in-cone vision cone-infront-degree
set flockmates-sidewise other birds in-cone vision cone-sidewise-degree
;; agentset substraction
if (count flockmates-infront > 0)[
set flockmates-sidewise (flockmates-sidewise with [not member? self flockmates-infront])
]
end
Can someone tell me what I am doing wrong or another possible solution for a substraction of two agentsets?
I see, now! Tough it could possibly have been guessed from the initial code sample you posted, I did not realize at first that flockmates-sidewise and flockmates-infront were breed variables.
As a consequence, in this line:
set flockmates-sidewise (flockmates-sidewise with [not member? self flockmates-infront])
...flockmates-infront refers to the breed variable of the agent executing the with block, not the variable of the agent running the find-flockmates reporter. (And that one may very well be 0 if it has not been initialized yet.) What you want is:
set flockmates-sidewise (flockmates-sidewise with [
not member? self [flockmates-infront] of myself
])
myself means "the turtle or patch who asked me to do what I'm doing right now."
I guess you were not getting the error when creating less than 10 birds because your if prevented the line from being executed at all in that case.