Reputation: 321
I have a model in which I place agents in a random locations within different polyline shapes. This works fine. I use the following function, which is called in the 'On startup' field of my agent:
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 ) );
The above code will place different amount of agents in each polyline for different runs. This is of course, expected. However, I would like to place a specific number of agents in each polyline at random locations. I am not very familiar with do-while loops so I have not found a solution myself. I tried including a counter like this:
int counter = 0;
if(counter < 100) ///
do {
x = uniform(0, 100 );
y = uniform(0, 100 );
counter++;
} while( ! pl_FirstShape.contains( x, y ) );
While the code compiles without error, I do not see any of my agents appear in these polyline shapes. Who can help me out?
Upvotes: 0
Views: 118
Reputation: 321
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.
Upvotes: 0