Java Reference

Java Reference

KnapsackSolver.java
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2  * This file was automatically generated by SWIG (http://www.swig.org).
3  * Version 4.0.2
4  *
5  * Do not make changes to this file unless you know what you are doing--modify
6  * the SWIG interface file instead.
7  * ----------------------------------------------------------------------------- */
8 
9 package com.google.ortools.algorithms;
10 
94 public class KnapsackSolver {
95  private transient long swigCPtr;
96  protected transient boolean swigCMemOwn;
97 
98  protected KnapsackSolver(long cPtr, boolean cMemoryOwn) {
99  swigCMemOwn = cMemoryOwn;
100  swigCPtr = cPtr;
101  }
102 
103  protected static long getCPtr(KnapsackSolver obj) {
104  return (obj == null) ? 0 : obj.swigCPtr;
105  }
106 
107  @SuppressWarnings("deprecation")
108  protected void finalize() {
109  delete();
110  }
111 
112  public synchronized void delete() {
113  if (swigCPtr != 0) {
114  if (swigCMemOwn) {
115  swigCMemOwn = false;
116  mainJNI.delete_KnapsackSolver(swigCPtr);
117  }
118  swigCPtr = 0;
119  }
120  }
121 
122  public KnapsackSolver(String solver_name) {
123  this(mainJNI.new_KnapsackSolver__SWIG_0(solver_name), true);
124  }
125 
126  public KnapsackSolver(KnapsackSolver.SolverType solver_type, String solver_name) {
127  this(mainJNI.new_KnapsackSolver__SWIG_1(solver_type.swigValue(), solver_name), true);
128  }
129 
133  public void init(long[] profits, long[][] weights, long[] capacities) {
134  mainJNI.KnapsackSolver_init(swigCPtr, this, profits, weights, capacities);
135  }
136 
140  public long solve() {
141  return mainJNI.KnapsackSolver_solve(swigCPtr, this);
142  }
143 
147  public boolean bestSolutionContains(int item_id) {
148  return mainJNI.KnapsackSolver_bestSolutionContains(swigCPtr, this, item_id);
149  }
150 
154  public boolean isSolutionOptimal() {
155  return mainJNI.KnapsackSolver_isSolutionOptimal(swigCPtr, this);
156  }
157 
158  public String getName() {
159  return mainJNI.KnapsackSolver_getName(swigCPtr, this);
160  }
161 
162  public boolean useReduction() {
163  return mainJNI.KnapsackSolver_useReduction(swigCPtr, this);
164  }
165 
166  public void setUseReduction(boolean use_reduction) {
167  mainJNI.KnapsackSolver_setUseReduction(swigCPtr, this, use_reduction);
168  }
169 
176  public void setTimeLimit(double time_limit_seconds) {
177  mainJNI.KnapsackSolver_setTimeLimit(swigCPtr, this, time_limit_seconds);
178  }
179 
186  public enum SolverType {
195  KNAPSACK_BRUTE_FORCE_SOLVER(mainJNI.KnapsackSolver_KNAPSACK_BRUTE_FORCE_SOLVER_get()),
203  KNAPSACK_64ITEMS_SOLVER(mainJNI.KnapsackSolver_KNAPSACK_64ITEMS_SOLVER_get()),
211  KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER(mainJNI.KnapsackSolver_KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER_get()),
218  KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER(mainJNI.KnapsackSolver_KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER_get()),
225  KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER(mainJNI.KnapsackSolver_KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER_get());
226 
227  public final int swigValue() {
228  return swigValue;
229  }
230 
231  public static SolverType swigToEnum(int swigValue) {
232  SolverType[] swigValues = SolverType.class.getEnumConstants();
233  if (swigValue < swigValues.length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue)
234  return swigValues[swigValue];
235  for (SolverType swigEnum : swigValues)
236  if (swigEnum.swigValue == swigValue)
237  return swigEnum;
238  throw new IllegalArgumentException("No enum " + SolverType.class + " with value " + swigValue);
239  }
240 
241  @SuppressWarnings("unused")
242  private SolverType() {
243  this.swigValue = SwigNext.next++;
244  }
245 
246  @SuppressWarnings("unused")
247  private SolverType(int swigValue) {
248  this.swigValue = swigValue;
249  SwigNext.next = swigValue+1;
250  }
251 
252  @SuppressWarnings("unused")
253  private SolverType(SolverType swigEnum) {
254  this.swigValue = swigEnum.swigValue;
255  SwigNext.next = this.swigValue+1;
256  }
257 
258  private final int swigValue;
259 
260  private static class SwigNext {
261  private static int next = 0;
262  }
263  }
264 
265 }
This library solves knapsack problems.
void init(long[] profits, long[][] weights, long[] capacities)
Initializes the solver and enters the problem to be solved.
long solve()
Solves the problem and returns the profit of the optimal solution.
boolean isSolutionOptimal()
Returns true if the solution was proven optimal.
KnapsackSolver(KnapsackSolver.SolverType solver_type, String solver_name)
KnapsackSolver(long cPtr, boolean cMemoryOwn)
void setTimeLimit(double time_limit_seconds)
Time limit in seconds.
boolean bestSolutionContains(int item_id)
Returns true if the item 'item_id' is packed in the optimal knapsack.
Enum controlling which underlying algorithm is used.
KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER
CBC Based Solver This solver can deal with both large number of items and several dimensions.
KNAPSACK_64ITEMS_SOLVER
Optimized method for single dimension small problems Limited to 64 items and one dimension,...
KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
Dynamic Programming approach for single dimension problems Limited to one dimension,...