Sunday, May 29, 2016

Non-stupid dragon rider travel algorithm

I'm not a fan of Eragon - see the Books page for details. One (not the largest) of my complaints involves a moment where a couple of people (or maybe three) have a horse to carry their baggage, and of course they're traveling with a dragon. Since the dragon can't carry everyone, they decide to walk from one town to a distant one.

Stupid.

Of course the most straightforward thing would be for the dragon to carry one person (or however much she can carry) in one trip, then return for the next load, and so forth. Presumably a dragon could make several trips between towns in less time than it could take a person to walk. But for some reason I found myself thinking a little harder about this - specifically, how to optimize things for the shortest possible trip.

I started with a few constraints, which may or may not be valid in the book, but that's fine. For the sake of simplicity, let's say that on a given trip the dragon can carry one human, or all of the luggage, or the horse. (Granted, the horse would weigh more than two humans, so the dragon should be able to carry them both, but maybe the humans don't feel comfortable being dangled by just one set of claws or something. Or maybe the dragon can't carry the horse, but that makes this problem less interesting.) Another possible constraint is to ensure that you don't leave the luggage or the horse unattended. If it's okay to leave the luggage alone, and if there are two humans, then we have a fairly simple way of shortening the time even more:

  1. The dragon carries the luggage to the end point. At the same time, one human starts off on the horse while the other starts walking.
  2. When the dragon drops off the luggage, it returns to pick up the walking human. 
  3. The dragon drops the human off with the luggage and returns for the horse. (By this time, the horse and remaining human have covered a lot of distance, so the dragon doesn't have nearly as far to go.)
  4. The dragon drops off the horse and goes back for the final human.

We can optimize things a little further, because at step 3, the dragon doesn't have to carry the human all the way to the luggage - just close enough that the human can reach it before the final trip is over.
It might be possible to advance things even further,  if we can assume that extra trips for the dragon are offset by less time spent with a human on foot. At step 3, the dragon drops the first human off at an even farther distance from the end. Then at step 4, the dragon carries the horse from the second person to the first, then goes back for the second person. This way, more time is spent with the slowest traveler on horseback rather than walking. Like I said, this might (depending on relative speeds and distances) offset the need for some extra partial trips for the dragon.

Things get even more complex if we add the constraint that the luggage must always be in the company of a human or dragon. (Maybe there are lots of thieves around.) In this case, the luggage becomes the limiting factor. Again, assuming two humans:

  1. The dragon takes human #1 far ahead, but not quite to the goal. The human starts walking. At the same time, the other human begins walking the the laden horse.
  2. The dragon takes the luggage to human #1's position. That human must now stop. But human #2 can now ride the horse and move faster.
  3. The dragon takes the horse to human #1, who can now move. Human #2 continues on foot.
  4. The dragon takes human #2 very near the end point
  5. The dragon takes the luggage to the end point, arriving at the same time as human #2. Human #1 can now ride.
  6. The dragon takes the horse to the end point. (Unfortunately, the horse's extra speed is now wasted.)
  7. The dragon takes human #1 to the end point.
Again, we might be able to optimize this by having the drop-off point for the luggage at step 5 some distance from the end point. That way human #2 can continue with the horse and luggage while the dragon goes back for human #1. But this would also mean that the human is stationary in-between receiving the luggage and receiving the horse, which might offset this. Of course, adding a third person would really free things up and allow the horse to continue running right up until the end, similar to the first scenario. (Although of course that would also add additional trips.)

You'll notice that I've left the math out of this completely, which is why it's not possible to come up with a definitive optimal solution. I guess this would be a good interview question if math were relevant. Maybe someday I'll write a program to simulate the situation - it would be pretty cool to have little icons representing the different elements and let you tell the dragon which thing to pick up and when to drop off. In the meantime, though, please don't make stupid choices like walking somewhere you're in a hurry to get to when there's fast and free transportation available.

No comments:

Post a Comment