初探 模拟退火算法 POJ2420 HDU1109
模拟退火算法来源于固体退火原理,更多的化学物理公式等等这里不再废话,我们直接这么来看
模拟退火算法简而言之就是一种暴力搜索算法,用来在一定概率下查找全局最优解
找的过程和固体退火原理有所联系,一般来讲,就是设置应该初始温度T(通常为100),一个退火的系数K(通常为0.98),一个退火的结束温度T1(通常为1e-8)
每进行一次查找,T = T * K
如果T = T1,查找停止,(即固体退火完成)
这里以POJ2420和HDU1109为例
POJ2420题目地址:
http://poj.org/problem?id=2420
这个题的题意是给n个点,让你找一个点的距离到这n个点的距离最小
直接设好温度和方向搜索就好了
注意POJ的G++编译器输出%0.0lf会WA
//POJ2420
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define it iterator
#define ll long long
#define eb emplace_back
#define lowbit(x) x & -x
#define all(x) x.begin(),x.end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define per(x,a,b) for(int x = a; x <= b; x++)
#define rep(x,a,b) for(int x = a; x >= b; x--)
#define IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) const int birth = ;
const int mo = ;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 1e9;
const double eps = 1e-;//停止的温度 char mp[][];
bool vis[][];
//******************THE PROGRAM BEGINING******************
int n;
int d[][] = { { , },{ -, },{ , },{ ,- } }; struct node
{
double x, y;
node() {}
}p[]; double dis(node p[], node x, int n)//求点X与各点距离
{
double sum = ;
per(i, , n - )
sum += sqrt(pow(p[i].x - x.x, ) + pow(p[i].y - x.y, )); return sum;
} double solve(node p[], int n)
{
double ans = 0x3f3f3f3f;
double T = ;//初始温度
double e = 0.98;//系数
node now = p[];//设置初始点(随便设个就好了)
while (T > eps)
{
bool flag = ;
while (flag)
{
flag = ;
per(i, , )
{
node next = now;
next.x += d[i][];
next.y += d[i][];
double temp = dis(p, next, n);
if (ans > temp)//更新
{
ans = temp;
flag = ;
now = next;
}
}
}
T *= e;//冷却
} return ans;
} int main()
{
//IO;
scanf("%d", &n);
per(i, , n - )
scanf("%lf %lf", &p[i].x, &p[i].y);
printf("%.0f\n", solve(p,n));
return ;
}
HDU1109题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1109
这个题的题意是给一个大小为x*y的房间和n个点,让你找在这个房间里一个点,这个点到n个点中离得它最近点之间距离是最大的
这个题如果跟上题一样做的话,有可能会掉进局部最优解,有两种解决方法:
1:在更新数值的时候允许一定几率跳出当前解法,几率随温度降低而降低
2:多设置几个点同时查找
我这里选择了第二种做法,个人认为更靠谱一点(其实都是随机的了,不过我觉得随机的越少越稳定)
这个题还要注意精度问题,这里采用2种不同的移动精度来保证速度和精度,根据当前温度来决定精度优先或移动优先
本来还打算记忆化搜索来加快速度的,结果MLE了。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define it iterator
#define ll long long
#define eb emplace_back
#define lowbit(x) x & -x
#define all(x) x.begin(),x.end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define per(x,a,b) for(int x = a; x <= b; x++)
#define rep(x,a,b) for(int x = a; x >= b; x--)
#define IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) const int birth = ;
const int mo = ;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 0x3fffffff;
const double eps = 1e-; //******************THE PROGRAM BEGINING******************
int n,x,y;
int d[][] = { { , },{ -, },{ , },{ ,- },{ , },{ -, },{ , },{ -,- } };
double d1[][] = { { 0.001, },{ -0.001, },{ ,0.001 },{ ,-0.001 },{ 0.001,0.001 },{ -0.001,0.001 },{ 0.001,-0.001 },{ -0.001,-0.001 } };
double ans[];
//bool vis[10000][10000];
struct node
{
double x, y;
node() {}
}p[], r[];; double dis(node p[], node x, int n)
{
double MIN = INF;
per(i, , n - )
{
MIN = min(MIN,sqrt(pow(p[i].x - x.x, 2.0) + pow(p[i].y - x.y, 2.0)));
} return MIN;
} node solve(node p[], int n)
{
double T = ;
double e = 0.98;
node now;
srand(birth);
per(i, , )
{
r[i].x = rand() % x;
r[i].y = rand() % y;
ans[i] = dis(p, r[i], n);
//vis[(int)r[i].x][(int)r[i].y] = 1;
}
while (T > eps)
{
bool flag = ;
while (flag)
{
flag = ;
per(j, , )
per(i, , )
{
node next = r[j];
//if (T < 0.1)
//printf("1: %lf %lf\n", next.x, next.y);
if (T > 0.5)
{
next.x += d[i][];
next.y += d[i][];
}
else
{
next.x += d1[i][];
next.y += d1[i][];
}
if (next.x < || next.y < || next.x > x || next.y > y)
continue;
//if (T > 1 && vis[(int)next.x][(int)next.y])
//continue;
double temp = dis(p, next, n);
//printf("2: %lf %lf\n", next.x ,next.y);
if (ans[j] < temp)
{
ans[j] = temp;
flag = ;
r[j] = next;
//if (T > 1)
//vis[(int)next.x][(int)next.y] = 1;
}
}
}
T *= e;
}
double min = ;
int pos;
per(i, , 5)
{
if (ans[i] > min)
min = ans[i],pos = i;
}
return r[pos];
} int main()
{
//IO;
int t;
scanf("%d", &t);
while (t--)
{
//ZERO(vis);
scanf("%d %d %d", &x, &y, &n);
per(i, , n - )
scanf("%lf %lf", &p[i].x, &p[i].y);
node ans = solve(p, n);
printf("The safest point is (%.1f, %.1f).\n", ans.x, ans.y);
/*
node a;
a.x = 1433.0;
a.y = 1669.9;
cout << dis(p, a, n) << endl;
a.y = 1669.8;
cout << dis(p, a, n) << endl;
*/
}
return ;
}
实际使用中,我们也不要把思路局限在对随机点的上下左右移动上,更要根据题目来判断对时间上和方向上的把握,灵活改变策略
如:POJ2069
大致就是这样吧
初探 模拟退火算法 POJ2420 HDU1109的更多相关文章
- 模拟退火算法-[HDU1109]
模拟退火算法的原理模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到 ...
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- [HDU1109]模拟退火算法
模拟退火的基本思想: (1) 初始化:初始温度T(充分大),初始解状态S(是算法迭代的起点),每个T值的迭代次数L (2) 对k=1,……,L做第(3)至第6步: (3) 产生新解$S\prime $ ...
- 【高级算法】模拟退火算法解决3SAT问题(C++实现)
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46453761 ---------------------------------- ...
- 模拟退火算法(SA)求解TSP 问题(C语言实现)
这篇文章是之前写的智能算法(遗传算法(GA).粒子群算法(PSO))的补充.其实代码我老早之前就写完了,今天恰好重新翻到了,就拿出来给大家分享一下,也当是回顾与总结了. 首先介绍一下模拟退火算法(SA ...
- 原创:工作指派问题解决方案---模拟退火算法C实现
本文忽略了对于模拟退火的算法的理论讲解,读者可参考相关的博文或者其他相关资料,本文着重于算法的实现: /************************************************ ...
- BZOJ 3680: 吊打XXX【模拟退火算法裸题学习,爬山算法学习】
3680: 吊打XXX Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 3192 Solved: 1198[Sub ...
- OI骗分神器——模拟退火算法
前言&&为什么要学模拟退火 最近一下子学了一大堆省选算法,所以搞一个愉快一点的东西来让娱乐一下 其实是为了骗到更多的分,然后证明自己的RP. 说实话模拟退火是一个集物理与IT多方面知识 ...
- 模拟退火算法 R语言
0 引言 模拟退火算法是用来解决TSP问题被提出的,用于组合优化. 1 原理 一种通用的概率算法,用来在一个打的搜索空间内寻找命题的最优解.它的原理就是通过迭代更新当前值来得到最优解.模拟退火通常使用 ...
随机推荐
- 【POJ1284】Primitive Roots
[题目大意] 给你一个素数n,求n的原根个数. [题解] 关于原根有一个定理: 定理:如果模有原根,那么它一共有个原根. 所以这题就是求phi[phi[n]] 很简单吧...(如果知道这个定理的话) ...
- jQuery代码在移动端不运行
今天写了个html网页发现在iOS系统上边不运行,于是真机连上Sarfari查看报错,于是乎 其实这是由于iOS的安全策略决定的,不允许加载非https的连接 报错:was not allowed t ...
- Intellij IDEA 安装插件 报 ‘plugin xxxx is incompatible‘ 解决方案
网上下载安装LOMBOK失败,直接下载插件安装: 在离线安装IDEA插件的时候,可能会出现该问题.引起的原因主要就是版本号不一致. 下面介绍下离线安装找到合适的版本号. 1.在IDEA的help-&g ...
- 面向对象的JavaScript-006-Function.prototype.apply()的3种作用
1. // Function.prototype.apply()的作用 // 1.Using apply to chain constructors Function.prototype.constr ...
- Python之FTP传输-乾颐堂
访问FTP,无非两件事情:upload和download,最近在项目中需要从ftp下载大量文件,然后我就试着去实验自己的ftp操作类,如下(PS:此段有问题,别复制使用,可以参考去试验自己的ftp类! ...
- 爬虫框架scrapy的基本内容
Scrapy介绍 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以帮助用户简单快速的部署一个专业的网络爬虫.如果说前面我们写的定制bs4爬虫是”手动挡“,那Scrapy就相当 ...
- 斐波那契数列—java实现
最近在面试的时候被问到了斐波那契数列,而且有不同的实现方式,就在这里记录一下. 定义 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- [GO]等待时间的使用
package main import ( "time" "fmt" ) func main() { <-time.After(*time.Second) ...
- ceph常用命令(转)
原文:http://michaelkang.blog.51cto.com/1553154/1698287 一:ceph集群启动.重启.停止 1:ceph 命令的选项如下: 选项简写描述 --verbo ...
- jQuary总结4: jquery操作字符串
1 jquery操作DOM -1 创建元素 $('<span>这是一个span元素</span>'); //创建了一个jQuery包装的span,此时并没有添加到DOM树上 - ...