模拟退火算法
来源:互联网 发布:虚拟现实java培训 编辑:程序博客网 时间:2024/05/29 19:18
模拟退火算法介绍
1.1 模拟退火算法的原理
模拟退火算法是一种元启发式(Meta-Heuristics)算法,来源于固体退火原理,将固体加热至充分高的温度,再让其徐徐冷却。加热时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到基态,内能减为最小。根据Metropolis准则,粒子在温度T时趋于平衡的概率为 ,其中E为温度T时的内能,ΔE为其改变量,k为Boltzmann常数。
1.2 模拟退火算法的模型
① 初始化:初始温度T(充分大),初始解状态S(算法迭代的起点), 每次迭代次数L
② for k=1 to L 做③至⑥
③ 产生新解S’
④ 计算增量Δt′=C(S′)-C(S),其中C(S)为评价函数
⑤ 若Δt′<0则接受S’作为新的当前解,否则以概率 接受S’作为新的当前解
⑥ 如果满足终止条件则输出当前解作为最优解,结束程序
⑦ T逐渐减少,然后转②
又是一种神奇的算法:模拟退火。就是一步步地退火,一步步地逼进。这种算法应该是只能用于一些单调函数性质的几何问题、现实问题吧。模拟退火与以往所学的算法大不相同,在某种程度上来说是一种万能算法。如果有什么图论、几何之类的题如果做不出,应该可以用模拟退火来试试人品。
POJ 2420 照着网上一个小代码轻松敲定。人品不错,一次AC。
Source Code
Problem: 2420 User: liu696639Memory: 352K Time: 0MSLanguage: C++ Result: Accepted
- Source Code
#include<iostream>#include<cmath>#include<stdio.h>using namespace std;struct POINT { double x,y; POINT(){x=0;y=0;} POINT (double a,double b){x=a;y=b;}}point[10000];double dist(POINT a,POINT b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int n;double all(POINT b){ double sum=0; for(int i=0;i<n;i++) { sum+=dist(b,point[i]); } return sum;}int dir[4][2]={0,1,0,-1,1,0,-1,0};//int dir[4][2]={(0,1),(0,-1),(1,0),(-1,0)};int main(){ while( scanf("%d",&n)!=EOF && n!=0) { for( int i=0;i<n;i++) { scanf("%lf%lf",&point[i].x,&point[i].y); } POINT ans=point[0]; double ret=all(ans); POINT temp; double step=500000; while( step>0.0002) { int flag=1; while( flag) { temp=ans; flag=0; for(int i=0;i<4;i++) { temp=POINT(ans.x+step*dir[i][0],ans.y+step*dir[i][1]); double tempdist=all(temp); if(ret>tempdist) { flag=1; ans=temp; ret=tempdist; break; }//cout<<ret<<" "; } } step/=2.0; }//cout<<endl; printf("%.lf\n",ret); }}
poj 2069 SUPER STAR:求点集的最小外接球
WA很多次,最后还是照着网上代码过的。自己一个人处理精度与寻找评估函数的能力还是很欠缺。Problem: 2069User: liu696639Memory: 200KTime: 16MSLanguage: C++Result: Accepted
Source Code
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
const double EP=1E-10;
const int maxn=10000;
struct POINT
{
double x,y,z;
POINT(double a,double b,double c){x=a,y=b,z=c;}
POINT(){}
}point[maxn];
double dist(POINT a,POINT b)
{
return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z) );
}
int n;
int maxdist(POINT p)
{
double ret=0,l;
int k=0;
for(int i=0;i<n;i++)
{
l=dist(p,point[i]);
if(l>ret)
{
ret=l;
k=i;
}
}
return k;
}
int main()
{
while( scanf("%d",&n)!=EOF && n!=0)
{
for(int i=0;i<n;i++)
{
scanf("%lf %lf %lf",&point[i].x,&point[i].y,&point[i].z);
}
POINT p(0,0,0);
double step=100;
double ans=dist(point[maxdist(p)],p);
int t=0;
while( step>=EP)
{
t++;
int id=maxdist(p);
double temp=dist(p,point[id]);
if(temp<ans)
ans=temp;
double dx=point[id].x-p.x;
double dy=point[id].y-p.y;
double dz=point[id].z-p.z;
p.x=p.x+(dx/temp)*step;
p.y=p.y+(dy/temp)*step;
p.z=p.z+(dz/temp)*step;
step*=0.99;
}
printf("%.5lf\n",ans);
}
}
- 模拟退火算法
- 模拟退火算法
- 模拟退火算法
- 模拟退火,遗传算法
- 模拟退火算法概述
- 模拟退火算法
- 遗传模拟退火算法
- 模拟退火遗传算法
- 模拟退火算法
- 模拟退火算法
- 模拟退火算法
- 解析模拟退火算法
- 模拟退火算法
- 模拟退火算法
- 模拟退火算法
- 模拟退火算法
- 模拟退火算法
- 模拟退火算法
- char转换为string的方法
- Linux僵尸进程产生及如何避免2
- java调用扫描仪1
- spring中的AOP
- HDU-1556 Color the ball 简单树状数组
- 模拟退火算法
- Android ndk下用AssetManager读取assets的资源
- HDU 3912 BFS走迷宫
- Java调用扫描仪2
- Maven:the way to add third-party jar-package to Local Maven Repository for using conveniently
- c语言的小总结
- 自创引擎II Demo
- 两个while循环求凸包 poj2187 poj1113
- PHP 代码延时执行后面的代码