Introduction to the simulation annealing method of C # numerical calculation (2)

zhaozj2021-02-16  38

In the previous article, the basic principle of simulation annealing is described. The following will be described below with an actual example, where all source code has been posted, and you can learn a lot of details.

Use analog annealing method F (X, Y) = 5sin (xy) x2 y2 minimum

Solution: According to the question, we design the cooling table schedule:

That is, the initial temperature is 100

Attenuation parameters are 0.95

The length of the Marco chain is 10000

Metropolis' s steps are 0.02

The end condition is based on the difference between the latest optimal solution and the latest optimal solution less than a certain tolerance.

Use the Metropolis acceptance criteria to simulate, the program is as follows

/ *

* Simulated annealing method function f (x, y) = 5sin (xy) x ^ 2 y ^ 2 minimum

* Date: 2004-4-16

* Author: ARMYLAU

* Email: armylulau2@163.com

* The end condition is the difference between the two optimal solutions less than a small amount.

* /

Using system;

Namespace Simulateanneland

{

Class class1

{

/ / Target function to require an 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)

{

/ / Searching the maximum interval

Const Double XMAX = 4;

Const Double Ymax = 4;

// Cooling table parameters

INT MARKOVLENGTH = 10000; // Marco chain length

Double decayscale = 0.95; // Attenuation parameters

Double stepFactor = 0.02; // Step length factor

Double Temperature = 100; // Initial temperature

Double tolerance = 1e-8; // tolerance

Double prex, Next;;; x x

Double prey, nexty; // prior and next value of y

Double prebestx, prebesty; // Previous optimal solution

Double Bestx, Besty; // Final Solution

Double Acceptpoints = 0.0; // General Accepted Point in the metropolis

Random rnd = new random ();

// Random selection

Prex = -Xmax * rnd.nextdouble ();

PREY = -ymax * rnd.nextdouble ();

Prebestx = bestx = prex;

Prebesty = BESTY = PREY;

/ / Each iteration is annealed once (cool down) until it is satisfied

DO

{

Temperature * = decayscale;

Acceptpoints = 0.0;

// Detert LOOP (ie Markov Chain Length) at the current temperature t

For (int i = 0; i

{

// 1) Randomly select a little near this point

DO

{

Nextx = prex stepfactor * xmax * (rnd.nextdouble () - 0.5); Next = prey stepfactor * ymax * (rnd.nextdouble () - 0.5);

}

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

// 2) Whether or not the global optimal solution

IF (objectFunction> ObjectFunction (nextx, nexty))

{

/ / Keep the previous best solution

Prebestx = BESTX;

Prebesty = BESTY;

// This is the new best solution

BESTX = NEXTX;

Besty = nexty;

}

// 3) Metropolis process

IF (ObjectFunction - ObjectFunction (Nextx, Next)> 0)

{

// Accepted, here LastPoint is the next iteration point at the beginning of the newly accepted point

Prex = nextx;

Prey = nexty;

AcceptPoints ;

}

Else

{

Double change = -1 * (objectFunction (nextx, nexty) - ObjectFunction (prex, prey)) / temperature

IF (Math.exp (Change)> rnd.nextdouble ())

{

Prex = nextx;

Prey = nexty;

AcceptPoints ;

}

// Do not accept, save the original solution

}

}

Console.writeline ("{0}, {1}, {2}, {3}", prex, pregra, ibjectfunction (prex, prey), temperature;

WHIL (Math.Abs ​​(objectFunction (Bestx, Besty) - ObjectFunction (Prebestx, Prebesty)> Tolerance);

Console.writeline ("minimum at point: {0}, {1}", bestx, besty;

Console.Writeline ("Minimum value: {0}", ObjectFunction (Bestx, Best));

}

}

}

l Results:

Minimum value at the point: -1.07678129318956, 1.07669421564618

The minimum is: -2.26401670947686

l Postscript:

It turns out that a series of articles, introduces how to use C # decodes, because there are very few numerical calculations on 9CBS, so I hope I can add.

Attack in the online search to simulate annealing data and want to be an example of C # value calculation, no ready-to-range source code. Later, I have been experimenting for a long time, I finally wrote this procedure, I didn't dare to hide, and took out an entry example of an annealing or use C # decoding value algorithm.

This article tries to avoid too academicization, such as mathematics and physical names and formulas, rushing the pen, there are many places that may not be very clear, I hope you understand. Any questions or criticism, Email and me: armylau2@163.com

In addition, analog annealing can also be applied to other more complex issues, such as combined optimization issues such as Salesman issues. This example is just a minimum problem with a two-dimensional function, and its choice of cooling table parameters is also too simple, only a preliminary entry profile, please pay attention. l Reference:

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

2. Numeric Recipes in C

3. Calculation Method Book Non-Numerical Parallel Algaith (Volume 1) Simulation Annealing Algorithm

转载请注明原文地址:https://www.9cbs.com/read-26316.html

New Post(0)