output variable values

240 views
Skip to first unread message

rbsoc...@gmail.com

unread,
Dec 1, 2021, 8:52:43 AM12/1/21
to netlogo-users

I have five turtles and two local variables with numeric values, call them x and y.  The variable values are manipulated in the program and change.  For internal checks I need to output the variable values at three points in the program.  So I would like to print out who, x, and y with these three labels vertically sorted by who in ascending order.  In other words, I want to output a table with three columns and five rows and who, x , and y labels for the columns.  What is the best way to do this?

Thanks, RB

Wannes...@hotmail.be

unread,
Dec 2, 2021, 11:09:22 AM12/2/21
to netlogo-users
Where do you want your table outputted? If a output window on the interface is sufficient, you could do something like the following

___________________________________________________
to setup
  ca
  crt 5
end

to monitor
  clear-output
  
  output-print  "who| x | y \n"
  
  let me 0
  let max-me 4
  while [me <= max-me]   [
    let x random 10
    let y random 10
    output-print (word " " me " | " x " |  " y "\n")
    set me me + 1
  ]
  
end
___________________________________________________

That gives you an output in the following format. Not the most clean but sufficient if you want to quickly check something.

who| x | y 

  0 | 5 |  6

 1 | 3 |  9

 2 | 2 |  0

 3 | 1 |  1

 4 | 0 |  4






Op woensdag 1 december 2021 om 14:52:43 UTC+1 schreef rbsoc...@gmail.com:

rbsoc...@gmail.com

unread,
Dec 2, 2021, 1:43:44 PM12/2/21
to netlogo-users
Thanks.  Will try. A quick follow-up.  In referencing "who", I mean the turtle id variable.  When using who, will it print out in order?  If not, how to do so preferably in ascending order?
RB

Wannes...@hotmail.be

unread,
Dec 2, 2021, 2:32:23 PM12/2/21
to netlogo-users
I never actually used who in the above example, just a string containing the word "who".

The ordering is why I introduced this while loop. I assume you want to let the turtles change x and y, then print it out and then let the next turtle change x an y and print and so on?
By increasing "me" every time and calling on "turtle me" (or the turtle whose "who" = "me").

I changed the example a bit, actually letting the turtles change the x and y value instead of letting the observer do that.

There might very well be better ways to cycle through your "who"s however I don't know my agentsets well enough yet to be able to help you with that.
____________________________
globals [x y]

to setup
  ca
  crt 5
end

to monitor
  clear-output

  output-print  "who| x | y \n"    ;   simple string to give your table a header

  let me 0    ;   the first turtle we want to look at is turtle 0
  let max-me 4    ;   since there are 5 turtles, the highest who is 4
  while [me <= max-me]     ;repeat the following once for each turtle
  [
    ask turtle me    ;   let turtle me execute some commands
    [
      fd 1
      set x random 10
      set y random 10
    ]
    output-print (word " " me " | " x " |  " y "\n")    ;   output the me, the x and the y
    set me me + 1    ;   on to the next turtle
  ]
end
____________________________
Op donderdag 2 december 2021 om 19:43:44 UTC+1 schreef rbsoc...@gmail.com:

rbsoc...@gmail.com

unread,
Dec 2, 2021, 2:50:30 PM12/2/21
to netlogo-users
Thanks again.  I maybe complexifying this problem for myself more than I need to.  Let me try a simpler version of question. 

Assume five turtles and they are randomly assigned a value for a variable x.  If you use the command:

print [x] of turtles

The five values are printed out horizontally in the command center.  However, relative to their turtle id (who), the order appears to be random.  How can I print the values in their ascending who order?  In other words, value of x for turtle 0 first, value of x for turtle 1 second, and so on.

RB

James Steiner

unread,
Dec 2, 2021, 4:08:14 PM12/2/21
to rbsoc...@gmail.com, netlogo-users
One key concept in Netlogo is the difference between an agentset (such as a set of turtles) and a list (such as a list of turtles). Agentsets are always randomized.

So you want to convert the set of turtles into a list of turtles, then output a table of turtle values. Easy-peasy.

We will use SORT to get a list of turtles sorted by WHO (use SORT-ON or SORT-BY to sort  other ways).
We will use the OUTPUT primitives so the results go either to the command center output, or an OUTPUT control, if one exists.
We will use the WORD primitive to concatenate strings if needed.
We will use the OF primitive to get values from the turtles.

Note that NetLogo's outputs use proportional fonts, so there's very little hope of producing nicely lined up columns, and I won't try here--it will make the code overly complicated-looking.

There are two main approaches to this--one is to compose the  entire table in a sting, then use a single OUTPUT-PRINT command to send the table to the output area. The other is to send the parts of the table to the OUTPUT as we go. There are pros and cons for both. I think I will do the former method.

turtles-own [ x y ]
to test
  clear-all
  create-turtles 5
  [ set x random 100
    set y random 100
  ]
  output-table turtles
end

to output-table [ agents ]
let // " | "  ;; define a divider. // is a valid variable name.
let end-of-line "\n"  ;; to help me avoid constantly using the wrong slash, or a backtick or whatever.
let table (word // "who" // "x" // "y" // end-of-line)  ;; initialize with header.
foreach sort agents
  [ this ->
    ;; add row of values to the table
    set table [ (word table // who // x // y // end-of-line) ] of this
  ]
output-print table
end









--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/11334b0d-c031-435f-8055-6d05be0ee5a6n%40googlegroups.com.

James Steiner

unread,
Dec 2, 2021, 4:54:26 PM12/2/21
to rbsoc...@gmail.com, netlogo-users
Correction: The outputs use fixed-width fonts, so it's relatively easy to get lined up columns.

turtles-own [ x y ]
to test
  clear-all
  create-turtles 5
  [ set x random 100
    set y random 100
  ]
  output-table turtles
end

to-report  pad [ value ]
  ;; reports value padded on left to 5 characters with spaces.
  set value (word "     " value)
  report substring value (length value - 5) length value
end
 

to output-table [ agents ]
  let << "| "   ;; row start

  let // " | "  ;; define a divider. // is a valid variable name.
  let >> " |\n"   ;; row end, includes new-line character
  let line "-----"
  let table ""
  set table (word table << pad "who" // pad "x" // pad "y" >>)  ;; initialize with header.
  set table (word table << pad line // pad line // pad line >> )
  foreach sort agents
  [ this-agent ->

    ;; add row of values to the table
    set table [ (word table << pad who // pad x  // pad y >>) ] of this-agent
  ]
  output-print table
end



rbsoc...@gmail.com

unread,
Dec 3, 2021, 9:29:34 AM12/3/21
to netlogo-users
Thanks for table setup.  RB

Jim Lyons

unread,
Dec 3, 2021, 10:27:23 AM12/3/21
to netlogo-users, rbsoc...@gmail.com
On Dec 1, 2021, at 8:52 AM, rbsoc...@gmail.com <rbsoc...@gmail.com> wrote:
I have five turtles and two local variables with numeric values, call them x and y.  The variable values are manipulated in the program and change.  For internal checks I need to output the variable values at three points in the program.  So I would like to print out who, x, and y with these three labels vertically sorted by who in ascending order.  In other words, I want to output a table with three columns and five rows and who, x , and y labels for the columns.  What is the best way to do this?

Several good suggestions have been made. Since NetLogo doesn’t have much in the way of data formatting built in, making a little table requires a little bit of coding. If you just needed to see the values you might do something like

foreach sort turtles [ t -> ask t [ show list x y ] ]  ; show identifies the turtles

But your question reminded me of something I did a while back. I found it and updated it to NL6. It converts a list of lists to a formatted table using the list primitives. Maybe overkill for your application, but for what it’s worth:


; This function converts a list of lists to a
; multi-line string of right-justified columns.
; The column widths are set automatically. The
; input must be a list of same-length lists.
;
to-report table-format [ #row-list ]
  ; first convert all inner elements to strings
  set #row-list map [row -> map [x -> (word x)] row] #row-list

  ; set up list of widths needed for each column
  let $width-list n-values length first #row-list [4]  ; min col widths
  set $width-list reduce [ [p r] -> (map [ [u v] -> max list u (1 + length v) ] p r) ]
                         fput $width-list #row-list

  ; report converted rows joined with new-lines
  report reduce [ [r1 r2] -> (word r1 "\n" r2) ]
                map [ row -> row-format row $width-list ] #row-list
end

; Given a list of strings and a list of widths, this function
; concatenates the strings with each one right-justified
; in a field of the corresponding width.
;
to-report row-format [ #row #width-list ]
  report reduce word (map [ [r w] -> right-just r w] #row #width-list)
end

; This function reports a string of the given width
; by padding the given string on the left with spaces.
;
to-report right-just [ #string #width ]
  report word n-spaces (#width - length #string) #string
end

to-report n-spaces [ #number ]  ;= string of given number of spaces
  report reduce word n-values #number [" "]
end


Use it like this:

turtles-own [ x-val y-val ]

to test
  ca
  crt 5 [
    set x-val precision random-float 20 2
    set y-val precision random-float 20 2
  ]
  let results [ ["WHO" "X-val" "Y-val"] ]
  foreach sort turtles [ t ->
    set results lput [(list who x-val y-val)] of t  results
  ]
  print table-format results
end


Jim

James Steiner

unread,
Dec 3, 2021, 10:29:01 AM12/3/21
to Jim Lyons, netlogo-users, rbsoc...@gmail.com
Nice. 

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages