Search code examples
global-variablesnetlogobehaviorspace

How to save a global variable with table format from NetLogo Behaviorspace


I have written a fairly complicated code for my ABM (634 agents having interactions, each having different variables some of which are lists with multiple values that are updated each tick). As I need to save the updated values for all agents, I have defined a global variable using table:make. This table has 634 keys (each key for one agent), and each key has a list of those values (from that agents-own list variable) for the correspondent agent. But when I use the name of this table to be reported as one of my outputs in Behavior Space, the result in csv file is a table with no keys and it has only a number in it: {{table: 1296}}. So, I was wondering how I could change this variable to be able to have all values.


Solution

  • If you're happy to do some post-processing with R or something after the fact, then table:to-list might be all you need. For example, with a simple setup example like:

    extensions [ table ]
    
    globals [ example-table ]
    
    turtles-own [ turtle-list ]
    
    to setup
      ca
      crt 3 [
        set turtle-list ( list random 10 one-of [ "A" "B" "C" ] random 100 )
      ]
      set example-table table:make
      foreach sort turtles [
        t ->
        table:put example-table ( word "turtle_" [who] of t ) [turtle-list] of t
      ]
      reset-ticks
    end
    

    And a to-report to clean each table item such that the first item is the key and all other items are the items in the list:

    to-report easier-read-table [ table_ ]
      let out []
      foreach table:to-list table_ [ i -> 
        set out lput ( reduce sentence i ) out
      ]
      report out
    end
    

    You can set up your BehaviorSpace experiment such that one of your reporters is that reporter, something like:

    enter image description here

    To get a .csv file like:

    enter image description here

    Where the reporter column outputs a list of lists that you can process how you like.

    However, I probably wouldn't use the basic BehaviorSpace output for this, but instead have a call in the experiment to call a manual table output procedure. For example, using the csv extension to make this output-table procedure:

    to output-table [ filename_ table_ ]
      let out [["key" "col1" "col2" "col3"]]
      foreach table:to-list table_ [ i ->
        set out lput ( reduce sentence i ) out
      ]
      csv:to-file filename_ out
    end
    

    This outputs a much more analysis-ready table if you're less comfortable cleaning the output of a list-of-lists that as far as I know is what you would get from the BehaviorSpace output. So, you can either call it at the end of your experiment, like:

    enter image description here

    To get a table like:

    enter image description here

    Which is a little nicer to deal with. You can obviously modify this to report more often if needed, for example:

    enter image description here

    which would output a table at each tick of the experiment (you can also do this in your code to make it a little easier).