啊啊模拟退火56分,请帮我调一调参数,谢谢
查看原帖
啊啊模拟退火56分,请帮我调一调参数,谢谢
736891
Eternality楼主2022/11/8 13:09
#include<bits/stdc++.h>
using namespace std;
#define PDD pair<double,double>

const int N=1e3+10;
int n;
PDD ans;
double ansnum=2e9;

struct T
{
	double x,y,z;
}t[N];

double rand(double l,double r)
{
	return (double)rand()/RAND_MAX*(r-l)+l;
}

double dis(PDD x,T y)
{
	return sqrt((x.first-y.x)*(x.first-y.x)+(x.second-y.y)*(x.second-y.y));
}

double calc(PDD k)
{
	double res=0;
	for(int i=1;i<=n;i++)
	{
		res+=dis(k,t[i])*t[i].z;
	}
	if(res<ansnum)ans=k,ansnum=res;
	return res;
}

void fire()
{
	PDD now(rand(-1e4,1e4),rand(-1e4,1e4));
	for(double T=2e4;T>=1e-6;T*=0.999)
	{
		PDD k(rand(now.first+T,now.first-T),rand(now.second+T,now.second-T));
		double dt=calc(k)-calc(now);
		if(exp(-dt/T)>rand(0,1))now=k;
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf%lf",&t[i].x,&t[i].y,&t[i].z);
	}
	while((double)clock()/CLOCKS_PER_SEC<0.9)fire();//卡时 
	printf("%.3lf %.3lf",ans.first,ans.second);
	return 0;
}
2022/11/8 13:09
加载中...