127
社区成员




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枚举法计算量太大,采用其他方法近似算法如并行算法;