Why does netlogo think that this input is 0? - netlogo

I have the code:
to calculate-road-length
if [pcolor] of patch mouse-xcor mouse-ycor = 7[
set this-thing road-number
set plabel road-number
set number lput road-number number]
Where road-number is an input. Why do I get the error "LPUT expected input to be a list but got the number 0 instead" even when I enter a number? Any help would be good.

The problem is not with road-number, it is with number: lput expects the second parameter to be a list. Have you initialized number to a list?
You probably want to put set number [] in your setup procedure.
(number is a strange name for a list, by the way. Should it be plural, i.e., numbers?)

Related

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.

NetLogo foreach list

I am trying to create a code that will allow me to get the who numbers of 10 turtles and store them in a list. However, the "who" number can only be added to the list if it has not been added to the list before.
I have to use the "foreach" command to check if the number has already been added to the list.
Any suggestions on how to do so?
My code:
turtles-own [contact-list]
to setup
clear-all
create-turtles 10 [
set contact-list []
print who
]
reset-ticks
end
to go
ask turtles [
set contact-list lput [who] of one-of other turtles-here contact-list
foreach contact-list[
; this is where the command goes
]
print contact-list
]
end
Might it be easier to add all the who numbers to the list and then use the remove-duplicates primitive to remove the duplicate numbers? I assume that you have a reason for putting who numbers into the list rather than having a list of the agents themselves, or an agentset. In general, who numbers are to be avoided.

Clarification regarding net-logo let and error message

I don't understand reporters very well, so I am having trouble creating them. However, I think this one should work. I already set food quality to be random to be a random value from 0-1 using a slider, and based on my understanding you set a variable to be a new local variable using let.
to-report food-quality-level
ifelse food-quality > 0.6
[let food-value high-quality]
[let food-value low-quality]
report food-quality-level
end
However, I get the error: nothing low-quality has been defined. How should this be corrected?
Then in the procedures, do I say:
ask breed
if food-quality-level [high-quality]
[action]
There are several problems with this, mostly to do with confusion between variable names, procedure names and variable values. You are (sort of) using 'food-quality-level' as both the procedure name and the variable name, and you are using variable name syntax when you want variable values.
What I have done here is add comments that say what each line actually does (rather than what you intended):
; create a procedure named "food-quality-level"
to-report food-quality-level
; check if value of variable "food-quality" is more than 0.6
ifelse food-quality > 0.6
; (on true) assign the contents of variable "high-quality" to variable "food-value"
[let food-value high-quality]
; (on false) assign the contents of variable "low-quality" to variable "food-value"
[let food-value low-quality]
; output the value of the variable "food-quality-level" (except it's not a variable)
report food-quality-level
end
What I think you want to do is return some text which categorises whether the variable value is high or low (with 0.6 as the break point). If so, you want something more like this:
to-report food-quality-level
ifelse food-quality > 0.6
[let food-value "high-quality"] ; creates the value as text
[let food-value "low-quality"]
report food-value ; returns the newly created variable
end
A more advanced version of the same thing is:
to-report food-quality-level
report ifelse-value food-quality > 0.6
["high-quality"]
["low-quality"]
end
Here, I am using ifelse-value instead of the ifelse ... [set ...][set ...] construction and reporting the output as it is created.

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

Storing / recalling the value of a variable in Netlogo

Is there a way to store / recall values of a variable in Netlogo from previous ticks?
I need a way to recall what a variable was from previous ticks. If my turtle variable R was equal to 0 the last 3 ticks, I need it to set another variable back to zero.
Here is what that I was thinking.
to regression
ask turtles [if (R = 0 from last 3 ticks [Set Oin 0 ]]
end
How about making a list of variables, then limiting the length of that list to how far back you'd like the agent to "remember"
set memory []
then add to the list each tick and get rid of old memories with
set memory lput value memory
if length memory >= 4 [set memory but-first memory]
and if zero is on the list, have that alter behavior in some way
if member? 0 memory [blah]