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
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.
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.