[英]Export data from Anylogic database
I have been struggling with model data exportation using Anylogic.我一直在努力使用 Anylogic 导出模型数据。
Some context.一些上下文。 When running a parameter variation experiment (several iterations and replicates) using parallelization, I haven't found a systematic way to collect the data to analyze them using Python or R. So, I want just to run experiments and save the output in a custom location (folder).在使用并行化运行参数变化实验(多次迭代和复制)时,我还没有找到一种系统的方法来收集数据以使用 Python 或 R 分析它们。因此,我只想运行实验并将输出保存在自定义位置(文件夹)。
What I have tried so far:到目前为止我尝试过的:
text file
Anylogic features, but they didn't work with a parallel setup (eg, not all rows were recorded)首先,我尝试了text file
Anylogic 功能,但它们不适用于并行设置(例如,并非所有行都被记录)try {
ResultSet rs = selectResultSet("SELECT * from MODEL_PARAMETERS");
CSVWriter csvWriter = new CSVWriter(new FileWriter("output/model_parameters.csv"), '\t');
csvWriter.writeAll(rs, true);
csvWriter.close();
deleteFrom(model_parameters).execute();
} catch (IOException e) {
getEngine().pause();
traceln("--> An Exception happened during initialization, continue? ...");
e.printStackTrace();
}
I am getting this error:我收到此错误:
The method writeAll(Iterable<String[]>, boolean) in the type CSVWriter is not applicable for the arguments (ResultSet, boolean)
The ResultSet is an interface: ResultSet 是一个接口:
https://help.anylogic.com/index.jsp?topic=%2Fcom.anylogic.help%2Fhtml%2Fjavadoc%2Fcom%2Fanylogic%2Fengine%2Fconnectivity%2FResultSet.html https://help.anylogic.com/index.jsp?topic=%2Fcom.anylogic.help%2Fhtml%2Fjavadoc%2Fcom%2Fanylogic%2Fengine%2Fconnectivity%2FResultSet.html
The selectResultSet
is: selectResultSet
是:
selectResultSet
public ResultSet selectResultSet(java.lang.String sql,
java.lang.Object... params)
Get the results as a result set object for the given sql and params
Parameters:
sql - string containing select query
params - array containing select query params
Returns:
ResultSet selected ResultSet
Any ideas on how to deal with this?关于如何处理这个问题的任何想法? Thanks!谢谢!
To write dbase records to a csv, use this setup:要将 dbase 记录写入 csv,请使用以下设置:
Create a "Text file" element and setup as below:创建一个“文本文件”元素并设置如下:
Then, use the code below when the Experiment ends, ie last iteration's last replication is done.然后,在实验结束时使用下面的代码,即完成最后一次迭代的最后一次复制。 Obviously adjust headers for your table structure:显然调整表结构的标题:
File outputDirectory = new File("outputs");
outputDirectory.mkdir();
String outputFileNameWithExtension = outputDirectory.getPath()+File.separator+"output_operations.csv";
file.setFile(outputFileNameWithExtension, Mode.WRITE_APPEND);
// create header
file.println( "unique_id"+","+"replication");
// Write data from dbase table
List<Tuple> rows = selectFrom(output_operations).list();
for (Tuple row : rows) {
file.println( row.get( output_operations.unique_id ) + "," +
row.get( output_operations.replication ));
}
file.close();
For now, the only thing that has worked for me is to create as many CSV files as iteration and replicates I have in my experiment, so, if I have 10 iterations with 100 replicates each, I will get 1000 files CSV per dataset I want to collect.目前,唯一对我有用的是创建与我在实验中进行的迭代和复制一样多的 CSV 文件,因此,如果我有 10 次迭代,每次有 100 次复制,我将获得 1000 个我想要的每个数据集的 CSV 文件去收集。
To make this work better, write your output data into the model dbase at runtime first, then export into 1 csv at the very end of all runs.为了使这项工作更好,首先在运行时将您的输出数据写入模型数据库,然后在所有运行的最后导出到 1 csv。
In your output dbase tables, have columns for "iteration" and "replication" numbers to ensure every record is absolutely unique (otherwise, some data is lost in parallel runs as you observed as well).在您的输出 dbase 表中,有“迭代”和“复制”数字的列,以确保每条记录都是绝对唯一的(否则,正如您所观察到的那样,一些数据在并行运行中也会丢失)。
This works and is very reliable (and fast), it really boils down to ensuring that any dbase record done in any parallel run is written into a unique dbase record.这有效并且非常可靠(且快速),它实际上归结为确保在任何并行运行中完成的任何 dbase 记录都写入唯一的 dbase 记录。 You can use getCurrentReplication()
and getCurrentIteration()
at runtime to ensure data is writtne correctly...您可以在运行时使用getCurrentReplication()
和getCurrentIteration()
来确保正确写入数据...
This is working for me now, just a function with two parameters: query and filename这现在对我有用,只是一个带有两个参数的函数:查询和文件名
Here a more general explanation: https://sdaza.com/blog/2020/anylogic-database/这里有一个更一般的解释: https : //sdaza.com/blog/2020/anylogic-database/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.