# 変数の作成 x = {} for i inrange(num_polling_stations): for j inrange(num_districts): x[i, j] = LpVariable(f"x_{i}_{j}", lowBound=0, cat=LpInteger)
4. 目的関数を定義する。
1 2
# 目的関数(調査費用の最小化) problem += lpSum(survey_costs[i] * x[i, j] for i inrange(num_polling_stations) for j inrange(num_districts))
5. 制約条件を追加する。
1 2 3 4 5 6 7 8
# 各選挙区には少なくとも1つの調査所が必要 for j inrange(num_districts): problem += lpSum(x[i, j] for i inrange(num_polling_stations)) >= 1
# 各選挙区の調査人数の制約 for j inrange(num_districts): problem += lpSum(x[i, j] for i inrange(num_polling_stations)) >= min_surveyors[j] problem += lpSum(x[i, j] for i inrange(num_polling_stations)) <= max_surveyors[j]
6. 問題を解く。
1
problem.solve()
7. 最適解を出力する。
1 2 3
for i inrange(num_polling_stations): for j inrange(num_districts): print(f"Polling Station {i+1} in District {j+1}: {value(x[i, j])}")
# 問題の作成 problem = LpProblem("Election Survey", LpMinimize)
# 変数の作成 x = {} for i inrange(num_polling_stations): for j inrange(num_districts): x[i, j] = LpVariable(f"x_{i}_{j}", lowBound=0, cat=LpInteger)
# 目的関数(調査費用の最小化) problem += lpSum(survey_costs[i] * x[i, j] for i inrange(num_polling_stations) for j inrange(num_districts))
# 各選挙区には少なくとも1つの調査所が必要 for j inrange(num_districts): problem += lpSum(x[i, j] for i inrange(num_polling_stations)) >= 1
# 各選挙区の調査人数の制約 for j inrange(num_districts): problem += lpSum(x[i, j] for i inrange(num_polling_stations)) >= min_surveyors[j] problem += lpSum(x[i, j] for i inrange(num_polling_stations)) <= max_surveyors[j]
# 各調査所の割り当て結果を表示 for i inrange(num_polling_stations): for j inrange(num_districts): print(f"Polling Station {i+1} in District {j+1}: {value(x[i, j])}")
上記のコードを実行すると、最適解と各調査所の割り当て結果が出力されます。
[実行結果]
Optimization Status: 1
Minimum Survey Cost: 100.0
Polling Station 1 in District 1: 2.0
Polling Station 1 in District 2: 3.0
Polling Station 1 in District 3: 1.0
Polling Station 1 in District 4: 2.0
Polling Station 1 in District 5: 2.0
Polling Station 2 in District 1: 0.0
Polling Station 2 in District 2: 0.0
Polling Station 2 in District 3: 0.0
Polling Station 2 in District 4: 0.0
Polling Station 2 in District 5: 0.0
Polling Station 3 in District 1: 0.0
Polling Station 3 in District 2: 0.0
Polling Station 3 in District 3: 0.0
Polling Station 3 in District 4: 0.0
Polling Station 3 in District 5: 0.0
最適化の結果、調査費用の最小値は100.0となりました。
調査所の割り当て結果を見ると、例えば”Polling Station 1 in District 1”では2人の調査員が配置されています。
また、”Polling Station 1 in District 4”や”Polling Station 1 in District 5”ではそれぞれ2人ずつの調査員が配置されています。
# 目的関数の定義 problem += lpSum(transport_vars[base][enemy] * transport_cost_dict[base][enemy] for base in base_names for enemy in enemy_bases)
# 制約条件の定義 # 各自国の拠点からの輸送量は、自国の拠点の供給量以下である必要がある supply_dict = {"Base1": 100, "Base2": 150, "Base3": 200} for base in base_names: problem += lpSum(transport_vars[base][enemy] for enemy in enemy_bases) <= supply_dict[base]
# 各敵地の拠点への輸送量は、敵地の拠点の需要量以上である必要がある demand_dict = {"Enemy1": 80, "Enemy2": 120, "Enemy3": 100} for enemy in enemy_bases: problem += lpSum(transport_vars[base][enemy] for base in base_names) >= demand_dict[enemy]
# 最適化の実行 problem.solve()
# 結果の出力 print("Optimal Solution:") for base in base_names: for enemy in enemy_bases: print(f"Transport from {base} to {enemy}: {transport_vars[base][enemy].varValue}")
Optimal Solution:
Transport from Base1 to Enemy1: 30.0
Transport from Base1 to Enemy2: 0.0
Transport from Base1 to Enemy3: 70.0
Transport from Base2 to Enemy1: 0.0
Transport from Base2 to Enemy2: 120.0
Transport from Base2 to Enemy3: 30.0
Transport from Base3 to Enemy1: 50.0
Transport from Base3 to Enemy2: 0.0
Transport from Base3 to Enemy3: 0.0
各自国の拠点から敵地の拠点への輸送量が表示されています。
例えば、”Transport from Base1 to Enemy1: 30.0” は、自国の Base1 から敵地の Enemy1 への輸送量が30であることを示しています。
# 目的関数を定義する prob += lpSum([cost[f] * food_vars[f] for f in food]), "Total Cost"
# 制約条件を定義する prob += lpSum([calories[f] * food_vars[f] for f in food]) >= target_calories, "Calories Minimum" prob += lpSum([calories[f] * food_vars[f] for f in food]) <= target_calories + 100, "Calories Maximum" prob += lpSum([protein[f] * food_vars[f] for f in food]) >= target_protein, "Protein Minimum" prob += lpSum([fat[f] * food_vars[f] for f in food]) >= target_fat, "Fat Minimum" prob += lpSum([carbs[f] * food_vars[f] for f in food]) >= target_carbs, "Carbs Minimum"
# 問題を解く prob.solve()
# 結果を表示する print("Status:", LpStatus[prob.status]) for v in prob.variables(): print(v.name, "=", v.varValue) print("Total Cost =", value(prob.objective))
この例では、最小化したい変数を cost[f] * food_vars[f] で表し、目的関数は lpSum([cost[f] * food_vars[f] for f in food]) として定義されています。
# 問題の定義 problem = pulp.LpProblem('school_lunch', pulp.LpMinimize)
# 変数の定義 a = pulp.LpVariable('a', lowBound=0, cat='Continuous') b = pulp.LpVariable('b', lowBound=0, cat='Continuous') c = pulp.LpVariable('c', lowBound=0, cat='Continuous')
# 目的関数の定義 problem += 2 * a + 3 * b + 4 * c
# 制約条件の定義 problem += 700 * a + 600 * b + 800 * c >= 10000 problem += 30 * a + 25 * b + 20 * c >= 500 problem += 10 * a + 15 * b + 20 * c >= 200 problem += 50 * a + 40 * b + 60 * c >= 700
# 結果の出力 print("Status:", LpStatus[status]) print("Optimal Solution:") for v in prob.variables(): print(v.name, "=", v.varValue) print("Optimal Value =", value(prob.objective))