旅行商问题Python代码,蛮力法解决旅行商问题 java

1,蛮力法解决旅行商问题 javaJAVA 图 最短路径
2 , 可运行的c语言程序旅行商求最短路径问题在无向完全图中,对于任意两个顶点vi和vj,我们可以在多项式时间内找到vi和vj这两个顶点之间的所有路径,选择其中路程最短的一条,令S[i,j]表示vi和vj这两个顶点之间最短距离的那条路径 。搜索路径S[i,j],找到vi到达的在S[i,j]上的第一个顶点,记该顶点为vk , 将其记录在数组中R[][],递归查找vi到vk和vk到vj的最短路径及其相应权值,最后将数组D[]中的顶点和权值之和打印出来即为所求,并用画图函数将行经过程画出 。旅行商问题(Traveling Salesman Problems,简写为TSP)是指一销售商从n个城市中的某一城市出发,不重复地走完其余n-1个城市并回到原出发点,在所有可能的路径中求出路径长度最短的一条 。编程提示:1分模块化编程,逐一解决问题 。本实验需要多次循环求解,所以要保证求解每一步的正确性
3,请问我用lingo求解多旅行商问题要求从一个点出发最后回到起点lingo如果你安装的版本完整 应该可以在安装文件夹的sample里面找到一个lsp的例子 不过用lingo解这种问题如果比较复杂是很难解出的MODEL: ! Traveling Salesman Problem for the cities of Atlanta, Chicago, Cincinnati, Houston, LA,Montreal; SETS:CITY / 1.. 6/: U; ! U( I) = sequence no. of city;LINK( CITY, CITY):DIST,! The distance matrix;X;! X( I, J) = 1 if we use link I, J; ENDSETS DATA:!Distance matrix, it need not be symmetric;DIST =0702454842 2396 11967020324 1093 21367644543240 1137 2180798842 1093 11370 1616 18572396 2136 2180 16160 29001196764798 1857 29000; ENDDATA !【旅行商问题Python代码,蛮力法解决旅行商问题 java】
4,求模拟退火算法解旅行商问题的C代码我自己写的:#include#include #include using namespace std; static double Tmax = 10, Tmin = 0.1, r = 0.999999; static int k = 100; inline void random_sele(int &fl, int &fp, int arr[], int n) { do { fl = rand() % n; fp = rand() % n; } while (fl == arr[fp] || arr[fl] == arr[fp]); }; inline bool accept(double r1, double r2, double T) { const unsigned int MASK((1 << 30) - 1); double p = 1.0 / (1.0 + exp((r2 - r1) / T)); unsigned int temp1=((rand()<<20)^(rand()<<10)^rand())&MASK; unsigned int temp2=((rand()<<20)^(rand()<<10)^rand())&MASK; double res=(1.0*temp1+1.0*temp2/MASK); return res < p*MASK; } void set_SA() { printf(" Tmax Tmin r k\n"); printf(" %.8f %.8f %.8f %d \n",Tmax,Tmin,r,k); printf("Input Tmax,Tmin,r,k:\n"); scanf("%lf%lf%lf%d",&Tmax,&Tmin,&r,&k); } void TSP_SA(int arr[], double &evl, int n, double map[][2000]) { double r1, r2, T = Tmax; int i, fl, sl, fp, sp; int show_s=0; srand(11827); while (T >= Tmin) { for (i = 0; i < k; i++) { random_sele(fl, fp, arr, n); sl = arr[fl], sp = arr[fp]; r1 = map[fl][sl] + map[fp][sp] + map[sp][arr[sp]]; r2 = map[fl][sp] + map[sp][sl] + map[fp][arr[sp]]; if (accept(r1, r2, T)) { arr[fp] = arr[sp]; arr[fl] = sp; arr[sp] = sl; sl = sp; evl = evl + r2 - r1; } } if(++show_s==1000000/k) { printf("T=%f evl=%f\n",T,evl); show_s=0; } T *= r; } }based on simulated annealing algorithm to solve traveling salesman problem5,蛮力法求解旅行商问题 求C代码#include<stdlib.h>#include<cmath>#include<algorithm>using namespace std;static double Tmax = 10, Tmin = 0.1, r = 0.999999;static int k = 100;inline void random_sele(int &fl, int &fp, int arr[], int n)dofl = rand() % n;fp = rand() % n;} while (fl == arr[fp] || arr[fl] == arr[fp]);};inline bool accept(double r1, double r2, double T)const unsigned int MASK((1 << 30) - 1);double p = 1.0 / (1.0 + exp((r2 - r1) / T));unsigned int temp1=((rand()<<20)^(rand()<<10)^rand())&MASK;unsigned int temp2=((rand()<<20)^(rand()<<10)^rand())&MASK;double res=(1.0*temp1+1.0*temp2/MASK);return res < p*MASK;}void set_SA()printf(" Tmax Tmin r k\n");printf(" %.8f %.8f %.8f %d \n",Tmax,Tmin,r,k);printf("Input Tmax,Tmin,r,k:\n");scanf("%lf%lf%lf%d",&Tmax,&Tmin,&r,&k);}void TSP_SA(int arr[], double &evl, int n, double map[][2000])double r1, r2, T = Tmax;int i, fl, sl, fp, sp;int show_s=0;srand(11827);while (T >= Tmin)for (i = 0; i < k; i++)random_sele(fl, fp, arr, n);sl = arr[fl], sp = arr[fp];r1 = map[fl][sl] + map[fp][sp] + map[sp][arr[sp]];r2 = map[fl][sp] + map[sp][sl] + map[fp][arr[sp]];if (accept(r1, r2, T))arr[fp] = arr[sp];arr[fl] = sp;arr[sp] = sl;sl = sp;evl = evl + r2 - r1;}}if(++show_s==1000000/k)printf("T=%f evl=%f\n",T,evl);show_s=0;}T *= r;}}搜一下:蛮力法求解旅行商问题 求C++代码#include#include #include using namespace std; static double Tmax = 10, Tmin = 0.1, r = 0.999999; static int k = 100; inline void random_sele(int &fl, int &fp, int arr[], int n) { do { fl = rand() % n; fp = rand() % n; } while (fl == arr[fp] || arr[fl] == arr[fp]); }; inline bool accept(double r1, double r2, double T) { const unsigned int MASK((1 << 30) - 1); double p = 1.0 / (1.0 + exp((r2 - r1) / T)); unsigned int temp1=((rand()<<20)^(rand()<<10)^rand())&MASK; unsigned int temp2=((rand()<<20)^(rand()<<10)^rand())&MASK; double res=(1.0*temp1+1.0*temp2/MASK); return res < p*MASK; } void set_SA() { printf(" Tmax Tmin r k\n"); printf(" %.8f %.8f %.8f %d \n",Tmax,Tmin,r,k); printf("Input Tmax,Tmin,r,k:\n"); scanf("%lf%lf%lf%d",&Tmax,&Tmin,&r,&k); } void TSP_SA(int arr[], double &evl, int n, double map[][2000]) { double r1, r2, T = Tmax; int i, fl, sl, fp, sp; int show_s=0; srand(11827); while (T >= Tmin) { for (i = 0; i < k; i++) { random_sele(fl, fp, arr, n); sl = arr[fl], sp = arr[fp]; r1 = map[fl][sl] + map[fp][sp] + map[sp][arr[sp]]; r2 = map[fl][sp] + map[sp][sl] + map[fp][arr[sp]]; if (accept(r1, r2, T)) { arr[fp] = arr[sp]; arr[fl] = sp; arr[sp] = sl; sl = sp; evl = evl + r2 - r1; } } if(++show_s==1000000/k) { printf("T=%f evl=%f\n",T,evl); show_s=0; } T *= r; } }

    推荐阅读