💥💥💥💞💞💞欢迎来到本博客❤️❤️❤️💥💥💥
🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。
⛳️座右铭:行百里者,半于九十。
目录
TSP问题是在数学领域中非常著名的一个问题,其本质就是一个典型的难以处理NP完全问题。随着计算规模的的逐渐增大,计算量会呈现指数级的增长,它能够广泛的应用在涉及相关路径规划的应用中。给定n个顶点无向完全图G=(V.E.r),在G中遍历所有的顶点形成闭合回路,使得回路上的所有边的权值之和达到最小,顶点集合V=[1.,2..n]表示被访问的城市,E是V中不同顶点的边集,r为边权值集合.数学模型如下:
公式(1)为目标函数,也就是TSP问题中的路径长度最小化,其中d,表示2个城市之间的权重,公式(2)、(3)表示每个城市有且只有一次被旅行商经过。公式(4)表示旅行商在任何1个城市真子集中不能形成循环,公式(5)表示决策变量取值,当值为1的时候,表示已经遍历过的城市,否则表示没有走过。因此从上述描述的过程来看,求解该类问题采用仿生学算法求解具有较好的效果。
部分代码:
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
[1]Hamdi Altaheri (2022). Solving TSP using A star, RBFS, and Hill-climbing algorithms