[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: commas & turtle orders in StarLogo
- Date: Fri, 7 Nov 97 00:56:26 -0800
- From: Andrew Begel <abegel@alum.mit.edu>
- Subject: Re: commas & turtle orders in StarLogo
>My understanding is that when you place a comma between two consecutive
>StarLogo commands, like
>"fd 1" and "setc blue", this forces all of the turtles to finish doing "fd
>1" before any of them do "setc blue". My first question is, does this work
>the same way when the commands separated by commas refer to user-defined
>procedures each consisting of several StarLogo primitives? And my second
>question is, does a comma work the same way when it is placed between a
>turtle command and a patch command?
>
Yes, the comma behaves just the same for user defined code as for
primitives.
A patch command also synchronizes the turtles when placed after a turtle
command. Basically, any type of patch command or observer command placed
after a turtle command forces all of
the turtles to synchronize at that spot, and StarLogo then runs the next
command.
You can think of a comma as an observer NOP. It simply tells the compiler
to synchronize the turtles after their commands and then continue.
>I am having synchronization problems in a project I am working on that are
>hard to track down, as follows. I have two breeds of turtles, who are
>competing for the same "food" (patch color). When there are two turtles,
>one of each breed, both sitting right beside a food patch, I want them both
>to have an "equal opportunity" to get the food. I assume that in general
>this would be no problem since the order in which turtles execute a command
>addressed to all of them is essentially random, so half the time turtle A
>would get the first shot and half the time turtle B would get the first
>shot (right?).
Yep, that's right.
>But now suppose that the turtles of breed A have some extra
>processing to do, say for example they need to run an extra procedure "foo"
>(i.e. ask-breedA [foo]) that turtles of breed B don't need to do. If,
>furthermore, this "foo" procedure needs to run *before* the turtles of
>either breed get a chance to eat, won't this extra processing for the A's
>tend to make them lose the race for the food? How should I think about
>writing my StarLogo code -- named procedures, commas, etc. -- to prevent
>this kind of thing from happening?
The race condition only occurs if all of the turtles stay in sync. When
they reach this decision point, if breedA takes a little longer, then it
will never get the food. To even out the odds, you should add a comma
after the ask-breedA [foo]. However, if your turtles aren't in sync to
begin with, then you don't really have to worry about the race condition
becase chances are it won't be a real factor in the turtles' behavior.
For example, if turtle 1 is three steps behind turtle 2, and it takes
turtle 1 three extra cycles to run foo before eating the food, then they
will compete fairly for the food. If turtle 2 is closer than that, it
will be able to eat the food first. But if you can't guarantee the
relative spacing (in time and space) of turtle1 and turtle2, then you
shouldn't have to worry about it.
Are you seeing any strange artifacts in your simulation that result from
this extra processing from breedA?
Andy
Andrew Begel
Dept. of Computer Science
UC Berkeley