• 基于A*、RBFS 和爬山算法求解 TSP问题(Matlab代码实现)


    💥💥💥💞💞💞欢迎来到本博客❤️❤️❤️💥💥💥

    🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。

    ⛳️座右铭:行百里者,半于九十。

    目录

    💥1 概述

    📚2 运行结果

    🎉3 参考文献

    🌈4 Matlab代码及详细文章

    💥1 概述

    TSP问题是在数学领域中非常著名的一个问题,其本质就是一个典型的难以处理NP完全问题。随着计算规模的的逐渐增大,计算量会呈现指数级的增长,它能够广泛的应用在涉及相关路径规划的应用中。给定n个顶点无向完全图G=(V.E.r),在G中遍历所有的顶点形成闭合回路,使得回路上的所有边的权值之和达到最小,顶点集合V=[1.,2..n]表示被访问的城市,E是V中不同顶点的边集,r为边权值集合.数学模型如下:

                     \begin{array}{l} \min Z=\sum_{i=1}^{n} \sum_{j=1}^{n} d_{i, j} x_{i, j} \\ \text { s.t. } \sum_{j=1}^{n} x_{i, j}=1 \quad(i=1,2, \cdots n) \\ \sum_{i=1}^{n} x_{i, j}=1 \quad(j=1,2, \cdots n) \\ \sum_{i, j \in S} x_{i, j} \leqslant Q-1,2 \leqslant|Q| \leqslant n-2, \\ Q \subset\{1,2, \cdots n\} \\ x_{i, j} \in\{0,1\} ;(i, j=1,2, \cdots n) \end{array}

    公式(1)为目标函数,也就是TSP问题中的路径长度最小化,其中d,表示2个城市之间的权重,公式(2)、(3)表示每个城市有且只有一次被旅行商经过。公式(4)表示旅行商在任何1个城市真子集中不能形成循环,公式(5)表示决策变量取值,当值为1的时候,表示已经遍历过的城市,否则表示没有走过。因此从上述描述的过程来看,求解该类问题采用仿生学算法求解具有较好的效果。

    📚2 运行结果

    部分代码:

    function cost = estimateDist(G, unvisitedGraph, startNode, currentNode)
        % caculate Minimum spanning tree using Prim鐥� algorithm
        T = minspantree(unvisitedGraph); 
        % estimated the distance to visit all unvisited nodes using MST heuristic
        h_n_MST = sum(T.Edges.Weight);
        
        unvisitedNs = table2cell(unvisitedGraph.Nodes);
        % caculate the minimum distance from an unvisited node to the current node
        idx_to_curr_node = findedge(G, currentNode, unvisitedNs);
        mindist_to_curr_node = min(G.Edges.Weight(idx_to_curr_node));

        % caculate the minimum distance from an unvisited node to the start node
        idx_to_str_node = findedge(G, startNode, unvisitedNs);
        mindist_to_str_node = min(G.Edges.Weight(idx_to_str_node));
        
        cost = h_n_MST + mindist_to_str_node + mindist_to_curr_node;
    end

    % expandNode function 
    % returns all the successors of a node 
    function nodelist = expandNode(G, startNode, currentNode)
        % For TSP problem (each node is visited only once)
        % here we just get the nodes that are not already in the current node path
        unvisitedGraph = G;
        node1 = currentNode;
        while(~isempty(node1))
            unvisitedGraph = rmnode(unvisitedGraph, node1.name);
            node1 = node1.parent;
        end
        % the expanded nodes
        unvisitedNs = table2cell(unvisitedGraph.Nodes);
        % the expanded nodes as structure (to be returned)
        nodelist=[]; 
        
        % estimate cost for the expanded nodes
        for i=1:size(unvisitedNs,1)
            expandedNode = unvisitedNs{i}; % expanded node name as a string (char array)
            % if the node is leaf (the last node in the path), the heuristic is  
            % the cost of connectin this node to the start node
            if (size(unvisitedNs,1)==1)
                idx_to_str_node = findedge(G, startNode, expandedNode);
                h_n = G.Edges.Weight(idx_to_str_node);
            else
                unvistg = rmnode(unvisitedGraph, expandedNode);
                % h_n = cost of the MST of the subgraph of expandedNode +
                % minimum cost to connecte MST to expandedNode +
                % minimum cost to connecte MST to start node 
                h_n = estimateDist(G, unvistg, startNode, expandedNode);
            end
            
            % create the successor node as structure
            node.name = expandedNode; % the successor node
            node.h_n = h_n; % the heuristic of the successor node
            % the cost (edge value) of connecting the current node with its 
            % successor (the expanded node)
            g_n = G.Edges.Weight(findedge(G, node.name ,currentNode.name));
            % the cost of connecting the current node to the first node
            % (currentNode.g_n). node.g_n is the cost of the successor node to
            % the start node
            node.g_n = currentNode.g_n + g_n;
            % the estimated cost from the current from start node to the goal 
            % node throw the successor node
            node.depth = currentNode.depth+1;
            node.cost = node.h_n + node.g_n;          
            node.parent = currentNode;
            nodelist = [nodelist; node];
        end
    end
     

     

    🎉3 参考文献

    [1]Hamdi Altaheri (2022). Solving TSP using A star, RBFS, and Hill-climbing algorithms 

    🌈4 Matlab代码及详细文章

  • 相关阅读:
    基于猕猴Spike运动解码的不同解码方法性能对比
    【Vue渲染】 条件渲染 | v-if | v-show | 列表渲染 | v-for
    贪心算法-Huffman算法
    直播平台Stacked完成1290万美元A轮融资,计划转型为Web3流媒体
    【C++进阶】:特殊类的设计
    案例分享-https证书链不完整导致请求失败
    统计学习方法P54中位数脚注
    c++ qt五子棋联网对战游戏
    迷宫寻路:(深搜广搜)
    大厂秋招真题【模拟】OPPO20230802秋招提前批T2-小欧的圆覆盖【欧弟算法】全网最全大厂秋招题解
  • 原文地址:https://blog.csdn.net/weixin_46039719/article/details/127721300