如何在AnyLogic中为Other>double[]类型变量赋值并计算90分位数?
解决方法
1. 修复变量添加元素报错
你用的double[]是Java固定长度数组,没有add()方法,这就是报错的核心原因。把变量类型换成ArrayList<Double>(AnyLogic里在变量类型选择Collection > ArrayList<Double>),就能正常用add()动态添加元素:
// 假设变量名为delayRecords delayRecords.add(time() - agent.downEndTime);
2. 排序并计算90分位数
步骤1:排序数据
直接对ArrayList排序即可:
Collections.sort(delayRecords);
步骤2:计算90分位数
分位数计算有两种常用方式,按需选择:
取对应位置元素(简单直接)
计算索引(注意数组/集合索引从0开始):int p90Index = (int) Math.ceil(0.9 * delayRecords.size()) - 1; double p90Value = delayRecords.get(p90Index);线性插值(精度更高)
适合需要更精确分位值的场景:double pos = 0.9 * (delayRecords.size() - 1); int lowerIndex = (int) Math.floor(pos); double fraction = pos - lowerIndex; double p90Value = delayRecords.get(lowerIndex) + fraction * (delayRecords.get(lowerIndex + 1) - delayRecords.get(lowerIndex));
内容的提问来源于stack exchange,提问作者Jahidul Hasan Razib
相关产品推荐
相关产品推荐

