Exporting cross-sectional data from NetLogo Behavior Space - netlogo

I have created an experiment in Behavior Space for my NetLogo model. I would like to save the value of some variables for each individual turtle, later to be processed with statistical software (Stata or R). My first attempt at a reporter was:
[my-variable] of turtles
This kind of works, but the formatting of the resulting CSV file is problematic. All values for an individual variable are stored in a space-separated list:
"run", "[my-variable] of turtles"
"1", "[48.234967724191584, 15.361986575058953, 19.613022950636537, ... ]"
...
What I would like:
"run", "[my-variable] of turtle_0", "[my-variable] of turtle_1", ...
"1", "48.234967724191584", "15.361986575058953", ... ]"
...
I am looking for something like the split() method in Python. Any suggestions? Thanks.
UPDATE: Cross-sectional analysis is predicated on the assumption that, during the same run of the model, all reporters list turtles-own variables in the same order. So, if I have two reporters:
[my-variable-1] of turtles => "[1 2]"
[my-variable-2] of turtles => "[3 4]"
I need to be sure that both reporters take turtles in the same order, so that turtle 0 has my-variable-1 equal to 1 and my-variable-2equal to 3, whereas turtle 1 has my-variable-1 equal to 2 and my-variable-2equal to 4. I cannot find a mention of this in the NetLogo documentation. Can anyone confirm this?

I can give you an answer to the latter part of your question. [my-variable-1] of turtles will give you the values of my-variable-1 in random order as of takes the agents in random order. (This is documented at http://ccl.northwestern.edu/netlogo/5.0/docs/dictionary.html#O).
If you want a fixed order by who number, then you can use sort and map.
map [[my-variable-1] of ?] sort turtles
sort turtles creates a sorted list of turtles and then that list is given to the `map' primitive to create the list of values ordered by the who number of the turtle. Of course you'd need be careful that no turtles die or are created during the run.
As for the writing of the observations to a CSV file, there are other posts that look at that specifically. You might also want to look at the stats extension to see if that would help by allowing you to write out all the results at the end of the run from a list of lists that is easily handled by the CSV extension.
Charles

Related

Lisp - Extracting info from a list of comma separated values

I've tried searching this but have yet to find something that suits anything close to my needs. I'm trying to create a Autocad LISP that takes a text file, which is a list of comma-separated values, and place a block at coordinates defined by the list. BUT, only for items on the list where the last entry starts with "HP"
So that's sounds a bit complex, but the text file is basically a UTM survey output, and looks like this:
1000,Easting,Northing,Elevation,Identifier
1001,Easting,Northing,Elevation,Identifier
Etc.
The identifier is a variety of values, but I want to extract the Northing,Easting,Elevation, and insert a block (this last part I've got) at that location when the identifier begins with "HP". The list can be long and the number of HPs can be 1 or 5000. I'm assuming there's a "for x=1:end, do" type of loop than can be made that reuses the same variables over and over.
I'm a newbie to LISP so I'm stuck in that spot between "here are I've-never-programmed-before tutorials to make hello world" and "here is a library of the 3000 different commands in alphabetical order"
I believe the functions you are needing to solve this question are open, read-line or read-char, close,strlen, and substr. The first four functions relate to AutoLisp writing and reading a file. The last two functions manipulate the string variables that were pulled from the file. With them, you can find the "HP" within the text. To loop through the same code, three come to my mind: repeat, while, and foreach.
For a list of variables to quickly reference with their descriptions, here's a good starting point. This particular page has the information broken up by category instead of alphabetical order.
https://help.solidworks.com/2022/English/api/draftsightlispreference/html/lisp_functions_overview.htm
Here are a few tutorials where AutoLisp code is used to write and read other files:
https://www.afralisp.net/autolisp/tutorials/file-handling.php
https://www.afralisp.net/autolisp/tutorials/external-data.php
Lastly, here's an example of AutoLisp writing and reading attributes from and to blocks.
https://github.com/GitHubUser5376/AttributeImportExport
You can use Lee-Mac's Reacd-CSV function to get a list of the csv values.
And for the "HP" detection yes you might have to go through(using loop options mentioned above like while, repeat,foreach) each and use
(substr Identifier 1 2)
to validate

Understanding NetLogo syntax

How should I understand the syntax of a line of code like this:
crt 50 [set breed ifelse-value (who mod 2 = 1) [mice] [frogs]]
It creates 50 turtles and set half of them to be mice and the other half to be frogs. My question is about the two reporter blocks in the ifelse-value statement. What does it mean that [mice] returns the mice breed.
How should I understand the identifier mice? The NetLogo manual says that NetLogo doesn't support Lisp-like symbols. But this seems to be something like that.
Does a code block that is expected to be a reporter and that contains an expression that can be evaluated return (report) the value of that expression?
How should I understand the identifier mice?
It's a reporter. It returns an agentset.
The NetLogo compiler turns the mice identifier into a parametrized call to the internal _breed() reporter. Other "breed" primitives (e.g., <breeds>-at) work the same way.
Does a code block that is expected to be a reporter and that contains an expression that can be evaluated return (report) the value of that expression?
Yes.

Behaviourspace - Measure Runs every X steps?

I am running a series of experiments using behaviourspace and outputting my results in spreadsheet mode. My model runs at a temporal rate of 1 day per tick. Every 365 days a year variable increments. I would like to report the value of another variable to the spreadsheet at the end of each year.
Does anyone know how to accomplish this using the behaviourspace "Measure Runs using these reporters" box? It seems that I can either report the value at every tick or at the end of the model. Ideally I would like to report every x ticks.
I have attempted to put a conditional statement in the reporters box but behaviourspace throws an error. An alternative option would be to create a list as the model runs and export it at the end of each run - but this produces unwieldy output for analysis. Is this my only option or am I missing something?
Any advice much appreciated. Thanks
I don't think you can report occasionally. Instead, I would do the following:
In your code, define a function called yearly-report and always report a value.
to-report yearly-report
ifelse (ticks mod 365) = 0
[report your-calculation-here ]
[report ""; where "" is an empty placeholder text that you may ignore.]
end
Then in your behavior-space commands, call yearly-report

Will this code allow me to assign a number to a list and incrementally increase the number?

[ set list N = 1 () set list N = 1
lput number-of-patches destination origin list N N + 1]
I wish to be able to store information about collections of patches and when the criteria for the filling of the list is met the number of the list will be increased. Will this code work?
Just looking at it, it will give you several syntax errors, regardless of whether the structure will do what you want. For example, the way you should construct a list with element '1' and name 'N' (which is what I think the first line is supposed to do) would be set N (list 1). You can test this by writing code as below and running test (eg by typing test in the command center at the bottom of the interface).
globals [N]
to test
set N (list 1)
print N
end
When writing code, your life is a lot easier if you build up the code in pieces, testing each one as you go either by inspecting agents to see if their property values change as you expect and/or putting print statements in lots of places to see what happens to your variables. This way you are introducing and fixing only a small number of errors in each step. Also, this means you are never writing code that you can't test immediately.

Error messages in NetLogo when trying to use `with`

I'm a new user of NetLogo (5.0.5) and I receive error messages when I try to do pretty basic things. In particular I get
Nothing named x has been defined
when I try to define/use various agentsets. For instance
show turtles with [color=red]
show turtles with [who<10]
ask turtles [set friends turtles with [color=red]]
generate this message (where friends is a turtle variable). However,
ask turtles [set friends turtles in-radius 7]
is ok. So it might have to do with 'with'. I'm not sure what do do here. I was following along Jose Vidal's excellent tutorial
https://www.youtube.com/watch?v=k5RMdrbJXpM&list=PLSx7bGPy9gbFCWOQ6bAb_4ASLIrblXhqP
and everything was working until i tried
show cats with [color=red]
which generated the above error-message. Can anyone help with this?
I haven't found anything about this anywhere but since I'm new there might be resources that do not show up on google (or here) that I've missed.
Unlike some other programming languages, NetLogo doesn't allow you to smash arithmetic and comparison operators up against identifiers; they have to be separated. So you need to write color = red, with the spaces, instead of color=red, and who < 10 instead of who<10. (If you smash them together, NetLogo thinks the whole thing is a single name for something.)
You can smash brackets [] and parentheses () up against other things without spaces, but not other punctuation.