HDU 5017 模拟退火算法

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1E9 + 7;
const double eps = 1E-8;
int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[8] = {1, -1, 0, 0, 1, 1, -1, -1};
double a, b, c, d, e, f;
double dis(double x, double y, double z)
{
	return sqrt(x * x + y * y + z * z);//计算距离
}
double calc(double x, double y)//有x,y计算z点
{
	double A = c;
	double B = d * y + e * x;
	double C = f * x * y + a * x * x + b * y * y - 1.0;
	double delta = B * B - 4.0 * A * C;//解二元一次方程组
	if (delta < 0.0)return MOD + 10.0;//对于当前x,y 方程组无解
	delta = sqrt(delta);//开根号
	double soz1 = (-B + delta) / (2.0 * A);
	double soz2 = (-B - delta) / (2.0 * A);
	if (dis(x, y, soz1) < dis(x, y, soz2))
		return soz1;
	return soz2;//取距离原点更近的z点
}
double Simulated_Annealing()
{
	double x = 0, y = 0, z = sqrt(1.0 / c);//从z轴与椭球的交点开始
	double step = 1.0, rate = 0.99;
	while (step > eps)//步长大于0,退火工作未完成
	{
		for (int k = 0; k < 8; k++)//分别朝向八个方向运动
		{
			double kx = x + step * dx[k];//每次运动step个步长
			double ky = y + step * dy[k];
			double kz = calc(kx, ky);
			if (kz >= MOD) continue;//对于x,y,方程无解
			if (dis(kx, ky, kz) < dis(x, y, z))
				x = kx, y = ky, z = kz;//更新
		}
		step *= rate;//运动步长以rate的速率变小,这就是在退火
	}
	return dis(x, y, z);//返回长度
}
int main(int argc, char const *argv[])
{
	while (cin >> a >> b >> c >> d >> e >> f)
		cout << setprecision(7) << fixed << Simulated_Annealing() << endl;
	return 0;
}


看注释

参考资料:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html

http://www.cnblogs.com/whatbeg/p/3975624.html


#include <bits/stdc++.h>
using namespace std;
const int MOD = 1E9 + 7;
const double eps = 1E-8;
int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[8] = {1, -1, 0, 0, 1, 1, -1, -1};
double a, b, c, d, e, f;
double dis(double x, double y, double z)
{
	return sqrt(x * x + y * y + z * z);
}
double calc(double x, double y)
{
	double A = c;
	double B = d * y + e * x;
	double C = f * x * y + a * x * x + b * y * y - 1.0;
	double delta = B * B - 4.0 * A * C;
	if (delta < 0.0)return MOD + 10.0;
	delta = sqrt(delta);
	double soz1 = (-B + delta) / (2.0 * A);
	double soz2 = (-B - delta) / (2.0 * A);
	return (dis(x, y, soz1) < dis(x, y, soz2) ? soz1 : soz2);
}
double Simulated_Annealing()
{
	double x = 0, y = 0, z = sqrt(1.0 / c);
	for (double step = 1.0, rate = 0.99; step > eps; step *= rate)//模拟退火的全过程
		for (int k = 0; k < 8; k++)
		{
			double kx = x + step * dx[k];
			double ky = y + step * dy[k];
			double kz = calc(kx, ky);
			if (kz >= MOD) continue;
			if (dis(kx, ky, kz) < dis(x, y, z))
				x = kx, y = ky, z = kz;
		}
	return dis(x, y, z);
}
int main(int argc, char const *argv[])
{
	while (cin >> a >> b >> c >> d >> e >> f)
		cout << setprecision(7) << fixed << Simulated_Annealing() << endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值