# 変数を作成 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()
np.random.seed(seed=4) n = 13 m = 100 X = np.linspace(0, 10, m) y_truth = f(X) x = 0.5 + 8.5 * np.random.rand(n) y = f(x)
次に、カーネルマトリックスを計算します。
1
K = np.array([[kernel(x[i], x[j]) for j inrange(n)] for i inrange(n)])
次に、カーネルメソッドによる予測を行います。
1 2 3 4 5 6 7 8
c = np.linalg.inv(K) @ y y_pred = [] for i inrange(m): sum = 0 for j inrange(n): sum += c[j] * kernel(x[j], X[i]) y_pred.append(sum) y_pred = np.array(y_pred)
# アイテムをビンに入れるかどうかを示すノードを作成(バイナリ変数) x = [[LpVariable("x(%s,%s)" % (i, j), cat="Binary") for j inrange(item_count)] for i inrange(bin_count)]
# 目的関数: 使用するビンの数を最小化する prob += lpSum(x[i][j] for i inrange(bin_count) for j inrange(item_count))
# 制約条件: 各アイテムは1つのビンにしか入らない for j inrange(item_count): prob += lpSum(x[i][j] for i inrange(bin_count)) == 1
# 制約条件: 各ビンの容量を超えてアイテムを入れられない for i inrange(bin_count): prob += lpSum(item_weight[j] * x[i][j] for j inrange(item_count)) <= bin_capacity[i]
# 最適化計算 prob.solve()
# 結果表示 for i inrange(bin_count): print("*****Bin", i, "*****") total_weight = 0 for j inrange(item_count): if value(x[i][j])==1: print("Item", j) total_weight+=item_weight[j] print("Total weight:", total_weight)
x = [[LpVariable("x(%s,%s)" % (i, j), cat="Binary") for j inrange(item_count)] for i inrange(bin_count)]
ビンにアイテムを入れるかどうかを示す変数 x をバイナリ変数として定義しています。
5. 目的関数の設定
1
prob += lpSum(x[i][j] for i inrange(bin_count) for j inrange(item_count))
使用するビンの数を最小化する目的関数を定義しています。
6. 制約条件の設定
1 2 3 4 5
for j inrange(item_count): prob += lpSum(x[i][j] for i inrange(bin_count)) == 1
for i inrange(bin_count): prob += lpSum(item_weight[j] * x[i][j] for j inrange(item_count)) <= bin_capacity[i]
各アイテムが1つのビンにしか入らない制約条件と、各ビンの容量を超えない制約条件を設定しています。
7. 最適化計算
1
prob.solve()
PuLPを使って最適化問題を解いています。
8. 結果表示
1 2 3 4 5 6 7 8
for i inrange(bin_count): print("*****Bin", i, "*****") total_weight = 0 for j inrange(item_count): if value(x[i][j])==1: print("Item", j) total_weight+=item_weight[j] print("Total weight:", total_weight)