企业网站 建设公司,长沙定制网站建设,网站建设要学哪些软件有哪些方面,久久诗歌网/*
Simulated Annealing(模拟退火算法)
求解旅行商问题(TSP)
网上给的数据是31个省会的坐标#xff0c;蚁群算法得到的结果是#xff1a;15378
我算的结果中#xff0c;最好的一次是#xff1a;15495
*/
#includeiostream
#includecstdio
#includecstd… /*
Simulated Annealing(模拟退火算法)
求解旅行商问题(TSP)
网上给的数据是31个省会的坐标蚁群算法得到的结果是15378
我算的结果中最好的一次是15495
*/
#includeiostream
#includecstdio
#includecstdlib
//#includectime
#includecmath
#includetime.h
#define N 31 //城市个数
#define Tmax 8000 //初始温度
#define Tmin 1E-10 //终止温度
#define RATE 0.95 //温度衰减率
#define in_loop 13000 //内层循环次数
#define out_loop 2000 //外层循环次数
#define p_limit 10000 //概率选择次数using namespace std;//31个省会x和y的坐标
double x[N]{1304,3639,4177,3712,3488,3326,3238,4196,4312,4386,3007,2562,2788,2381,1332,3715,3918,4061,3780,3676,4029,4263,3429,3507,3394,3439,2935,3140,2545,2778,2370};
double y[N]{2312,1315,2244,1399,1535,1556,1229,1004,790,570,1970,1756,1491,1676,695,1678,2179,2370,2212,2578,2838,2931,1908,2367,2643,3201,3240,3550,2357,2826,2975};//两个城市之间的距离
double d[N][N];void init(){//初始化任意两个城市的距离for(int i0;iN;i)for(int j0;ji;j){d[i][j]d[j][i]sqrt((y[j]-y[i])*(y[j]-y[i])(x[j]-x[i])*(x[j]-x[i]));}for(int i0;iN;i)d[i][i]0;srand((unsigned)time(0));
}//路径
class path{
public:path(){}~path(){}int city[N]; //依次经过的城市编号double dis; //总的距离void totalLen(){dis0;for(int i0;iN-1;i)disd[city[i]][city[i1]];disd[city[0]][city[N-1]];}
};//最优解
path bestpath;//产生新路径
path newpath(path prepath){path newp prepath;int x,y,t;do{xrand()%N;yrand()%N;}while(xy);tnewp.city[x];newp.city[x]newp.city[y];newp.city[y]t;newp.totalLen();return newp;
}//Annealing
void Anne()
{init();//随机选一个初始解for(int i0;iN;i)bestpath.city[i]i;bestpath.totalLen();int out_t0,p_t0;double TTmax,delta,prob, rnd;path np,cp; //新路径当前路径cpbestpath;while(out_tout_loopTTmin){for(int i0;iin_loop;i){npnewpath(cp);if(np.discp.dis){cpnp;out_t0;p_t0;}else{deltanp.dis-cp.dis;probexp(-delta/T);rndrand()%10000/10000.0;if(probrnd)cpnp;p_t;}if(p_tp_limit){out_t;break;}}if(cp.disbestpath.dis)bestpathcp;T*RATE;printf(dis %f\n,bestpath.dis);}
}int main()
{Anne();return 0;
} 转载于:https://www.cnblogs.com/littlehoom/p/3612306.html