永发信息网

把这伪码改成JAVA或C#

答案:2  悬赏:30  手机版
解决时间 2021-05-05 02:07
procedure simulated annealing
begin
t:=0;
initialize temperature T
select a current string vc at random;
evaluate vc;
repeat
repeat
select a new string vn in the neighborhood of vc; (1)
if f(vc)<f(vn)
then vc:=vn;
else if random [0,1] <exp ((f (vn)-f (vc))/T) (2)
then vc:=vn;
until (termination-condition) (3)
T:=g(T,t); (4)
T:=t+1;
until (stop-criterion) (5)
end;
最佳答案

恩翻译过来是: 第一个是:模拟退火算,:第二个是:遗传算法,第三个是:禁忌搜索算法(又叫“tabu搜索算法”).说白了你的意思就是用JAVA或者C#来实现这些算法,这些东西你随便 百度或者google一下文章多的是。这里我贴一个 C#实现一个模拟退火算法求TSP问题 希望对你有帮助。(不知道你从哪看来的这些 。。- -多多交流:JAVA交流群:53308744 ,C#交流群:62464919)


// ******************************************************************************************
// 物体降温退火过程中,其能量转移服从Boltzmann distribution规律 P(E) ∝ exp(-E/kT);
// P(E)是系统处于低能E的概率(probability),k为Boltzmann常数,T为系统温度。
// 模拟退火算法原理
// (1)初始温度T(0) = T0。迭代次数t=0,任意初始状态做当前解V(0)∈V
// (2)按某一规则有当前状态产生当前状态的下一次侯选状态。
// 计算目标代价变化△C,若△C<0则接受解,(△ delta)
// 否则考虑当前温度下状态活动概率 P(△C) = exp(-△C/T);P(△C)≥λ则接受解,否则不接受
// λ(lambda)为预先指定或随机产生的正数。 (0<λ<1)
// (3)按某种策略对T更新(降温)。
// (4)按某种标准检查退火过程是否应该结束,若不满足则转(2)
// (5)输出状态V(t)做问题解
// ******************************************************************************************

using System;
using System.Drawing;

namespace Algorithm.SimulatedAnnealing
{
public class TravellingSalesmanProblem
{
private Point[] _points;
private double[,] _distanceMatrix;
private double _initialTemperature = 0d;
private double _currentTemperature = 0d;

private int _routerCount;

private int[] _currentRouter;
private int[] _resultRouter;

private Random _rdm;
private bool _IsCalculated = false;
private DateTime calculate_begin;
private DateTime calculate_end;

public TravellingSalesmanProblem(Point[] pts)
{
_points = (Point[])pts.Clone();

_routerCount = _points.Length;
_currentRouter = new int[_routerCount];
_resultRouter = new int[_routerCount];
_distanceMatrix = new double[_routerCount, _routerCount];

_rdm = new Random((int)DateTime.Now.Ticks);

double minDistance = 10000d;
double maxDistance = 0d;

for(int i = 0; i < _points.Length; i++)
{
_currentRouter[i] = i; // init point router 1 > 2 > 3 > n
_resultRouter[i] = i;
for(int j = i + 1; j < _points.Length; j++)
{
double offsetX = (double)(_points[i].X - _points[j].X);
double offsetY = (double)(_points[i].Y - _points[j].Y);
double distance = Math.Sqrt(offsetX * offsetX + offsetY * offsetY); //计算两点间的距离
_distanceMatrix[i, j] = distance;
_distanceMatrix[j, i] = distance;
if(distance > maxDistance) maxDistance = distance;
if(distance < minDistance) minDistance = distance;
}
}
_initialTemperature = 20 * (_routerCount * maxDistance - _routerCount * minDistance);// 计算起始温度
_currentTemperature = _initialTemperature;
}

// 得出最优距离
public double GetTotalDistancOptimization()
{
if(!_IsCalculated)
Calculation();
return CalculateTotalDistance(_resultRouter);
}

// 得到最优路由
public int[] GetRouterOptimization()
{
if(!_IsCalculated)
Calculation();
return _resultRouter;
}

public double GetCalculationalTime()
{
if(_IsCalculated)
{
TimeSpan span = calculate_end - calculate_begin;
return span.TotalMilliseconds;
}
else
return 0d;
}

// 求出最优解
private void Calculation()
{
//Random rdm = new Random((int)DateTime.Now.Ticks);
calculate_begin = DateTime.Now;
while(true)
{
int IterCount = 0;
while(true)
{
double totalDistance1 = CalculateTotalDistance(_resultRouter);
BuildRandomRouter(); // change router
double totalDistance2 = CalculateTotalDistance(_currentRouter);


double deltaTotalDistance = totalDistance2 - totalDistance1;//delta
//原理见顶部注释
if(deltaTotalDistance <= 0d)
{
for(int i = 0; i < _resultRouter.Length; i++)
_resultRouter[i] = _currentRouter[i];//赋值到结果路由
}
else
{
//原理见顶部注释
double probability = Math.Exp( -(deltaTotalDistance/_currentTemperature));
double lambda = ((double)(_rdm.Next() % 10000)) / 10000d;//随机指定的正数lambda
if(probability > lambda)
{
for(int i = 0; i < _resultRouter.Length; i++)
_resultRouter[i] = _currentRouter[i];
}
}
if(IterCount >= _routerCount * _routerCount * _routerCount) //某一温度下的循环次数是点个数的三次方
{
break;
}
else
{
IterCount++;
}
}
if(_currentTemperature <= 0.01) //温度小于等于0.01时跳出外循环
{
break;
}
else
{
_currentTemperature = 0.96 * _currentTemperature;// 温度下降方式 T(k+1) = K*T(k),K=0.95
}
}
_IsCalculated = true;
calculate_end = DateTime.Now;
}


private double CalculateTotalDistance(int[] router)
{
double totalDistance = 0d;
for(int i = 0; i < router.Length - 1; i++)
{
totalDistance += _distanceMatrix[router[i], router[i + 1]];
}
totalDistance += _distanceMatrix[_currentRouter[router.Length - 1], router[0]];
return totalDistance;
}


// 按某种概率过程产生新的搜索状态
// 随机产生一个新的路由
private void BuildRandomRouter()
{
int rdm1, rdm2;
while(true)
{
rdm1 = _rdm.Next() % _currentRouter.Length - 1;
if(rdm1 >= 0) break;
}
while(true)
{
rdm2 = _rdm.Next() % _currentRouter.Length - 1;
if(rdm1 != rdm2 && rdm2 >= 0)
{
int temp = _currentRouter[rdm1];
_currentRouter[rdm1] = _currentRouter[rdm2];
_currentRouter[rdm2] = temp;
break;
}
}
}


}
}

全部回答
你好哦。 很高兴看到你的问题。 但是又很遗憾到现在还没有人回答你的问题。也可能你现在已经在别的地方找到了答案,那就得恭喜你啦。 可能是你问的问题有些专业了,没人会。或者别人没有遇到或者接触过你的问题,所以帮不了你。建议你去问题的相关论坛去求助,那里的人通常比较多,也比较热心,可能能快点帮你解决问题。 祝你好运~! 希望我的回答也能够帮到你! 谢谢
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
挑食怎么办?
大哥大姐,谁有收集的JS各种效果滑动门
炫舞进的时候不卡进去了总是慢动作
无锡工艺职业技术学院的宿舍是几人间的?
当个好人有啥错!
rain有自己的经典舞步吗,例如mj的月球漫步和
为什么QQ设了密保卡,上下子就自动重起?
盐城市中兴纸箱厂地址在什么地方,想过去办事
从哪里可以学习到黑客知识?
伊索寓言精彩句子赏析,伊索寓言精彩语句30字
浙江卫视星期1到星期5晚上8点播放什么节目?
大家帮帮忙了,怎样才能静下心来做自己没感觉
我喊我女友喊【宝贝】,她问我,她喊我什么啊
宏基4745G笔记本win7与手机的蓝牙连接问题
我想加盟一个店
推荐资讯
电脑桌面小问题
谁会制作内挂版本传奇脚本?跪求
方组词有哪些词语,方的组词有哪些
谁知道茂林初中的邮箱?快点告诉我
建华东道/滨河路(路口)在什么地方啊,我要过
用手机怎么充支付宝吗?
十一个月的小孩能喝豆浆吗
QQ飞车15号送那么多永久车
辽宁师范的校址
问问QQ斗地主
开业一个月的祝福短信,一月开始的加油的句子
用直充充电反而会因没电关机吗?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?