贪婪算法实现tsp(担货郎问题)

下面的两种算法都用和模拟退火算法一样的结构体:

typedef struct
{                         // A struct to store a problem description. Welcome to pointers
  char name[SIZE];
  char comment[SIZE];
  int type;
  int capacity;
  int dimension;
  int edge_weight_type;?
  int edge_weight_format; 
  int edge_data_format; 
  int node_coord_type;
  int display_data_type;
  double *node_coord;
  int *matrix;
  int *tour;
  double *display;
  int *cityindex;
} Problem;

 

 

                                      Greedy algorithm


In this algorithm ,We can make whatever choice seems best at the moment and then solve the subproblems that arise later , a greedy algorithm never reconsiders its choices .
A greedy algorithm takes the path of ”least resistance” towards a solution ,it tends to build a solution by adding one component at a time and never ”undoes” a decision - they are one-way processes .
Greedy algorithms mostly (but not always) fail to find the globally optimal solution, because they usually do not operate exhaustively on all the data. They can make commitments to certain choices too early which prevent them from finding the best overall solution later. For example, all known greedy algorithms for the TSP and all other NP-complete problems do not consistently find optimum solutions. Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum. If a greedy algorithm can be proven to yield the global optimum for a given problem class, it typically becomes the method of choice because it is faster than other optimisation methods .

 

 

void greedy(Problem *Pb)

 int t=1;//tempcity index.----
 double sum;
 double tempdistance;
 double smallest;
 int *path;
 int temp;
 int j;
 int m;
 int counter=0;
 int k=0;
 int u=0;
 path=malloc(sizeof(int));
 path[0]=1;
 sum=0;
 
 for(k;k<Pb->dimension;k++)
  {    u=0;
          j=1;
   if(counter!=Pb->dimension-1)
   {
   for( j;j<Pb->dimension;j++)
   {
    if(Pb->cityindex[j]!=-1)
    {
     tempdistance=distance(Pb,t,Pb->cityindex[j]);
     if(u==0)
      {
       smallest=tempdistance;
       
      }
      u++;
       if(tempdistance<=smallest)
      {
       smallest=tempdistance;
       temp=j;
      } 
     
    }
   }
   path[counter+1]=Pb->cityindex[temp]; 
   t=Pb->cityindex[temp]; 
   sum=sum+smallest;
   // t=Pb->cityindex[temp];/
   Pb->cityindex[temp]=-1;
       counter++;
         k=temp-1;
   }
   else
   {
    break;
   }
  }
 sum=sum+distance(Pb,t,Pb->cityindex[0]);//back to the start city
 printf("the tour in greedy is:");
    for(m=0;m<Pb->dimension;m++)
    {
     printf("->%d ",path[m]);
   } 
   printf("/n the total distance is %f:",sum);
}

 

Random algorithm(为tsp随机产生一条合理路线)


Random algorithm is an algorithm which we choose the necessary data randomly .Like in the TSP problem ,we choose a city in random as the destination until all the cities have been visited .
A random algorithm is very simple in theory , it is stochastic .A best solution is selected when the algorithm has been implemated as well as the possibility of many other solutions exist , the solution is uncertainly produced .That is to say ,finding a solution is easy ,but the solution is good or bad ?we don’t know ,maybe it’s the best , maybe it’s the worst .

 

double random(Problem *Pb)

 int *path;
 int counter=0;
 int j;
 int i=1;
 int k=0;
 double sum=0;
 double d;
 int current=1;//--------current variable for city number;
 //int t=1;
 path=calloc(Pb->dimension,sizeof(int));
 path[0]=1;
 for(i;i<Pb->dimension;i++)
 { 
  srand((unsigned)time(NULL));//initialize the seed
  
  do
     {
      j=1+rand()%(Pb->dimension-1);// 1.....Pb->dimension-1
     }while(Pb->cityindex[j]==-1);
         printf("j is %d:/n",j);
   d=distance(Pb,current,Pb->cityindex[j]);
   sum=sum+d;
      path[counter+1]=Pb->cityindex[j];
   current=Pb->cityindex[j];
      Pb->cityindex[j]=-1;
      counter++;
   
 }
 sum=sum+distance(Pb,current,Pb->cityindex[0]);//to the start city
 printf("the tour in random is:");
 for( k=0;k<Pb->dimension;k++)
 {
  printf("-> %d",path[k]);
 }
 printf("/n the distance for random is: %f",sum);

 return sum;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值