Rafaela
Rafaela

Reputation: 449

How to adjust a file (to remove []) to export result in NetLogo?

I have a question and couldn't find an answer to it. The question is:

  1. I have a code that exports the following result:

enter image description here

I would like the column (energy-of-my-agent) to be exported without the brackets [], like the figure below.

enter image description here

The code:

globals [ output-filename ]

turtles-own [ energy my-home ]

patches-own [ patch-id my-agent energy-of-my-agent ]

to setup
ca
reset-ticks
set output-filename "energy-of-my-agent.csv"
ask patches [
sprout 1
set patch-id [ who ] of turtles-here
set my-agent turtles-here ]
  ask turtles [
set my-home patch-here
set energy 0
]
  initialize-data-file
end
   

to initialize-data-file
  if output-data?
  [
    file-close-all

    if behaviorspace-run-number <= 1 [
      ;; we only want to delete the existing file if we're running in console
      ;; when running in console,  behaviorspace-run-number = 0,
      ;; first run in behavior space is behaviorspace-run-number = 1
      if file-exists? output-filename [
        file-delete output-filename
      ]
      ;; write a header to the file
      file-open output-filename
      file-print (word "run-number, ticks, energy-of-my-agent, pxcor, pycor" )
    ]
  ]
end

to go
ask turtles [
    let times repetitions
    repeat times [
let step random 5
fd step
set energy energy - step
  ]
  ]
ask patches [
    set energy-of-my-agent [ energy ] of my-agent
    if output-data? [
      if ticks mod output-every-n-ticks = 0  [       ;;output-every-n-ticks
        write-output-data energy-of-my-agent pxcor pycor
      ]
    ]
  ]

tick
end

to write-output-data [ #energy-of-my-agent #xpos #ypos ]

  file-open output-filename
  file-print (word  behaviorspace-run-number ", " ticks ", " #energy-of-my-agent ", " #xpos ", " #ypos  )
  file-flush
end

Is it possible? If so, how can I do this? Any kind of help is very welcome.

Thanks in advance

Upvotes: 0

Views: 82

Answers (1)

Matteo
Matteo

Reputation: 2926

In NetLogo, square brackets containing items represent a list.

In fact, the energy-of-my-agent variable in your output is in square brackets because it is a list (lists don't have to contain multiple items: empty lists or lists of one item are perfectly possible lists).

Why is energy-of-my-agent a list and not a single value? Because it comes from an agentset and not from an agent (as we already discussed here).

The rule is:

  • reporter of agent -> single value (unless the value is already a list in itself)
  • reporter of agentset -> list

In your case: energy-of-my-agent is made by [energy] of my-agent, and my-agent is an agentset, not an agent.

Why so? Because my-agent is made by turtles-here, and turtles-here is an agentset, not an agent, even if it contains one only agent. In the same way as lists, in fact, agentsets don't have to contain multiple agents: empty agentsets or agentsets of one agent are perfectly possible agentsets.

So you have three alternative options:

  1. Use set my-agent one-of turtles-here. This will give you a single agent, because one-of reports a single agent. If you are sure (as it seems to be the case) that patches will always only sprout 1, then one-of turtles-here will give you the exact same agent as turtles-here - but as an agent indeed, and not as an agentset. This in turn means that [energy] of my-agent will be a single value, and not a list of one value.

  2. When outputting values, use sum energy-of-my-agent. In fact, sum takes a list and reports a single value. Given that the sum of a list of one value is exactly that value, in this case sum will report the only value in the list, but without square brackets.

  3. When using sprout 1, you can make the new-born turtle assign itself to the patch: sprout 1 [set my-agent self]. This is possible because turtles can read and modify the variables of the patch they are on: in this case, the new turtle is able to operate on my-agent and, in particular, on my-agent of the patch it stands on.

I think that, between the three options above...

#2 is the least preferable: it never changes the fact that my-agent is an agentset (it only converts energy-of-my-agent from list to number at the end of the simulation), and I think this is not convenient in terms of memory and time because working with agentsets is a heavier process than working with agents.

#1 does not have this problem, because from the beginning it makes sure that my-agent is an agent and not an agentset (using one-of), so it is a better solution than number two. However, although it is perfectly fine, from a stylistic point of view using one-of <agentset> reads as if you're literally looking for a single random member of the agentset, instead of looking for the specific single member of the agentset.

#3 does not even have this latter problem: even from a syntactial point of view, this approach makes it very clear that each turtle, and only such turtle, is my-agent of the patch it sprouted from.

Upvotes: 2

Related Questions