OR-Tools  8.1
knapsack_solver.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
15 #define OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
16 
17 #include <math.h>
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/memory/memory.h"
26 #include "ortools/base/logging.h"
27 #include "ortools/base/macros.h"
29 
30 namespace operations_research {
31 
32 class BaseKnapsackSolver;
33 
121  public:
127  enum SolverType {
135 
143 
151 
152 #if defined(USE_CBC)
153 
159 #endif // USE_CBC
160 
167 
168 #if defined(USE_SCIP)
169 
175 #endif // USE_SCIP
176 
177 #if defined(USE_XPRESS)
178 
183  KNAPSACK_MULTIDIMENSION_XPRESS_MIP_SOLVER = 7,
184 #endif
185 
186 #if defined(USE_CPLEX)
187 
192  KNAPSACK_MULTIDIMENSION_CPLEX_MIP_SOLVER = 8,
193 #endif
194  };
195 
196  explicit KnapsackSolver(const std::string& solver_name);
197  KnapsackSolver(SolverType solver_type, const std::string& solver_name);
198  virtual ~KnapsackSolver();
199 
203  void Init(const std::vector<int64>& profits,
204  const std::vector<std::vector<int64> >& weights,
205  const std::vector<int64>& capacities);
206 
210  int64 Solve();
211 
215  bool BestSolutionContains(int item_id) const;
219  bool IsSolutionOptimal() const { return is_solution_optimal_; }
220  std::string GetName() const;
221 
222  bool use_reduction() const { return use_reduction_; }
223  void set_use_reduction(bool use_reduction) { use_reduction_ = use_reduction; }
224 
230  void set_time_limit(double time_limit_seconds) {
231  time_limit_seconds_ = time_limit_seconds;
232  time_limit_ = absl::make_unique<TimeLimit>(time_limit_seconds_);
233  }
234 
235  private:
236  // Trivial reduction of capacity constraints when the capacity is higher than
237  // the sum of the weights of the items. Returns the number of reduced items.
238  int ReduceCapacities(int num_items,
239  const std::vector<std::vector<int64> >& weights,
240  const std::vector<int64>& capacities,
241  std::vector<std::vector<int64> >* reduced_weights,
242  std::vector<int64>* reduced_capacities);
243  int ReduceProblem(int num_items);
244  void ComputeAdditionalProfit(const std::vector<int64>& profits);
245  void InitReducedProblem(const std::vector<int64>& profits,
246  const std::vector<std::vector<int64> >& weights,
247  const std::vector<int64>& capacities);
248 
249  std::unique_ptr<BaseKnapsackSolver> solver_;
250  std::vector<bool> known_value_;
251  std::vector<bool> best_solution_;
252  bool is_solution_optimal_ = false;
253  std::vector<int> mapping_reduced_item_id_;
254  bool is_problem_solved_;
255  int64 additional_profit_;
256  bool use_reduction_;
257  double time_limit_seconds_;
258  std::unique_ptr<TimeLimit> time_limit_;
259 
260  DISALLOW_COPY_AND_ASSIGN(KnapsackSolver);
261 };
262 
263 #if !defined(SWIG)
264 // The following code defines needed classes for the KnapsackGenericSolver
265 // class which is the entry point to extend knapsack with new constraints such
266 // as conflicts between items.
267 //
268 // Constraints are enforced using KnapsackPropagator objects, in the current
269 // code there is one propagator per dimension (KnapsackCapacityPropagator).
270 // One of those propagators, named master propagator, is used to guide the
271 // search, i.e. decides which item should be assigned next.
272 // Roughly speaking the search algorithm is:
273 // - While not optimal
274 // - Select next search node to expand
275 // - Select next item_i to assign (using master propagator)
276 // - Generate a new search node where item_i is in the knapsack
277 // - Check validity of this new partial solution (using propagators)
278 // - If valid, add this new search node to the search
279 // - Generate a new search node where item_i is not in the knapsack
280 // - Check validity of this new partial solution (using propagators)
281 // - If valid, add this new search node to the search
282 //
283 // TODO(user): Add a new propagator class for conflict constraint.
284 // TODO(user): Add a new propagator class used as a guide when the problem has
285 // several dimensions.
286 
287 // ----- KnapsackAssignement -----
288 // KnapsackAssignement is a small struct used to pair an item with its
289 // assignment. It is mainly used for search nodes and updates.
291  KnapsackAssignment(int _item_id, bool _is_in)
292  : item_id(_item_id), is_in(_is_in) {}
293  int item_id;
294  bool is_in;
295 };
296 
297 // ----- KnapsackItem -----
298 // KnapsackItem is a small struct to pair an item weight with its
299 // corresponding profit.
300 // The aim of the knapsack problem is to pack as many valuable items as
301 // possible. A straight forward heuristic is to take those with the greatest
302 // profit-per-unit-weight. This ratio is called efficiency in this
303 // implementation. So items will be grouped in vectors, and sorted by
304 // decreasing efficiency.
305 // Note that profits are duplicated for each dimension. This is done to
306 // simplify the code, especially the GetEfficiency method and vector sorting.
307 // As there usually are only few dimensions, the overhead should not be an
308 // issue.
309 struct KnapsackItem {
310  KnapsackItem(int _id, int64 _weight, int64 _profit)
311  : id(_id), weight(_weight), profit(_profit) {}
313  return (weight > 0)
314  ? static_cast<double>(profit) / static_cast<double>(weight)
315  : static_cast<double>(profit_max);
316  }
317 
318  // The 'id' field is used to retrieve the initial item in order to
319  // communicate with other propagators and state.
320  const int id;
321  const int64 weight;
322  const int64 profit;
323 };
325 
326 // ----- KnapsackSearchNode -----
327 // KnapsackSearchNode is a class used to describe a decision in the decision
328 // search tree.
329 // The node is defined by a pointer to the parent search node and an
330 // assignment (see KnapsackAssignement).
331 // As the current state is not explicitly stored in a search node, one should
332 // go through the search tree to incrementally build a partial solution from
333 // a previous search node.
335  public:
338  int depth() const { return depth_; }
339  const KnapsackSearchNode* const parent() const { return parent_; }
340  const KnapsackAssignment& assignment() const { return assignment_; }
341 
342  int64 current_profit() const { return current_profit_; }
343  void set_current_profit(int64 profit) { current_profit_ = profit; }
344 
345  int64 profit_upper_bound() const { return profit_upper_bound_; }
346  void set_profit_upper_bound(int64 profit) { profit_upper_bound_ = profit; }
347 
348  int next_item_id() const { return next_item_id_; }
349  void set_next_item_id(int id) { next_item_id_ = id; }
350 
351  private:
352  // 'depth' field is used to navigate efficiently through the search tree
353  // (see KnapsackSearchPath).
354  int depth_;
355  const KnapsackSearchNode* const parent_;
356  KnapsackAssignment assignment_;
357 
358  // 'current_profit' and 'profit_upper_bound' fields are used to sort search
359  // nodes using a priority queue. That allows to pop the node with the best
360  // upper bound, and more importantly to stop the search when optimality is
361  // proved.
362  int64 current_profit_;
363  int64 profit_upper_bound_;
364 
365  // 'next_item_id' field allows to avoid an O(number_of_items) scan to find
366  // next item to select. This is done for free by the upper bound computation.
367  int next_item_id_;
368 
369  DISALLOW_COPY_AND_ASSIGN(KnapsackSearchNode);
370 };
371 
372 // ----- KnapsackSearchPath -----
373 // KnapsackSearchPath is a small class used to represent the path between a
374 // node to another node in the search tree.
375 // As the solution state is not stored for each search node, the state should
376 // be rebuilt at each node. One simple solution is to apply all decisions
377 // between the node 'to' and the root. This can be computed in
378 // O(number_of_items).
379 //
380 // However, it is possible to achieve better average complexity. Two
381 // consecutively explored nodes are usually close enough (i.e., much less than
382 // number_of_items) to benefit from an incremental update from the node
383 // 'from' to the node 'to'.
384 //
385 // The 'via' field is the common parent of 'from' field and 'to' field.
386 // So the state can be built by reverting all decisions from 'from' to 'via'
387 // and then applying all decisions from 'via' to 'to'.
389  public:
391  const KnapsackSearchNode& to);
392  void Init();
393  const KnapsackSearchNode& from() const { return from_; }
394  const KnapsackSearchNode& via() const { return *via_; }
395  const KnapsackSearchNode& to() const { return to_; }
397  int depth) const;
398 
399  private:
400  const KnapsackSearchNode& from_;
401  const KnapsackSearchNode* via_; // Computed in 'Init'.
402  const KnapsackSearchNode& to_;
403 
404  DISALLOW_COPY_AND_ASSIGN(KnapsackSearchPath);
405 };
406 
407 // ----- KnapsackState -----
408 // KnapsackState represents a partial solution to the knapsack problem.
410  public:
411  KnapsackState();
412 
413  // Initializes vectors with number_of_items set to false (i.e. not bound yet).
414  void Init(int number_of_items);
415  // Updates the state by applying or reverting a decision.
416  // Returns false if fails, i.e. trying to apply an inconsistent decision
417  // to an already assigned item.
418  bool UpdateState(bool revert, const KnapsackAssignment& assignment);
419 
420  int GetNumberOfItems() const { return is_bound_.size(); }
421  bool is_bound(int id) const { return is_bound_.at(id); }
422  bool is_in(int id) const { return is_in_.at(id); }
423 
424  private:
425  // Vectors 'is_bound_' and 'is_in_' contain a boolean value for each item.
426  // 'is_bound_(item_i)' is false when there is no decision for item_i yet.
427  // When item_i is bound, 'is_in_(item_i)' represents the presence (true) or
428  // the absence (false) of item_i in the current solution.
429  std::vector<bool> is_bound_;
430  std::vector<bool> is_in_;
431 
432  DISALLOW_COPY_AND_ASSIGN(KnapsackState);
433 };
434 
435 // ----- KnapsackPropagator -----
436 // KnapsackPropagator is the base class for modeling and propagating a
437 // constraint given an assignment.
438 //
439 // When some work has to be done both by the base and the derived class,
440 // a protected pure virtual method ending by 'Propagator' is defined.
441 // For instance, 'Init' creates a vector of items, and then calls
442 // 'InitPropagator' to let the derived class perform its own initialization.
444  public:
445  explicit KnapsackPropagator(const KnapsackState& state);
446  virtual ~KnapsackPropagator();
447 
448  // Initializes data structure and then calls InitPropagator.
449  void Init(const std::vector<int64>& profits,
450  const std::vector<int64>& weights);
451 
452  // Updates data structure and then calls UpdatePropagator.
453  // Returns false when failure.
454  bool Update(bool revert, const KnapsackAssignment& assignment);
455  // ComputeProfitBounds should set 'profit_lower_bound_' and
456  // 'profit_upper_bound_' which are constraint specific.
457  virtual void ComputeProfitBounds() = 0;
458  // Returns the id of next item to assign.
459  // Returns kNoSelection when all items are bound.
460  virtual int GetNextItemId() const = 0;
461 
462  int64 current_profit() const { return current_profit_; }
463  int64 profit_lower_bound() const { return profit_lower_bound_; }
464  int64 profit_upper_bound() const { return profit_upper_bound_; }
465 
466  // Copies the current state into 'solution'.
467  // All unbound items are set to false (i.e. not in the knapsack).
468  // When 'has_one_propagator' is true, CopyCurrentSolutionPropagator is called
469  // to have a better solution. When there is only one propagator
470  // there is no need to check the solution with other propagators, so the
471  // partial solution can be smartly completed.
472  void CopyCurrentStateToSolution(bool has_one_propagator,
473  std::vector<bool>* solution) const;
474 
475  protected:
476  // Initializes data structure. This method is called after initialization
477  // of KnapsackPropagator data structure.
478  virtual void InitPropagator() = 0;
479 
480  // Updates internal data structure incrementally. This method is called
481  // after update of KnapsackPropagator data structure.
482  virtual bool UpdatePropagator(bool revert,
483  const KnapsackAssignment& assignment) = 0;
484 
485  // Copies the current state into 'solution'.
486  // Only unbound items have to be copied as CopyCurrentSolution was already
487  // called with current state.
488  // This method is useful when a propagator is able to find a better solution
489  // than the blind instantiation to false of unbound items.
491  std::vector<bool>* solution) const = 0;
492 
493  const KnapsackState& state() const { return state_; }
494  const std::vector<KnapsackItemPtr>& items() const { return items_; }
495 
496  void set_profit_lower_bound(int64 profit) { profit_lower_bound_ = profit; }
497  void set_profit_upper_bound(int64 profit) { profit_upper_bound_ = profit; }
498 
499  private:
500  std::vector<KnapsackItemPtr> items_;
501  int64 current_profit_;
502  int64 profit_lower_bound_;
503  int64 profit_upper_bound_;
504  const KnapsackState& state_;
505 
506  DISALLOW_COPY_AND_ASSIGN(KnapsackPropagator);
507 };
508 
509 // ----- KnapsackCapacityPropagator -----
510 // KnapsackCapacityPropagator is a KnapsackPropagator used to enforce
511 // a capacity constraint.
512 // As a KnapsackPropagator is supposed to compute profit lower and upper
513 // bounds, and get the next item to select, it can be seen as a 0-1 Knapsack
514 // solver. The most efficient way to compute the upper bound is to iterate on
515 // items in profit-per-unit-weight decreasing order. The break item is
516 // commonly defined as the first item for which there is not enough remaining
517 // capacity. Selecting this break item as the next-item-to-assign usually
518 // gives the best results (see Greenberg & Hegerich).
519 //
520 // This is exactly what is implemented in this class.
521 //
522 // When there is only one propagator, it is possible to compute a better
523 // profit lower bound almost for free. During the scan to find the
524 // break element all unbound items are added just as if they were part of
525 // the current solution. This is used in both ComputeProfitBounds and
526 // CopyCurrentSolutionPropagator.
527 // For incrementality reasons, the ith item should be accessible in O(1). That's
528 // the reason why the item vector has to be duplicated 'sorted_items_'.
530  public:
532  ~KnapsackCapacityPropagator() override;
533  void ComputeProfitBounds() override;
534  int GetNextItemId() const override { return break_item_id_; }
535 
536  protected:
537  // Initializes KnapsackCapacityPropagator (e.g., sort items in decreasing
538  // order).
539  void InitPropagator() override;
540  // Updates internal data structure incrementally (i.e., 'consumed_capacity_')
541  // to avoid a O(number_of_items) scan.
542  bool UpdatePropagator(bool revert,
543  const KnapsackAssignment& assignment) override;
545  std::vector<bool>* solution) const override;
546 
547  private:
548  // An obvious additional profit upper bound corresponds to the linear
549  // relaxation: remaining_capacity * efficiency of the break item.
550  // It is possible to do better in O(1), using Martello-Toth bound U2.
551  // The main idea is to enforce integrality constraint on the break item,
552  // ie. either the break item is part of the solution, either it is not.
553  // So basically the linear relaxation is done on the item before the break
554  // item, or the one after the break item.
555  // This is what GetAdditionalProfit method implements.
556  int64 GetAdditionalProfit(int64 remaining_capacity, int break_item_id) const;
557 
558  const int64 capacity_;
559  int64 consumed_capacity_;
560  int break_item_id_;
561  std::vector<KnapsackItemPtr> sorted_items_;
562  int64 profit_max_;
563 
564  DISALLOW_COPY_AND_ASSIGN(KnapsackCapacityPropagator);
565 };
566 
567 // ----- BaseKnapsackSolver -----
568 // This is the base class for knapsack solvers.
570  public:
571  explicit BaseKnapsackSolver(const std::string& solver_name)
572  : solver_name_(solver_name) {}
573  virtual ~BaseKnapsackSolver() {}
574 
575  // Initializes the solver and enters the problem to be solved.
576  virtual void Init(const std::vector<int64>& profits,
577  const std::vector<std::vector<int64> >& weights,
578  const std::vector<int64>& capacities) = 0;
579 
580  // Gets the lower and upper bound when the item is in or out of the knapsack.
581  // To ensure objects are correctly initialized, this method should not be
582  // called before ::Init.
583  virtual void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in,
584  int64* lower_bound,
585  int64* upper_bound);
586 
587  // Solves the problem and returns the profit of the optimal solution.
588  virtual int64 Solve(TimeLimit* time_limit, bool* is_solution_optimal) = 0;
589 
590  // Returns true if the item 'item_id' is packed in the optimal knapsack.
591  virtual bool best_solution(int item_id) const = 0;
592 
593  virtual std::string GetName() const { return solver_name_; }
594 
595  private:
596  const std::string solver_name_;
597 };
598 
599 // ----- KnapsackGenericSolver -----
600 // KnapsackGenericSolver is the multi-dimensional knapsack solver class.
601 // In the current implementation, the next item to assign is given by the
602 // master propagator. Using SetMasterPropagator allows changing the default
603 // (propagator of the first dimension), and selecting another dimension when
604 // more constrained.
605 // TODO(user): In the case of a multi-dimensional knapsack problem, implement
606 // an aggregated propagator to combine all dimensions and give a better guide
607 // to select the next item (see, for instance, Dobson's aggregated efficiency).
609  public:
610  explicit KnapsackGenericSolver(const std::string& solver_name);
611  ~KnapsackGenericSolver() override;
612 
613  // Initializes the solver and enters the problem to be solved.
614  void Init(const std::vector<int64>& profits,
615  const std::vector<std::vector<int64> >& weights,
616  const std::vector<int64>& capacities) override;
617  int GetNumberOfItems() const { return state_.GetNumberOfItems(); }
618  void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in,
619  int64* lower_bound,
620  int64* upper_bound) override;
621 
622  // Sets which propagator should be used to guide the search.
623  // 'master_propagator_id' should be in 0..p-1 with p the number of
624  // propagators.
625  void set_master_propagator_id(int master_propagator_id) {
626  master_propagator_id_ = master_propagator_id;
627  }
628 
629  // Solves the problem and returns the profit of the optimal solution.
630  int64 Solve(TimeLimit* time_limit, bool* is_solution_optimal) override;
631  // Returns true if the item 'item_id' is packed in the optimal knapsack.
632  bool best_solution(int item_id) const override {
633  return best_solution_.at(item_id);
634  }
635 
636  private:
637  // Clears internal data structure.
638  void Clear();
639 
640  // Updates all propagators reverting/applying all decision on the path.
641  // Returns true if fails. Note that, even if fails, all propagators should
642  // be updated to be in a stable state in order to stay incremental.
643  bool UpdatePropagators(const KnapsackSearchPath& path);
644  // Updates all propagators reverting/applying one decision.
645  // Return true if fails. Note that, even if fails, all propagators should
646  // be updated to be in a stable state in order to stay incremental.
647  bool IncrementalUpdate(bool revert, const KnapsackAssignment& assignment);
648  // Updates the best solution if the current solution has a better profit.
649  void UpdateBestSolution();
650 
651  // Returns true if new relevant search node was added to the nodes array, that
652  // means this node should be added to the search queue too.
653  bool MakeNewNode(const KnapsackSearchNode& node, bool is_in);
654 
655  // Gets the aggregated (min) profit upper bound among all propagators.
656  int64 GetAggregatedProfitUpperBound() const;
657  bool HasOnePropagator() const { return propagators_.size() == 1; }
658  int64 GetCurrentProfit() const {
659  return propagators_.at(master_propagator_id_)->current_profit();
660  }
661  int64 GetNextItemId() const {
662  return propagators_.at(master_propagator_id_)->GetNextItemId();
663  }
664 
665  std::vector<KnapsackPropagator*> propagators_;
666  int master_propagator_id_;
667  std::vector<KnapsackSearchNode*> search_nodes_;
668  KnapsackState state_;
669  int64 best_solution_profit_;
670  std::vector<bool> best_solution_;
671 
672  DISALLOW_COPY_AND_ASSIGN(KnapsackGenericSolver);
673 };
674 #endif // SWIG
675 } // namespace operations_research
676 
677 #endif // OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
operations_research::KnapsackSearchPath::KnapsackSearchPath
KnapsackSearchPath(const KnapsackSearchNode &from, const KnapsackSearchNode &to)
Definition: knapsack_solver.cc:111
operations_research::KnapsackSolver::IsSolutionOptimal
bool IsSolutionOptimal() const
Returns true if the solution was proven optimal.
Definition: knapsack_solver.h:219
operations_research::KnapsackCapacityPropagator::InitPropagator
void InitPropagator() override
Definition: knapsack_solver.cc:253
operations_research::KnapsackCapacityPropagator::GetNextItemId
int GetNextItemId() const override
Definition: knapsack_solver.h:534
operations_research::KnapsackAssignment
Definition: knapsack_solver.h:290
operations_research::KnapsackCapacityPropagator::~KnapsackCapacityPropagator
~KnapsackCapacityPropagator() override
Definition: knapsack_solver.cc:218
operations_research::KnapsackSearchNode::KnapsackSearchNode
KnapsackSearchNode(const KnapsackSearchNode *const parent, const KnapsackAssignment &assignment)
Definition: knapsack_solver.cc:101
integral_types.h
operations_research::KnapsackSolver::KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER
@ KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER
Generic Solver.
Definition: knapsack_solver.h:166
operations_research::KnapsackPropagator::KnapsackPropagator
KnapsackPropagator(const KnapsackState &state)
Definition: knapsack_solver.cc:162
time_limit.h
operations_research::KnapsackSolver::KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER
@ KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER
SCIP based solver.
Definition: knapsack_solver.h:174
operations_research::KnapsackSearchPath::to
const KnapsackSearchNode & to() const
Definition: knapsack_solver.h:395
operations_research::KnapsackPropagator::state
const KnapsackState & state() const
Definition: knapsack_solver.h:493
operations_research::KnapsackCapacityPropagator
Definition: knapsack_solver.h:529
operations_research::KnapsackPropagator::InitPropagator
virtual void InitPropagator()=0
operations_research::KnapsackItem::KnapsackItem
KnapsackItem(int _id, int64 _weight, int64 _profit)
Definition: knapsack_solver.h:310
operations_research::KnapsackSearchPath::from
const KnapsackSearchNode & from() const
Definition: knapsack_solver.h:393
operations_research::KnapsackSolver::KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
@ KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
Dynamic Programming approach for single dimension problems.
Definition: knapsack_solver.h:150
operations_research::KnapsackAssignment::is_in
bool is_in
Definition: knapsack_solver.h:294
logging.h
operations_research::BaseKnapsackSolver::GetLowerAndUpperBoundWhenItem
virtual void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64 *lower_bound, int64 *upper_bound)
Definition: knapsack_solver.cc:1372
operations_research::KnapsackItem::GetEfficiency
double GetEfficiency(int64 profit_max) const
Definition: knapsack_solver.h:312
operations_research::KnapsackState::Init
void Init(int number_of_items)
Definition: knapsack_solver.cc:140
operations_research::KnapsackCapacityPropagator::KnapsackCapacityPropagator
KnapsackCapacityPropagator(const KnapsackState &state, int64 capacity)
Definition: knapsack_solver.cc:209
operations_research::KnapsackPropagator::profit_upper_bound
int64 profit_upper_bound() const
Definition: knapsack_solver.h:464
operations_research::KnapsackGenericSolver::GetLowerAndUpperBoundWhenItem
void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64 *lower_bound, int64 *upper_bound) override
Definition: knapsack_solver.cc:368
operations_research::KnapsackGenericSolver::best_solution
bool best_solution(int item_id) const override
Definition: knapsack_solver.h:632
operations_research::KnapsackGenericSolver::set_master_propagator_id
void set_master_propagator_id(int master_propagator_id)
Definition: knapsack_solver.h:625
macros.h
operations_research::KnapsackSolver::~KnapsackSolver
virtual ~KnapsackSolver()
Definition: knapsack_solver.cc:1176
operations_research::KnapsackSearchPath::Init
void Init()
Definition: knapsack_solver.cc:115
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::KnapsackCapacityPropagator::CopyCurrentStateToSolutionPropagator
void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const override
Definition: knapsack_solver.cc:282
operations_research::KnapsackPropagator::profit_lower_bound
int64 profit_lower_bound() const
Definition: knapsack_solver.h:463
operations_research::KnapsackState::KnapsackState
KnapsackState()
Definition: knapsack_solver.cc:138
operations_research::KnapsackGenericSolver::Solve
int64 Solve(TimeLimit *time_limit, bool *is_solution_optimal) override
Definition: knapsack_solver.cc:394
int64
int64_t int64
Definition: integral_types.h:34
operations_research::KnapsackState::is_bound
bool is_bound(int id) const
Definition: knapsack_solver.h:421
operations_research::KnapsackGenericSolver::GetNumberOfItems
int GetNumberOfItems() const
Definition: knapsack_solver.h:617
operations_research::TimeLimit
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:105
operations_research::KnapsackCapacityPropagator::UpdatePropagator
bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment) override
Definition: knapsack_solver.cc:267
operations_research::KnapsackSolver
This library solves knapsack problems.
Definition: knapsack_solver.h:120
operations_research::KnapsackItem::id
const int id
Definition: knapsack_solver.h:320
operations_research::KnapsackSearchNode::next_item_id
int next_item_id() const
Definition: knapsack_solver.h:348
operations_research::KnapsackGenericSolver::~KnapsackGenericSolver
~KnapsackGenericSolver() override
Definition: knapsack_solver.cc:345
operations_research::KnapsackPropagator::Update
bool Update(bool revert, const KnapsackAssignment &assignment)
Definition: knapsack_solver.cc:184
operations_research::KnapsackPropagator
Definition: knapsack_solver.h:443
operations_research::KnapsackSearchNode::parent
const KnapsackSearchNode *const parent() const
Definition: knapsack_solver.h:339
operations_research::KnapsackSearchNode::current_profit
int64 current_profit() const
Definition: knapsack_solver.h:342
operations_research::KnapsackSearchNode
Definition: knapsack_solver.h:334
operations_research::KnapsackState::UpdateState
bool UpdateState(bool revert, const KnapsackAssignment &assignment)
Definition: knapsack_solver.cc:146
time_limit
SharedTimeLimit * time_limit
Definition: cp_model_solver.cc:2103
operations_research::KnapsackGenericSolver::Init
void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities) override
Definition: knapsack_solver.cc:347
operations_research::KnapsackSearchNode::assignment
const KnapsackAssignment & assignment() const
Definition: knapsack_solver.h:340
operations_research::KnapsackAssignment::KnapsackAssignment
KnapsackAssignment(int _item_id, bool _is_in)
Definition: knapsack_solver.h:291
operations_research::KnapsackCapacityPropagator::ComputeProfitBounds
void ComputeProfitBounds() override
Definition: knapsack_solver.cc:222
operations_research::KnapsackSearchNode::set_profit_upper_bound
void set_profit_upper_bound(int64 profit)
Definition: knapsack_solver.h:346
operations_research::KnapsackSolver::Init
void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities)
Initializes the solver and enters the problem to be solved.
Definition: knapsack_solver.cc:1178
operations_research::KnapsackPropagator::UpdatePropagator
virtual bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment)=0
operations_research::KnapsackPropagator::GetNextItemId
virtual int GetNextItemId() const =0
operations_research::KnapsackSolver::KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER
@ KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER
CBC Based Solver.
Definition: knapsack_solver.h:158
operations_research::KnapsackSearchPath
Definition: knapsack_solver.h:388
operations_research::BaseKnapsackSolver::Init
virtual void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities)=0
operations_research::KnapsackState::GetNumberOfItems
int GetNumberOfItems() const
Definition: knapsack_solver.h:420
basictypes.h
operations_research::BaseKnapsackSolver::best_solution
virtual bool best_solution(int item_id) const =0
operations_research::KnapsackSolver::KNAPSACK_BRUTE_FORCE_SOLVER
@ KNAPSACK_BRUTE_FORCE_SOLVER
Brute force method.
Definition: knapsack_solver.h:134
operations_research::KnapsackAssignment::item_id
int item_id
Definition: knapsack_solver.h:293
operations_research::BaseKnapsackSolver::BaseKnapsackSolver
BaseKnapsackSolver(const std::string &solver_name)
Definition: knapsack_solver.h:571
operations_research::KnapsackState
Definition: knapsack_solver.h:409
operations_research::KnapsackSearchNode::profit_upper_bound
int64 profit_upper_bound() const
Definition: knapsack_solver.h:345
operations_research::KnapsackState::is_in
bool is_in(int id) const
Definition: knapsack_solver.h:422
operations_research::KnapsackSolver::use_reduction
bool use_reduction() const
Definition: knapsack_solver.h:222
operations_research::KnapsackSolver::KNAPSACK_64ITEMS_SOLVER
@ KNAPSACK_64ITEMS_SOLVER
Optimized method for single dimension small problems.
Definition: knapsack_solver.h:142
operations_research::KnapsackPropagator::CopyCurrentStateToSolution
void CopyCurrentStateToSolution(bool has_one_propagator, std::vector< bool > *solution) const
Definition: knapsack_solver.cc:196
operations_research::KnapsackPropagator::current_profit
int64 current_profit() const
Definition: knapsack_solver.h:462
operations_research::BaseKnapsackSolver::Solve
virtual int64 Solve(TimeLimit *time_limit, bool *is_solution_optimal)=0
operations_research::KnapsackSolver::set_time_limit
void set_time_limit(double time_limit_seconds)
Time limit in seconds.
Definition: knapsack_solver.h:230
operations_research::KnapsackPropagator::set_profit_lower_bound
void set_profit_lower_bound(int64 profit)
Definition: knapsack_solver.h:496
operations_research::BaseKnapsackSolver::~BaseKnapsackSolver
virtual ~BaseKnapsackSolver()
Definition: knapsack_solver.h:573
operations_research::KnapsackSearchNode::set_next_item_id
void set_next_item_id(int id)
Definition: knapsack_solver.h:349
operations_research::KnapsackSolver::BestSolutionContains
bool BestSolutionContains(int item_id) const
Returns true if the item 'item_id' is packed in the optimal knapsack.
Definition: knapsack_solver.cc:1361
operations_research::BaseKnapsackSolver::GetName
virtual std::string GetName() const
Definition: knapsack_solver.h:593
operations_research::KnapsackSolver::SolverType
SolverType
Enum controlling which underlying algorithm is used.
Definition: knapsack_solver.h:127
operations_research::KnapsackSearchPath::via
const KnapsackSearchNode & via() const
Definition: knapsack_solver.h:394
operations_research::KnapsackSolver::KnapsackSolver
KnapsackSolver(const std::string &solver_name)
Definition: knapsack_solver.cc:1119
operations_research::KnapsackSolver::GetName
std::string GetName() const
Definition: knapsack_solver.cc:1369
operations_research::KnapsackPropagator::ComputeProfitBounds
virtual void ComputeProfitBounds()=0
operations_research::KnapsackPropagator::Init
void Init(const std::vector< int64 > &profits, const std::vector< int64 > &weights)
Definition: knapsack_solver.cc:171
operations_research::KnapsackSearchPath::MoveUpToDepth
const KnapsackSearchNode * MoveUpToDepth(const KnapsackSearchNode &node, int depth) const
Definition: knapsack_solver.cc:128
operations_research::KnapsackGenericSolver
Definition: knapsack_solver.h:608
operations_research::KnapsackSearchNode::depth
int depth() const
Definition: knapsack_solver.h:338
operations_research::KnapsackItem::weight
const int64 weight
Definition: knapsack_solver.h:321
capacity
int64 capacity
Definition: routing_flow.cc:129
operations_research::KnapsackPropagator::~KnapsackPropagator
virtual ~KnapsackPropagator()
Definition: knapsack_solver.cc:169
operations_research::KnapsackItem::profit
const int64 profit
Definition: knapsack_solver.h:322
operations_research::KnapsackSearchNode::set_current_profit
void set_current_profit(int64 profit)
Definition: knapsack_solver.h:343
operations_research::KnapsackSolver::set_use_reduction
void set_use_reduction(bool use_reduction)
Definition: knapsack_solver.h:223
operations_research::KnapsackPropagator::CopyCurrentStateToSolutionPropagator
virtual void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const =0
operations_research::KnapsackGenericSolver::KnapsackGenericSolver
KnapsackGenericSolver(const std::string &solver_name)
Definition: knapsack_solver.cc:336
operations_research::KnapsackItemPtr
KnapsackItem * KnapsackItemPtr
Definition: knapsack_solver.h:324
operations_research::KnapsackPropagator::items
const std::vector< KnapsackItemPtr > & items() const
Definition: knapsack_solver.h:494
operations_research::BaseKnapsackSolver
Definition: knapsack_solver.h:569
operations_research::KnapsackPropagator::set_profit_upper_bound
void set_profit_upper_bound(int64 profit)
Definition: knapsack_solver.h:497
operations_research::KnapsackSolver::Solve
int64 Solve()
Solves the problem and returns the profit of the optimal solution.
Definition: knapsack_solver.cc:1354
operations_research::KnapsackItem
Definition: knapsack_solver.h:309
profit_max
const int64 profit_max
Definition: knapsack_solver.cc:44