模拟退火求函数最值问题求解
2015-12-14 20:35
330 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2899
题意:给出方程
![](https://img-blog.csdn.net/20150104134331750)
,其中
![](https://img-blog.csdn.net/20150104134411546)
,输入
![](https://img-blog.csdn.net/20150104134451888)
,求
![](https://img-blog.csdn.net/20150104134543859)
的最小值。
#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;
}
题意:给出方程
,其中
,输入
,求
的最小值。
#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;
}
相关文章推荐
- iOS_开发中遇到的那些问题_3
- 【Beta】Daily Scrum Meeting第七次
- undefined
- QTP中使用强制退出程序语句小记
- PHP读取TXT中文乱码的解决方式
- 【Beta】Daily Scrum Meeting第七次
- PCL—低层次视觉—关键点检测(rangeImage)
- firefox浏览器批处理插件imacros
- linux 搭建git 服务器
- 文本相似度算法
- Zero Copy 简介
- Spring中Bean的4种依赖检查模式
- 网格的学习—1
- Swift的74标准功能
- Python中strip()函数
- uva 10655 - Contemplation! Algebra 【矩阵快速幂】【最坑的输入】
- myeclipse快捷键
- hdoj 5597 GTW likes function 【打表找规律】
- ARM926 interrupts in QEMU
- 一个数字与时间切换的函数