Netlogo 没有将结果正确写入文件
Netlogo not writing results to file correctly
我正在 运行使用 Netlogo 进行模拟,使用编码将结果自动发送到电子表格。这发生在模拟结束时,以保存每个人的变量(例如,身份、家庭范围大小等)。电子表格结果正常情况下看起来不错,但在使用 BehaviorSpace 时,偶尔无法正确打印来自个人变量的数据(例如,错误列中的数据或丢失的数据,请参见下面的屏幕截图)。
我想知道在 BehaviorSpace 中并行 运行s 期间,当模拟碰巧同时结束时,是否会导致同时写入文件。这可能是怎么回事?除了这些明显的问题之外,我是否应该有理由相信剩余的结果已正确打印到 table?最重要的是,我怎样才能避免这些文件打印错误?示例代码如下。
奖金问题[已解决]:我认为打印日期和时间会识别唯一的模拟运行s,但事实并非如此,因为每只乌龟的变量都需要超过一瞬间的时间才能打印到文件中.有没有一种快速的方法可以为每个模拟添加唯一标识符运行?
谢谢!
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
Update/Solution: 我的解决方案是让每个模型 运行 写入单独的 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 中有类似的奇怪之处。
我正在 运行使用 Netlogo 进行模拟,使用编码将结果自动发送到电子表格。这发生在模拟结束时,以保存每个人的变量(例如,身份、家庭范围大小等)。电子表格结果正常情况下看起来不错,但在使用 BehaviorSpace 时,偶尔无法正确打印来自个人变量的数据(例如,错误列中的数据或丢失的数据,请参见下面的屏幕截图)。
我想知道在 BehaviorSpace 中并行 运行s 期间,当模拟碰巧同时结束时,是否会导致同时写入文件。这可能是怎么回事?除了这些明显的问题之外,我是否应该有理由相信剩余的结果已正确打印到 table?最重要的是,我怎样才能避免这些文件打印错误?示例代码如下。
奖金问题[已解决]:我认为打印日期和时间会识别唯一的模拟运行s,但事实并非如此,因为每只乌龟的变量都需要超过一瞬间的时间才能打印到文件中.有没有一种快速的方法可以为每个模拟添加唯一标识符运行?
谢谢!
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
Update/Solution: 我的解决方案是让每个模型 运行 写入单独的 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 中有类似的奇怪之处。