NW-Extension icon indicating copy to clipboard operation
NW-Extension copied to clipboard

`save-matrix` should order rows by the corresponding turtles `who` number

Open qiemem opened this issue 10 years ago • 12 comments

Currently, we're storing the turtles data structure which dictate the order in which the rows are generated in a set, so there is no guaranteed order.

qiemem avatar Mar 20 '15 15:03 qiemem

So can whatever order the matrix is in be ouput too? So the columns/rows might be in a random order, but we'd know what that order is.

Thanks,

Thomas

ThomasCNotts avatar Mar 23 '15 14:03 ThomasCNotts

I'd like to make the rows order by who number so it won't even be an issue. By the way, in case you haven't seen it, I created this issue after this question on stack overflow: https://stackoverflow.com/questions/29152481/netlogo-nwsave-matrix-ordering-of-the-nodes/29170170

I also posted a workaround there, but will put it here as well for convenience:

to save-matrix [ filename ]
  if file-exists? filename [ file-delete filename ]
  file-open filename
  let turtle-list sort turtles
  foreach turtle-list [
    let source ?
    foreach turtle-list [
       let target ?
       ifelse [ link-neighbor? target ] of source [
         file-type "1 "
       ] [
         file-type "0 "
       ]
    ]
    file-print ""
  ]
  file-close
end

qiemem avatar Mar 23 '15 17:03 qiemem

Thank you that's really useful.

How would you get output of a turtle variable in the same order please? For instance, if I wanted to output the colour of each turtle so they came out in the same order as the adjacency matrix?

Thank you,

Thomas

ThomasCNotts avatar Mar 24 '15 10:03 ThomasCNotts

Editing what you did I came up with the following. I think it works.

  file-open "colordat.csv"
  let turtle-list sort turtles
  foreach turtle-list [
    let target ?
    file-print [color] of target
  ]
  file-close  

ThomasCNotts avatar Mar 24 '15 10:03 ThomasCNotts

Yup, that's about right. You could simplify like so:

to save-colors [ filename ]
  file-open filename
  foreach sort turtles [ file-print [ color ] of ? ]
  file-close
end

qiemem avatar Mar 25 '15 02:03 qiemem

Hi. I am having the same issue right now, and I tried what you did. But it doesnt work maybe due to netlogo version update? To be more specific, netlogo reply "nothing name ? has been defined." at the code "let source ?" and "let target ?".

hariWong avatar Oct 24 '22 10:10 hariWong

@hariWong Yeah, that's the old task syntax from before the anonymous procedure changes were done in version 6. Try this instead:

LaCuneta avatar Oct 24 '22 13:10 LaCuneta

@LaCuneta Thank you so much. But actually I am having questions with the code below:

to save-matrix [ filename ] if file-exists? filename [ file-delete filename ] file-open filename let turtle-list sort turtles foreach turtle-list [ let source ? foreach turtle-list [ let target ? ifelse [ link-neighbor? target ] of source [ file-type "1 " ] [ file-type "0 " ] ] file-print "" ] file-close end

Still, MANY thanks.

hariWong avatar Nov 02 '22 13:11 hariWong

@hariWong

Gotcha, okay, applying that same conversion process in the Transition Guide to that code we get:

I didn't test that, but I think it's correct.

LaCuneta avatar Nov 02 '22 14:11 LaCuneta

@LaCuneta Thank you! It worked! Now that save-matrix can be in order rows by the corresponding turtles who number, I am thinking if it is possible to support link weights, since "save-matrix does not support link weights" as the user manual said.

Here's some of my code:

undirected-link-breed [ss s]
undirected-link-breed [os o]
directed-link-breed [as a]
undirected-link-breed [sos so]
undirected-link-breed [oas oa]
links-own [weight]

……

to save-weighted-matrix
  file-open file-name
  foreach sort turtles [ [source] ->
    foreach sort turtles [ [target] ->
      ifelse [ link-neighbor? target ] of source [
        file-type [weight] of (link [who] of source [who] of target)
       ] [
         file-type "0 "
       ]
    ]
    file-print ""
  ]
  file-close
end

Netlogo reported--OF expected input to be a link agentset or link but got NOBODY instead. I am not sure how to revise it, or is it just not working anyway and I better wait for nw extension update.

hariWong avatar Nov 04 '22 13:11 hariWong

@hariWong I think we're getting a little off-topic for this issue. I'd recommend posting your problem with the code you wrote to the NetLogo Users Group or StackOverflow with the [netlogo] tag if you're more comfortable using that site. I also reply to posts on both of those sites.

LaCuneta avatar Nov 04 '22 13:11 LaCuneta

@LaCuneta Oh right. Thank you!

hariWong avatar Nov 04 '22 14:11 hariWong