模拟退火解决TSP问题
来源:互联网 发布:快乐宝拉 知乎 编辑:程序博客网 时间:2024/05/24 01:43
// monituihuo.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <math.h>#include <time.h>#include <iostream>#include <fstream>#include <Windows.h>using namespace std;const int MAXN = 27; //城市数量const double MAX = 27.0; //城市数量const double INIT_T = 3000; //初始温度const double RATE = 0.95; //温度衰减率const double FINNAL_T = 1E-10; //终止温度const int IN_LOOP = 15000; //内循环次数const int LIMIT = 10000; //概率选择上限const int FINL_LOOP = 1000; //外层循环double DD=0;double D_Length[MAXN][MAXN]={0};struct path{//定义路线结构 int citys[MAXN]; double length;}D_BestPath;struct point{//定义点结构 double x; double y;}D_Point[MAXN];//计算点和点之间的距离void point_dist(){ int i, j; double x; for(i=0; i<MAXN; i++) { for(j=i+1; j<MAXN; j++) { x = (D_Point[i].x-D_Point[j].x)*(D_Point[i].x-D_Point[j].x); x += (D_Point[i].y-D_Point[j].y)*(D_Point[i].y-D_Point[j].y); D_Length[i][j] = sqrt(x); D_Length[j][i] = D_Length[i][j]; } }}//初始化void init(){ int i; printf("初始状态路径:"); D_BestPath.length = 0; for(i=0; i<MAXN; i++) {//初始顺序经过路径 D_BestPath.citys[i] = i; printf("%d--", i); } for(i=0; i<MAXN-1; i++) {//计算路径长度 D_BestPath.length += D_Length[i][i+1]; } printf("\n路径长度为:%.3lf\n\n", D_BestPath.length); }void Dprintf(path p){//用于显示过程变化情况,打印 int i; printf("路径是:"); for(i=0; i<MAXN; i++) { printf("%d--", p.citys[i]); } printf("\n路径长度为:%.3lf\n\n", p.length);}//输入城市坐标信息void input(){ int i,ll = 1; ifstream f1("C:\\city.txt",ios::in); for(i=0; i<MAXN; i++){ if(ll % 2 != 0) f1 >> D_Point[i].x; if(ll % 2 == 0) f1 >> D_Point[i].y; ll++; } f1.close();}path getnext(path p){ path ret; int i, x, y; int te; ret = p; do { x = (int)(MAX*rand()/(RAND_MAX + 1.0)); y = (int)(MAX*rand()/(RAND_MAX + 1.0)); } while(x == y); te = ret.citys[x]; ret.citys[x] = ret.citys[y]; ret.citys[y] = te; ret.length = 0; for(i=0; i<MAXN-1; i++) {//计算路径长度 ret.length += D_Length[ret.citys[i]][ret.citys[i+1]]; } Dprintf(ret); DD++; return ret;}void sa(){ int i, P_L=0, P_F=0;; path curPath, newPath; double T = INIT_T; double p, delta; srand((int)time(0)); curPath = D_BestPath; while(true) { for(i=0; i<IN_LOOP; i++) { newPath = getnext(curPath); delta = newPath.length - curPath.length; if(delta < 0) {//更新长度 curPath = newPath; P_L = 0; P_F = 0; } else { p = (double)(1.0*rand()/(RAND_MAX+1.0)); if(exp(delta/T) < 1 && exp(delta/T) > p) { curPath = newPath; } P_L ++; } if(P_L > LIMIT) { P_F ++; break; } } if(curPath.length < newPath.length) { D_BestPath = curPath; } if(P_F > FINL_LOOP || T<FINNAL_T) break; T = T * RATE; }}void main(){ input(); point_dist(); init(); sa(); Dprintf(D_BestPath); printf("\n共测试%.0lf次\n", DD); system("pause");}
参考:http://blog.csdn.net/oxoxzhu/article/details/8142306
0 0
- 模拟退火解决TSP问题
- 模拟退火解决tsp
- 模拟退火算法解决TSP问题
- 模拟退火算法解决TSP问题
- 模拟退火算法解决TSP问题
- 模拟退火TSP问题
- 模拟退火算法-TSP问题
- 模拟退火解TSP问题
- 模拟退火算法 解决旅行商(TSP)问题
- 模拟退火算法解决TSP问题之大白话【转】
- [AI]模拟退火解决TSP问题(含源码)
- 模拟退火算法解决TSP(旅行商)问题
- 退火算法解决tsp问题
- 模拟退火算法求解TSP问题
- 模拟退火算法求解TSP问题
- TSP问题的模拟退火算法
- 模拟退火算法求解TSP问题
- TSP问题之模拟退火算法
- jvm钩子:Java应用中使用ShutdownHook友好地清理现场
- Android 之WebView+ProgressBar 完美结合
- Javascript单元测试框架Qunit和Jasmine的比较
- /var/run/yum.pid 已被锁定 解决办法
- Nginx Load Balancing
- 模拟退火解决TSP问题
- 通过java反射机制获取该类的所有属性类型、值。
- android APN解析
- 安装 Ubuntu 14.04 之后要做的一些事
- css3时钟
- 目标检测的图像特征提取之Haar特征
- spring自动扫描的问题
- 安装Android studio出现'tools.jar' seems to be not in Android Studio classpath的解决方法
- Android 自定义Dialog二次打开报错问题解决