您的位置:首页 > 编程语言 > C语言/C++

Simulated Annealing Minimization: Single variable polynomial C++ source code.

2014-03-12 03:28 381 查看
Finding the minimum of x^2-9x+20 through simulated annealing:

This is a very simple example problem. Most people that are interested in simulated annealing probably could figure out the answer without even a pen and paper, so this is just to explore the method.

This process is a simulation of cooling metal to settle the structure to a stable formation. In this case we are settling a point into what is hopefully a global minimum of a polynomial.

The general idea:

Start with a random x value and find that point on the graph.
Test a random point just to the right or left of that point.

New point has a lower value: (possibly closer to a minimum)

Accept and save it.

Value is higher:

There is a chance for it to be accepted based on the current simulated temperature. The higher the temperature, the higher the chance it will be accepted.

(This chance allows for the saved point to get out of local minimums and makes the global minimum have the highest chance of containing the point)

Repeat from 2 until the simulated temperature is blow some preset threshold.

(typically a very large number of iterations.)
Output the current point and the value at that point.

Read more to see the code.

Note:

To find the minimum of another single variable function, just change the content of the function at the top, E(x).

For functions with more variables, you’ll have to add the new variables to the code as well as adjust them every iteration. For each new variable you will have to add a new line similar to “double xNew = x + ((rand()/(double)RAND_MAX)*2 – 1);” and save the
new values in new variables named something like x2New.

Of course the exact solution is:

E(x) = x^2 – 9x + 20

E’(x) = 2x – 9 = 0

=> 2x = 9

=> x = 4.5

E(4.5) = (4.5)^2-9(4.5)+20

= 20.25 – 40.5 + 20

= -0.25

That program’s result is pretty close to the exact answer, not bad for a glorified guess and check.

This code is an adaption of code I found here.
It solves a two variable problem, but the approach is the same.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: