【问题标题】:Anylogic: how to Batch agents with similar parameters?Anylogic:如何批处理具有相似参数的代理?
【发布时间】:2021-10-24 22:02:15
【问题描述】:

我有一个叫 products 的代理,在这个代理中,我分配了一个叫 sp 的参数;在模拟中,我有相同的代理,其 sp 范围从 1 到 5 不同。我想在同一批次中批处理具有相同 sp 的代理,即 10。所以如果我有 200代理,其中 49 个 sp 等于 1,我想将它们分成 5 个批次(10,10,10,10,9),并且 sp 等于 2另一个批次等等。

非常感谢您提供的任何帮助。

【问题讨论】:

    标签: java simulation anylogic


    【解决方案1】:

    有多种方法可以做到这一点,但考虑到批次不一致(即批次为 10,最后一个批次为 9),我首先将所有代理放入其中wait 块(或者您可以使用 queue)然后以编程方式控制它们

    举个小例子

    我等到我的 200 个“产品”代理到达等待区,然后按下调用函数 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
    } 
    
    

    您创建一个地图以根据产品的sp 编号将产品存储在列表中。如果在任何时候你发现你有足够的单位来创建一个批次,我们会停止并从等待块中释放它们。

    如果我们到了 for 循环的末尾,并且我们没有任何 sp 编号有足够的单位来组成一整批,我们简单地释放我们所拥有的。

    在此示例中,您需要每次单击按钮来释放批次,或者您可以将批次检查函数调用添加到批次对象的发布代码中。这一切都将顺序发生,但在同一时间步骤

    【讨论】:

    • 你应该可以在等待块上调用命令freeAll(),它们都会被释放。在释放 batch.set_batchSize(wait.size()); 之前,您只需将批处理大小设置为等于等待块中的项目数
    • 您好,Jaco,我还有一个问题。如何根据 sp 批量处理不同的批量大小?例如,如果 sp = 1,则批量大小为 20,如果 sp = 2,则批量大小为 7,依此类推。批量大小是固定的,没有发布,因此省略了最后一个 for 循环。非常感谢,我很感激。
    • 您可以设置一个地图,您可以在其中查找SP编号并获得相应的批量大小LinkedHashMap&lt;Integer, Integer&gt;开始一个新问题以获得更详细的答案和示例。虽然这是标准 Java ;-)
    • 谢谢,雅科。我在这里link 开始了一个新问题,我在第二个问题中尝试了Felipe 的解决方案,但它对我不起作用。我对Java很陌生;这就是原因之一。如果您能提供帮助,我想请您看看另一个问题。非常感谢。
    猜你喜欢
    • 2021-10-25
    • 2019-02-11
    • 2014-12-13
    • 2016-12-05
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多