Clarification regarding net-logo let and error message - netlogo

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.

Related

How to report agent variables in a consistent order in Netlogo's Behaviourspace

Picture of my behaviourspace menu
I'm working on an agent based model where a variable (agentvariable1) owned by all agents changes every tick. I want to report a time series for the values of this variable for every agent using Behaviourspace.
However, when I measure runs using the following reporter
[agentvariable1] of turtles
the values that are reported for agentvariable1 are randomly shuffled, because "turtles" calls all turtles in a random order, which is different every tick. Because of this the data that is exported is not usable to create a time-series.
Is it posstible to create a reporter in Behaviourspace that reports the values of the agentvariable1 in a sequence that remains the same every tick?
Using sort on an agentset creates a list of those agents sorting them by some criteria. In the case of turtles, they are sorted by their who which means that their relative order will always be the same.
However you cannot directly do [agentvariable1] of sort turtles, because of expects an agent/agentset but you are giving it a list.
What you can do is creating a global variable as a list: at each tick the list is emptied, and later all turtles (sorted as per sort) will append their value to the list.
That list is what you will report in your Behavior Space.
globals [
all-values
]
turtles-own [
my-value
]
to setup
clear-all
reset-ticks
create-turtles 5
end
to go
set all-values (list)
ask turtles [
set my-value random 10
]
foreach sort turtles [
t ->
ask t [
set all-values lput my-value all-values
]
]
show all-values
tick
end
As an alternative to Matteo's answer (which is perfectly suitable and directly addresses your intention, I just present another option depending on preference) you could also pair the variable of interest with some turtle identifier and report that as a list of lists. This adds a bit of flexibility in cases where the number of turtles increases or decreases. In this example, I use who and xcor for simplicity, but you may want to create your own unique turtle identifier for more explicit tracking. With this toy model:
to setup
ca
crt 5
reset-ticks
end
to go
ask turtles [
rt random 30 - 15
fd 1
]
tick
end
to-report report-who-x
report list who xcor
end
At any point, you can call the list with [report-who-x] of turtles to get a list of lists. With a behaviorspace setup such as:
you get an output that would look something like:

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

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]

Why does netlogo think that this input is 0?

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