#include #include #include #include /* Approximately solve the traveling salesman problem (TSP) using simulated annealing. */ /* The number of points. */ static int n = 100; /* The coordinates of the points. */ static double* X; static double* Y; /* The starting temperature. */ static double T = 5; /* The final temperature. */ static double TF = 0.05; /* Calculate the distance travelled by visiting the points in X,Y in the order given in I. */ double func(const int* const I) { double d = 0, u; int i; for (i=1; i TF) { step(I, Inew); sc1 = func(Inew); if (log(drand48()) < (sc-sc1)/T) { /* Swap the pointers and update the score. */ IS = I; I = Inew; Inew = IS; sc = sc1; /* Cool a bit. */ T *= 0.9999; printf("%f\t%f\n", T, sc); } break; } /* Output the final configuration. */ F = fopen("anneal_out", "w"); for (i=0; i