获得较少的最佳解决方案TSP

时间:2019-07-07 10:42:39

标签: c

我正在尝试对tsplib API进行一些测试,这些城市的API数据文件相互协调,成本最高,并且游览成本最高。

https://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/tsp/

我正在使用 lysses22 数据文件, 我的程序

  • 从ulysses22.tsp文件中读取每个城市的坐标,
  • 生成成本矩阵,
  • 并计算游览费用。这是 代码:
#include <stdio.h>
#include <math.h>
#define NbrCities 22

double city_x[NbrCities], city_y[NbrCities];
float citymap[NbrCities*NbrCities];
int ids[NbrCities];


 float L2distance(float x1, float y1, float x2, float y2) {
    float x_d = pow(x1 - x2, 2);
    float y_d = pow(y1 - y2, 2);
    return sqrt(x_d + y_d); 
}


void costMatrix(float *citymap){
    for(int i=0; i<NbrCities; i++) {
        for(int j=0; j<NbrCities; j++) {
            if(i!=j) {
                citymap[i*NbrCities+j] = L2distance(city_x[i], city_y[i], city_x[j], city_y[j]);
            } else {
                citymap[i*NbrCities+j] = 0;
            }
        }
     }
}


float cost(int* population, float* citymap) {

   float distance = 0;
   float population_cost;
    for (int j = 0; j < NbrCities-1; j++) {
        distance += citymap[ NbrCities*population[j] + population[j+1] ];
    }
        distance += citymap[ NbrCities*population[0] + population[NbrCities-1] ];

     return distance;
}


int main()
{

    FILE *myFile,*out;
    myFile = fopen("test.txt", "r");            
    out = fopen("out.txt", "w");

    int tour[]={1,14,13,12,7,6,15,5,11,9,10,19,20,21,16,3,2,17,0,4,18,8};

   myFile = fopen("test.txt", "r");
        for (int i = 0; i < NbrCities; i++)
            {
               fscanf(myFile,"%d %lf %lf",&ids[i],&city_x[i],&city_y[i]);  
            }

        costMatrix(citymap);



printf("  %f ",cost(tour,citymap));


 return 0;

}

问题是我要支付 153.66

但是在网站https://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/STSP.html中,最佳解决方案必须是lysses22数据文件为: 7013

我不知道为什么我的结果少于网站上的解决方案! 在此先感谢!!!

0 个答案:

没有答案