Anylogic中CustomDistribution无法解析为类型的问题求助
AnyLogic配送中心云模型:CustomDistribution无法解析类型问题
问题说明
在学习AnyLogic配送中心云模型时,Distribution Center/Main/discrete-Function里触发错误:
CustomDistribution cannot be resolved to a type
出错的函数代码如下:
int[] vals = new int[palletTypesNum+1]; for (int i = 0; i <= palletTypesNum; i++) vals[i] = i; CustomDistribution distribution = new CustomDistribution(vals, weights, new java.util.Random()); return (int)distribution.get();
已尝试的操作
- 按对应配置调整(相关配置截图略)
- 在代码导入区添加语句:
import com.anylogic.engine.markup.CustomDistribution;
解决办法
1. 修正类的导入路径
不同版本AnyLogic中,CustomDistribution的包位置可能存在差异,尝试替换为以下导入语句:
import com.anylogic.engine.distributions.CustomDistribution;
若仍无效,可通过AnyLogic的类搜索功能(快捷键Ctrl+Shift+T)查找CustomDistribution的实际包路径,再对应修改导入语句。
2. 用可视化组件替代代码实现
无需手动编写代码创建CustomDistribution,直接在模型中添加「Custom Distribution」可视化组件,配置好数值与权重后,在函数中调用该组件的get()方法即可。这种方式更贴合AnyLogic的可视化建模逻辑,也能避免类路径匹配问题。
3. 手动实现权重随机选择逻辑
如果上述方法均无效,可绕过CustomDistribution类,手动实现权重随机选择逻辑:
double totalWeight = 0; for(double w : weights) totalWeight += w; double rand = random() * totalWeight; double cumulative = 0; for(int i=0; i<weights.length; i++){ cumulative += weights[i]; if(rand <= cumulative){ return vals[i]; } } return vals[vals.length-1];
内容的提问来源于stack exchange,提问作者Jahidul Hasan Razib
相关产品推荐
相关产品推荐

