模拟退火求函数最值问题求解
生活随笔
收集整理的這篇文章主要介紹了
模拟退火求函数最值问题求解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目:http://acm.hdu.edu.cn/showproblem.php?pid=2899
?
?? 題意:給出方程,其中,輸入,求的最小值。
#include<iostream> #include<stdlib.h> #include<stdio.h> #include<string> #include<algorithm> #include<time.h> using namespace std;#define ITERS 10 #define T 100 #define eps 1e-8 #define delta 0.98 #define INF 1e99double x[ITERS]; int judge(double x, double y) {if (abs(x - y) < eps) return 0;return x < y ? -1 : 1; }double random() {double x = rand()*1.0 / RAND_MAX;if (rand() & 1) x *= -1;return x; }double F(double x, double y) {return 6 * pow(x, 7) + 8 * pow(x, 6) + 7 * pow(x, 3) + 5 * pow(x, 2) - x*y; } void init() {for (int i = 0; i < ITERS; i++){x[i] = fabs(random()) * 100;} }double sa(double y) {double ans = INF;double t = T;while (t < eps){for (int i = 0; i < ITERS; i++){double tmp = F(x[i], y);for (int j = 0; j < ITERS; j++){double _x = x[i] + random()*t;if (judge(_x, 0) >= 0 && judge(_x, 100) <= 0){double f = F(_x, y);if (tmp>f)x[i] = _x;}}}t = t*delta;}for (int i = 0; i < ITERS; i++){ans = min(ans, F(x[i], y));}return ans; } int main() {int t;cin >> t;while (t--){double y;cin >> y;srand(time(NULL));init();cout << sa(y) << endl;}return 0; }
總結(jié)
以上是生活随笔為你收集整理的模拟退火求函数最值问题求解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: trace--求矩阵的迹
- 下一篇: 三角剖分(delaunay)拓扑结构 高