![]() |
Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
|
KnapsackCapacityPropagator is a KnapsackPropagator used to enforce a capacity constraint. More...
KnapsackCapacityPropagator is a KnapsackPropagator used to enforce a capacity constraint.
As a KnapsackPropagator is supposed to compute profit lower and upper bounds, and get the next item to select, it can be seen as a 0-1 Knapsack solver. The most efficient way to compute the upper bound is to iterate on items in profit-per-unit-weight decreasing order. The break item is commonly defined as the first item for which there is not enough remaining capacity. Selecting this break item as the next-item-to-assign usually gives the best results (see Greenberg & Hegerich).
This is exactly what is implemented in this class.
When there is only one propagator, it is possible to compute a better profit lower bound almost for free. During the scan to find the break element all unbound items are added just as if they were part of the current solution. This is used in both ComputeProfitBounds and CopyCurrentSolutionPropagator.
For incrementality reasons, the ith item should be accessible in O(1). That's the reason why the item vector has to be duplicated 'sorted_items_'.
Definition at line 532 of file knapsack_solver.h.
#include <knapsack_solver.h>
Public Member Functions | |
| KnapsackCapacityPropagator (const KnapsackState &state, int64_t capacity) | |
| KnapsackCapacityPropagator (const KnapsackCapacityPropagator &)=delete | |
| KnapsackCapacityPropagator & | operator= (const KnapsackCapacityPropagator &)=delete |
| ~KnapsackCapacityPropagator () override | |
| void | ComputeProfitBounds () override |
| int | GetNextItemId () const override |
| Public Member Functions inherited from operations_research::KnapsackPropagator | |
| KnapsackPropagator (const KnapsackState &state) | |
| KnapsackPropagator (const KnapsackPropagator &)=delete | |
| KnapsackPropagator & | operator= (const KnapsackPropagator &)=delete |
| virtual | ~KnapsackPropagator () |
| void | Init (const std::vector< int64_t > &profits, const std::vector< int64_t > &weights) |
| Initializes data structure and then calls InitPropagator. | |
| bool | Update (bool revert, const KnapsackAssignment &assignment) |
| int64_t | current_profit () const |
| int64_t | profit_lower_bound () const |
| int64_t | profit_upper_bound () const |
| void | CopyCurrentStateToSolution (bool has_one_propagator, std::vector< bool > *solution) const |
Protected Member Functions | |
| void | InitPropagator () override |
| bool | UpdatePropagator (bool revert, const KnapsackAssignment &assignment) override |
| void | CopyCurrentStateToSolutionPropagator (std::vector< bool > *solution) const override |
| Protected Member Functions inherited from operations_research::KnapsackPropagator | |
| const KnapsackState & | state () const |
| const std::vector< KnapsackItemPtr > & | items () const |
| void | set_profit_lower_bound (int64_t profit) |
| void | set_profit_upper_bound (int64_t profit) |
| operations_research::KnapsackCapacityPropagator::KnapsackCapacityPropagator | ( | const KnapsackState & | state, |
| int64_t | capacity ) |
Definition at line 219 of file knapsack_solver.cc.
|
delete |
|
overridedefault |
|
overridevirtual |
ComputeProfitBounds should set 'profit_lower_bound_' and 'profit_upper_bound_' which are constraint specific.
Implements operations_research::KnapsackPropagator.
Definition at line 232 of file knapsack_solver.cc.
|
overrideprotectedvirtual |
Copies the current state into 'solution'. Only unbound items have to be copied as CopyCurrentSolution was already called with current state. This method is useful when a propagator is able to find a better solution than the blind instantiation to false of unbound items.
Implements operations_research::KnapsackPropagator.
Definition at line 292 of file knapsack_solver.cc.
|
inlineoverridevirtual |
Returns the id of next item to assign. Returns kNoSelection when all items are bound.
Implements operations_research::KnapsackPropagator.
Definition at line 545 of file knapsack_solver.h.
|
overrideprotectedvirtual |
Initializes KnapsackCapacityPropagator (e.g., sort items in decreasing order).
Implements operations_research::KnapsackPropagator.
Definition at line 263 of file knapsack_solver.cc.
|
delete |
|
overrideprotectedvirtual |
Updates internal data structure incrementally (i.e., 'consumed_capacity_') to avoid a O(number_of_items) scan.
Implements operations_research::KnapsackPropagator.
Definition at line 277 of file knapsack_solver.cc.