贪婪算法实现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;
}
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;
}
相关文章推荐
- 遗传算法实现 但货郎问题(tsp)
- 2-opt求解TSP(旅行商)问题的python实现
- 利用HTML5 Canvas和Javascript实现的蚁群算法求解TSP问题演示
- 背包问题 插入排序 快排 TSP问题(hamiltonian 回路) Python实现
- TSP问题之基本蚁群算法cpp实现
- 蚁群算法java实现以及TSP问题蚁群算法求解
- 遗传算法Java实现以及TSP问题遗传算法求解
- 货郎问题 Java 实现
- 用C#实现蚁群算法解决TSP问题
- 利用模拟退火算法求解TSP问题(C++实现)
- TSP问题之基本遗传算法 cpp实现
- 遗传算法解决TSP问题(c++实现)
- 模拟退火算法(SA)求解TSP 问题(C语言实现)
- 遗传算法与TSP问题的MATLAB实现
- 蚁群算法java实现以及TSP问题蚁群算法求解
- 关于用C#实现蚁群算法解决TSP问题
- 遗传算法 解决TSP问题 C++实现(一)
- 蚁群算法java实现以及TSP问题蚁群算法求解
- 模拟退火算法原理及求解TSP问题的Java实现
- 遗传算法解决TSP问题实现以及与最小生成树的对比