Simulated annealing algorithm

Source: Internet
Author: User

BZOJ3680 amputated xxx

There are n points, which ask for a point to all points of weighted distance and minimum

Simulated annealing algorithm

Nature generally follows the trend towards low energy, and the cooling of objects is a process like this.
In the process of cooling, the molecules do a drastic irregular movement, gradually seeking to reach a lower energy level, the temperature gradually reduced, and the intensity of irregular movement gradually reduced, and finally stabilized state is likely to be a state of almost the lowest energy in all States

Like the mountain climbing algorithm, we want to find a high point and move from the current state to a nearby point higher than the current state, until it cannot be moved.
Obviously it is possible to find a local top point, but it is likely to overlook some of the higher points

In order to solve the problem of this algorithm, we imitate the annealing process, we set a temperature, over time by one (0,1) between the number and gradually reduce
In this process, we are constantly looking for points around the current point, the distance is also determined by the temperature, the greater the temperature, the distance may be greater, the smaller the temperature, the distance will become fine
① if we find a point that's better than the current point, it's obviously going to move past
② if we find a point that is worse than the current one, transfer the past with a certain probability, the probability is \ (E^{\frac{\delta d}{k*t}}\), where \ (\delta d\) is the temperature difference "that is the difference between the excellent degree of the answer", K is a constant. Because it is worse than the current, so \ (\delta d\) is a negative value, then \ (E^{\frac{\delta d}{k*t}} \in (0,1) \), and decreases as t decreases, that is, the smaller the T, the more stable

In conclusion, we get an algorithm such as:
① Determining an initial section
② set the temperature T, gradually reduce
1 ' random jump to a nearby solution, distance is determined by T
2 ' Comparison of new solutions and current solutions, if better, then transfer, otherwise at a certain probability of transfer
③ finally randomly try small shifts to approximate the final answer.

#include <iostream>#include <cmath>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#define LL Long Long int#define REP (i,n) for (int i = 1; I <= (n); i++)#define Redge (U) for (int k = h[u],to; k; k = ed[k].nxt)#define BUG (s,n) for (int i = 1; I <= (n); i++) cout<<s[i]<< '; puts ("");using namespaceStdConst intMAXN =10005, MAXM =100005, INF =1000000000;inline intRead () {intout =0, flag =1;Charc = GetChar (); while(C < -|| C > $) {if(c = ='-') flag =-1; c = GetChar ();} while(c >= -&& C <= $) {out = (out <<3) + (out <<1) + C-' 0 '; c = GetChar ();}returnOut * FLAG;}intNintX[MAXN],Y[MAXN],W[MAXN];DoubleAnsx,ansy,d;DoubleR () {return(Double) (rand ()%10000) /10000;}DoubleCalDoubleNxDoubleNY) {DoubleAns =0; for(inti =1; I <= N;    i++) {ans + = sqrt ((nx-x[i]) * (Nx-x[i]) + (Ny-y[i]) * (Ny-y[i])) * W[i]; }if(Ans < D) {ANSX = NX; ansy = NY;    D = ans; }returnAns;}voidSA () {DoubleDe,t =100000;DoubleNOWX = Ansx,nowy = Ansy,nx,ny; while(T >0.001) {NX = Nowx + T * (2* R ()-1); NY = nowy + T * (2* R ()-1); DE = Cal (Nowx,nowy)-Cal (Nx,ny);if(DE >0|| Exp (de/t) > R ()) {nowx = NX;        Nowy = NY; } T *=0.97; } REP (I, +) {NX = Ansx + T * (2* R ()-1); NY = Ansy + T * (2* R ()-1);    Cal (Nx,ny); }}intMain () {n = read ();        REP (i,n) {X[i] = read (), y[i] = Read (), w[i] = read (); ANSX + = X[i];    Ansy + = Y[i]; } ANSX/= N; Ansy/= N;    D = cal (Ansx,ansy);    SA (); printf"%.3LF %.3lf\n", Ansx,ansy);return 0;}

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.