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

AnyLogic中CustomDistribution()函数为何无法使用?

问题描述

此前在AnyLogic中通过CustomDistribution()函数,从TableFunction编程生成随机值,用以下代码验证TableFunction是否正常工作——抽取10000个样本并保存到CSV文件:

String data="";
Random r=new Random(1);
CustomDistribution n=new CustomDistribution(tableFunction,r);
for(int i=0;i<10000;i++){
    data+=n.get()+"\n";
}
try{
    Files.write(Paths.get("files.csv"), data.getBytes());
}catch(IOException e){
    e.printStackTrace();
}

运行代码时提示CustomDistribution()函数不存在,查阅AnyLogic帮助文档及网络未找到解决方案。

解决方案

1. 补全类的完整路径

AnyLogic中的CustomDistribution属于引擎核心包,需确保使用完整类名调用,或提前导入对应类:

  • 直接使用完整类名:
com.anylogic.engine.distributions.CustomDistribution n = new com.anylogic.engine.distributions.CustomDistribution(tableFunction, r);
  • 先添加导入语句(在代码块最顶部):
import com.anylogic.engine.distributions.CustomDistribution;

2. 适配AnyLogic版本

不同版本的AnyLogic对分布类的定义有差异:

  • 若使用AnyLogic 8及以上版本,需确保模型中已添加分布库(在左侧Palette面板的「Analysis」→「Distributions」中拖拽任意分布到画布,确保引擎相关类被加载)。
  • 若使用旧版本,可尝试替换为TableDistribution类(部分早期版本中从TableFunction生成随机值的类为TableDistribution):
TableDistribution n = new TableDistribution(tableFunction, r);

3. 优化代码性能(额外建议)

原代码通过字符串拼接生成大文本效率极低,建议改用StringBuilder:

StringBuilder data = new StringBuilder();
Random r = new Random(1);
com.anylogic.engine.distributions.CustomDistribution n = new com.anylogic.engine.distributions.CustomDistribution(tableFunction, r);
for(int i=0;i<10000;i++){
    data.append(n.get()).append("\n");
}
try{
    Files.write(Paths.get("files.csv"), data.toString().getBytes());
}catch(IOException e){
    e.printStackTrace();
}

4. 更简洁的替代方案

AnyLogic的TableFunction本身提供了随机采样方法,无需额外创建分布类:

StringBuilder data = new StringBuilder();
Random r = new Random(1);
for(int i=0;i<10000;i++){
    data.append(tableFunction.sample(r)).append("\n");
}
try{
    Files.write(Paths.get("files.csv"), data.toString().getBytes());
}catch(IOException e){
    e.printStackTrace();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 00:02:46