I'm trying to modify a variable belonging to a specific patch in NetLogo.
set [informed] of target [informed] of target - floor(diff / 2)
I get the error: This isn't something you can use "set" on.
'target' is a specific patch I've defined earlier.
I've used syntax like this ([variable-name] of specific-patch) at the end of expressions to define other variables, but it doesn't work when that's the variable you're trying to define. Removing brackets just results in a different error. I'm not sure whether I'm missing some major fundamental understanding or some silly syntactic mistake. Any help would be appreciated, thanks.
Related
What are the available options (if any) for defining constants in NetLogo? I am looking at having the constants defined in the code, not in the interface. The value(s) are not intended to be received from the user as an input via the interface.
The goals are as follows:
Define the constant in one place in the code and use it wherever required. This would enable one to tweak the value in one place.
Prevent accidental modification of the constant value elsewhere in the code.
I am looking at creating something similar to the mathematical constants e or pi which are baked into NetLogo but at a single model level.
Is it possible to create such a constant?
Are there more than one ways to define such a constant? If yes, what are the available options and the associated pros and cons?
Two ways come to my mind:
(1) Use global variables that are specified upon setup
For example:
globals [
a-cool-number
a-number-that-I-don't-like
]
to setup
clear-all
set a-cool-number 7
set a-number-that-I-don't-like 44
create-turtles 1
end
to go
ask turtles [
show word "The best number is " a-cool-number
show word "The same is not true for " a-number-that-I-don't-like
]
end
Pros: It works.
Cons: The more global variables you declare, the less easy it becomes to approach your model by reading code (although using comments to single those variables out by saying that they are just constants would help). Also, in theory those variables could be accidentally modified by agents or by you (although I think this is a remote risk, if it is true that these variables are only specified upon setup and never again).
(2) Use reporter procedures
For example:
to setup
clear-all
create-turtles 1
end
to go
ask turtles [
show word "The best number is " a-cool-number
show word "The same is not true for " a-number-that-I-don't-like
]
end
to-report a-cool-number
report 7
end
to-report a-number-that-I-don't-like
report 44
end
Pros: It works. Also, there is no way the value of your constants could be inadvertently modified by agents and not even by you yourself (unless you directly go and change the code in the reporter procedure, of course).
Cons: None that I can think of now.
All in all, given the way you asked your question, I think that reporter procedures are the best option for you.
Matteo's answer is good, I'd use reporters, too. But I wanted to add one more method that's a little "sneaky". You can create widgets, inputs, sliders, or choosers, and then place a label widget with background transparency disabled over the top to "hide" them. Widgets are presented in the order they were added to the model, so the label widget gets drawn in last over the top of the other global variable widgets.
This lets you have a global variable that you can use in code that has no apparent backing widget. Any value you give it will be saved with the model and so will be "constant".
The only time I would use this technique would be if I really didn't want anyone looking at the model code to see where the constant value was coming from for some reason. I can't even think of too many uses for that, but hey, I just wanted to make the possibility known.
A big downside of this method is that you can change the value through the command center or other code and not notice it, as the value is hidden from sight.
Here is a picture that hopefully makes it clear. I've not completely hidden the input widgets and I've left text in the label just to make the picture not be a big blank area.
A while ago I was reviewing some legacy code, and I incurred in the following issue: the developer create many variables starting with a dot (like .variable1), syntax reserved to define namesspaces.
When quizzed about the use of that convention, he replied that essentially that was a way to create global variable within the scope of a function - what in Python you would achieve doing the following:
def define():
global a
a = 2
define()
print(a)
In q, that translated in something like this:
f1:{.b1:2;}
f2:{b1:2;}
f1[] / this creates a variable .b1, which you can use globally
f2[] / this creates a variable b1 within the scope of the function, not usable outside
While my preference would have been to create a global variable using a namespace (something like f1:{.my_namespace.b1:2;}), the code was doing its job without any issues.
The problem arises if I want to delete the global variable defined which starts with a dot (.b1 in the case just described), given that approaches like
delete .b1 from `.
do not seem to work. All the references I was able to find (like this) suggest that namespaces cannot be delete, which would imply these unproperly defined variable will stay there.
To be clear, the problem is not about how to delete variables from namespaces, but to delete those namespaces which are used as variables - if possible at all.
Any ideas?
Based on your question, you want to delete variables from a namespace. You can do this by running:
delete b1 from `.my_namespace
Which is also explained in the link you pasted.
What you can't do is actually delete the reference to my_namespace.
As a side note you can also set variables globally in the root namespace using the set keyword:
{`var set 2}[]
Which will let you avoid having to create single use namespaces.
I've been programming for over 6 years now and i've always avoided using global variables because there is always another way around the problems.
Today, i work on a (big) project, we want to use a dictionnary of mathematical CONSTANTS what will never be modified anywhere. The only problem i seem to find with globals on internet is the fact that if someone overwrites one it could bug out the whole project. But as mine are constants this problem doesnt apply.
(as a second security to avoid people creating a variable with the same name as one of the constants i will probably pack them all in a single global struct)
Does anyone know of problems that still happend using global constants?
Thanks for your answers! :)
In MATLAB, your best bet for mathematical constants is to define a class with properties that have the Constant attribute. This is described in the doc here, and here's the leading example from that page:
classdef NamedConst
properties (Constant)
R = pi/180
D = 1/NamedConst.R
AccCode = '0145968740001110202NPQ'
RN = rand(5)
end
end
This way, the values cannot be overridden. (Note that there's something a little perhaps unexpected in this example - the value of the property RN changes each time the class is loaded! I personally wouldn't write code like that...)
The old-fashioned standard way to create a constant in MATLAB is to write a function. For example pi is a function. It could be written as:
function value = pi
value = 3.14159;
end
Of course we can overwrite the value of pi in MATLAB, but it is always a local change, it is not possible to affect another workspace.
I'm trying to make my model compatible with an interface framework that can't handle the csvs I normally export to, and requires lists. What I need to do, specifically, is export a list of lists of variables associated with each of a certain breed of agent.
Ideally:
set master-list (foreach person set traits-list list (who) (color) (heading) (xcor) (ycor) (etc...))
But the primary issue I'm having is that after the first two variables set up in the set traits-list list (items) way, it starts throwing up errors on any subsequent variables. I can just lput each individual variable, but that seems like a really unnecessarily messy way to set it up. Am I missing something?
I've tried seeing if it's the individual variable that's the issue, but the error persists no matter what the third variable is.
If anyone wants to look at the complete code in question, I'm trying to make https://github.com/efyoungud/stationfire work with https://github.com/hlynka-a/SRTI.
JenB had the answer with needing the bracket.
Final, functional code:
ask people [ foreach [self] of people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)(breed)(hidden?)(size) (alarmed?) (age)(visited?)(group-number) (group-type) (group-constant)(speed) (leadership-quality) (leader) (goal) (energy)(speed-limit)
)]] ; doesn't include next-desired-patch or path because that's calculated each step and doesn't need to be exported
set master-list [traits-list] of people
end ```
this might sound like a dumb question but, does anyone know how can I get a list of the variables (including auxiliary and dummies) from my Netlogo code?
I think the Reflexion extension might be what you're looking for.
As per the documentation:
reflection:globals reports a list with the names of all global variables breeds
and:
reflection:breeds reports a list of all breed names, and all their variables, default variables included.