import networkx as nx import matplotlib.pyplot as plt
# グラフを作成 G = nx.DiGraph() G.add_nodes_from(range(nodes)) for i, edge inenumerate(edges): G.add_edge(edge[0], edge[1], capacity=capacities[i], cost=costs[i], flow=flow.value[i])
# グラフを描画 pos = nx.spring_layout(G, seed=42) nx.draw(G, pos, with_labels=True, node_color="skyblue", node_size=2000, font_size=20, font_weight="bold") edge_labels = {(u, v): f"{d['flow']:.1f}/{d['capacity']} (cost: {d['cost']})"for u, v, d in G.edges(data=True)} nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12)
# 予測エラーの変数 errors = [pulp.LpVariable(f"error_{i}", lowBound=0) for i inrange(N)]
# 観測ステーションの設置変数 stations = [pulp.LpVariable(f"station_{i}", cat=pulp.LpBinary) for i inrange(K)]
Step 3: 目的関数を定義します。
1 2
# 目的関数を定義 problem += pulp.lpSum(errors[i] * error_coefficients[i] for i inrange(N))
Step 4: 制約条件を追加します。
1 2 3 4 5 6 7 8
# 各地域の予測エラーを計算 for i inrange(N): # 地域の予測エラーを観測ステーションの影響で制約 for j inrange(K): problem += errors[i] >= pulp.lpSum(stations[j] * distance(coordinates[i], coordinates[k]) for k inrange(N))
# 観測ステーションの数制約 problem += pulp.lpSum(stations) == K
# 予測エラーの変数 errors = [pulp.LpVariable(f"error_{i}", lowBound=0) for i inrange(N)]
# 観測ステーションの設置変数 stations = [pulp.LpVariable(f"station_{i}", cat=pulp.LpBinary) for i inrange(K)]
# 目的関数を定義 problem += pulp.lpSum(errors[i] * error_coefficients[i] for i inrange(N))
# 各地域の予測エラーを計算するための制約条件 for i inrange(N): for j inrange(K): problem += errors[i] >= pulp.lpSum(stations[j] * distance(coordinates[i], coordinates[k]) for k inrange(N))
# 観測ステーションの数制約 problem += pulp.lpSum(stations) == K