I’ve been using this python implementation of simulated annealing to deduce Singapore bus service from a database of bus stops (see previous posts about Google Map API).
I wanted to play a little bit more with this algo and decided to port it to OCaml: you can find the code on GitHub.
As a first try, I wrote a direct code translation. Only few points differ:
- The OCaml code use a stream implementation (lazy list) … (mostly equivalent to my previous Erlang implementation).
- The OCaml code uses array structure where Python uses list.
- The OCaml implementation of function reversed_section do only one array reverse in all cases.
Result is quite ok. Without any optimization on the algo, the OCaml native code performed quite faster (around 10 times faster), meaning that solving the TSP problem for SG bus services took 40 minutes for OCaml version where the Python code ran for hours (of course we could improve this version too).
The algo itself can be improve (I guestimate the time for SG map problem can be reduce by one more order of magnitude with the same simulated annealing approach).
But in coming post (code is there, need time to clean it), I’ll be looking at other interesting problems I found inside this one, especially on how to walk in solutions space in quasi-random order.
June 12, 2008 at 8:21 am
Hey, glad to hear you’ve found a use for my simulated annealing code.
So how big are the routes you’re optimising?
June 12, 2008 at 12:34 pm
Thanks John, your code/explanation was in fact really useful!
The routes I’m optimizing are of length 60 on average, but the length distribution is quite linear ranging from 5 ‘places’ up to 180 ‘places’.
That’s a good question you asked … I’m still struggling to know when we could decide to stop the search for better solutions; maybe the length could be taken into account for that purpose…
June 18, 2008 at 12:07 pm
It’s always tricky knowing what the stopping conditions should be. So often it seems like you just a few more iterations make the difference. Plus tuning the annealing parameters sometimes feels like you need another optimisation algorithm just for that!
October 13, 2008 at 3:49 pm
[...] public links >> ocaml Simulated annealing in OCaml Saved by daevu on Sun 12-10-2008 Ocaml Sockets Saved by cooliskillingus on Fri 03-10-2008 [...]
January 10, 2009 at 3:52 am
haven’t read the complete article, but this seems to be an interesting topic (simulated annealing) for people reading this post
http://ocamlnews.blogspot.com/2009/01/solving-traveling-salesman-problem.html