【悬关】萌新刚学模拟退火的疑问
查看原帖
【悬关】萌新刚学模拟退火的疑问
481527
AC_CSP楼主2024/3/1 19:15

为什么加上:

else if (exp(-det/t) > Rand()) {
	tw = nw;
	tx = nx;
	ty = ny;
}

就不对了呢?

代码:

# include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
const double dd = 0.996;
int a[N], b[N], w[N], n;
int sumx, sumy; double tx, ty, tw;
inline double f(double p, double q) {
	double dw = 0;
	for ( int i = 1; i <= n; i ++ ) {
		double dx = p - a[i], dy = q - b[i];
		dw += sqrt(dx * dx + dy * dy) * w[i];
	}
	return dw;
} 
inline double Rand() {
	return 1.0 * rand() / RAND_MAX;
}
inline void sa() {
	double t = 1e5 ;
	while ( t >= 0.0001 ) {
//		cout << tx << " " << ty << " " << tw << "\n";
		double nx = tx + (Rand() * 2 - 1) * t;
		double ny = ty + (Rand() * 2 - 1) * t;
		double nw = f(nx, ny); double det = nw - tw;
		if ( det < 0 ) {
			tw = nw;
			tx = nx;
			ty = ny;
		}
//		else if (exp(-det/t) > Rand()) {
//			tw = nw;
//			tx = nx;
//			ty = ny;
//		}
		t *= dd;
	}
}
inline void solve() {
	sa(); sa(); sa(); sa();
}
int main () {
	srand(time(0));
	ios :: sync_with_stdio(false); cin.tie(0), cout.tie(0);
	cin >> n;
	for ( int i = 1; i <= n ; i ++ ) {
		cin >> a[i] >> b[i] >> w[i];
		sumx += a[i], sumy += b[i];
	}
	tx = sumx * 1.0 / n; ty = sumy * 1.0 / n;
	tw = f(tx, ty); solve();
	printf ( "%.3lf %.3lf\n", tx, ty );
	return 0;
}
2024/3/1 19:15
加载中...