[英]Anylogic: how to Batch agents with similar parameters?
我有一个名为 products 的代理,在这个代理中,我分配了一个名为sp的参数; 在模拟中,我有相同的代理,其 sp 范围从 1 到 5。我想在同一批次中对具有相同sp的代理进行批处理,即 10。因此,如果我有 200 个代理,其中 49 个具有sp等于 1,我想将它们分成 5 个批次(10、10、10、10、9), sp等于 2 和另一批次,依此类推。
我真的很感激你能提供的任何帮助。
有很多方法可以做到这一点,但考虑到你有不一致的批次(即你有 10 个批次,然后最后一个是 9 个批次)我首先将所有代理放在一个wait
块中(或者您可以使用queue
)然后以编程方式控制它们
举下面的小例子
我一直等到我的 200 个“产品”代理到达等待块,然后按下调用 function batchReleaseCheck()
的“检查批次”按钮
这是代码
LinkedHashMap<Integer, List<Product>> productsWaiting = new LinkedHashMap<Integer, List<Product>>();
for (int i = 0; i < wait.size(); i ++){
Product product = wait.get(i);
int sp = product.sp;
if (!productsWaiting.containsKey(sp)) productsWaiting.put(sp, new ArrayList<Product>());
productsWaiting.get(sp).add(product);
//Check the batch size if sufficient we release it
if (productsWaiting.get(sp).size() == batchSize) {
for (Product p:productsWaiting.get(sp)) {
wait.free(p);
}
return; // we exit the loop since we have released a batch
}
}
// If we get to the end of the loop we were not able to release any batch we now release each SP type regardless of their current batch size
for (int i = 1; i < 6; i ++) {
if (productsWaiting.get(i) == null) continue;
for (Product p:productsWaiting.get(i)) {
wait.free(p);
}
batch.set_batchSize(productsWaiting.get(i).size()); //Since the batch is less than the standard we need to change it to what ever we are releasing
return; // we exit the loop since we have released a batch
}
您创建一个 map 以根据产品的sp
编号将产品存储在列表中。 如果您在任何时候发现您有足够的单位来创建一个批次,我们将停止并将它们从等待块中释放。
如果我们到达 for 循环的末尾并且我们没有任何 sp 编号有足够的单位来组成整个批次,我们简单地释放我们拥有的。
在此示例中,您需要每次单击按钮才能发布批次,或者您可以将批次检查 function 调用添加到批次 object 的发布代码中。这将按顺序发生,但同时执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.