# 問題の定義 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))
# ②問題を定義する problem = LpProblem("Nurse Scheduling", LpMinimize)
# ③変数を作成する shifts = LpVariable.dicts("Shift", ((n, s) for n inrange(num_nurses) for s inrange(num_shifts)), cat='Binary')
# ④目的関数を設定する problem += lpSum([shifts[(n, s)] for n inrange(num_nurses) for s inrange(num_shifts)])
# ⑤制約条件を設定する for s inrange(num_shifts): problem += lpSum([shifts[(n, s)] for n inrange(num_nurses)]) >= shift_requirements[s] for n inrange(num_nurses): problem += lpSum([shifts[(n, s)] for s inrange(num_shifts)]) == 1 for n inrange(num_nurses): for s inrange(num_shifts): if nurse_preferences[n][s] == 0: problem += shifts[(n, s)] == 0
# ⑥問題を解く status = problem.solve()
# ⑦結果を出力する print("ステータス:", LpStatus[status])
for n inrange(num_nurses): for s inrange(num_shifts): if value(shifts[(n, s)]) == 1: print("看護師{}は、シフト{}に割り当てられました".format(n, s))
# 2. 変数の定義 A = LpVariable("Product A Units", lowBound=0, cat='Integer') B = LpVariable("Product B Units", lowBound=0, cat='Integer') C = LpVariable("Product C Units", lowBound=0, cat='Integer')
# 3. 目的関数の定義 prob += 3000*A + 5000*B + 2000*C
# 4. 制約条件の定義 prob += 3*A + 2*B + 1.5*C <= 1500 prob += 1.5*A + 2.5*B + 3*C <= 3000 prob += A + B + C <= 1000 prob += A >= 1 prob += B >= 1 prob += C >= 1
# 5. 最適化の実行 prob.solve()
# 6. 結果の表示 print("Status:", LpStatus[prob.status]) for v in prob.variables(): print(v.name, "=", v.varValue) print("Total Profit = ", value(prob.objective))
# 問題の初期化 problem = LpProblem("Task Assignment Problem", LpMaximize)
# 変数の定義 staff = ['Staff1', 'Staff2', 'Staff3'] tasks = ['Task1', 'Task2', 'Task3', 'Task4', 'Task5'] x = LpVariable.dicts('x', [(i, j) for i in staff for j in tasks], lowBound=0, upBound=1, cat=LpInteger)
# 目的関数の定義 problem += lpSum([x[i, j] for i in staff for j in tasks])
# 制約条件の定義 for j in tasks: problem += lpSum([x[i, j] for i in staff]) == 1
for i in staff: problem += lpSum([x[i, j] for j in tasks]) <= 2
# 問題の解決 status = problem.solve()
# 結果の出力 print("ステータス:", LpStatus[status]) for i in staff: for j in tasks: if x[i, j].value() == 1: print(i, "は", j, "に割り当てられました。")
🔹ある工場で、製品 A と製品 B を生産することができます。 🔹製品 A の生産には 2 時間かかり、製品 B の生産には 3 時間かかります。 🔹工場は最大10時間稼働することができます。 🔹製品 A 1 個あたりの利益は 10 ドルで、製品 B 1 個あたりの利益は 15 ドルです。 🔹工場で作ることのできる製品の最大量は 50 個です。
# ①問題を定義する problem = LpProblem("Hospital Patient Assignment Problem", LpMinimize)
# ②患者がどの病院に割り当てられるかを決定する変数を定義する patient_to_hospital = LpVariable.dicts("Patient to Hospital", [(i, j) for i inrange(3) for j inrange(10)], cat="Binary")
# ④制約条件を設定する hospital_capacities = [5, 7, 3] for i inrange(3): problem += lpSum([patient_to_hospital[(i, j)] for j inrange(10)]) <= hospital_capacities[i] for j inrange(10): problem += lpSum([patient_to_hospital[(i, j)] for i inrange(3)]) == 1
# ⑤問題を解く problem.solve()
# ⑥結果を出力する for i inrange(3): for j inrange(10): if value(patient_to_hospital[(i, j)]) == 1: print("Patient {} is assigned to Hospital {}".format(j+1, i+1))
Patient 1 is assigned to Hospital 1
Patient 3 is assigned to Hospital 1
Patient 4 is assigned to Hospital 1
Patient 6 is assigned to Hospital 1
Patient 7 is assigned to Hospital 1
Patient 2 is assigned to Hospital 2
Patient 5 is assigned to Hospital 2
Patient 8 is assigned to Hospital 3
Patient 9 is assigned to Hospital 3
Patient 10 is assigned to Hospital 3