You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Anylogic技术求助:如何读取41×17矩阵的全部数值

解决AnyLogic读取多维矩阵的实操方案

一、数据库导入后的矩阵结构化处理

假设你已将矩阵数据导入AnyLogic内置数据库,先确保数据表结构匹配矩阵的行、列、值:

  • 表字段建议设为:rowIndex(行索引,可从0或1开始)、colIndex(列索引)、value(对应位置的数值)
  • 比如距离矩阵的表命名为distanceMatrix,发货量矩阵表命名为volumeMatrix

二、用ArrayList构建二维矩阵

不要直接让参数绑定整张表,通过代码初始化二维ArrayList:

  1. 新建参数,类型设为ArrayList<ArrayList<Double>>(整数数值则用Integer),例如命名为distanceMatrix
  2. 在模型的On startup代码块中写入初始化逻辑:
// 初始化外层ArrayList
distanceMatrix = new ArrayList<>();
// 获取数据库中最大行、列索引
int maxRow = selectMaxFrom(distanceMatrix, rowIndex);
int maxCol = selectMaxFrom(distanceMatrix, colIndex);

// 遍历每一行
for(int i=1; i<=maxRow; i++){
    ArrayList<Double> row = new ArrayList<>();
    // 遍历该行的每一列
    for(int j=1; j<=maxCol; j++){
        // 查询对应行列的数值
        Double val = selectFrom(distanceMatrix)
                        .where(rowIndex == i && colIndex == j)
                        .firstResult(value);
        row.add(val != null ? val : 0.0); // 空值补0避免报错
    }
    distanceMatrix.add(row);
}

三、调用矩阵数值的正确方式

注意ArrayList索引从0开始,若数据库索引从1开始,需做减1转换:

// 示例:获取第3行第5列的距离值
double targetDistance = distanceMatrix.get(2).get(4);

四、固定尺寸矩阵的替代方案:二维数组

由于你的矩阵是固定的41×17,也可直接用二维数组提升性能:

  1. 新建参数,类型设为double[][],命名为distanceArray
  2. 在On startup中初始化:
distanceArray = new double[41][17];
List<distanceMatrixRecord> records = selectAllFrom(distanceMatrixRecord);
for(distanceMatrixRecord rec : records){
    // 转换索引(数据库索引从1开始时)
    int row = rec.rowIndex - 1;
    int col = rec.colIndex - 1;
    distanceArray[row][col] = rec.value;
}

调用时直接用distanceArray[row][col]即可。

五、常见问题排查

  • 核对数据库索引与代码遍历逻辑的起始值(0或1)是否一致
  • 确保数据库中无缺失的行列值,否则会触发NullPointerException
  • 若从Excel导入矩阵,需勾选「跳过表头」避免表头被识别为数据行

内容的提问来源于stack exchange,提问作者MysterL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:46:01