📜  运输问题第七组(运输中的简并性问题)(1)

📅  最后修改于: 2023-12-03 15:41:55.057000             🧑  作者: Mango

运输问题第七组(运输中的简并性问题)

介绍

在物流运输领域,简并性问题属于常见的运输问题之一。简而言之,简并性问题指的是多个可用的运输路径或方案,其所需的运输时间、成本、服务质量等指标相同或相近,因此难以从中选择合适的运输路径或方案。

在这种情况下,我们通常会采用一些优化策略,如规划运输路径的剪枝、随机选择运输路径等方式,来寻找最优的运输方案。

解决方案
剪枝法

剪枝法是基于深度优先搜索的策略。在搜索的过程中,如果当前搜索路径比当前已知的最优路径的成本高,则直接返回上一层继续搜索其他路径,以此避免搜索无谓的路径。

在应用剪枝法时,需要注意以下问题:

  • 如何定义当前已知最优路径
  • 如何计算当前搜索路径的成本
随机选择法

随机选择法是指在多个等价的运输方案中,采用随机的方式进行选择。通过随机选择,可以避免选择固定的路径而导致的偏见和误差。

在应用随机选择法时,需要注意以下问题:

  • 如何定义等价的运输方案
  • 如何选择合适的随机算法
代码实现
剪枝法代码
def dfs(curr_path, best_path, cost_matrix, visited, curr_cost):
    # 如果当前路径已经搜索完毕,更新最优路径
    if len(curr_path) == len(cost_matrix):
        if curr_cost < best_path['cost']:
            best_path['path'] = curr_path
            best_path['cost'] = curr_cost
        return

    # 依次尝试每个节点作为下一个访问节点
    for i in range(len(cost_matrix)):
        if visited[i]:
            continue
        visited[i] = True
        curr_cost += cost_matrix[curr_path[-1]][i]
        if curr_cost < best_path['cost']:
            curr_path.append(i)
            dfs(curr_path, best_path, cost_matrix, visited, curr_cost)
            curr_path.pop()
        curr_cost -= cost_matrix[curr_path[-1]][i]
        visited[i] = False

start_node = 0
cost_matrix = [[0, 2, 3, 6],
               [2, 0, 4, 1],
               [3, 4, 0, 2],
               [6, 1, 2, 0]]
visited = [False] * len(cost_matrix)
visited[start_node] = True
curr_path = [start_node]
best_path = {'path': [], 'cost': float('inf')}
dfs(curr_path, best_path, cost_matrix, visited, 0)
print(best_path)
随机选择法代码
import random

cost_matrix = [[0, 2, 3, 6],
               [2, 0, 4, 1],
               [3, 4, 0, 2],
               [6, 1, 2, 0]]

# 定义等价性函数,这里以运输成本是否相同作为等价性标准
def is_equivalent(path1, path2, cost_matrix):
    cost1 = 0
    for i in range(len(path1)-1):
        cost1 += cost_matrix[path1[i]][path1[i+1]]
    cost2 = 0
    for i in range(len(path2)-1):
        cost2 += cost_matrix[path2[i]][path2[i+1]]
    return cost1 == cost2

# 随机选择算法
def random_choose(paths):
    return random.choice(paths)

start_node = 0
target_node = 3

# 随机生成一堆运输路径
paths = [[0, 1, 2, 3], [0, 2, 1, 3], [0, 2, 3, 1], [0, 3, 2, 1]]
# 随机选择所有等价路径中的一个路径
equivalent_paths = [path for path in paths if is_equivalent(path, [start_node, target_node], cost_matrix)]
chosen_path = random_choose(equivalent_paths)

print(chosen_path)