2015-02-23 25 views
2

我试图在杀死一只乌龟后恢复一个初始状态(回到它的第一个状态)的龟和链接。 我一直在尝试从http://ccl.northwestern.edu/netlogo/docs/nw.html的解决方案,但它不起作用。 下面是我的代码如何存储要在netlogo中恢复的网络环境?

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 

我一直在使用“NW:设置情境老乌龟老链接”从的NetLogo文档中,但似乎原来的乌龟和链接方面,我存储在“OLD-无论我如何储存它们,乌龟旧链接“都将被有目的地改变。我在想如果[死]功能改变存储的代理集?当我杀死节点时,旧海龟和旧链接的大小逐渐变小。我没有将新的环境存储回老龟和旧链接。

或者没有人有其他方式来存储旧的代理集和链接并恢复到其原始网络结构?

感谢您阅读。

回答

1

杀死一只乌龟确实将其从所有代理组中删除,因此恢复上下文不会使其恢复。你可以尝试从背景中移除乌龟而不是杀死它。你可以隐藏乌龟及其链接以重现杀死它的视觉效果。这将是这样的:

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

这样,乌龟是从您的计算目的的情况下删除,但没有杀死,所以它的结构信息被记住。 nw:with-context为你处理存储和恢复上下文,但是这段代码在没有它的情况下也能正常工作(你只需要自己恢复上下文)。

+0

感谢您的回答,以及您对杀死龟的验证,这也会将其从所有代理商中移除。非常有趣的属性。稍后我会尝试使用“隐藏”功能。目前,我正在使用一种非常暴力的方法来恢复我的所有节点和链接。 – user1142930 2015-02-23 19:00:33