tsp问题对比meoh结果
This commit is contained in:
parent
b6890051c6
commit
805e99b8a1
@ -363,12 +363,12 @@ def tsp_05(distances):
|
|||||||
# 尝试所有可能的插入位置
|
# 尝试所有可能的插入位置
|
||||||
for i in range(len(route)):
|
for i in range(len(route)):
|
||||||
if i == len(route) - 1:
|
if i == len(route) - 1:
|
||||||
increase = (distances[route[i]][city] +
|
increase = (distances[route[i]][city] +
|
||||||
distances[city][route[0]] -
|
distances[city][route[0]] -
|
||||||
distances[route[i]][route[0]])
|
distances[route[i]][route[0]])
|
||||||
else:
|
else:
|
||||||
increase = (distances[route[i]][city] +
|
increase = (distances[route[i]][city] +
|
||||||
distances[city][route[i+1]] -
|
distances[city][route[i+1]] -
|
||||||
distances[route[i]][route[i+1]])
|
distances[route[i]][route[i+1]])
|
||||||
|
|
||||||
if increase < min_increase:
|
if increase < min_increase:
|
||||||
@ -411,3 +411,63 @@ def tsp_05(distances):
|
|||||||
best_route = two_opt(best_route, distances)
|
best_route = two_opt(best_route, distances)
|
||||||
|
|
||||||
return best_route
|
return best_route
|
||||||
|
|
||||||
|
def tsp_06(distances: np.ndarray) -> List[int]:
|
||||||
|
"""
|
||||||
|
基于邻域矩阵的TSP求解算法
|
||||||
|
Args:
|
||||||
|
distances: 距离矩阵
|
||||||
|
Returns:
|
||||||
|
访问顺序列表
|
||||||
|
"""
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def generate_neighborhood_matrix(distance_matrix):
|
||||||
|
n = len(distance_matrix)
|
||||||
|
neighborhood_matrix = np.zeros((n, n), dtype=int)
|
||||||
|
for i in range(n):
|
||||||
|
sorted_indices = np.argsort(distance_matrix[i])
|
||||||
|
neighborhood_matrix[i] = sorted_indices
|
||||||
|
return neighborhood_matrix
|
||||||
|
|
||||||
|
def select_next_node(current_node: int, destination_node: int, unvisited_nodes: np.ndarray, distance_matrix: np.ndarray) -> int:
|
||||||
|
"""
|
||||||
|
Design a novel algorithm to select the next node in each step.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
current_node: ID of the current node.
|
||||||
|
destination_node: ID of the destination node.
|
||||||
|
unvisited_nodes: Array of IDs of unvisited nodes.
|
||||||
|
distance_matrix: Distance matrix of nodes.
|
||||||
|
|
||||||
|
Return:
|
||||||
|
ID of the next node to visit.
|
||||||
|
"""
|
||||||
|
current_dist = distance_matrix[current_node, unvisited_nodes]
|
||||||
|
dest_dist = distance_matrix[destination_node, unvisited_nodes]
|
||||||
|
|
||||||
|
# Normalize distances
|
||||||
|
norm_current = current_dist / np.max(current_dist)
|
||||||
|
norm_dest = dest_dist / np.max(dest_dist)
|
||||||
|
|
||||||
|
# Weighted score (higher weight for proximity to current node)
|
||||||
|
score = 0.7 * norm_current + 0.3 * (1 - norm_dest)
|
||||||
|
|
||||||
|
return unvisited_nodes[np.argmin(score)]
|
||||||
|
|
||||||
|
n = len(distances)
|
||||||
|
neighbor_matrix = generate_neighborhood_matrix(distances)
|
||||||
|
route = np.zeros(n, dtype=int)
|
||||||
|
current_node = 0
|
||||||
|
destination_node = 0
|
||||||
|
for i in range(1, n - 1):
|
||||||
|
near_nodes = neighbor_matrix[current_node][1:]
|
||||||
|
mask = ~np.isin(near_nodes, route[:i])
|
||||||
|
unvisited_near_nodes = near_nodes[mask]
|
||||||
|
next_node = select_next_node(current_node, destination_node, unvisited_near_nodes, distances)
|
||||||
|
current_node = next_node
|
||||||
|
route[i] = current_node
|
||||||
|
mask = ~np.isin(np.arange(n), route[:n - 1])
|
||||||
|
last_node = np.arange(n)[mask]
|
||||||
|
route[n - 1] = last_node[0]
|
||||||
|
return route.tolist()
|
||||||
|
File diff suppressed because one or more lines are too long
Binary file not shown.
Before Width: | Height: | Size: 220 KiB After Width: | Height: | Size: 283 KiB |
Loading…
x
Reference in New Issue
Block a user