public class KnapsackSolver
extends java.lang.Object
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()
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();
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();
Modifier and Type | Class and Description |
---|---|
static class |
KnapsackSolver.SolverType
Enum controlling which underlying algorithm is used.
|
Modifier and Type | Field and Description |
---|---|
protected boolean |
swigCMemOwn |
Modifier | Constructor and Description |
---|---|
|
KnapsackSolver(KnapsackSolver.SolverType solver_type,
java.lang.String solver_name) |
protected |
KnapsackSolver(long cPtr,
boolean cMemoryOwn) |
|
KnapsackSolver(java.lang.String solver_name) |
Modifier and Type | Method and Description |
---|---|
boolean |
bestSolutionContains(int item_id)
Returns true if the item 'item_id' is packed in the optimal knapsack.
|
void |
delete() |
protected void |
finalize() |
protected static long |
getCPtr(KnapsackSolver obj) |
java.lang.String |
getName() |
void |
init(long[] profits,
long[][] weights,
long[] capacities)
Initializes the solver and enters the problem to be solved.
|
boolean |
isSolutionOptimal()
Returns true if the solution was proven optimal.
|
void |
setTimeLimit(double time_limit_seconds)
Time limit in seconds.
|
void |
setUseReduction(boolean use_reduction) |
long |
solve()
Solves the problem and returns the profit of the optimal solution.
|
protected static long |
swigRelease(KnapsackSolver obj) |
boolean |
useReduction() |
protected KnapsackSolver(long cPtr, boolean cMemoryOwn)
public KnapsackSolver(java.lang.String solver_name)
public KnapsackSolver(KnapsackSolver.SolverType solver_type, java.lang.String solver_name)
protected static long getCPtr(KnapsackSolver obj)
protected static long swigRelease(KnapsackSolver obj)
protected void finalize()
finalize
in class java.lang.Object
public void delete()
public void init(long[] profits, long[][] weights, long[] capacities)
public long solve()
public boolean bestSolutionContains(int item_id)
public boolean isSolutionOptimal()
public java.lang.String getName()
public boolean useReduction()
public void setUseReduction(boolean use_reduction)
public void setTimeLimit(double time_limit_seconds)
Copyright © 2025. All rights reserved.