【问题标题】:Placing a pre-determined number of agents in random places in a polyline in AnyLogic在 AnyLogic 中将预定数量的代理放置在折线中的随机位置
【发布时间】:2021-02-16 23:11:53
【问题描述】:

我有一个模型,我将代理放置在不同折线形状内的随机位置。这工作正常。我使用以下函数,该函数在我的代理的“启动时”字段中调用:

double rand = uniform(1);
double x;
double y;

if(rand <=  0.5) /// 
    do {
        x = 0, 100 );
        y = 0, 100 );
    } while( ! pl_FirstShape.contains( x, y ) );
else if(rand > 0.5) /// 
    do {
        x = uniform( 0, 100 );
        y = uniform( 0, 100 );
    } while( ! pl_SecondShape.contains( x, y ) );

以上代码将在每条折线上放置不同数量的代理以进行不同的运行。这当然是意料之中的。但是,我想在随机位置的每条折线上放置特定数量的代理。我对 do-while 循环不是很熟悉,所以我自己还没有找到解决方案。我尝试包括这样的计数器:

int counter = 0;
if(counter < 100) /// 
    do {
        x = uniform(0, 100 );
        y = uniform(0, 100 );
        counter++;
    } while( ! pl_FirstShape.contains( x, y ) );

虽然代码编译没有错误,但我没有看到我的任何代理出现在这些折线形状中。谁能帮帮我?

【问题讨论】:

    标签: anylogic


    【解决方案1】:

    我已经写了一段代码来做我想做的事。 v_counter 是一个全球性的 Main 中的变量,用于跟踪生成的代理数量。参数 p_CountAgentsFirstShapep_CountAgentsSecondShape 也在 Main 中,包含我想要在每个形状中的代理数量。

    double x = 0;
    double y = 0;
    
    if(v_counter <= p_CountAgentsFirstShape) {
        do {
            x = uniform( 0,100 );
            y = uniform(0, 100);
            if(pl_FirstShape.contains(x, y)) {
                v_counter = v_counter + 1;
                }
            } while( ! pl_FirstShape.contains( x, y ) );
    }
    else if(v_counter >= p_CountAgentsFirstShape & v_counter <= p_CountAgentsSecondShape){ 
        do {
            x = uniform(0, 100);
            y = uniform(0, 100);
            if(pl_SecondShape.contains(x, y)) {
                v_counter= v_counter+ 1;
                }
        } while( ! pl_SecondShape.contains( x, y ) );
    }
    else
        do {
            x = uniform(0, 100);
            y = uniform(0, 100);
        } while( ! countrybounds.contains( x, y ) );
    
    agent.setXY( x, y );
    

    当然,最好将 do-while 循环的内容功能化以减少重复。

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多