跳到正文

算法·理论

模拟退火优化

Bobi2014约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年7月29日
更新
2026年7月29日
篇幅
约 7 分钟

版本

  1. v1当前

起因

今天下午我偶然发明了一种疑似新的优化,起因是我在做 P4035 时一直错一个点,于是我发明了一个基于模拟退火的优化。

模拟退火算法虽然可以卡过很多题,但是正确性仍然不是很高,能不能过一定程度上靠运气,比如 P4035,我参考了这篇题解,但是一直错一个点,于是我想到了优化方法。

思路

我们做完一遍模拟退火后可以大概地确定答案的范围一定在当前答案附近,但是有可能有一点误差,但是如果做多遍模拟退火,那可能就会出现答案又跳走或者精度仍然不够高的情况。

我们能不能先用一次温度较大的模拟退火来确定答案的范围,再用一次或多次温度较小的模拟退火来加强精度呢?

于是我发现了一个优化方法,那就是先做一次温度较大的模拟退火,然后逐步减少温度,实现提高正确率的方法。

例题

P4035

参考这篇题解,但是会错,所以加上我们的优化,代码如下

C++
#include<bits/stdc++.h>
#define db double
#define N 20
#define P(x) ((x) * (x))
using namespace std;
int n;
db p[N][N],dis[N],ans[N],delta[N],avg;
void calc(){
	avg = 0;
	for(int i = 1;i <= n + 1;i ++){
		dis[i] = 0;
		for(int j = 1;j <= n;j ++){
			dis[i] += P(p[i][j] - ans[j]);
		}
		dis[i] = sqrt(dis[i]);
		avg += dis[i];
	}
	avg /= (n + 1);
}
void solve(db T){
	while(T >= 1e-4){
		calc();
		for(int i = 1;i <= n;i ++){
			delta[i] = 0;
		}
		for(int i = 1;i <= n + 1;i ++){
			for(int j = 1;j <= n;j ++){
				delta[j] += (dis[i] - avg) * (p[i][j] - ans[j]) / avg;
			}
		}
		for(int i = 1;i <= n;i ++){
			ans[i] += delta[i] * T;
		}
		T *= 0.9999;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n + 1;i ++){
		for(int j = 1;j <= n;j ++){
			cin >> p[i][j];
			ans[j] += p[i][j];
		}
	}
	for(int i = 1;i <= n;i ++){
		ans[i] /= (n + 1);
	}
	solve(10000);
	while(clock() <= 0.9 * CLOCKS_PER_SEC){
		solve(1);
	}
	for(int i = 1;i <= n;i ++){
		cout << fixed << setprecision(3) << ans[i] << ' ';
	}
	return 0;
}

P5544

参考这篇题解的上半部分,但是会错,所以加上我们的优化,并且初始答案可以同时使用平均值和中位数,代码如下

C++
#include<bits/stdc++.h>
#define db long double
#define N 15
#define M 1005
#define P(x) ((x) * (x))
#define dist(ax,ay,bx,by) (sqrt(P((ax) - (bx)) + P((ay) - (by))))
using namespace std;
struct building{
	db x,y,r;
} b[N];
struct people{
	db x,y;
} p[M];
int n,m,ans,o[M],t1x,t1y,t2x,t2y;
db R,ans_x,ans_y;
int calc(db x,db y){
	db r = R;
	for(int i = 1;i <= n;i ++){
		r = min(r,dist(x,y,b[i].x,b[i].y) - b[i].r);
	}
	int cnt = 0;
	for(int i = 1;i <= m;i ++){
		if(dist(x,y,p[i].x,p[i].y) <= r){
			cnt ++;
		}
	}
	return cnt;
}
void solve(db x,db y,db T,db eps,db drc){
	while(T > eps){
		db nx = (rand() * 2 - RAND_MAX) * T + x;
		db ny = (rand() * 2 - RAND_MAX) * T + y;
		int cnt = calc(nx,ny) - ans;
		if(cnt > 0){
			x = nx;
			y = ny;
			ans += cnt;
			ans_x = nx;
			ans_y = ny;
		}else if(rand() > exp(-(db)(cnt) / T) * (db)(RAND_MAX)){
			x = nx;
			y = ny;
		}
		T *= drc;
	}
}
int main(){
	srand(time(0));
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> R;
	for(int i = 1;i <= n;i ++){
		cin >> b[i].x >> b[i].y >> b[i].r;
	}
	for(int i = 1;i <= m;i ++){
		cin >> p[i].x >> p[i].y;
		ans_x += p[i].x;
		ans_y += p[i].y;
		o[i] = p[i].x;
	}
	ans_x /= m;
	ans_y /= m;
	t1x = ans_x;
	t1y = ans_y;
	ans = calc(ans_x,ans_y);
	while(clock() < 0.4 * CLOCKS_PER_SEC){
		solve(t1x,t1y,2e4,1e-14,0.999);
	}
	sort(o + 1,o + m + 1);
	if((m & 1) == 0){
		t2x = (o[m / 2] + o[m / 2 + 1]) / 2.0;
	}else{
		t2x = o[m / 2 + 1];
	}
	for(int i = 1;i <= m;i ++){
		o[i] = p[i].y;
	}
	sort(o + 1,o + m + 1);
	if((m & 1) == 0){
		t2y = (o[m / 2] + o[m / 2 + 1]) / 2.0;
	}else{
		t2y = o[m / 2 + 1];
	}
	while(clock() < 0.6 * CLOCKS_PER_SEC){
		solve(t2x,t2y,2e4,1e-14,0.999);
	}
	while(clock() < 0.9 * CLOCKS_PER_SEC){
		solve(ans_x,ans_y,10,1e-15,0.999);
	}
	cout << ans;
	return 0;
}

总结

该优化可以使用于所有生成新的答案与温度有关的模拟退火算法,比如其中几道例题,它每次生成的答案是和温度有关的。

该优化也可以用于部分生成答案与温度无关的模拟退火算法。

后记

本人是蒟蒻,内容上如有错误、疏漏、疑问可以私信交流。

原文