旅行商问题

发布于 2024-10-17 14:33:14 字数 2653 浏览 3 评论 0原文

我正在尝试根据旅行商问题算法用 C++ 开发一个程序。我需要一个距离矩阵和一个成本矩阵。使用所有公式后,我得到一个新的结果矩阵。但我不明白该矩阵显示什么。 假设得到的矩阵是:

1 2 3
4 5 6
7 8 9

现在我想知道这个矩阵显示什么?假设我有3个城市要穿越。

请告诉我流程。如果有这个算法的示例程序会更有利.. 谢谢。

我的程序是:

#include<iostream.h>
#include<conio.h>
#include <stdlib.h>

void main()
{
    clrscr();
    int a,b,c,d,ctr,j,Q=1,K=1 ;
    float q0=0.7, p = 0.5 ;
    int phe[3][3];
    double dist[3][3] , mem[3][3],exp[3][3],eplt[3][3], rnd;
    cout<<"enter the iterations, cities , ants ";
    cin>>a>>b>>c;
    for (int i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            dist[i][j]=(double)rand()/(double)RAND_MAX;
            if (i==j)
            dist[i][j]=0;
        }
    }
    for (i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            cout<< dist[i][j]<<"\t";
        }
        cout<<"\n";
    }

    cout<<"pheromone matrix "<<endl;
    for (i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            if (i==j)
                phe[i][j]=0;
            else
                phe[i][j]=1;
        }
    }

    for ( i=0;i<3;i++)
    {
        for ( j=0;j<3;j++)
        {
            cout<< phe[i][j]<<"\t";
        }
        cout<<"\n";
    }

    cout<< "after iteration "<<endl;
    for (i=0;i<3;i++)
    {
        ctr=0;
        for (int k=0;k<3;k++)
        {
            // mem[i][k]=(rand()%b)+1;
            // cout<<"memory"<<mem[i][k]<<"\n";
            rnd= (double)rand()/(double)RAND_MAX;
            cout<<"hhhhhhh"<<rnd;
            if (rnd<=q0)
            {
                cout<<"Exploitation\n";
                eplt[i][ctr] =(p*phe[i][k])+(Q/K);  
            }
            else
            {
                cout<<"EXPLORATION\n";
                eplt[i][ctr]= phe[i][k]/dist[i][k];
            }
            ctr++;
        }
    }
    for (i=0;i<3;i++)
    {
        for (int k=0;k<3;k++)
        {
            cout <<eplt[i][k]<<"\t";
        }
        cout<<"\n";
    }
    getch();
}

输出:

enter the iterations, cities , ants 3
4
4
0       0.003967        0.335154
0.033265        0       0.2172
0.536973        0.195776        0
pheromone matrix
0       1       1
1       0       1
1       1       0
after iteration
hhhhhhh0.949919EXPLORATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.949919EXPLORATION

I am trying to develop a program in C++ from Travelling Salesman Problem Algorithm. I need a distance matrix and a cost matrix. After using all the formulas, i get a new resultant matrix. But I dont understand what that matrix shows.
Suppose the resultant matrix is:

1 2 3
4 5 6
7 8 9

Now I want to know what this matrix shows? Assume I have 3 cities to traverse.

Please tell me the flow. A sample program of this algorithm will be more favorable..
Thank you.

My Program is:

#include<iostream.h>
#include<conio.h>
#include <stdlib.h>

void main()
{
    clrscr();
    int a,b,c,d,ctr,j,Q=1,K=1 ;
    float q0=0.7, p = 0.5 ;
    int phe[3][3];
    double dist[3][3] , mem[3][3],exp[3][3],eplt[3][3], rnd;
    cout<<"enter the iterations, cities , ants ";
    cin>>a>>b>>c;
    for (int i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            dist[i][j]=(double)rand()/(double)RAND_MAX;
            if (i==j)
            dist[i][j]=0;
        }
    }
    for (i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            cout<< dist[i][j]<<"\t";
        }
        cout<<"\n";
    }

    cout<<"pheromone matrix "<<endl;
    for (i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            if (i==j)
                phe[i][j]=0;
            else
                phe[i][j]=1;
        }
    }

    for ( i=0;i<3;i++)
    {
        for ( j=0;j<3;j++)
        {
            cout<< phe[i][j]<<"\t";
        }
        cout<<"\n";
    }

    cout<< "after iteration "<<endl;
    for (i=0;i<3;i++)
    {
        ctr=0;
        for (int k=0;k<3;k++)
        {
            // mem[i][k]=(rand()%b)+1;
            // cout<<"memory"<<mem[i][k]<<"\n";
            rnd= (double)rand()/(double)RAND_MAX;
            cout<<"hhhhhhh"<<rnd;
            if (rnd<=q0)
            {
                cout<<"Exploitation\n";
                eplt[i][ctr] =(p*phe[i][k])+(Q/K);  
            }
            else
            {
                cout<<"EXPLORATION\n";
                eplt[i][ctr]= phe[i][k]/dist[i][k];
            }
            ctr++;
        }
    }
    for (i=0;i<3;i++)
    {
        for (int k=0;k<3;k++)
        {
            cout <<eplt[i][k]<<"\t";
        }
        cout<<"\n";
    }
    getch();
}

OUTPUT:

enter the iterations, cities , ants 3
4
4
0       0.003967        0.335154
0.033265        0       0.2172
0.536973        0.195776        0
pheromone matrix
0       1       1
1       0       1
1       1       0
after iteration
hhhhhhh0.949919EXPLORATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.356777EXPLOITATION
hhhhhhh0.949919EXPLORATION

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小梨窩很甜 2024-10-24 14:33:14

首先,我猜当你说我的程序时,你的意思是论文中的程序,因为它基本上是过时的C++。标准库头文件没有附加 .h,而 conio.h 是一个 MS-DOS 头文件 - 我见过的大多数代码都使用来自 Borland Turbo 的代码C++。如果您打算尝试在现代系统上编译该演示,请记住这一点。

接下来,您看到的是邻接矩阵。我根本不相信矩阵是输出的一部分;我相信它是用于演示目的的模型的一部分。我相信,鉴于您有一个信息素矩阵,您在这里看到的是蚁群优化,一种解决 TSP 和其他可简化为 TSP 的问题的概率方法。

从你的输出来看,不清楚结果存储在哪里或如何存储,而且由于这是家庭作业,我很懒,你只是要求一个直接的答案,我不会阅读该代码。蚁群优化的前提是蚂蚁在图上随机行走所铺设的信息素轨迹,会随着时间(迭代次数)而衰减。蚂蚁沿着特定顶点(距离)移动的时间越长,所放置的信息素衰减得越多。此时,蚂蚁开始根据沿路径铺设的信息素的强度做出决定。因此,蚂蚁开始比其他路线更喜欢某些路线,并沿着该路线不断强化信息素。

因此,在那里的某个地方,必须有一个类似于邻接矩阵的矩阵,存储每条路线的信息素水平。结合路线的长度,每次迭代都应该检测衰减率。

First up, I'm guessing when you say My Program you mean The program in the paper since it is basically out of date C++. Standard library headers don't have .h appended, and conio.h is an MS-DOS header - most code that I've seen that uses that comes from Borland Turbo C++. Worth bearing in mind if you're going to try to compile that demo on a modern system.

Next up, what you're looking at is an adjacancy matrix. I don't believe that matrix is part of the output at all; I believe it is part of the model being used, for demonstration purposes. I believe, given you have a pheromone matrix, that what you're looking at here is Ant Colony Optimisation, a probabilistic method of solving the TSP and other problems that can be reduced to it.

From your output, it isn't clear where or how the result is being stored, and since this is homework, I am lazy and you're just asking for an outright answer, I'm not going to read that code. The premise of Ant Colony optimisation is that pheromone trails laid by ants, which walk the graph at random, decay over time (number of iterations). The longer it takes an ant to move along a particular vertex (distance), the more the laid pheromone decays. At this point, ants start to make decisions based on the strength of the laid pheromone along a path. So what happens is ants start to prefer certain routes over others, and continually re-inforce the pheromone along that path.

So, somewhere in there, there must be a matrix like the adjacancy matrix, storing the pheromone levels for each route. Combined with the length of the route, each iteration should detect a rate of decay.

彩虹直至黑白 2024-10-24 14:33:14
  • 您的输入变量 a、b、c 从未被使用。
  • 您的变量 ctr 的使用方式与同一循环的变量 k 完全相同。
  • 你的现象矩阵表明使用了蚁群优化算法,为什么不在你的问题中说出来呢?
  • 这样的“迭代”应该是迭代的,所以你给我们的输出(不是正常的输出)可能不是最终的解决方案,而是算法的临时结果。
  • Your input variables a, b, c are never used.
  • Your variable ctr is used in the exact same incremental way as the variable k of the same loop.
  • Your phenomone matrix indicates use of an ant colony optimization algorithm, why just not say it in your question ?
  • Such "iteration" should be, well, iterated, so probably the output you give us (which is not a normal output) is not the definitive solution, rather a provisory result of the algorithm.
行雁书 2024-10-24 14:33:14

在这篇文章中,讨论了简单解决方案的实现。

  • 将城市 1 或 0 视为起点和终点。由于路线是
    循环,可以考虑任意点作为起点。

  • 生成所有 (n-1) 个!城市的排列。

  • 计算每个排列的成本并跟踪最小成本
    排列。

  • 返回具有最小成本的排列。

#include
使用命名空间 std;

int main(void){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        int graph[n][n];
        for(int i =0;i<n;i++){
            for(int j =0;j<n;j++){
                scanf("%d",&graph[i][j]);
            }
        }
        vector<int> v;
        int s = 0;
        for(int i =0;i<n;i++){
            if(i!=s){
                v.push_back(i);
            }
        }
        int ans = INT_MAX;
        do{
            int current_pathsum = 0;
            int k = s;
            for(int i = 0;i<v.size();i++){
                current_pathsum += graph[k][v[i]];
                k = v[i];
            }
            current_pathsum += graph[k][s];
            ans = min(ans,current_pathsum);
        }while(next_permutation(v.begin(),v.end()));
        cout<<ans<<endl;
    }
}

In this post, implementation of simple solution is discussed.

  • Consider city 1 or 0 as the starting and ending point. Since route is
    cyclic, we can consider any point as starting point.

  • Generate all (n-1)! permutations of cities.

  • Calculate cost of every permutation and keep track of minimum cost
    permutation.

  • Return the permutation with minimum cost.

#include <bits/stdc++.h>
using namespace std;

int main(void){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        int graph[n][n];
        for(int i =0;i<n;i++){
            for(int j =0;j<n;j++){
                scanf("%d",&graph[i][j]);
            }
        }
        vector<int> v;
        int s = 0;
        for(int i =0;i<n;i++){
            if(i!=s){
                v.push_back(i);
            }
        }
        int ans = INT_MAX;
        do{
            int current_pathsum = 0;
            int k = s;
            for(int i = 0;i<v.size();i++){
                current_pathsum += graph[k][v[i]];
                k = v[i];
            }
            current_pathsum += graph[k][s];
            ans = min(ans,current_pathsum);
        }while(next_permutation(v.begin(),v.end()));
        cout<<ans<<endl;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文