Placing a pre-determined number of agents in random places in a polyline in AnyLogic

I have written a piece of code that does what I want. v_counter is a global variable in Main that keeps track of the number of agents generated. Parameters p_CountAgentsFirstShape and p_CountAgentsSecondShape are also in Main and contain the number of agents I want in each shape.

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 );

Of course, functionizing the contents of the do-while loop is preferred to reduce repitition.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top