"Algorithm talk" simulated annealing algorithm

Source: Internet
Author: User

Recently saw the quantum annealing computer "D-ware" successful development, bloggers expressed great excitement.

I'm just going to talk a little bit about "simulated annealing algorithm"

----------------------"Starting from the mountain climbing algorithm"----------------------------------

"Sorry, the picture is too slag."

OK, now we're going to assume that you're climbing this mountain, and your task is to tell me where the highest point is when you're done climbing the mountain.

Then we use the idea of mountain climbing algorithm to simulate.

First of all, you came to A, you thought: This is the highest place I've ever been. ), so in the current case the optimal value is a

For a moment, you came to B, and you found that B seemed a little taller than a, so in the current case the highest is B.

Then, you find that your brain capacity is not enough, in order to be faster, you can only note the height of the left and right two points, so compare A and B, you forget the height of a.

Sad things happened, you went to C, found here is really high, should be the highest place in the world, you want to look around, found that B and D are not C high, so You think C is the highest point of the mountain (...). )

Obviously, you are blinded by the local optimal solution ...

Although this approach is very fast for handling hyper-scale data, you find that this algorithm has great bugs.

------------------------------------"Simulated annealing algorithm (SA)"----------------------------------------------------------

So how do we jump over this hole???
Then we found a (metaphysical) algorithm, since your brain capacity is only enough to remember the height of the best two points, then why don't we try to walk around casually?

"Back to this picture."

We're going to try it with the idea of an annealing algorithm.

First of all, you come to a, you remember the height here.

Then you skip B, direct (teleport??? To C, you find this place very high, so you remember C.

Then you randomly ( accept the move in a certain probability, and this probability gradually decreases over time (gradually decreasing to stabilize)) jumps to D, you find that D is really short, but you look right, you find E, and you forget C and a.

When you come to E, you go to F, you find that f is too short, and then go to the right you find no way ...

Through the simulation, we have successfully avoided the bug which caught the local optimal value by the simulated annealing algorithm.

The calculation of "certain probability" here refers to the annealing process of metal smelting, which is the origin of the name of the simulated annealing algorithm.

So how does this probability count:

According to the thermodynamic principle, when the temperature is T, there is a probability that the energy difference for the de's cooling is P (DE), expressed as:

P (DE) = exp (de/(KT))

where k is a constant, exp represents a natural exponent, and de<0. The formula is plainly: the higher the temperature, the greater the probability that the energy difference will reduce the cooling of the de, and the lower the temperature, the less the probability of cooling. And since DE is always less than 0 (otherwise it is not called annealing), so de/kt < 0, so the function value range of P (DE) is (0,1).

As the temperature t decreases, the P (DE) gradually decreases.

We see the movement of a poor solution at once as a temperature-hopping process, and we accept such movement with probability P (dE).

Let's make an image of the metaphor:

Mountain climbing algorithm is a sighted person in the mountain climbing, he climbed to the Alps think this is the highest mountain in the world, but do not know that Everest is still waiting for him.

Simulated annealing algorithm is a drunk person in the mountain climbing, he began to walk casually, he may even step into the basin. But he began to wake up and run to the heights.

-------------------------------------"SA algorithm to solve practical problems"-------------------------------------------------

SA (simulated annealing) algorithm solves TSP (traveling salesman problem): There are N cities, the only way to traverse all cities, and then go back to the city of departure, to find the shortest route.

Of course the SA algorithm is a random-based algorithm that can get approximate solutions at very fast speeds, how fast? O (n). This speed is already very fast, much faster than an enumeration-based algorithm.

So how to actually solve the TSP problem?

The travel salesman problem belongs to the so-called NP complete problem (unknown to Baidu itself), and the precise solution to the TSP can only be done by exhaustive all the path combinations, whose time complexity is O (n!). So, to solve the TSP asap, use the SA algorithm.

The approximate optimal path of TSP can be obtained quickly by using simulated annealing algorithm. (using the genetic algorithm is also possible, I will introduce in the next article) simulated annealing to solve the TSP idea:

1. Generate a new traverse path P (i+1), calculate the length of the path P (i+1) L (P (i+1))

2. If L (P (i+1)) < L (P (i)), accept P (i+1) as a new path, otherwise accept P (i+1) as the probability of simulated annealing, and then cool down

3. Repeat steps until exit conditions are met

There are a number of ways to generate a new traversal path, with 3 of them listed below:

1. Randomly select 2 nodes, exchanging the order of the 2 nodes in the path.

2. Randomly select 2 nodes to reverse the sequence of nodes between the 2 nodes in the path.

3. Randomly select 3 node m,n,k, then shift the node between node m and N to after node K.

Code to serve!!!

#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <ctime >//FUNNY!!!!!! const int MAXN = 27; City Number Const Double MAX = 27.0; City Number Const Double init_t = 3000; Initial temperature const Double rate = 0.95; Temperature decay Rate Const double finnal_t = 1E-10; terminating temperature const int in_loop = 15000; Number of internal loops const int LIMIT = 10000; Probability selection upper bound const int finl_loop = 1000; Outer Loop Double dd=0;struct path{//define alignment structure int CITYS[MAXN];d ouble length;} D_bestpath;struct point{//defines the point structure double x;double y;} d_point[maxn];//calculates the distance between points and points void point_dist () {int I, j;double x;for (i=0; i<maxn; i++) {for (j=i+1; j<maxn; J + +) {x = (D _point[i].x-d_point[j].x) * (d_point[i].x-d_point[j].x); x + = (d_point[i].y-d_point[j].y) * (D_point[i].y-d_point[j]. Y);D _length[i][j] = sqrt (x);D _length[j][i] = D_length[i][j];}}} Initialize void init () {int i;printf ("Initial state path:");D _bestpath.length = 0;for (i=0; i<maxn; i++) {//initial order through path d_bestpath.citys[i ] = i;printf ("%d--", I);} for (i=0; i<maxn-1; i++) {//Calculate path length d_bestpath.length + = D_leNGTH[I][I+1];} printf ("\ n path Length:%.3lf\n\n", d_bestpath.length);} void Initi () {//test int i;d_bestpath.length = 0;d_bestpath.citys[0] = 0;d_bestpath.citys[1] = 1;d_bestpath.citys[2] = 5;D_B ESTPATH.CITYS[3] = 4;d_bestpath.citys[4] = 11;d_bestpath.citys[5] = 12;d_bestpath.citys[6] = 3;D_BestPath.citys[7] = 17 ;D _bestpath.citys[8] = 18;d_bestpath.citys[9] = 19;d_bestpath.citys[10] = 20;d_bestpath.citys[11] = 9;D_BestPath.citys [[] = 13;d_bestpath.citys[13] = 14;d_bestpath.citys[14] = 7;d_bestpath.citys[15] = 6;d_bestpath.citys[16] = 10;D_ BESTPATH.CITYS[17] = 8;d_bestpath.citys[18] = 2;d_bestpath.citys[19] = 16;d_bestpath.citys[20] = 22;D_BestPath.citys[ ] = 21;d_bestpath.citys[22] = 15;d_bestpath.citys[23] = 26;d_bestpath.citys[24] = 25;d_bestpath.citys[25] = 24;D_ BESTPATH.CITYS[26] = 23;for (i=0; i<maxn-1; i++) {//Calculate path length d_bestpath.length + = D_length[d_bestpath.citys[i]][d_ BESTPATH.CITYS[I+1]];}} Enter city coordinate information void input () {int i;for (i=0; i<maxn; i++) scanf ("%lf%lf", &d_point[i].x, &d_poinT[I].Y);} Path GetNext (path p) {Path ret;int I, x, y;int te;ret = p;do{x = (int) (Max*rand ()/(Rand_max + 1.0)); y = (int) (Max*rand ()/( Rand_max + 1.0));} while (x = = y), Te = ret.citys[x];ret.citys[x] = Ret.citys[y];ret.citys[y] = Te;ret.length = 0;for (i=0; i<maxn-1; i++) {// Calculate path length Ret.length + = d_length[ret.citys[i]][ret.citys[i+1]];} Dd++;return ret;} void SA () {int I, p_l=0, p_f=0;; Path Curpath, newpath;double T = Init_t;double p, delta;srand ((int) time (0)), Curpath = D_bestpath;while (True) {for (i=0; i& Lt;in_loop; i++) {NewPath = GetNext (curpath);d elta = newpath.length-curpath.length;if (Delta < 0) {//Update length curpath = NewPath; p_l = 0; P_f = 0;} Else{p = (double) (1.0*rand ()/(rand_max+1.0)), if (exp (DELTA/T) < 1 && exp (DELTA/T) > P) {curpath = NewPath;} p_l + +;} if (p_l > LIMIT) {p_f ++;break;}} if (Curpath.length < newpath.length) D_bestpath = curpath;if (P_f > Finl_loop | |    t<finnal_t) break; t = t * rate;}} int main () {input ();p oint_dist (); init (); SA ();}

Typing really * * * tired ...

"Algorithm talk" simulated annealing algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.