# 変数を作成 x = [] for i in families: x.append(solver.IntVar(0, 1, 'x[%i]' % i)) # x[i] = 家族iに食糧を配達するかどうか (0 または 1)
# 制約を定義 budget = 30# 予算 solver.Add(sum(transport_costs[i] * x[i] for i in families) <= budget)
# 目的関数を定義 solver.Maximize(scalar_multiplier * sum(food_required[i] * x[i] for i in families) - sum(transport_costs[i] * x[i] for i in families))
# 問題を解く solver.Solve()
# 結果を出力 print("Total food delivered = ", sum(food_required[i] * x[i].solution_value() for i in families)) delivered = [x[i].solution_value() for i in families] plt.figure(figsize=(10,5)) plt.bar(range(len(families)), delivered) plt.title("Food delivered to each family") plt.xlabel("Family") plt.ylabel("Food quantity") plt.xticks(range(len(families)), families) plt.show()
print("Total food delivered = ", sum(food_required[i] * x[i].solution_value() for i in families)) delivered = [x[i].solution_value() for i in families] plt.figure(figsize=(10,5)) plt.bar(range(len(families)), delivered) plt.title("Food delivered to each family") plt.xlabel("Family") plt.ylabel("Food quantity") plt.xticks(range(len(families)), families) plt.show()