在 AnyLogic 中的多段线中随机放置预定数量的代理
Placing a pre-determined number of agents in random places in a polyline in AnyLogic
我有一个模型,我在其中将代理放置在不同折线形状内的随机位置。这很好用。我使用以下函数,它在我代理的 'On startup' 字段中调用:
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 ) );
虽然代码编译没有错误,但我没有看到我的任何代理出现在这些折线形状中。谁能帮帮我?
我已经写了一段代码来完成我想要的。 v_counter
是全局的
Main 中的变量,用于跟踪生成的代理数。参数 p_CountAgentsFirstShape
和 p_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循环的内容是减少重复的首选。
我有一个模型,我在其中将代理放置在不同折线形状内的随机位置。这很好用。我使用以下函数,它在我代理的 'On startup' 字段中调用:
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 ) );
虽然代码编译没有错误,但我没有看到我的任何代理出现在这些折线形状中。谁能帮帮我?
我已经写了一段代码来完成我想要的。 v_counter
是全局的
Main 中的变量,用于跟踪生成的代理数。参数 p_CountAgentsFirstShape
和 p_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循环的内容是减少重复的首选。