首页 > 解决方案 > Anylogic:使用参数丢弃给定数量

问题描述

我的模型中有一列火车,里面有不同的代理。在其中一个站点,我想放下每个代理的一部分。

我在火车上的代理人是:儿童、青少年、成人和golden_oldies。在给定的站点,我想下车:

20% 的儿童

40% 的青少年

在 Drop-off 代理中,我更改了以下输入:

Dropoff : 给定数量(如果有)

数量:children.size(0.2) + Teens.size(0.4)

点击这里查看下车属性

标签: javaanylogic

解决方案


为了实现这一点,您应该在您的火车接载代理之前为代理中的变量创建一个值。创建一个名为 willBeDropped 的变量,作为默认值 false 的布尔值。此变量应存在于您的 4 种代理类型中的每一种中。

所以当代理人被火车接走时,你应该为 willBeDropped 分配一个 true 或 false 的值......所以当火车到达给定的站点时,20% 的孩子将拥有willBeDropped=true80%的孩子willBeDropped=false

在dropoff中,您应该在条件为真时使用“while condition is true”agent.willBeDropped==true

我不知道你是怎么接乘客的,所以我不知道如何帮助你……你的问题不完整,但你可以自己弄清楚我刚才所说的。

但是,如果您有以下配置,其中每个人都被添加到火车一个唯一的时间:
在此处输入图像描述

您可以使用以下代码:

int num1=count(queue,q->q instanceof Children);
int num2=count(queue,q->q instanceof Teenager);

int numToDropOff1=(int)round(num1*0.2);
int numToDropOff2=(int)round(num2*0.4);

int counter1=0;
int counter2=0;

for(int i=0;i<queue.size();i++){
    if(counter1<= numToDropOff1 && queue.get(i) instanceof Children){
        queue.get(i).willBeDropped=true;
        counter1++;
    }else if(counter2<= numToDropOff2 && queue.get(i) instanceof Teenager){
        queue.get(i).willBeDropped=true;
        counter2++;
    }
}

推荐阅读