1. 程式人生 > >貪婪演算法實現tsp(擔貨郎問題)

貪婪演算法實現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;
}