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

如何按指定概率为订单选择多个固定截止时间选项?

按概率规则设置订单截止时间

要实现17:45:00的选中概率为另外两个时间的两倍,可利用AnyLogic的randomDiscrete()函数按权重抽样,具体实现如下:

修改后的完整代码

Order order = new Order();
// 定义截止时间选项的权重和对应的时分值
double[] weights = {2, 1, 1}; // 17:45权重为2,另外两个时间权重为1
int[][] timeOptions = {{17, 45}, {19, 15}, {20, 15}}; // 对应三个目标时间的时、分

// 按权重随机选中一个时间选项
int selectedIndex = randomDiscrete(weights);
int selectedHour = timeOptions[selectedIndex][0];
int selectedMinute = timeOptions[selectedIndex][1];

// 生成并设置截止时间
Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR_OF_DAY, selectedHour); 
cal.set(Calendar.MINUTE, selectedMinute); 
cal.set(Calendar.SECOND, 0); 
cal.set(Calendar.MILLISECOND, 0); 
order.atrDueDate = cal.getTime();

// 订单加工时间设置
order.varProcTime = triangular(1,2,3);
order.markParametersAreSet();
enter1.take(order);

核心逻辑说明

  • 权重匹配概率:weights数组的数值对应各时间的选中权重,总权重为4,因此17:45的选中概率为50%,另外两个时间各为25%,完全符合需求。
  • 随机抽样实现:randomDiscrete(weights)函数会根据权重返回选中选项的索引,直接对应timeOptions里的时分组合。
  • 时间赋值:通过选中的索引取出目标时分,赋值给Calendar对象生成最终截止时间。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 20:55:17