基于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代码及详细文章

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝科研社

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值