Brief introduction of simulated annealing method for C # numerical calculation (II.)

Source: Internet
Author: User
In the previous article on the fundamentals of simulated annealing, the following is a practical example to illustrate that all of the source code has been posted, you can learn a lot of details.



Using simulated annealing method to find the function f (x,y) = 5sin (XY) + x2 + y2 minimum value




Solution: According to the order, we design the cooling table schedule as follows:

That's the initial temperature of 100.

Attenuation parameter is 0.95

Markov chain length is 10000

The Metropolis step is 0.02

The ending condition is less than a tolerance based on the difference between the last optimal solution and the newest one.





Using the Metropolis acceptance criteria for simulation, the procedure is as follows



/*

* Simulated annealing method to find the function f (x,y) = 5sin (XY) + x^2 + y^2 Minimum value

* Date: 2004-4-16

* Author: Armylau

* email:armylau2@163.com

* The end condition is two times the difference of the optimal solution is less than a small amount

*/

Using System;

Namespace Simulateannealing

{

Class Class1

{

Objective function requiring optimal value

Static double objectfunction (double x, double y)

{

Double z = 0.0;

z = 5.0 * Math.sin (x*y) + x*x + y*y;

return z;

}



[STAThread]

static void Main (string[] args)

{

Maximum range of searches

Const double xmax = 4;

Const double ymax = 4;



Cooling table Parameters

int markovlength = 10000; Markov chain length

Double Decayscale = 0.95; Attenuation parameters

Double stepfactor = 0.02; Step factor

Double temperature = 100; Initial temperature

Double tolerance = 1e-8; Tolerance



Double prex,nextx; Prior and next value of X

Double prey,nexty; Prior and next value of Y

Double Prebestx, Prebesty; Last optimal solution

Double bestx,besty; Final Solution

Double acceptpoints = 0.0; Total acceptance point in Metropolis process



Random rnd = new Random ();



Random Selecting points

PreX =-xmax * rnd. Nextdouble ();

Prey =-ymax * rnd. Nextdouble ();

PREBESTX = Bestx = PreX;

Prebesty = Besty = Prey;



Annealing once per iteration (cooling) until the iteration condition is met

Todo

{

Temperature *=decayscale;

acceptpoints = 0.0;



Iteration loop (i.e. Markov chain length) at current temperature T

for (int i=0;i<markovlength;i++)

{

1 randomly select a spot near this point

Todo

{

NEXTX = PreX + stepfactor*xmax* (rnd. Nextdouble ()-0.5);

Nexty = Prey + stepfactor*ymax* (rnd. Nextdouble ()-0.5);

}

while (!) ( NEXTX >=-xmax && nextx <= xmax && nexty >=-ymax && nexty <= ymax));



2) is the global optimal solution

if (objectfunction (bestx,besty) > Objectfunction (nextx,nexty))

{

Preserve the last optimal solution

Prebestx =bestx;

Prebesty = Besty;

This is the new optimal solution.

BESTX=NEXTX;

Besty=nexty;

}



3) Metropolis Process

if (Objectfunction (Prex,prey)-objectfunction (Nextx,nexty) > 0)

{

Accept, where lastpoint the next iteration point begins with the newly accepted point

PREX=NEXTX;

Prey=nexty;

acceptpoints++;



}

Else

{

Double change =-1 * (Objectfunction (nextx,nexty)-objectfunction (Prex,prey))/temperature;

if (MATH.EXP) > rnd. Nextdouble ())

{

PREX=NEXTX;

Prey=nexty;

acceptpoints++;

}

Do not accept, save the original solution

}

}

Console.WriteLine ("{0},{1},{2},{3}", PreX, Prey, objectfunction (PreX, Prey), temperature);



while (Math.Abs objectfunction (bestx,besty) –objectfunction (PREBESTX, prebesty)) > Tolerance);



Console.WriteLine ("Minimum value at point: {0},{1}", Bestx, Besty);

Console.WriteLine ("min: {0}", Objectfunction (Bestx, besty));

}

}

}



L Results:

Minimum value at point: -1.07678129318956,1.07669421564618

The minimum value is:-2.26401670947686



L PostScript:

Originally wanted to write a series of articles, how to use C # to solve numerical problems, this is because there are few csdn on the numerical calculation of the article, so I hope to be able to add.

At the beginning of the online search simulated annealing data and want to as a C # numerical calculation of an example, can not find ready-made source code. Later, he experimented for a long time, and finally wrote the program, dare not to stash, take out the role of simulated annealing or using C # to solve the problem of numerical algorithms an introductory example.

This article tries to avoid too academic, such as mathematical and physical names and formulas, hasty writing, there are many places may not be very clear, I hope you understand. Any questions or criticisms that can be emailed with me: armylau2@163.com

In addition, simulated annealing can be applied to other more complex problems, such as "salesman problem" and other combinatorial optimization problems. This example is only to find a two-dimensional function of the minimum value problem, and its cooling table parameter selection is too simple, can only function a preliminary introduction, please readers attention.





L Reference Documents:

1. Http://www.computer-dictionary-online.org/index.asp?q=simulated+annealing Computer Dictionary

2. Numeric Recipes in C

3. Computational Methods Series non-numerical parallel algorithm (first volume) 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.