您的位置:首页 > 其它

模拟退火求函数最值问题求解

2015-12-14 20:35 330 查看
题目: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 1e99

double 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: