简体   繁体   English

从 Anylogic 数据库导出数据

[英]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:到目前为止我尝试过的:

  • First, I tried the text file Anylogic features, but they didn't work with a parallel setup (eg, not all rows were recorded)首先,我尝试了text file Anylogic 功能,但它们不适用于并行设置(例如,并非所有行都被记录)
  • I tried using databases and then exporting the data to Excel.我尝试使用数据库,然后将数据导出到 Excel。 But I had the problem of Excel size limitation (about 1M rows).但是我遇到了 Excel 大小限制(大约 1M 行)的问题。 I am exploring several iterations and replicates so that Excel files wouldn't work.我正在探索多次迭代和复制,以便 Excel 文件不起作用。
  • I have been trying to connect to the database using R and Python without success.我一直在尝试使用 R 和 Python 连接到数据库,但没有成功。 Still, I would need some wrapper to convert the tables into a format I can use with R or Python.尽管如此,我还是需要一些包装器来将表转换为我可以在 R 或 Python 中使用的格式。 That would need to be done within Anylogic, so every experiment data are saved into a given folder.这需要在 Anylogic 中完成,因此每个实验数据都保存在给定的文件夹中。
    • Connect to the database using Python使用 Python 连接到数据库
    • Read the data and convert them to a format I can use independently, let's say a CSV file.读取数据并将它们转换为我可以独立使用的格式,比如说一个 CSV 文件。
  • 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 文件去收集。
  • Another option would be to convert a database (query) into a csv file using Java.另一种选择是使用 Java 将数据库(查询)转换为 csv 文件。 Before the experiment starts I clear all the databases.在实验开始之前,我清除了所有数据库。 At the end of the experiment, I would like to save the data, and clear the databases, running this code:在实验结束时,我想保存数据,并清除数据库,运行以下代码:
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM