2413043332 韩宇涛 A题

2501_91484481 2025-04-13 14:18:06

 

import networkx as nx
import matplotlib.pyplot as plt


def draw_network():
    G = nx.DiGraph()
#创建节点
    nodes = list(range(1, 10))
    for node in nodes:
        G.add_node(node)
    edges = [(1, 2), (1, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 6), (4, 7), (5, 6), (5, 7), (6, 8), (6, 9), (7, 8), (7, 9),
             (8, 9)]#连接边
    for edge in edges:
        G.add_edge(*edge)
    pos = nx.spring_layout(G)
    nx.draw(G, pos, with_labels=True, node_size=2000, node_color='skyblue', font_size=12, font_weight='bold', arrowsize=30)
    plt.title('交通网络2')
    plt.show()


if __name__ == "__main__":
    draw_network()

 

import itertools
import pulp


# 假设网络结构,这里需要根据实际网络修改
network = {
    1: [2, 3],
    2: [1, 3, 4, 6],
    3: [1, 2, 4, 5, 7],
    4: [2, 3, 8, 9],
    5: [3, 7, 9],
    6: [2, 7, 8],
    7: [3, 5, 6, 8],
    8: [4, 6, 7, 9],
    9: [4, 5, 8]
}
# 假设需求数据,这里需要根据实际需求修改
demand_data = {
    (1, 9): 100,
    (3, 7): 100
}


# 生成所有可能的路径,限制路径长度不超过5
def generate_paths(start, end):
    paths = []
    stack = [(start, [start])]
    while stack:
        node, path = stack.pop()
        if node == end and len(path) <= 5:
            paths.append(path)
        else:
            for neighbor in network[node]:
                if neighbor not in path:
                    new_path = path + [neighbor]
                    stack.append((neighbor, new_path))
    return paths


# 构建线性规划模型并求解
def solve_traffic_allocation():
    # 创建问题实例
    prob = pulp.LpProblem("Traffic_Allocation_Problem", pulp.LpMaximize)

    # 定义决策变量
    x_vars = {}
    for origin, destination in demand_data.keys():
        paths = generate_paths(origin, destination)
        for path in paths:
            x_vars[(origin, destination, tuple(path))] = pulp.LpVariable(
                f"x_{origin}_{destination}_{'_'.join(map(str, path))}", lowBound=0
            )

    # 定义目标函数:所有交通需求的期望可达率最大
    segments = []
    for node1 in network:
        for node2 in network[node1]:
            segments.append((node1, node2))
    prob += pulp.lpSum(
        (1 / len(segments)) * pulp.lpSum(
            (1 - (pulp.lpSum(
                (1 if (node1, node2) in zip(path[:-1], path[1:]) else 0) +
                (1 if (node2, node1) in zip(path[:-1], path[1:]) else 0)
                for path in generate_paths(origin, destination)
            ) / len(generate_paths(origin, destination)))) *
            x_vars[(origin, destination, tuple(path))]
            for path in generate_paths(origin, destination)
        ) / demand_data[(origin, destination)]
        for origin, destination in demand_data.keys() for node1, node2 in segments
    )

    # 约束条件1:满足各(起点,终点)对的交通需求
    for origin, destination in demand_data.keys():
        prob += pulp.lpSum(
            x_vars[(origin, destination, tuple(path))] for path in generate_paths(origin, destination)
        ) == demand_data[(origin, destination)]

    # 求解模型
    prob.solve()

    return prob


# 计算期望可达率
def calculate_expected_reachability(prob, demand_data, network):
    segments = []
    for node1 in network:
        for node2 in network[node1]:
            segments.append((node1, node2))
    total_expected_reachability = 0
    for origin, destination in demand_data.keys():
        demand = demand_data[(origin, destination)]
        origin_destination_reachability = 0
        for segment in segments:
            reachability = 0
            for var in prob.variables():
                if var.name.startswith(f"x_{origin}_{destination}_"):
                    path = list(map(int, var.name.split('_')[3:]))
                    if (segment[0] in path and segment[1] in path) and (
                            (path.index(segment[0]) + 1 == path.index(segment[1])) or (
                            path.index(segment[1]) + 1 == path.index(segment[0]))):
                        reachability += 0 * pulp.value(var)
                    else:
                        reachability += 1 * pulp.value(var)
            origin_destination_reachability += (1 / len(segments)) * (reachability / demand)
        total_expected_reachability += origin_destination_reachability
    return total_expected_reachability


prob = solve_traffic_allocation()
expected_reachability = calculate_expected_reachability(prob, demand_data, network)
print(f"期望可达率为: {expected_reachability}")

 

问题2求解

对于问题2枚举法计算量太大,采用其他方法近似算法如并行算法;

  • 结果分析
  •  
  • 模型优缺点
  • 优点:为城市交通提供了初步的解决方案,优化了交通,能在有限程度大大提高效率;
  • 缺点:计算量太大,降低了运行效率时间复杂度太大,占用内存存储太大。
  • 改进方向:优化算法,降低时间复杂度采用不同的期望可达率算法优化模型,改进数学模型函数,提高精确度。
  • 参考文献:《数学模型》(姜启源等著);《数学建模竞赛教程》(李尚志等编著)

 

 

 

 

 

...全文
54 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

127

社区成员

发帖
与我相关
我的任务
社区描述
生乎混沌,分其昏明,万象有道,真理永存。 中北大学数学建模协会欢迎各位小伙伴们的加入! 今后,我们将会在社区分享数学建模相关的知识,一起去体验数学建模的乐趣。同时,也欢迎大家于此分享自己的心得。
数学建模 高校 山西省·太原市
社区管理员
  • 釉色清风
  • 豆尔顽
  • 江海浮沉一孤舟
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧