Loading solver…
Problem: choose the most valuable subset of items without exceeding the knapsack weight capacity.
Values, resource weights and capacities are nonnegative integers. Zero-weight items are allowed.
import {
KnapsackSolver,
KnapsackSolverType,
} from 'or-tools-wasm/knapsack';
const values = [
360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,
78, 256,
];
const weights = [[
7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42,
], [
12, 4, 38, 20, 55, 45, 9, 52, 41, 35, 48, 16, 8, 22, 6, 12, 10, 28,
]];
const capacities = [240, 150];
const solver = new KnapsackSolver(
KnapsackSolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
'KnapsackExample',
);
solver.init(values, weights, capacities);
const result = await solver.solve({ executor: 'worker' });
const packedItems = values
.map((_, item) => item)
.filter((item) => result.contains(item));
console.log(result.profit, packedItems, result.optimal);