【回溯法】-排列树-旅行商(TSP)问题C++实现

旅行商问题描述

有n个城市,任意两个城市之间的距离已知。一个旅行商从某个城市出发经过每个城市且任一城市只经过一次,最后回到出发城市,如何确定代价最小的线路
在这里插入图片描述
商人从西安出发,经过图中所示的所有城市回到西安,可以选择先到成都,然后到达广州,继续访问HK,访问完香港后不能回到广州,因为每个城市智能访问一次,离开香港后可以去上海,然后继续北上来到徐州,最后经过北京回到西安。上述路线是该问题的一个可行解,当然这个解不一定是最优解。
现在明确了什么是商旅问题。思考一下回溯法求解问题的基本步骤,

回溯法步骤

1、针对所给的问题,定义问题的解空间。
2、确定易于搜索的解空间结构(排列数,子集树)。
3、从根结点开始,采用深度优先的方式遍历解空间树。

在遍历的过程中为了避免无效搜索,采用剪枝策略提高搜索的速度。
1、利用约束函数减去不满足约束条件的子树。
2、利用限界函数减去得不到最优解的子树。

举例

在这里插入图片描述
假设有四个城市,编号分别为1,2,3,4.
该问题不是从4个城市中选择若干城市,而是求解对4个城市的访问顺序
因此需要使用排列数组织解空间。
在这里插入图片描述从根到叶子的路径对应问题的一个可能解。采用深度优先的方法搜索这个树。
从跟结点A开始,到达结点B,继续深度优先,到达结点C,此时的含义是从城市1出发到城市2,继续深度优先到达结点F,此时的含义是,访问完城市2之后访问城市3,继续深度优先到达结点L,此时的含义是访问完城市3之后访问城市4,结点L是一个叶子结点,因此得到第一个解,即1,2,3,4。该可行解的距离代价是59
到达叶子结点L后,从L开始回溯到结点F,此时结点C的左子树访问完毕,开始访问结点C的右子树,到达结点G,此时的含义是访问城市2之后,访问城市4,当前的距离是40,如果继续深度优先,到达点M,访问的代价是60,说明还没有回到原点,访问代价就已经超过了目前得到的最优解。因此使用限界函数将结点M剪枝,
在这里插入图片描述

源代码

/**
   回溯法-旅行商(TSP)问题
*/
#include<iostream>
#include<algorithm>
#define MAX 100
using  namespace std;
int n;                               //城市个数
int a[MAX][MAX];                   //城市间距离
int x[MAX];                       //记录路径
int bestx[MAX]  = {0};           //记录最优路径
int bestp = 63355;              //最短路径长
int cp = 0;                    //当前路径长
void backpack(int t){
     if(t>n){
        if((a[x[n]][1])&&(a[x[n]][1]+cp<bestp)){
              bestp = a[x[n]][1]+cp;
              for(int i = 1;i<=n;i++){
                 bestx[i] = x[i];
              }
        }
     }else{
         for(int i = t;i<=n;i++){
             /*约束为当前节点到下一节点的长度不为0,限界为走过的长度+当前要走的长度之和小于最优长度*/
            if((a[x[t-1]][x[i]])&&(cp+a[x[t-1]][x[i]]<bestp)){
                swap(x[t],x[i]);   
                cp+=a[x[t-1]][x[t]];
                backpack(t+1);
                cp-=a[x[t-1]][x[t]];
                swap(x[t],x[i]);
            }
         }
    }
}
int main(){
    cout<<"输入城市个数:"<<endl;
    cin>>n;      //顶点数
    for(int i = 1;i<=n;i++){
         x[i] = i;
    }
    cout<<"输入城市之间的距离(0表示城市间不通):"<<endl;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    backpack(2);
    cout<<"最少旅行费用为: "<<bestp<<endl;
    cout<<"旅行路径为:"<<endl;
    for(int i = 1;i<=n;i++){
       cout<<bestx[i]<<" ";
    }
    cout<<bestx[1];
    return 0;
}
/*
输出结果
4
0 30 6 4
30 0 5 10
6 5 0 20
4 10 20 0
*/
  • 12
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
TSP问题是一个经典的组合优化问题,遗传算法是一种常用的求解优化问题的方法。下面是一个基于遗传算法TSP问题求解程序的C++代码示例: ```c++ #include <iostream> #include <algorithm> #include <vector> #include <random> using namespace std; const int POPULATION_SIZE = 100; const int GENERATION_NUM = 1000; const double CROSSOVER_PROBABILITY = 0.9; const double MUTATION_PROBABILITY = 0.05; default_random_engine generator(random_device{}()); // 旅行TSP问题的距离矩阵 const int N = 5; int dist[N][N] = { {0, 2, 3, 4, 5}, {2, 0, 6, 7, 8}, {3, 6, 0, 9, 10}, {4, 7, 9, 0, 11}, {5, 8, 10, 11, 0} }; // 基因型 struct Chromosome { vector<int> genes; int fitness; Chromosome() { genes.resize(N); for (int i = 0; i < N; i++) { genes[i] = i; } shuffle(genes.begin(), genes.end(), generator); fitness = calculate_fitness(); } Chromosome(const vector<int>& genes) { this->genes = genes; fitness = calculate_fitness(); } int calculate_fitness() const { int res = 0; for (int i = 0; i < N - 1; i++) { res += dist[genes[i]][genes[i+1]]; } res += dist[genes[N-1]][genes[0]]; return res; } bool operator < (const Chromosome& other) const { return fitness < other.fitness; } Chromosome crossover(const Chromosome& other) const { Chromosome child; uniform_int_distribution<int> dis(0, N-1); int r = dis(generator); for (int i = 0; i < N; i++) { if (i < r) { child.genes[i] = genes[i]; } else { if (find(child.genes.begin(), child.genes.end(), other.genes[i]) == child.genes.end()) { child.genes[i] = other.genes[i]; } else { for (int j = 0; j < N; j++) { if (find(child.genes.begin(), child.genes.end(), genes[j]) == child.genes.end()) { child.genes[i] = genes[j]; break; } } } } } return child; } void mutate() { uniform_int_distribution<int> dis(0, N-1); int r1 = dis(generator), r2 = dis(generator); swap(genes[r1], genes[r2]); fitness = calculate_fitness(); } }; // 种群 struct Population { vector<Chromosome> chromosomes; Population() { for (int i = 0; i < POPULATION_SIZE; i++) { chromosomes.emplace_back(); } sort(chromosomes.begin(), chromosomes.end()); } void evolve() { vector<Chromosome> new_chromosomes; // 选择 for (int i = 0; i < POPULATION_SIZE * CROSSOVER_PROBABILITY; i++) { uniform_int_distribution<int> dis(0, POPULATION_SIZE-1); Chromosome parent1 = chromosomes[dis(generator)]; Chromosome parent2 = chromosomes[dis(generator)]; Chromosome child = parent1.crossover(parent2); new_chromosomes.push_back(child); } // 变异 uniform_real_distribution<double> mutation_dis(0, 1); for (int i = 0; i < POPULATION_SIZE; i++) { if (mutation_dis(generator) < MUTATION_PROBABILITY) { new_chromosomes[i].mutate(); } } // 替换 sort(chromosomes.begin(), chromosomes.end()); sort(new_chromosomes.begin(), new_chromosomes.end()); for (int i = 0; i < POPULATION_SIZE * (1 - CROSSOVER_PROBABILITY); i++) { chromosomes[i] = new_chromosomes[i]; } sort(chromosomes.begin(), chromosomes.end()); } Chromosome get_best_chromosome() const { return chromosomes[0]; } }; int main() { Population population; for (int i = 0; i < GENERATION_NUM; i++) { population.evolve(); } Chromosome best_chromosome = population.get_best_chromosome(); cout << "Best solution: "; for (int i = 0; i < N; i++) { cout << best_chromosome.genes[i] << " "; } cout << "\nBest fitness: " << best_chromosome.fitness << endl; return 0; } ``` 该代码实现了一个基于遗传算法TSP问题求解程序。在程序中,首先定义了遗传算法的一些参数,如种群大小、迭代次数、交叉概率、变异概率等。然后定义了基因型 `Chromosome` 和种群 `Population`,并实现了基因型的计算适应度函数、交叉、变异等操作,以及种群的进化操作。最后,在主函数中进行了遗传算法的迭代,并输出了最优解和最优适应度。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值