您的位置:首页 > 其它

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

2010-01-27 07:04 507 查看
下面的两种算法都用和模拟退火算法一样的结构体:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: