Netlogo没有正确地将结果写入文件

时间:2017-12-15 14:59:09

标签: netlogo

我在Netlogo中运行模拟,编码将结果自动发送到电子表格。这发生在模拟结束时,以便保存每个个体的变量(例如,身份,家庭范围大小等)。电子表格结果通常看起来很好,但在使用BehaviorSpace时,有时候个别变量的数据打印不正确(例如,错误列中的数据或缺失,请参见下面的截图)。

我想知道在BehaviorSpace中并行运行期间,当模拟发生同时结束时,这会导致同时写入文件。这可能发生了什么?除了这些明显的问题之外,我是否应该合理地确信剩余的结果是否正确地打印在桌面上?最重要的是,如何避免这些错误的文件?示例代码如下。

奖金问题[已解决]:我认为打印日期和时间会识别独特的模拟运行,但事实并非如此,因为每个乌龟的变量都需要不止一瞬间打印到文件中。有没有快速的方法为每次模拟运行添加唯一标识符?

谢谢!

to start-output-file ;; Observer procedure, called from setup. 
  set-current-directory "C:\\... 
  file-open "Table_results.csv"
  ;; Define the names of the variables:
     file-type "Run,"
     file-type "Landscape,"
     file-type "Individual,"
     file-type "Home range size,"
     ;; ... and so on, ~60 results saved for each individual            
     file-print ""
     file-close ]
end

to end-simulation ;; Observer procedure, called from go.
  write-outputs
  file-close 
end

to write-outputs ;; Observer procedure, called from end-simulation.
  set-current-directory "C:\\...
  file-open "Table_results.csv"
  ask turtles
    [ file-type (word date-and-time ", ") ;;<---Would like a unique run identifier here instead if possible.
      file-type (word Landscape ", ")
      file-type (word Who ", ")
      file-type (word Home-range-size ", ")
      ;; ...
      file-print "" ]]    
end

enter image description here

1 个答案:

答案 0 :(得分:0)

更新/解决方案::我的解决方案是让每个运行的模型写入单独的CSV。这使我可以保存每个座席变量的结果。每个文件将具有唯一的标识符,因此以后不会被覆盖或添加到文件中。然后,可以使用程序R轻松导入和合并所有CSV。

示例代码::在设置过程中定义一个CSV文件名,并要求乌龟将其结果写入该文件:

extensions [ csv ]
globals [ filename ] ;; Will define as the date/time and BehaviorSpace run #.

to setup
  clear-all 
  let run-ID (remove-item 6 (remove-item 7 (remove-item 8 (remove "-"(remove " "(remove "." (remove ":" date-and-time)))))))
  set filename (word "Results, " run-ID ", " word behaviorspace-run-number".csv")
  file-open filename
  start-output-file
  ;; Etc.
end

to start-output-file ;; Called from setup.
   file-open filename
   file-type "Turtle identity,"
   file-type "Home range size,"
   ;; Etc., rest of column headers.
   file-print ""
end

to end-simulation ;; Observer procedure, called from go.
  ask turtles [ write-outputs ]
  file-close 
end

to write-outputs ;; Called from end-simulation.
  file-type (word Who ", ") 
  file-type (word home-range-size ", ") ;; (Turtle variable for home range size.)
  ;; Etc., i.e., each turtle variable you are writing to file.
  file-print ""
end

希望这可以帮助其他希望做类似事情的人,或者注意到来自BehaviorSpace的CSV输出中出现类似奇怪情况的人。