Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
com.google.ortools.algorithms.KnapsackSolver Class Reference

Classes

enum  SolverType
 

Public Member Functions

synchronized void delete ()
 
 KnapsackSolver (String solver_name)
 
 KnapsackSolver (KnapsackSolver.SolverType solver_type, String solver_name)
 
void init (long[] profits, long[][] weights, long[] capacities)
 
long solve ()
 
boolean bestSolutionContains (int item_id)
 
boolean isSolutionOptimal ()
 
String getName ()
 
boolean useReduction ()
 
void setUseReduction (boolean use_reduction)
 
void setTimeLimit (double time_limit_seconds)
 

Protected Member Functions

 KnapsackSolver (long cPtr, boolean cMemoryOwn)
 
void finalize ()
 

Static Protected Member Functions

static long getCPtr (KnapsackSolver obj)
 
static long swigRelease (KnapsackSolver obj)
 

Protected Attributes

transient boolean swigCMemOwn
 

Detailed Description

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:

    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.SolverType
    .KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
    'Multi-dimensional solver')
    solver.init(profits, weights, capacities)
    profit = solver.solve()


    C++:

    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:

    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();

Definition at line 94 of file KnapsackSolver.java.

Constructor & Destructor Documentation

◆ KnapsackSolver() [1/3]

com.google.ortools.algorithms.KnapsackSolver.KnapsackSolver ( long cPtr,
boolean cMemoryOwn )
protected

Definition at line 98 of file KnapsackSolver.java.

◆ KnapsackSolver() [2/3]

com.google.ortools.algorithms.KnapsackSolver.KnapsackSolver ( String solver_name)

Definition at line 134 of file KnapsackSolver.java.

◆ KnapsackSolver() [3/3]

com.google.ortools.algorithms.KnapsackSolver.KnapsackSolver ( KnapsackSolver.SolverType solver_type,
String solver_name )

Definition at line 138 of file KnapsackSolver.java.

Member Function Documentation

◆ bestSolutionContains()

boolean com.google.ortools.algorithms.KnapsackSolver.bestSolutionContains ( int item_id)

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

Definition at line 159 of file KnapsackSolver.java.

◆ delete()

synchronized void com.google.ortools.algorithms.KnapsackSolver.delete ( )

Definition at line 124 of file KnapsackSolver.java.

◆ finalize()

void com.google.ortools.algorithms.KnapsackSolver.finalize ( )
protected

Definition at line 120 of file KnapsackSolver.java.

◆ getCPtr()

static long com.google.ortools.algorithms.KnapsackSolver.getCPtr ( KnapsackSolver obj)
staticprotected

Definition at line 103 of file KnapsackSolver.java.

◆ getName()

String com.google.ortools.algorithms.KnapsackSolver.getName ( )

Definition at line 170 of file KnapsackSolver.java.

◆ init()

void com.google.ortools.algorithms.KnapsackSolver.init ( long[] profits,
long weights[][],
long[] capacities )

Initializes the solver and enters the problem to be solved.

Definition at line 145 of file KnapsackSolver.java.

◆ isSolutionOptimal()

boolean com.google.ortools.algorithms.KnapsackSolver.isSolutionOptimal ( )

Returns true if the solution was proven optimal.

Definition at line 166 of file KnapsackSolver.java.

◆ setTimeLimit()

void com.google.ortools.algorithms.KnapsackSolver.setTimeLimit ( double time_limit_seconds)

Time limit in seconds.

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

Definition at line 188 of file KnapsackSolver.java.

◆ setUseReduction()

void com.google.ortools.algorithms.KnapsackSolver.setUseReduction ( boolean use_reduction)

Definition at line 178 of file KnapsackSolver.java.

◆ solve()

long com.google.ortools.algorithms.KnapsackSolver.solve ( )

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

Definition at line 152 of file KnapsackSolver.java.

◆ swigRelease()

static long com.google.ortools.algorithms.KnapsackSolver.swigRelease ( KnapsackSolver obj)
staticprotected

Definition at line 107 of file KnapsackSolver.java.

◆ useReduction()

boolean com.google.ortools.algorithms.KnapsackSolver.useReduction ( )

Definition at line 174 of file KnapsackSolver.java.

Member Data Documentation

◆ swigCMemOwn

transient boolean com.google.ortools.algorithms.KnapsackSolver.swigCMemOwn
protected

Definition at line 96 of file KnapsackSolver.java.


The documentation for this class was generated from the following file: