A New Improved Ant

Tozier and I got together Tuesday, and decided to play a bit more with the spiders and ants. We’re interested in it because it’s kind of fun watching them run around, because we wanted to get insight into the mathematical question of whether the spiders can catch the ant, and because Tozier will surely work a Genetic Programming exercise on this sooner or later.

We were concerned about the way the ant was deciding what to do. It seemed to turn onto lines with a spider when it shouldn’t, and we couldn’t figure out whether it was happening, nor why it was, if it was. So we thought of the idea of “hot corners”. We wanted to identify visually a corner that had a spider “close” to it. The idea of “close” is that the spider could get to the corner in the time it would take the ant to traverse a whole edge. For example, if the ratio of ant speed to spider speed is 3, then a corner should be “hot” if a spider is within 1/3 of the edge length. We wanted to highlight those corners as we observed what decisions the spiders and ant might make. So we built this:

What was happening was that the while said < 3 and should have said <= 3. This meant that the ant would never select the third path, even if the second one was unsafe. He just accepted his fate, which was usually bad.

With the change above, the ant was making better decisions. They still weren’t great. For example, suppose the ant proceeds down a safe edge, but the corner at the other end is hot: that is, a spider can get there before the ant does. This is bad: if everyone starts at zero (and at this point they were), the ant will run right into his demise. So we decided to give the ant more intelligence: if you see the corner ahead of you go hot, reverse. It goes like this:

Note that this code is a bit odd. We wrote moveAnt by intention, inquiring whether the ant’s stop location was hot, thinking it might be tricky. But it wasn’t so we just implemented isHot. We should have folded that function back in, but we didn’t.

And thereby hangs a tale, which we’ll talk about next time.

Seems messy …

As we built this thing, I tended to type in new functions at locations where I could see the things that related to them, not in any particularly organized fashion. As we worked, we noticed more and more that we were looking up and down the file to find functions.

We also noticed that having three spider definitions and one ant written out longhand was a problem:

Every time we wanted to change where they started or anything like that, we had to change all four of these little bits of code. Since we wanted to observe various things, we did that a lot. It was a pain.

We needed some kind of object to represent our bugs. If not an actual class, then at least some kind of copyable table. We went through a phase like that in the Spacewar game and it might have been enough for this situation, though my inclination is to go directly to a class (or even a little hierarchy). I don’t know what we’d do until we do it, but I can speculate.

As you read the code below, make your own assessment of its quality. I think you’ll agree that it isn’t very good.

Then tune in soon for the next article, which accidentally, but conveniently, makes a point I feel needs to be made often. See you then!