Reputation: 73
I am trying to restore an initial(back to its first state) context of turtles and link after killing one turtle. I have been trying the solution from http://ccl.northwestern.edu/netlogo/docs/nw.html, but somehow it doesn't work. Below is my code
to cycle
if Measure = "Efficiency of network"
[ ;store node and link
nw:set-context turtles with [ shape = "circle" ] links with [ color = blue ]
show map sort nw:get-context
set old-turtles item 0 nw:get-context
show old-turtles
set old-links item 1 nw:get-context
show old-links
;start process
process-performance
]
end
to process-performance
if NumberOfNodes = 1
[file-open "1node.txt"
while [not file-at-end?]
[
;calculate initial performance value
set initial nw:mean-path-length
show initial
let nodeseq read-from-string (word "[" file-read-line "]")
show item 0 nodeseq
ask turtle (item 0 nodeseq) [ die ]
update-plots
;calculate new performance value
set final nw:mean-path-length
show final
set result (1 - (final / initial)) * 100
show result
nw:set-context old-turtles old-links
show map sort nw:get-context
]
file-close
]
end
I have been using "nw:set-context old-turtles old-links" in the documentation from netlogo but it seems that the original turtle and link context I store in "old-turtles old-links" will be purposefully altered no matter how I store them. I am thinking if [die] function alter the agent-set stored? The old-turtles and old-links are progressively smaller in size as I kill the node. I did not store the renewed context of the nw back to old-turtles and old-links.
Or does anyone have other ways in storing the old agent-set and link and restoring back to its original network structure?
Thanks for reading through.
Upvotes: 2
Views: 116
Reputation: 12580
Killing a turtle does indeed remove it from all agentsets, so restoring the context won't bring it back. You might try removing the turtle from the context rather than killing it. You could hide the turtle and its links to reproduce the visuals of killing it as well. This would be something like:
...
let target-turtle turtle (item 0 nodeseq)
ask target-turtle [
hide-turtle
ask my-links [ hide-link ]
]
nw:with-context (remove turtle (item 0 nodeseq) old-turtles) old-links [
update-plots
;calculate new performance value
set final nw:mean-path-length
show final
set result (1 - (final / initial)) * 100
show result
]
...
This way, the turtle is removed from the context for the purpose of your calculations, but not killed, so its structural information is remembered. nw:with-context
handles storing and restoring the context for you, but this code works just as well without it (you just have to restore the context yourself).
Upvotes: 1