Error adding patches to patchset - netlogo

I can't understand the problem with the code:
ask patches with [....][set patches-in-box (patch-set patches-in-box self)]
It gives me an error
PATCH-SET expected input to be a patch agentset or patch but got the number 0 instead.
error while patch 15 4 running PATCH-SET
I am using self incorrectly here?

I think you mean self here, not myself.
self refers to this agent. myself refers to some other agent, the agent that asked this agent to do what it is currently doing.
observer> crt 2
observer> ask turtle 0 [ ask turtle 1 [ print self print myself ] ]
(turtle 1)
(turtle 0)
But that can't explain the error that you're getting, which states that one of the inputs you're giving to patch-set is the number 0. Neither self nor myself can ever be a number, so the culprit must be patches-in-box.
Apparently before beginning you've forgotten to initialize patches-in-box to the empty set, like this:
set patches-in-box no-patches

Related

Error in plotting: OF expected input to be a turtle agentset or turtle

I am studying a model where children play with some toys.
The characteristics of the toys are included in attribute1. I had to create a new variable, attribute1_b, as I wanted to have initially the same original value for the neighbours in order to subtract a small quantity m. I would like to plot the new value of attribute1 to study how it changes through time, respect of fun2 (i.e. 1- (attribute1-m)), but unfortunately I have found difficulties in defining fun2 (global, children-own, and/or toys-own).
After plotting
ask children [plotxy [attribute1] of picked_toy fun2]
I have got the following error message:
Runtime Error: OF expected input to be a turtle agentset or turtle but
good the number 0 instead
Could you please tell me what the message is referring to and how to fix it?
It is important for me to consider the attribute1 related to the picked_toy, as its value can be equal to attribute1 (as in myself) or attribute1 - m (as in ask link-neighbors).
The code that I am using for building the model is the following
globals [
this-toy
attribute1_b
]
breed [children child]
breed [toys toy]
children-own [
bag
fun1
fun2
attribute1
picked_toy
]
toys-own[
fun1
attribute1
m_children
]
One procedure, to proc1, includes the definition of attribute1 and fun1 as follows:
if breed = children[
set selected children
hatch-toys 1 [
set m_children selected
set attribute1 random-float 1
set attribute1_b attribute1
set fun1 (1 - attribute1)
set this-toy self
ask myself [
set bag fput this-toy bag
]
]
ask link-neighbors [
let m random-float 0.01
set attribute1 attribute1_b - m
set bag fput this-toy bag
]
]
Another one,to proc2, should include the definition of fun2:
if breed = children [
set picked_toy max-one-of turtle-set bag [attribute1]
set fun2 (1 - [attribute1] of picked_toy)
set bag fput picked_toy bag
]
UPDATE: I am also getting the following error message (because of max-one-of turtle-set):
List inputs to TURTLE-SET must only contain turtle or turtle agentset
elements. The list [(toy 20) 0] contained 0 which is NOT a turtle or
turtle agentset.
I cannot understand where 0 comes from...
All comments and clarification are more than welcome. Thanks
First, 'picked_toy' should be a turtle attribute so uncomment it there and delete it from the global variables. A global variable is something that is the same for every agent in your model. Clearly, each child has its own toy, so that needs to be an agent variable. This is what agent variables do - each agent has its own copy (which can be different or the same as those of other agents). You need to get this concept clear if you want to make any progress in NetLogo and I suggest redoing some tutorials.
This line set picked_toy max-one-of turtle-set bag [attribute1] doesn't really make sense. The variable 'bag' is a list. The primitive max-one-of applies to an agentset. I think you have tried to use turtle-set to convert the list to an agentset, but that won't work. The correct way to turn a list of turtles into an agentset is:
turtles with [member? self listname]
Does your code work if you have:
set picked_toy max-one-of (children with [member? self bag]) [attribute1]
As I have mentioned in responses to some of your other questions, unless you have a good reason to use lists (like needing to keep the order, or having multiple copies), it is generally better to use agentsets because they are easier to work with, particularly for beginners.

Averaging neighbors' variables to inform decision

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).

Netlogo: asking "up to a maximum number"

Is there a way to ask up to a certain number of patches? For example, ask up to 100 patches but there are only 50 available, so the action takes on this 50 patches. Thanks.
The way to do this at the moment would be something like:
to-report at-most [n agents]
report ifelse-value (n <= count agents) [ agents ] [ n-of n agents ]
end
You can then say ask at-most 100 patches [ ... ] and you will get what you want.
Note that this doesn't work if there is a chance that your variable contains nobody instead of an agentset. In that case, you can convert nobody to an agentset using patch-set, turtle-set or link-set, depending on the type of agent you expect it to contain. For example:
ask one-of turtle-set other turtles-here [ ... ]
Note that the need to jump through all these hoops might disappear in the near future. There is currently an open proposal to add a primitive to NetLogo for handling these cases: https://github.com/NetLogo/NetLogo/issues/1594.

NetLogo: Finding out all turtles linked to each other and assigning them to a group id

I have searched all topics for the last week and haven't be able to yet find a solution to my problem. The scenario is as follows:
I have various groups of turtles that are linked to each other
I would like to assign a group name or id to each group
In order to do the above, I am thinking that I need a procedure that will find out the id of a turtle and then tell me to which other turtles this particular turtle is linked to. So for example I have a group of 4 turtles, all linked to each other. Their ids are 1, 2, 3, 4. Now I would like to get a list of each turtle, which also tells me to whom it is linked to. Here an example list:
Turtle 1 -> Linked to Turtle 2, 3 and 4
Turtle 2 -> Linked to Turtle 1, 3 and 4
...
Turtle 4 -> Linked to Turtle 1, 2 and 3
Once I now which this (although maybe there is a much easier way). I could assign a group id to these 4 turtles.
Any feedback / help very much appreciated.
Here is part of the code:
to go
ask turtles [
set neighbours turtle-set turtles in-radius neighbourhood
set turtle-dim-similar min-one-of other neighbours [myfunc myself self]
create-link-with turtle-dim-similar
]
set beasts sort-by [[who] of ?1 < [who] of ?2] turtles
foreach beasts [
ask ? [ ;; each agent undertakes the list of commands
set closest sort [link-neighbors] of turtle who
print (word "id:" who " ; val1:" value1 " ; val2:" value2 " ; closest:" closest)
]
]
end
If you are using directed links i.e. links created with create-link-to, create-link-from, create-links-to or create-links-from than out-link-neighbors is what you are looking for. It reports a set of the turtles linked to.
If you are using un-directed links, ones created with create-link-with or create-links-with , use link-neighbors.
This solves the first part of the problem for the second half use recursion.
Turtles-own[group-id]
To id [tag]
Set group-id tag
Ask link-neighbors with[Group-id != tag]
[Id tag]
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.