How to read formula including turtle values from a input box? - netlogo

Im having trouble including a formula in the input box into my model and hoped someone could help. In short I want the global variable "subsidy_absolute_threshold" to be read as "100 + m2 / 1600". So that wherever that variable is implemented, that code is implemented instead. The current code reads something like
turtles-own [m2 subsidy_eligible]
globals [subsidy_absolute_threshold]
[...]
set subsidy_absolute_threshold "100 + m2 / 1600"
[...]
ask turtles [
if ambition < read-from-string subsidy_absolute_threshold [set subsidy_eligible true]
]
Technically the global value is an input box set as a string, but it should be functionally the same thing. The error message I get is "Extra characters after literal." which I struggle to understand.
I have tried all the "types" of input boxes, but none work. I have also tried directly writing in the formula, as in replacing subsidy_absolute_threshold with 100 + m2 / 1600, which works.

Use the native command runresult instead of read-from-string.
I'm somewhat unsure exactly why read-from-string does not work, so if someone wants to present an explanation please do.

Related

Netlogo use of myself

With the use of the myself primitive, I want a patch with a certain land use to make buildings of that type in a specific radius, but it's not working. Here is the code:
patches-own [land-use] ;land-use can take many types: "residential", "industrial", "commercial"
...
to grow
ask patches with [land-use = "residential"]
[ask other patches in-radius 10 [
if [land-use = "residential"] of myself [build-house]]]
end
How can this be fixed?
The problem there is not myself but the use of of. Two things to fix:
of requires the reporter to be in square brackets. Only the reporter that of refers to, not the whole condition being tested (as in your case where you are testing a condition).
You should not specify the value of the variable but just its name. Just as in normal language: you wouldn't say "If your hair colour equals my blonde", but you would say "If your hair colour equals my hair colour".
The resulting syntax is:
if (land-use = [land-use] of myself) [
build-house
]
Parentheses surrounding the condition are optional, I use them as a stylistic choice.
PS Regarding point 2 above... of course you could keep the value of the variable (i.e. "residential") instead of the name of the variable (i.e. land-use), and in that case drop of myself.
You would have:
if (land-use = "residential") [
build-house
]
And as of now it would work because the patches starting this process are just those with land-use = "residential"... but this doesn't look like great code, as it is not extendable to other variable's values and it contains more hard-coding than necessary.

How to rewrite a code that uses "values-from" syntax to a code that uses the "of" syntax

I need to convert a code from version 3 of netlogo to version 6. Until now everything was going just fine, but I got stuck in a line of code that I couldn't convert. The line is using the values-from syntax, and this syntax was replaced by the of syntax (https://ccl.northwestern.edu/netlogo/docs/transition.html). I tried to convert the code based on this but I get an error.
The code in the version 3 is the following:
let list-temp1 values-from aircrafts with [Team != Team-of myself and distance-nowrap myself <= radius][self]
Where aircrafts is a breed and Team is a variable from aircrafts.
The way I translated the code is the following:
let list-temp1 of aircrafts with [Team != [Team] of myself and distance-nowrap myself <= radius][self]
But I get the following error with this code: OF expected this input to be a reporter block, but got anything instead
I don't know what to do to get this code working. How can I translate it?
The original syntax is unclear what agent attribute is intended to be stored in the list. So the conversion to of had no variable to insert. What I think was intended in the original is to create a list of the agents rather than a list of the variable values belonging to the agents. In modern NetLogo, that would look something like:
let list-temp1 aircrafts in-radius radius with [Team != [Team] of myself]
That would need to be within an ask aircrafts [] block. Does that do what you want?
I assume Team is an aircrafts-own variable and radius is a global variable (though it will work with turtle variable as well). By the way, make sure you have the world settings (on the interface) set without wrapping.

Create an "itinerary" agentset of patches

I'm building a model where turtles "search" a subset of patches for a resource according to different search criteria.
I'm trying to build reports that return a sorted list or agentset of patches that a turtle can then use as an itinerary for it's search.
For some reason I'm having trouble storing the itinerary in a turtle owned variable.
an example reporter is:
to-report availability
let sorted-patches sort-on [ ( (space - occupants) / space ) ] patches with [space > 0]
report sorted-patches
end
when I do show availability in the console, it prints out what I expect, an ordered list of patches.
But if I do
let test-variable availability
show test-variable
it returns
ERROR: Nothing named TEST-VARIABLE has been defined.
is this a problem of scope somehow, can I not use let as an observer?
Is it a problem of type? Can I not store an agentset as a named turtle-owned variable?
Is there a way to do the same thing with a list instead of an agent set?
Thanks
From your description, it's a scoping problem. But the problem is not that you are trying to use let with an observer, it's the scope of let. NetLogo is not really interactive in the sense you are trying to do - the variable created by let is thrown away at the end of the line.
If you type let test 3, hit enter, then type show test, you will get the same error. However if you type let test 3 show test, then it will return 3.
Why are you needing this from the console? If it's for testing, then you can look at it the way you have already found - simply by show availability. If you are using it for turtles while the model is running, then it is not interactive and there's no problem.

Expected input to be a number but got TRUE/FALSE

I have got this block of code:
to catch-rose
let prey one-of roses-here
if prey != nobody
[
set energy energy + 1
set rose_ramasse rose_ramasse + 1
ask prey [ die ]
]
end
When I launch the simulation, I get the following error message
+ expected input to be a number but got the TRUE/FALSE false instead.
error while unefeebleue 2 running +
called by procedure CATCH-ROSE
called by procedure GO
called by Button 'go'*
I've been trying to solve out my problem myself but I can't. Why it does not want to recognize the second SET command? Why does it think it is true/false statement?
The error message is telling you the following:
It starts with the operation that failed. Often this is a procedure name, in this case it is +. So you know that it is one of the inputs to + that is failing.
It's telling you that one of the inputs to + is a TRUE/FALSE value.
Now, you've told us that the line it refers to is the second set statement. There are two inputs to the + on that line, rose_ramasse and 1
So, your problem is that rose_ramasse is a TRUE/FALSE (boolean) value. Without the rest of your code, it's not clear whether rose_ramasse is a global variable or not, so it is hard to go any further to help you work out where rose_ramasse is being assigned a boolean value.
I can replicate the error message with a simple function like the following:
globals [age]
to go
set age TRUE
set age age + 1

Netlogo Turtles - sharing the difference between two turtles

I am fairly new to Netlogo and have been working through Railsback & Grimms excellent book while trying my own stuff on the side
Currently, I am trying to code a kind of money transfer whereby two turtles meet (e.g. Turtle 1 and Turtle 2), and the two turtles then share money (e.g. T1 has $4 and T2 has $6, they both leave with $5)
The code I have tried using is as below
to currency-share
let neighbor one-of bystanders-here ; identify neighbor around
if neighbor != nobody ;; is there a neighbor? if there is
[ set currency round ((currency + neighbor currency) / 2)] ; share money
end
Unfortunately the code isn't working and I can't find any examples through the forums which use a similar idea. Perhaps I'm searching incorrectly (using key words like turtle exchange, share etc. normally comes up with patch sharing). If anyone has any models they could recommend where such exchanges happen, or if anyone knows how I may improve my code, please let me know. Thank you.
At first glance, this seems like a simple syntax problem. To get the currency of the neighbor, you should be using of:
set currency round ((currency + [ currency ] of neighbor) / 2)
But since you also said that you want both turtles to get the new amount, you also need to add:
ask neighbor [ set currency [ currency ] of myself ]
Or, perhaps less confusingly you could do something like:
set new-amount round ((currency + [ currency ] of neighbor) / 2)
set currency new-amount
ask neighbor [ set currency new-amount ]
One last variant, slightly better in my opinion because more general (and perhaps even clearer) is:
let sharers (turtle-set self one-of bystanders-here)
let new-amount mean [ currency ] of sharers
ask sharers [ set currency new-amount ]
In this last one, you don't even need the if neighbor != nobody check because if there are no bystanders, turtle-set will build an agentset containing only self and the mean currency of that set will just be the present currency value.