From: "Eric I." Date: 2007-10-09T11:30:03+09:00 Subject: Re: Itinerary for a Traveling Salesman (#142) My solution is split across a few files, so rather than pasting it all into this posting, I'll offer a link instead: http://learnruby.com/examples/ruby-quiz-142.tgz To run it for a grid of 7x7, say, you'd issue the command: ruby find_route.rb 7 Like James Edward Gray's solution, I created a generic GA class that didn't have any specific coding for routes and grids. And the Route class has some methods to create routes from other routes that correspond to various GA operators. But there's also an interface class that allows the GA class to deal with routes. I implemented both of the suggested operations -- exchange and reverse. And I wanted an operation that combined two routes, and so I used the one that I saw in James Koppel's solution -- partner guided reorder. I also allow one to specify the relative frequency with which the various operators are randomly chosen. And I do keep track of each route's ancestory, to look for possible patterns as to which operations tended to be the most useful, for use in tuning the operator frequencies. Finally, if RMagick is available, it generates a GIF image of the best route found. It was interesting to see how easy a GA was to implement given Ruby's nice set of Array/Enumerable methods. It was a very nice Ruby Quiz as it sucked me into spending more time on it than I would have liked. Eric ---- Are you interested in on-site Ruby training that uses well-designed, real-world, hands-on exercises? http://LearnRuby.com