当前位置:编程学习 > JAVA >>

把这伪码改成JAVA或C#

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;
补充:procedure genetic algorithm 
begin
initialize a group and evaluate the fitness value ; (1)
while not convergent (2)
begin
select; (3)
if random[0,1]<pc then
crossover; (4)
if random (0,1)<pm then
mutation; (5)
end;
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;
}
}
}

}
}

你好哦。
很高兴看到你的问题。
但是又很遗憾到现在还没有人回答你的问题。也可能你现在已经在别的地方找到了答案,那就得恭喜你啦

上一个:Java中@SuppressWarings("serial")
下一个:JAVA中的异常处理机制的原理

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,