ortools.algorithms.python.knapsack_solver

class KnapsackSolver(pybind11_builtins.pybind11_object):

This library solves knapsack problems.

Problems the library solves include: - 0-1 knapsack problems, - Multi- dimensional knapsack problems,

Given n items, each with a profit and a weight, given a knapsack of capacity c, the goal is to find a subset of items which fits inside c and maximizes the total profit. The knapsack problem can easily be extended from 1 to d dimensions. As an example, this can be useful to constrain the maximum number of items inside the knapsack. Without loss of generality, profits and weights are assumed to be positive.

From a mathematical point of view, the multi-dimensional knapsack problem can be modeled by d linear constraints:

ForEach(j:1..d)(Sum(i:1..n)(weight_ij * item_i) <= c_j where item_i is a 0-1 integer variable.

Then the goal is to maximize:

Sum(i:1..n)(profit_i * item_i).

There are several ways to solve knapsack problems. One of the most efficient is based on dynamic programming (mainly when weights, profits and dimensions are small, and the algorithm runs in pseudo polynomial time). Unfortunately, when adding conflict constraints the problem becomes strongly NP-hard, i.e. there is no pseudo-polynomial algorithm to solve it. That's the reason why the most of the following code is based on branch and bound search.

For instance to solve a 2-dimensional knapsack problem with 9 items, one just has to feed a profit vector with the 9 profits, a vector of 2 vectors for weights, and a vector of capacities. E.g.:

Python:

{.py}
profits = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
weights = [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
]
capacities = [ 34, 4 ]

solver = knapsack_solver.KnapsackSolver(
knapsack_solver.KnapsackSolver
.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
'Multi-dimensional solver')
solver.init(profits, weights, capacities)
profit = solver.solve()

C++:

{.cpp}
const std::vector<int64_t> profits = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const std::vector<std::vector<int64_t>> weights =
{ { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
const std::vector<int64_t> capacities = { 34, 4 };

KnapsackSolver solver(
KnapsackSolver::KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
"Multi-dimensional solver");
solver.Init(profits, weights, capacities);
const int64_t profit = solver.Solve();

Java:

{.java}
final long[] profits = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final long[][] weights = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
final long[] capacities = { 34, 4 };

KnapsackSolver solver = new KnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
"Multi-dimensional solver");
solver.init(profits, weights, capacities);
final long profit = solver.solve();
KnapsackSolver(arg0, arg1: str)

__init__(self: KnapsackSolver, arg0: operations_research::KnapsackSolver::SolverType, arg1: str) -> None

def init( self, profits: List[int], weights: List[List[int]], capacities: List[int]) -> None:

init(self: KnapsackSolver, profits: List[int], weights: List[List[int]], capacities: List[int]) -> None

Initializes the solver and enters the problem to be solved.

def solve(self) -> int:

solve(self: KnapsackSolver) -> int

Solves the problem and returns the profit of the optimal solution.

def best_solution_contains(self, item_id: int) -> bool:

best_solution_contains(self: KnapsackSolver, item_id: int) -> bool

Returns true if the item 'item_id' is packed in the optimal knapsack.

def is_solution_optimal(self) -> bool:

is_solution_optimal(self: KnapsackSolver) -> bool

Returns true if the solution was proven optimal.

def set_time_limit(self, time_limit_seconds: float) -> None:

set_time_limit(self: KnapsackSolver, time_limit_seconds: float) -> None

Time limit in seconds.

When a finite time limit is set the solution obtained might not be optimal if the limit is reached.

def set_use_reduction(self, use_reduction: bool) -> None:

set_use_reduction(self: KnapsackSolver, use_reduction: bool) -> None

class SolverType(pybind11_builtins.pybind11_object):

Enum controlling which underlying algorithm is used.

This enum is passed to the constructor of the KnapsackSolver object. It selects which solving method will be used.

Members:

KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER : Generic Solver.

This solver can deal with both large number of items and several dimensions. This solver is based on branch and bound.

KNAPSACK_BRUTE_FORCE_SOLVER : Brute force method.

Limited to 30 items and one dimension, this solver uses a brute force algorithm, ie. explores all possible states. Experiments show competitive performance for instances with less than 15 items.

KNAPSACK_64ITEMS_SOLVER : Generic Solver.

This solver can deal with both large number of items and several dimensions. This solver is based on branch and bound.

KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER : Dynamic Programming approach for single dimension problems

Limited to one dimension, this solver is based on a dynamic programming algorithm. The time and space complexity is O(capacity * number_of_items).

KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER : CBC Based Solver

This solver can deal with both large number of items and several dimensions. This solver is based on Integer Programming solver CBC.

KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER : SCIP based solver

This solver can deal with both large number of items and several dimensions. This solver is based on Integer Programming solver SCIP.

KNAPSACK_DIVIDE_AND_CONQUER_SOLVER : Divide and Conquer approach for single dimension problems

Limited to one dimension, this solver is based on a divide and conquer technique and is suitable for larger problems than Dynamic Programming Solver. The time complexity is O(capacity * number_of_items) and the space complexity is O(capacity + number_of_items).

KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER : CP_SAT based solver

This solver can deal with both large number of items and several dimensions. This solver is based on the CP-SAT solver.

SolverType(value: int)

__init__(self: SolverType, value: int) -> None

name: str

name(self: handle) -> str

value: int

(arg0: SolverType) -> int

KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER: 5>
KNAPSACK_BRUTE_FORCE_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_BRUTE_FORCE_SOLVER: 0>
KNAPSACK_64ITEMS_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_64ITEMS_SOLVER: 1>
KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER: 2>
KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER: 3>
KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER: 6>
KNAPSACK_DIVIDE_AND_CONQUER_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_DIVIDE_AND_CONQUER_SOLVER: 9>
KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER: ClassVar[SolverType] = <SolverType.KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER: 10>
KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER: SolverType = <SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER: 5>
KNAPSACK_BRUTE_FORCE_SOLVER: SolverType = <SolverType.KNAPSACK_BRUTE_FORCE_SOLVER: 0>
KNAPSACK_64ITEMS_SOLVER: SolverType = <SolverType.KNAPSACK_64ITEMS_SOLVER: 1>
KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER: SolverType = <SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER: 2>
KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER: SolverType = <SolverType.KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER: 3>
KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER: SolverType = <SolverType.KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER: 6>
KNAPSACK_DIVIDE_AND_CONQUER_SOLVER: SolverType = <SolverType.KNAPSACK_DIVIDE_AND_CONQUER_SOLVER: 9>
KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER: SolverType = <SolverType.KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER: 10>