Application of Python annealing algorithm in equation

Source: Internet
Author: User

First, Introduction

Annealing algorithm is self-evident, is the process of iron and steel in the quenching process of temperature and stability, the higher the thermodynamic temperature (internal energy) the more unstable the atomic state, and the temperature has a radiation to the low temperature zone of the physical process, when the material energy is no longer reduced when the matter of the atomic state gradually become stable ordered state, This is a useful reference for us to find out the optimal solution from the stochastic complex problem, and the process is converted into an algorithm, see other data in detail.

Second, the computational equation

The equation we want to calculate is f (x) = (x-2) * (x + 3) * (x + 8) * (X-9), which is a unary four-time equation, which we call equation, and of course the opening of this function is upward, so we may not find the maximum point in an infinitely long range, so we try to We become the optimal solution for the smallest point in the inter-solution.

Solution 1:

There is no doubt that the mathematical method of multiple derivation can be solved, but the process is more complex, but also easy to calculate the wrong, I will not repeat, the reader has time to try to solve their own.

Solution Two:

The solution is to solve the problem of violence, we only solve the optimal solution on the interval [ -10,10], directly randomly 200 points, divided by 10 (so that a non-integer horizontal axis), and then calculate its ordinate f (x), Min{f (x)}, Use the index method of the list to find the minimum corresponding position on the line, and then draw a picture roughly aim.

Direct Sticker Code:

1 ImportRandom2 ImportMatplotlib.pyplot as Plt3 4list_x = []5 #For I in range (1):6 ##print (Random.randint (0,100))7 #For i in range (0,100):8 #print ("SSS", i)9 #Ten #list_x.append (Random.randint (0,100)) One  forIinchRange (-100,100): AList_x.append (I/10) -  - Print("The horizontal axis is:", list_x) the Print(Len (list_x)) -  -  -List_y = [] +  forXinchlist_x: -     #print (x) +     #y = x*x*x-60*x*x-4*x +6 Ay = (x-2) * (x + 3) * (x + 8) * (x-9) at list_y.append (y) - Print("Ordinate is:", list_y) -  - #Proven , the results shown here are 6.5 and the optimal solution 1549 are all right. - Print("The minimum value is:", Min (list_y)) -num =min (list_y) in Print("Optimal Solution:", List_y.index (num)/10) - Print("Section", List_y.index (num)/10-10,"position to get the minimum value of") to  +Plt.plot (list_x, list_y, label='NM') - #Plt.plot (x2, y2, label= ' Second line ') thePlt.xlabel ('X')#Horizontal axis Title *Plt.ylabel ('Y')#Ordinate title $ #plt.title (' Interesting graph\ncheck it out ', loc= "right") #图像标题Panax Notoginseng #plt.title (' Interesting graph\ncheck it out ') -Plt.legend ()#displays settings for FISRT line and second line (label) thePlt.savefig ('C:/users/zhengyong/desktop/1.png') +Plt.show ()

The following results are obtained:

Then we get the coordinates of the optimal solution (6.5,-1549.6875), the result is put here first, and then the annealing algorithm to see if it can be solved.

Solution Three:

Let's take a look at a diagram (the method in solution two) and then talk about the core idea of the annealing algorithm.

First, random a random solution between [-10.10], as the initial solution space, for example, a random one located in [ -2.5.2.5] The highest point is the point 1 (horizontal axis is x1), he has a value for the ordinate Y1, This time we put the horizontal axis of this point randomly add or subtract a value (note that the value of the size is very important, we first call him to move the value), add or subtract to get a new horizontal value x2, and then calculate the horizontal axis of the corresponding ordinate (y2), compared to the size of the previous ordinate, here set

Delta = y2-y1, found no matter what is less than the original ordinate (if the random moving value is small enough), this time we put the new X2 assigned to X1, the current X2 value to x1,x1 is the original random value, the process can be repeated iter_num times, The size is based on your own range.

The whole process is carried out at one temperature, and after this process we update the temperature with the temperature and then repeat the above steps.

Temperature update I was using the usual formula is T (t) =at0 (t-1), where 0.85≦a≦0.99. It can also be calculated using the corresponding thermal attenuation formula, T (t) =t0/(1+LNT), t=1,2,3,..., which is a simple state Update method.

That is, no matter how random you are, I can move in the direction of optimization (provided the non-optimal point).

Second, point 2 is the same, the difference is that he is the local optimal solution, then jump out of the local optimal solution of the mechanism is what?

If the initial point is (X3,Y3), then use the above method to get (X4,Y4), the delta at point two is definitely greater than 0, then how to do? When it is greater than 0, we each have a certain probability to accept this seemingly not optimal point, called the Metropolis Criterion, specifically:

Here the E is y,t is the current temperature, the delta is less than 0 is the new value, whether it is accepted by this probability, when the iteration of the time, each right to move the step up to 1 times he will be able to find the final optimal solution, the step is cumulative but the probability is tired, This means that the probability is small, but once the number of iterations is sure to run out to the optimal solution.

Optimal, point 3 does not explain Ha, same as above.

Then we're on the code:

1 #the calculation method of self-rewriting annealing algorithm calculation equation (x-2) * (x + 3) * (x + 8) * (x-9)2 #class doesn't work .3 ImportNumPy as NP4 ImportMatplotlib.pyplot as Plt5  fromMatplotlibImportPyplot as Plt6 7 8 #Set Basic Parameters9 #T Initial temperature, t_stop,iter_num number of iterations per temperature, q temperature AttenuationTen classtuihuo_alg (): One     def __init__(self,t_start,iter_num,t_stop,q,xx,init_x): ASelf. T_start =T_start -Self.iter =Iter_num -Self. T_stop =T_stop theSelf. Q =Q -self.xx =xx -self.init_x =init_x -     #def cal_x2y (self): +     #return (x-2) * (x + 3) * (x + 8) * (x-9) -  +  A if __name__=='__main__': at  -     defcal_x2y (x): -         #print ((x-2) * (x + 3) * (x + 8) * (x-9)) -         return(x-2) * (x + 3) * (x + 8) * (x-9) -T_start = 1000 -Iter_num = 1000 inT_stop = 1 -Q = 0.95 toK = 1 +L_boundary =-10 -R_boundary = 10 the     #Initial value *xx = Np.linspace (l_boundary, R_boundary, 300) $yy =cal_x2y (XX)Panax NotoginsengInit_x =10 * (2 * Np.random.rand ()-1) -     Print("init_x:", init_x) the  +t =tuihuo_alg (t_start,iter_num,t_stop,q,xx,init_x) A  theVal_list =[init_x] +      whileT_start>T_stop: -          forIinchRange (iter_num): $Init_y =cal_x2y (init_x) $             #this interval (2 * np.random.rand ()-1) itself is ( -1,1), so plus is a random plus or minus process -new_x = init_x + (2 * Np.random.rand ()-1) -             ifL_boundary <= new_x <=r_boundary: theNew_y =cal_x2y (new_x) -         #print ("new_x:", new_x)Wuyi         #print (' new_y: ', new_y) theDelta = new_y-init_y#new minus old -                 ifDelta <0: Wuinit_x =new_x -                 Else: Aboutp = np.exp (-delta/(K *T_start)) $                     ifNp.random.rand () <P: -init_x =new_x -             #print ("new_x:", new_x) -             #print ("Current temperature:", T_start) AT_start = T_start *Q +  the Print("the optimal solution x is:", init_x)#It was originally written in new_x, so the result has been wrong . - Print("The optimal solution is:", init_y) $ #For example, I add new_x, the error between true and False is actually the last assignment "init_x = new_x" the Print("the false optimal solution X is:", new_x)#It was originally written in new_x, so the result has been wrong . the Print("the false Optimal solution is:", new_y) the  thexx = Np.linspace (l_boundary,r_boundary,300) -yy =cal_x2y (XX) inPlt.plot (xx, yy, label='Tuihuo') the #Plt.plot (x2, y2, label= ' Second line ') thePlt.xlabel ('X for Tuihuo')#Horizontal axis Title AboutPlt.ylabel ('Y for Tuihuo')#Ordinate title the #plt.title (' Interesting graph\ncheck it out ', loc= "right") #图像标题 the #plt.title (' Interesting graph\ncheck it out ') thePlt.legend ()#displays settings for FISRT line and second line (label) +Plt.savefig ('C:/users/zhengyong/desktop/1.png') -Plt.show ()

The class is used here, it is not necessary to find it, but I don't want to change it.

The best results are:

The results are as follows:

Three, summary

The concrete idea of annealing algorithm I didn't say much, but I wrote the core points, After verifying that the annealing algorithm has obtained the optimal solution of (6.551677228904226,-1548.933671426107), look at the solution two (6.5,-1549.6875), we found that, hehe, almost, error words, can accept, of course, the reader can also run a few more data out to verify.

My lab environment is python3.6,numpy1.14.3,matplotlib2.2.2,64 bit win10,1709 Education Edition, OS kernel 16299.547, so let's try to tell the details.

Application of Python annealing algorithm in equation

Related Article

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.