Skip to content

Commit

Permalink
Update algorithms doc
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jun 28, 2019
1 parent 96cc264 commit bddfb62
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
4 changes: 1 addition & 3 deletions ortools/algorithms/java/knapsack_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
#include "ortools/algorithms/knapsack_solver.h"
%}

typedef int64_t int64;
typedef uint64_t uint64;

%ignoreall

%unignore operations_research;
%unignore operations_research::KnapsackSolver;
%unignore operations_research::KnapsackSolver::KnapsackSolver;
Expand Down
55 changes: 29 additions & 26 deletions ortools/algorithms/knapsack_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
// That's the reason why the most of the following code is based on branch and
// bound search.
//
// For instance to solve a 2-dimension knapsack problem with 9 items,
// 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.:
Expand All @@ -49,11 +49,33 @@
// [1, 1, 1, 1, 1, 1, 1, 1, 1]]
// vector: capacities = [34, 4]
// And then:
// KnapsackSolver solver(KnapsackSolver::KNAPSACK_MULTIDIMENSION_SOLVER,
// "Multi-dimensional solver");
// KnapsackSolver solver(
// KnapsackSolver::KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
// "Multi-dimensional solver");
// solver.Init(profits, weights, capacities);
// int64 profit = solver.Solve();

//
// Currently four algorithms are implemented:
// - KNAPSACK_BRUTE_FORCE_SOLVER: 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: Limited to 64 items and one dimension, this
// solver uses a branch & bound algorithm. This solver is about 4 times
// faster than KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER.
// - KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER: 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_BRANCH_AND_BOUND_SOLVER: This solver can deal
// with both large number of items and several dimensions. This solver is
// based on branch and bound.
// - KNAPSACK_MULTIDIMENSION_CBC_MIP_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: This solver can deal with both
// large number of items and several dimensions. This solver is based on
// Integer Programming solver SCIP.
//
#ifndef OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
#define OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_

Expand All @@ -75,29 +97,10 @@ namespace operations_research {
// ----- KnapsackSolver -----
// KnapsackSolver is a factory for knapsack solvers. Several solvers are
// implemented, some can deal with a limited number of items, some can deal with
// several dimensions...
// Currently 4 algorithms are implemented:
// - KNAPSACK_BRUTE_FORCE_SOLVER: 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: Limited to 64 items and one dimension, this
// solver uses a branch & bound algorithm. This solver is about 4 times
// faster than KNAPSACK_MULTIDIMENSION_SOLVER.
// - KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER: 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_BRANCH_AND_BOUND_SOLVER: This solver can deal
// with both large number of items and several dimensions. This solver is
// based on branch and bound.
// - KNAPSACK_MULTIDIMENSION_CBC_MIP_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: This solver can deal with both
// large number of items and several dimensions. This solver is based on
// Integer Programming solver SCIP.
// several dimensions.
//
// KnapsackSolver also implements a problem reduction algorithm based on lower
// Besides the four algorithms listed above, KnapsackSolver also implements a
// problem reduction algorithm based on lower
// and upper bounds (see Ingargolia and Korsh: A reduction algorithm for
// zero-one single knapsack problems. Management Science, 1973). This reduction
// method is preferred to better algorithms (see, for instance, Martello
Expand Down
8 changes: 4 additions & 4 deletions ortools/algorithms/samples/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def main():
total_weight = 0
print('Total value =', computed_value)
for i in range(len(values)):
if solver.BestSolutionContains(i):
packed_items.append(i)
packed_weights.append(weights[0][i])
total_weight += weights[0][i]
if solver.BestSolutionContains(i):
packed_items.append(i)
packed_weights.append(weights[0][i])
total_weight += weights[0][i]
print('Total weight:', total_weight)
print('Packed items:', packed_items)
print('Packed_weights:', packed_weights)
Expand Down

0 comments on commit bddfb62

Please sign in to comment.