Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
qiemem opened this issue Mar 20, 2015 · 12 comments
Open

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

qiemem opened this issue Mar 20, 2015 · 12 comments

Comments

@qiemem
Copy link
Member

qiemem commented Mar 20, 2015

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.

@ThomasCNotts
Copy link

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

@qiemem
Copy link
Member Author

qiemem commented Mar 23, 2015

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

@ThomasCNotts
Copy link

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
Copy link

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  

@qiemem
Copy link
Member Author

qiemem commented Mar 25, 2015

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

@hariWong
Copy link

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 ?".

@LaCuneta
Copy link
Contributor

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

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

@hariWong
Copy link

hariWong commented Nov 2, 2022

@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.

@LaCuneta
Copy link
Contributor

LaCuneta commented Nov 2, 2022

@hariWong

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

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

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

@hariWong
Copy link

hariWong commented Nov 4, 2022

@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.

@LaCuneta
Copy link
Contributor

LaCuneta commented Nov 4, 2022

@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.

@hariWong
Copy link

hariWong commented Nov 4, 2022

@LaCuneta Oh right. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants