`save-matrix` should order rows by the corresponding turtles `who` number
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.
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
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
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
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
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
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 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
@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
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.
@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 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 Oh right. Thank you!