25#include "absl/log/check.h"
26#include "absl/strings/string_view.h"
27#include "absl/time/time.h"
31#include "ortools/sat/cp_model.pb.h"
39const int kNoSelection = -1;
40const int kPrimaryPropagatorId = 0;
41const int kMaxNumberOfBruteForceItems = 30;
42const int kMaxNumberOf64Items = 64;
46struct CompareKnapsackItemsInDecreasingEfficiencyOrder {
47 explicit CompareKnapsackItemsInDecreasingEfficiencyOrder(int64_t _profit_max)
51 return item1->GetEfficiency(profit_max) > item2->GetEfficiency(profit_max);
61struct CompareKnapsackSearchNodePtrInDecreasingUpperBoundOrder {
62 bool operator()(
const KnapsackSearchNode* node_1,
63 const KnapsackSearchNode* node_2)
const {
64 const int64_t profit_upper_bound_1 = node_1->profit_upper_bound();
65 const int64_t profit_upper_bound_2 = node_2->profit_upper_bound();
66 if (profit_upper_bound_1 == profit_upper_bound_2) {
67 return node_1->current_profit() < node_2->current_profit();
69 return profit_upper_bound_1 < profit_upper_bound_2;
73typedef std::priority_queue<
74 KnapsackSearchNode*, std::vector<KnapsackSearchNode*>,
75 CompareKnapsackSearchNodePtrInDecreasingUpperBoundOrder>
79inline bool WillProductOverflow(int64_t value_1, int64_t value_2) {
84 const int kOverflow = 61;
85 return MostSignificantBitPosition1 + MostSignificantBitPosition2 > kOverflow;
89int64_t UpperBoundOfRatio(int64_t numerator_1, int64_t numerator_2,
90 int64_t denominator) {
91 DCHECK_GT(denominator, int64_t{0});
92 if (!WillProductOverflow(numerator_1, numerator_2)) {
93 const int64_t numerator = numerator_1 * numerator_2;
95 const int64_t result = numerator / denominator;
99 (
static_cast<double>(numerator_1) *
static_cast<double>(numerator_2)) /
100 static_cast<double>(denominator);
102 const int64_t result =
static_cast<int64_t
>(
floor(
ratio + 0.5));
112 : depth_((parent == nullptr) ? 0 : parent->depth() + 1),
114 assignment_(assignment),
116 profit_upper_bound_(
std::numeric_limits<int64_t>::
max()),
117 next_item_id_(kNoSelection) {}
122 : from_(from), via_(nullptr), to_(
to) {}
127 CHECK_EQ(node_from->
depth(), node_to->
depth());
130 while (node_from != node_to) {
131 node_from = node_from->
parent();
132 node_to = node_to->
parent();
140 while (current_node->
depth() > depth) {
141 current_node = current_node->
parent();
150 is_bound_.assign(number_of_items,
false);
151 is_in_.assign(number_of_items,
false);
158 is_bound_[assignment.
item_id] =
false;
160 if (is_bound_[assignment.
item_id] &&
164 is_bound_[assignment.
item_id] =
true;
174 profit_lower_bound_(0),
175 profit_upper_bound_(
std::numeric_limits<int64_t>::
max()),
181 const std::vector<int64_t>& weights) {
182 const int number_of_items = profits.size();
184 for (
int i = 0; i < number_of_items; ++i) {
185 items_[i] =
new KnapsackItem(i, weights[i], profits[i]);
188 profit_lower_bound_ = std::numeric_limits<int64_t>::min();
189 profit_upper_bound_ = std::numeric_limits<int64_t>::max();
195 if (assignment.
is_in) {
197 current_profit_ -= items_[assignment.
item_id]->profit;
199 current_profit_ += items_[assignment.
item_id]->profit;
206 bool has_one_propagator, std::vector<bool>*
solution)
const {
209 const int item_id = item->id;
210 (*solution)[item_id] = state_.
is_bound(item_id) && state_.
is_in(item_id);
212 if (has_one_propagator) {
222 consumed_capacity_(0),
223 break_item_id_(kNoSelection),
233 break_item_id_ = kNoSelection;
235 int64_t remaining_capacity = capacity_ - consumed_capacity_;
236 int break_sorted_item_id = kNoSelection;
237 const int number_of_sorted_items = sorted_items_.size();
238 for (
int sorted_id = 0; sorted_id < number_of_sorted_items; ++sorted_id) {
239 const KnapsackItem*
const item = sorted_items_[sorted_id];
240 if (!
state().is_bound(item->
id)) {
241 break_item_id_ = item->
id;
243 if (remaining_capacity >= item->
weight) {
244 remaining_capacity -= item->
weight;
247 break_sorted_item_id = sorted_id;
255 if (break_sorted_item_id != kNoSelection) {
256 const int64_t additional_profit =
257 GetAdditionalProfit(remaining_capacity, break_sorted_item_id);
263 consumed_capacity_ = 0;
264 break_item_id_ = kNoSelection;
265 sorted_items_ =
items();
268 profit_max_ = std::max(profit_max_, item->profit);
271 CompareKnapsackItemsInDecreasingEfficiencyOrder compare_object(profit_max_);
272 std::stable_sort(sorted_items_.begin(), sorted_items_.end(), compare_object);
278 if (assignment.
is_in) {
280 consumed_capacity_ -=
items()[assignment.
item_id]->weight;
282 consumed_capacity_ +=
items()[assignment.
item_id]->weight;
283 if (consumed_capacity_ > capacity_) {
292 std::vector<bool>*
solution)
const {
294 int64_t remaining_capacity = capacity_ - consumed_capacity_;
296 if (!
state().is_bound(item->id)) {
297 if (remaining_capacity >= item->weight) {
298 remaining_capacity -= item->weight;
299 (*solution)[item->id] =
true;
307int64_t KnapsackCapacityPropagator::GetAdditionalProfit(
308 int64_t remaining_capacity,
int break_item_id)
const {
309 const int after_break_item_id = break_item_id + 1;
310 int64_t additional_profit_when_no_break_item = 0;
311 if (after_break_item_id < sorted_items_.size()) {
314 const int64_t next_weight = sorted_items_[after_break_item_id]->weight;
315 const int64_t next_profit = sorted_items_[after_break_item_id]->profit;
316 additional_profit_when_no_break_item =
317 UpperBoundOfRatio(remaining_capacity, next_profit, next_weight);
320 const int before_break_item_id = break_item_id - 1;
321 int64_t additional_profit_when_break_item = 0;
322 if (before_break_item_id >= 0) {
323 const int64_t previous_weight = sorted_items_[before_break_item_id]->weight;
327 if (previous_weight != 0) {
328 const int64_t previous_profit =
329 sorted_items_[before_break_item_id]->profit;
330 const int64_t overused_capacity =
331 sorted_items_[break_item_id]->weight - remaining_capacity;
332 const int64_t
ratio = UpperBoundOfRatio(overused_capacity,
333 previous_profit, previous_weight);
334 additional_profit_when_break_item =
335 sorted_items_[break_item_id]->profit -
ratio;
339 const int64_t additional_profit = std::max(
340 additional_profit_when_no_break_item, additional_profit_when_break_item);
341 CHECK_GE(additional_profit, 0);
342 return additional_profit;
349 primary_propagator_id_(kPrimaryPropagatorId),
352 best_solution_profit_(0),
358 const std::vector<int64_t>& profits,
359 const std::vector<std::vector<int64_t>>& weights,
360 const std::vector<int64_t>& capacities) {
361 CHECK_EQ(capacities.size(), weights.size());
364 const int number_of_items = profits.size();
365 const int number_of_dimensions = weights.size();
366 state_.
Init(number_of_items);
367 best_solution_.assign(number_of_items,
false);
368 for (
int i = 0; i < number_of_dimensions; ++i) {
369 CHECK_EQ(number_of_items, weights[i].
size());
373 propagator->
Init(profits, weights[i]);
374 propagators_.push_back(propagator);
376 primary_propagator_id_ = kPrimaryPropagatorId;
384 const bool fail = !IncrementalUpdate(
false, assignment);
391 ? propagators_[primary_propagator_id_]->profit_lower_bound()
396 const bool fail_revert = !IncrementalUpdate(
true, assignment);
404 double time_limit_in_second,
405 bool* is_solution_optimal) {
407 DCHECK(is_solution_optimal !=
nullptr);
408 best_solution_profit_ = 0LL;
409 *is_solution_optimal =
true;
411 SearchQueue search_queue;
417 search_nodes_.push_back(root_node);
419 if (MakeNewNode(*root_node,
false)) {
420 search_queue.push(search_nodes_.back());
422 if (MakeNewNode(*root_node,
true)) {
423 search_queue.push(search_nodes_.back());
427 while (!search_queue.empty() &&
428 search_queue.top()->profit_upper_bound() > best_solution_profit_) {
430 *is_solution_optimal =
false;
436 if (node != current_node) {
439 const bool no_fail = UpdatePropagators(path);
441 CHECK_EQ(no_fail,
true);
444 if (MakeNewNode(*node,
false)) {
445 search_queue.push(search_nodes_.back());
447 if (MakeNewNode(*node,
true)) {
448 search_queue.push(search_nodes_.back());
451 return best_solution_profit_;
454void KnapsackGenericSolver::Clear() {
460bool KnapsackGenericSolver::UpdatePropagators(
const KnapsackSearchPath& path) {
463 const KnapsackSearchNode* node = &path.from();
464 const KnapsackSearchNode* via = &path.via();
465 while (node != via) {
466 no_fail = IncrementalUpdate(
true, node->assignment()) && no_fail;
467 node = node->parent();
471 while (node != via) {
472 no_fail = IncrementalUpdate(
false, node->assignment()) && no_fail;
473 node = node->parent();
478int64_t KnapsackGenericSolver::GetAggregatedProfitUpperBound()
const {
479 int64_t
upper_bound = std::numeric_limits<int64_t>::max();
480 for (KnapsackPropagator*
const prop : propagators_) {
481 prop->ComputeProfitBounds();
482 const int64_t propagator_upper_bound = prop->profit_upper_bound();
488bool KnapsackGenericSolver::MakeNewNode(
const KnapsackSearchNode& node,
490 if (node.next_item_id() == kNoSelection) {
493 KnapsackAssignment assignment(node.next_item_id(), is_in);
494 KnapsackSearchNode new_node(&node, assignment);
496 KnapsackSearchPath path(node, new_node);
498 const bool no_fail = UpdatePropagators(path);
500 new_node.set_current_profit(GetCurrentProfit());
501 new_node.set_profit_upper_bound(GetAggregatedProfitUpperBound());
502 new_node.set_next_item_id(GetNextItemId());
503 UpdateBestSolution();
507 KnapsackSearchPath revert_path(new_node, node);
509 UpdatePropagators(revert_path);
511 if (!no_fail || new_node.profit_upper_bound() < best_solution_profit_) {
516 KnapsackSearchNode* relevant_node =
new KnapsackSearchNode(&node, assignment);
517 relevant_node->set_current_profit(new_node.current_profit());
518 relevant_node->set_profit_upper_bound(new_node.profit_upper_bound());
519 relevant_node->set_next_item_id(new_node.next_item_id());
520 search_nodes_.push_back(relevant_node);
525bool KnapsackGenericSolver::IncrementalUpdate(
526 bool revert,
const KnapsackAssignment& assignment) {
529 bool no_fail = state_.
UpdateState(revert, assignment);
530 for (KnapsackPropagator*
const prop : propagators_) {
531 no_fail = prop->Update(revert, assignment) && no_fail;
536void KnapsackGenericSolver::UpdateBestSolution() {
537 const int64_t profit_lower_bound =
539 ? propagators_[primary_propagator_id_]->profit_lower_bound()
540 : propagators_[primary_propagator_id_]->current_profit();
542 if (best_solution_profit_ < profit_lower_bound) {
543 best_solution_profit_ = profit_lower_bound;
544 propagators_[primary_propagator_id_]->CopyCurrentStateToSolution(
545 HasOnePropagator(), &best_solution_);
563 void Init(
const std::vector<int64_t>& profits,
564 const std::vector<std::vector<int64_t>>& weights,
565 const std::vector<int64_t>& capacities)
override;
569 bool* is_solution_optimal)
override;
573 return (best_solution_ &
OneBit32(item_id)) != 0U;
578 int64_t profits_weights_[kMaxNumberOfBruteForceItems * 2];
580 int64_t best_solution_profit_;
581 uint32_t best_solution_;
585 absl::string_view solver_name)
589 best_solution_profit_(0LL),
590 best_solution_(0U) {}
593 const std::vector<int64_t>& profits,
594 const std::vector<std::vector<int64_t>>& weights,
595 const std::vector<int64_t>& capacities) {
597 CHECK_EQ(weights.size(), 1)
598 <<
"Brute force solver only works with one dimension.";
599 CHECK_EQ(capacities.size(), weights.size());
601 num_items_ = profits.size();
602 CHECK_EQ(num_items_, weights.at(0).size());
603 CHECK_LE(num_items_, kMaxNumberOfBruteForceItems)
604 <<
"To use KnapsackBruteForceSolver the number of items should be "
605 <<
"less than " << kMaxNumberOfBruteForceItems
606 <<
". Current value: " << num_items_ <<
".";
608 for (
int i = 0; i < num_items_; ++i) {
609 profits_weights_[i * 2] = profits.at(i);
610 profits_weights_[i * 2 + 1] = weights.at(0).at(i);
612 capacity_ = capacities.at(0);
616 double time_limit_in_second,
617 bool* is_solution_optimal) {
618 DCHECK(is_solution_optimal !=
nullptr);
619 *is_solution_optimal =
true;
620 best_solution_profit_ = 0LL;
623 const uint32_t num_states =
OneBit32(num_items_);
624 uint32_t prev_state = 0U;
625 uint64_t sum_profit = 0ULL;
626 uint64_t sum_weight = 0ULL;
627 uint32_t diff_state = 0U;
628 uint32_t local_state = 0U;
632 for (uint32_t state = 1U; state < num_states; ++state, ++prev_state) {
633 diff_state = state ^ prev_state;
637 if (diff_state & 1U) {
638 if (local_state & 1U) {
639 sum_profit += profits_weights_[item_id];
640 sum_weight += profits_weights_[item_id + 1];
641 CHECK_LT(item_id + 1, 2 * num_items_);
643 sum_profit -= profits_weights_[item_id];
644 sum_weight -= profits_weights_[item_id + 1];
645 CHECK_LT(item_id + 1, 2 * num_items_);
649 local_state = local_state >> 1;
650 diff_state = diff_state >> 1;
653 if (sum_weight <= capacity_ && best_solution_profit_ < sum_profit) {
654 best_solution_profit_ = sum_profit;
655 best_solution_ = state;
659 return best_solution_profit_;
675 static_cast<double>(_weight)
676 : static_cast<double>(_profit_max)) {}
693 void Init(
const std::vector<int64_t>& profits,
694 const std::vector<std::vector<int64_t>>& weights,
695 const std::vector<int64_t>& capacities)
override;
699 bool* is_solution_optimal)
override;
703 return (best_solution_ &
OneBit64(item_id)) != 0ULL;
707 int GetBreakItemId(int64_t capacity)
const;
709 void GoToNextState(
bool has_failed);
710 void BuildBestSolution();
712 std::vector<KnapsackItemWithEfficiency> sorted_items_;
713 std::vector<int64_t> sum_profits_;
714 std::vector<int64_t> sum_weights_;
719 int64_t best_solution_profit_;
720 uint64_t best_solution_;
721 int best_solution_depth_;
724 int64_t state_weight_;
726 int64_t rejected_items_profit_;
728 int64_t rejected_items_weight_;
747 best_solution_profit_(0LL),
748 best_solution_(0ULL),
749 best_solution_depth_(0),
751 rejected_items_profit_(0LL),
752 rejected_items_weight_(0LL) {}
755 const std::vector<int64_t>& profits,
756 const std::vector<std::vector<int64_t>>& weights,
757 const std::vector<int64_t>& capacities) {
758 CHECK_EQ(weights.size(), 1)
759 <<
"Brute force solver only works with one dimension.";
760 CHECK_EQ(capacities.size(), weights.size());
762 sorted_items_.clear();
763 sum_profits_.clear();
764 sum_weights_.clear();
766 capacity_ = capacities[0];
767 const int num_items = profits.size();
768 CHECK_LE(num_items, kMaxNumberOf64Items)
769 <<
"To use Knapsack64ItemsSolver the number of items should be "
770 <<
"less than " << kMaxNumberOf64Items <<
". Current value: " << num_items
772 int64_t
profit_max = *std::max_element(profits.begin(), profits.end());
774 for (
int i = 0; i < num_items; ++i) {
775 sorted_items_.push_back(
779 std::sort(sorted_items_.begin(), sorted_items_.end(),
782 int64_t sum_profit = 0;
783 int64_t sum_weight = 0;
784 sum_profits_.push_back(sum_profit);
785 sum_weights_.push_back(sum_weight);
786 for (
int i = 0; i < num_items; ++i) {
787 sum_profit += sorted_items_[i].profit;
788 sum_weight += sorted_items_[i].weight;
790 sum_profits_.push_back(sum_profit);
791 sum_weights_.push_back(sum_weight);
796 double time_limit_in_second,
797 bool* is_solution_optimal) {
798 DCHECK(is_solution_optimal !=
nullptr);
799 *is_solution_optimal =
true;
800 const int num_items = sorted_items_.size();
803 state_weight_ = sorted_items_[0].weight;
804 rejected_items_profit_ = 0LL;
805 rejected_items_weight_ = 0LL;
806 best_solution_profit_ = 0LL;
807 best_solution_ = 0ULL;
808 best_solution_depth_ = 0;
813 while (state_depth_ >= 0) {
815 if (state_weight_ > capacity_ || state_depth_ >= num_items) {
821 best_solution_ = state_;
822 best_solution_depth_ = state_depth_;
825 fail = fail || best_solution_profit_ >=
upper_bound;
830 return best_solution_profit_;
833int Knapsack64ItemsSolver::GetBreakItemId(int64_t capacity)
const {
834 std::vector<int64_t>::const_iterator binary_search_iterator =
835 std::upper_bound(sum_weights_.begin(), sum_weights_.end(), capacity);
836 return static_cast<int>(binary_search_iterator - sum_weights_.begin()) - 1;
846void Knapsack64ItemsSolver::GetLowerAndUpperBound(int64_t*
lower_bound,
848 const int64_t available_capacity = capacity_ + rejected_items_weight_;
849 const int break_item_id = GetBreakItemId(available_capacity);
850 const int num_items = sorted_items_.size();
851 if (break_item_id >= num_items) {
852 *
lower_bound = sum_profits_[num_items] - rejected_items_profit_;
857 *
lower_bound = sum_profits_[break_item_id] - rejected_items_profit_;
859 const int64_t consumed_capacity = sum_weights_[break_item_id];
860 const int64_t remaining_capacity = available_capacity - consumed_capacity;
861 const double efficiency = sorted_items_[break_item_id].efficiency;
862 const int64_t additional_profit =
863 static_cast<int64_t
>(remaining_capacity * efficiency);
872void Knapsack64ItemsSolver::GoToNextState(
bool has_failed) {
873 uint64_t mask =
OneBit64(state_depth_);
876 state_ = state_ | (mask << 1);
877 state_weight_ += sorted_items_[state_depth_].weight;
880 while ((state_ & mask) == 0ULL && state_depth_ >= 0) {
881 const KnapsackItemWithEfficiency& item = sorted_items_[state_depth_];
882 rejected_items_profit_ -= item.profit;
883 rejected_items_weight_ -= item.weight;
889 state_ = state_ & ~mask;
890 const KnapsackItemWithEfficiency& item = sorted_items_[state_depth_];
891 rejected_items_profit_ += item.profit;
892 rejected_items_weight_ += item.weight;
893 state_weight_ -= item.weight;
898void Knapsack64ItemsSolver::BuildBestSolution() {
899 int64_t remaining_capacity = capacity_;
900 int64_t check_profit = 0LL;
904 for (
int i = 0;
i <= best_solution_depth_; ++
i) {
906 remaining_capacity -= sorted_items_[
i].weight;
907 check_profit += sorted_items_[
i].profit;
912 const int num_items = sorted_items_.size();
913 for (
int i = best_solution_depth_ + 1;
i < num_items; ++
i) {
914 int64_t
weight = sorted_items_[
i].weight;
915 if (remaining_capacity >=
weight) {
916 remaining_capacity -=
weight;
917 check_profit += sorted_items_[
i].profit;
918 best_solution_ = best_solution_ |
OneBit64(i);
920 best_solution_ = best_solution_ & ~OneBit64(i);
923 CHECK_EQ(best_solution_profit_, check_profit);
929 uint64_t tmp_solution = 0ULL;
930 for (
int i = 0;
i < num_items; ++
i) {
932 const int original_id = sorted_items_[
i].id;
933 tmp_solution = tmp_solution |
OneBit64(original_id);
937 best_solution_ = tmp_solution;
952 void Init(
const std::vector<int64_t>& profits,
953 const std::vector<std::vector<int64_t>>& weights,
954 const std::vector<int64_t>& capacities)
override;
958 bool* is_solution_optimal)
override;
962 return best_solution_.at(item_id);
966 int64_t SolveSubProblem(int64_t capacity,
int num_items);
968 std::vector<int64_t> profits_;
969 std::vector<int64_t> weights_;
971 std::vector<int64_t> computed_profits_;
972 std::vector<int> selected_item_ids_;
973 std::vector<bool> best_solution_;
978 absl::string_view solver_name)
984 selected_item_ids_(),
988 const std::vector<int64_t>& profits,
989 const std::vector<std::vector<int64_t>>& weights,
990 const std::vector<int64_t>& capacities) {
991 CHECK_EQ(weights.size(), 1)
992 <<
"Current implementation of the dynamic programming solver only deals"
993 <<
" with one dimension.";
994 CHECK_EQ(capacities.size(), weights.size());
997 weights_ = weights[0];
998 capacity_ = capacities[0];
1001int64_t KnapsackDynamicProgrammingSolver::SolveSubProblem(int64_t capacity,
1003 const int64_t capacity_plus_1 = capacity + 1;
1004 std::fill_n(selected_item_ids_.begin(), capacity_plus_1, 0);
1005 std::fill_n(computed_profits_.begin(), capacity_plus_1, int64_t{0});
1006 for (
int item_id = 0; item_id < num_items; ++item_id) {
1007 const int64_t item_weight = weights_[item_id];
1008 const int64_t item_profit = profits_[item_id];
1009 for (int64_t used_capacity = capacity; used_capacity >= item_weight;
1011 if (computed_profits_[used_capacity - item_weight] + item_profit >
1012 computed_profits_[used_capacity]) {
1013 computed_profits_[used_capacity] =
1014 computed_profits_[used_capacity - item_weight] + item_profit;
1015 selected_item_ids_[used_capacity] = item_id;
1019 return selected_item_ids_.at(capacity);
1023 double time_limit_in_second,
1024 bool* is_solution_optimal) {
1025 DCHECK(is_solution_optimal !=
nullptr);
1026 *is_solution_optimal =
true;
1027 const int64_t capacity_plus_1 = capacity_ + 1;
1028 selected_item_ids_.assign(capacity_plus_1, 0);
1029 computed_profits_.assign(capacity_plus_1, 0LL);
1031 int64_t remaining_capacity = capacity_;
1032 int num_items = profits_.size();
1033 best_solution_.assign(num_items,
false);
1035 while (remaining_capacity > 0 && num_items > 0) {
1036 const int selected_item_id = SolveSubProblem(remaining_capacity, num_items);
1037 remaining_capacity -= weights_[selected_item_id];
1038 num_items = selected_item_id;
1039 if (remaining_capacity >= 0) {
1040 best_solution_[selected_item_id] =
true;
1044 return computed_profits_[capacity_];
1060 void Init(
const std::vector<int64_t>& profits,
1061 const std::vector<std::vector<int64_t>>& weights,
1062 const std::vector<int64_t>& capacities)
override;
1066 bool* is_solution_optimal)
override;
1070 return best_solution_.at(item_id);
1075 void SolveSubProblem(
bool first_storage, int64_t capacity,
int start_item,
1079 int64_t DivideAndConquer(int64_t capacity,
int start_item,
int end_item);
1081 std::vector<int64_t> profits_;
1082 std::vector<int64_t> weights_;
1084 std::vector<int64_t> computed_profits_storage1_;
1085 std::vector<int64_t> computed_profits_storage2_;
1086 std::vector<bool> best_solution_;
1091 absl::string_view solver_name)
1096 computed_profits_storage1_(),
1097 computed_profits_storage2_(),
1101 const std::vector<int64_t>& profits,
1102 const std::vector<std::vector<int64_t>>& weights,
1103 const std::vector<int64_t>& capacities) {
1104 CHECK_EQ(weights.size(), 1)
1105 <<
"Current implementation of the divide and conquer solver only deals"
1106 <<
" with one dimension.";
1107 CHECK_EQ(capacities.size(), weights.size());
1110 weights_ = weights[0];
1111 capacity_ = capacities[0];
1114void KnapsackDivideAndConquerSolver::SolveSubProblem(
bool first_storage,
1118 std::vector<int64_t>& computed_profits_storage =
1119 (first_storage) ? computed_profits_storage1_ : computed_profits_storage2_;
1120 const int64_t capacity_plus_1 = capacity + 1;
1121 std::fill_n(computed_profits_storage.begin(), capacity_plus_1, 0LL);
1122 for (
int item_id = start_item; item_id < end_item; ++item_id) {
1123 const int64_t item_weight = weights_[item_id];
1124 const int64_t item_profit = profits_[item_id];
1125 for (int64_t used_capacity = capacity; used_capacity >= item_weight;
1127 if (computed_profits_storage[used_capacity - item_weight] + item_profit >
1128 computed_profits_storage[used_capacity]) {
1129 computed_profits_storage[used_capacity] =
1130 computed_profits_storage[used_capacity - item_weight] + item_profit;
1136int64_t KnapsackDivideAndConquerSolver::DivideAndConquer(int64_t capacity,
1139 int item_boundary = start_item + ((end_item - start_item) / 2);
1141 SolveSubProblem(
true, capacity, start_item, item_boundary);
1142 SolveSubProblem(
false, capacity, item_boundary, end_item);
1144 int64_t max_solution = 0, capacity1 = 0, capacity2 = 0;
1146 for (int64_t capacity_id = 0; capacity_id <= capacity; capacity_id++) {
1147 if ((computed_profits_storage1_[capacity_id] +
1148 computed_profits_storage2_[(capacity - capacity_id)]) > max_solution) {
1149 capacity1 = capacity_id;
1150 capacity2 = capacity - capacity_id;
1151 max_solution = (computed_profits_storage1_[capacity_id] +
1152 computed_profits_storage2_[(capacity - capacity_id)]);
1156 if ((item_boundary - start_item) == 1) {
1157 if (weights_[start_item] <= capacity1) best_solution_[start_item] =
true;
1158 }
else if ((item_boundary - start_item) > 1) {
1159 DivideAndConquer(capacity1, start_item, item_boundary);
1162 if ((end_item - item_boundary) == 1) {
1163 if (weights_[item_boundary] <= capacity2)
1164 best_solution_[item_boundary] =
true;
1165 }
else if ((end_item - item_boundary) > 1) {
1166 DivideAndConquer(capacity2, item_boundary, end_item);
1168 return max_solution;
1172 double time_limit_in_second,
1173 bool* is_solution_optimal) {
1174 DCHECK(is_solution_optimal !=
nullptr);
1175 *is_solution_optimal =
true;
1176 const int64_t capacity_plus_1 = capacity_ + 1;
1177 computed_profits_storage1_.assign(capacity_plus_1, 0LL);
1178 computed_profits_storage2_.assign(capacity_plus_1, 0LL);
1179 best_solution_.assign(profits_.size(),
false);
1181 return DivideAndConquer(capacity_, 0, profits_.size());
1187 absl::string_view solver_name);
1190 void Init(
const std::vector<int64_t>& profits,
1191 const std::vector<std::vector<int64_t>>& weights,
1192 const std::vector<int64_t>& capacities)
override;
1196 bool* is_solution_optimal)
override;
1200 return best_solution_.at(item_id);
1205 std::vector<int64_t> profits_;
1206 std::vector<std::vector<int64_t>> weights_;
1207 std::vector<int64_t> capacities_;
1208 std::vector<bool> best_solution_;
1213 absl::string_view solver_name)
1222 const std::vector<std::vector<int64_t>>& weights,
1223 const std::vector<int64_t>& capacities) {
1226 capacities_ = capacities;
1230 double time_limit_in_second,
1231 bool* is_solution_optimal) {
1232 DCHECK(is_solution_optimal !=
nullptr);
1233 *is_solution_optimal =
true;
1236 const int num_items = profits_.size();
1237 std::vector<MPVariable*> variables;
1241 const int num_dimensions = capacities_.size();
1242 CHECK(weights_.size() == num_dimensions)
1243 <<
"Weights should be vector of num_dimensions (" << num_dimensions
1244 <<
") vectors of size num_items (" << num_items <<
").";
1245 for (
int i = 0; i < num_dimensions; ++i) {
1247 for (
int j = 0; j < num_items; ++j) {
1248 ct->SetCoefficient(variables.at(j), weights_.at(i).at(j));
1256 for (
int j = 0; j < num_items; ++j) {
1262 solver.
SetTimeLimit(absl::Seconds(time_limit_in_second));
1265 best_solution_.assign(num_items,
false);
1268 const float kRoundNear = 0.5;
1269 for (
int j = 0; j < num_items; ++j) {
1270 const double value = variables.at(j)->solution_value();
1271 best_solution_.at(j) =
value >= kRoundNear;
1276 return -objective->
Value() + kRoundNear;
1278 *is_solution_optimal =
false;
1289 void Init(
const std::vector<int64_t>& profits,
1290 const std::vector<std::vector<int64_t>>& weights,
1291 const std::vector<int64_t>& capacities)
override;
1295 bool* is_solution_optimal)
override;
1299 return best_solution_.at(item_id);
1303 std::vector<int64_t> profits_;
1304 std::vector<std::vector<int64_t>> weights_;
1305 std::vector<int64_t> capacities_;
1306 std::vector<bool> best_solution_;
1317 const std::vector<std::vector<int64_t>>& weights,
1318 const std::vector<int64_t>& capacities) {
1321 capacities_ = capacities;
1325 double time_limit_in_seconds,
1326 bool* is_solution_optimal) {
1327 DCHECK(is_solution_optimal !=
nullptr);
1328 *is_solution_optimal =
true;
1333 const int num_items = profits_.size();
1334 std::vector<sat::BoolVar> variables;
1335 variables.reserve(num_items);
1336 for (
int i = 0; i < num_items; ++i) {
1337 variables.push_back(
model.NewBoolVar());
1341 const int num_dimensions = capacities_.size();
1342 CHECK(weights_.size() == num_dimensions)
1343 <<
"Weights should be vector of num_dimensions (" << num_dimensions
1344 <<
") vectors of size num_items (" << num_items <<
").";
1345 for (
int i = 0; i < num_dimensions; ++i) {
1347 for (
int j = 0; j < num_items; ++j) {
1348 expr += variables.at(j) * weights_.at(i).at(j);
1350 model.AddLessOrEqual(expr, capacities_.at(i));
1355 for (
int j = 0; j < num_items; ++j) {
1356 objective += variables.at(j) * profits_.at(j);
1358 model.Maximize(objective);
1361 parameters.set_num_workers(num_items > 100 ? 16 : 8);
1362 parameters.set_max_time_in_seconds(time_limit_in_seconds);
1364 const sat::CpSolverResponse response =
1368 best_solution_.assign(num_items,
false);
1369 if (response.status() == sat::CpSolverStatus::OPTIMAL ||
1370 response.status() == sat::CpSolverStatus::FEASIBLE) {
1371 for (
int j = 0; j < num_items; ++j) {
1372 best_solution_.at(j) = SolutionBooleanValue(response, variables.at(j));
1374 *is_solution_optimal = response.status() == sat::CpSolverStatus::OPTIMAL;
1376 return response.objective_value();
1378 *is_solution_optimal =
false;
1385 :
KnapsackSolver(KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
1389 const std::string& solver_name)
1393 mapping_reduced_item_id_(),
1394 is_problem_solved_(false),
1395 additional_profit_(0LL),
1396 use_reduction_(true),
1397 time_limit_seconds_(
std::numeric_limits<double>::infinity()) {
1398 switch (solver_type) {
1400 solver_ = std::make_unique<KnapsackBruteForceSolver>(solver_name);
1403 solver_ = std::make_unique<Knapsack64ItemsSolver>(solver_name);
1406 solver_ = std::make_unique<KnapsackDynamicProgrammingSolver>(solver_name);
1409 solver_ = std::make_unique<KnapsackGenericSolver>(solver_name);
1412 solver_ = std::make_unique<KnapsackDivideAndConquerSolver>(solver_name);
1416 solver_ = std::make_unique<KnapsackMIPSolver>(
1420#if defined(USE_SCIP)
1422 solver_ = std::make_unique<KnapsackMIPSolver>(
1426#if defined(USE_XPRESS)
1428 solver_ = std::make_unique<KnapsackMIPSolver>(
1432#if defined(USE_CPLEX)
1434 solver_ = std::make_unique<KnapsackMIPSolver>(
1439 solver_ = std::make_unique<KnapsackCpSat>(solver_name);
1442 LOG(FATAL) <<
"Unknown knapsack solver type.";
1449 const std::vector<std::vector<int64_t>>& weights,
1450 const std::vector<int64_t>& capacities) {
1451 for (
const std::vector<int64_t>&
w : weights) {
1452 CHECK_EQ(profits.size(),
w.size())
1453 <<
"Profits and inner weights must have the same size (#items)";
1455 CHECK_EQ(capacities.size(), weights.size())
1456 <<
"Capacities and weights must have the same size (#bins)";
1457 time_limit_ = std::make_unique<TimeLimit>(time_limit_seconds_);
1458 is_solution_optimal_ =
false;
1459 additional_profit_ = 0LL;
1460 is_problem_solved_ =
false;
1462 const int num_items = profits.size();
1463 std::vector<std::vector<int64_t>> reduced_weights;
1464 std::vector<int64_t> reduced_capacities;
1465 if (use_reduction_) {
1466 const int num_reduced_items = ReduceCapacities(
1467 num_items, weights, capacities, &reduced_weights, &reduced_capacities);
1468 if (num_reduced_items > 0) {
1469 ComputeAdditionalProfit(profits);
1472 reduced_weights = weights;
1473 reduced_capacities = capacities;
1475 if (!is_problem_solved_) {
1476 solver_->Init(profits, reduced_weights, reduced_capacities);
1477 if (use_reduction_) {
1478 const int num_reduced_items = ReduceProblem(num_items);
1480 if (num_reduced_items > 0) {
1481 ComputeAdditionalProfit(profits);
1484 if (num_reduced_items > 0 && num_reduced_items < num_items) {
1485 InitReducedProblem(profits, reduced_weights, reduced_capacities);
1489 if (is_problem_solved_) {
1490 is_solution_optimal_ =
true;
1494int KnapsackSolver::ReduceCapacities(
1495 int num_items,
const std::vector<std::vector<int64_t>>& weights,
1496 const std::vector<int64_t>& capacities,
1497 std::vector<std::vector<int64_t>>* reduced_weights,
1498 std::vector<int64_t>* reduced_capacities) {
1499 known_value_.assign(num_items,
false);
1500 best_solution_.assign(num_items,
false);
1501 mapping_reduced_item_id_.assign(num_items, 0);
1502 std::vector<bool> active_capacities(weights.size(),
true);
1503 int number_of_active_capacities = 0;
1504 for (
int i = 0; i < weights.size(); ++i) {
1505 int64_t max_weight = 0;
1506 for (int64_t
weight : weights[i]) {
1509 if (max_weight <= capacities[i]) {
1510 active_capacities[i] =
false;
1512 ++number_of_active_capacities;
1515 reduced_weights->reserve(number_of_active_capacities);
1516 reduced_capacities->reserve(number_of_active_capacities);
1517 for (
int i = 0;
i < weights.size(); ++
i) {
1518 if (active_capacities[i]) {
1519 reduced_weights->push_back(weights[i]);
1520 reduced_capacities->push_back(capacities[i]);
1523 if (reduced_capacities->empty()) {
1526 for (
int item_id = 0; item_id < num_items; ++item_id) {
1527 known_value_[item_id] =
true;
1528 best_solution_[item_id] =
true;
1530 is_problem_solved_ =
true;
1538int KnapsackSolver::ReduceProblem(
int num_items) {
1539 known_value_.assign(num_items,
false);
1540 best_solution_.assign(num_items,
false);
1541 mapping_reduced_item_id_.assign(num_items, 0);
1542 additional_profit_ = 0LL;
1544 for (
int item_id = 0; item_id < num_items; ++item_id) {
1545 mapping_reduced_item_id_[item_id] = item_id;
1548 int64_t best_lower_bound = 0LL;
1549 std::vector<int64_t> J0_upper_bounds(num_items,
1550 std::numeric_limits<int64_t>::max());
1551 std::vector<int64_t> J1_upper_bounds(num_items,
1552 std::numeric_limits<int64_t>::max());
1553 for (
int item_id = 0; item_id < num_items; ++item_id) {
1554 if (time_limit_->LimitReached()) {
1558 int64_t
upper_bound = std::numeric_limits<int64_t>::max();
1559 solver_->GetLowerAndUpperBoundWhenItem(item_id,
false, &
lower_bound,
1562 best_lower_bound = std::max(best_lower_bound,
lower_bound);
1564 solver_->GetLowerAndUpperBoundWhenItem(item_id,
true, &
lower_bound,
1567 best_lower_bound = std::max(best_lower_bound,
lower_bound);
1570 int num_reduced_items = 0;
1571 for (
int item_id = 0; item_id < num_items; ++item_id) {
1572 if (best_lower_bound > J0_upper_bounds[item_id]) {
1573 known_value_[item_id] =
true;
1574 best_solution_[item_id] =
false;
1575 ++num_reduced_items;
1576 }
else if (best_lower_bound > J1_upper_bounds[item_id]) {
1577 known_value_[item_id] =
true;
1578 best_solution_[item_id] =
true;
1579 ++num_reduced_items;
1583 is_problem_solved_ = num_reduced_items == num_items;
1584 return num_reduced_items;
1587void KnapsackSolver::ComputeAdditionalProfit(
1588 const std::vector<int64_t>& profits) {
1589 const int num_items = profits.size();
1590 additional_profit_ = 0LL;
1591 for (
int item_id = 0; item_id < num_items; ++item_id) {
1592 if (known_value_[item_id] && best_solution_[item_id]) {
1593 additional_profit_ += profits[item_id];
1598void KnapsackSolver::InitReducedProblem(
1599 const std::vector<int64_t>& profits,
1600 const std::vector<std::vector<int64_t>>& weights,
1601 const std::vector<int64_t>& capacities) {
1602 const int num_items = profits.size();
1603 const int num_dimensions = capacities.size();
1605 std::vector<int64_t> reduced_profits;
1606 for (
int item_id = 0; item_id < num_items; ++item_id) {
1607 if (!known_value_[item_id]) {
1608 mapping_reduced_item_id_[item_id] = reduced_profits.size();
1609 reduced_profits.push_back(profits[item_id]);
1613 std::vector<std::vector<int64_t>> reduced_weights;
1614 std::vector<int64_t> reduced_capacities = capacities;
1615 for (
int dim = 0; dim < num_dimensions; ++dim) {
1616 const std::vector<int64_t>& one_dimension_weights = weights[dim];
1617 std::vector<int64_t> one_dimension_reduced_weights;
1618 for (
int item_id = 0; item_id < num_items; ++item_id) {
1619 if (known_value_[item_id]) {
1620 if (best_solution_[item_id]) {
1621 reduced_capacities[dim] -= one_dimension_weights[item_id];
1624 one_dimension_reduced_weights.push_back(one_dimension_weights[item_id]);
1627 reduced_weights.push_back(std::move(one_dimension_reduced_weights));
1629 solver_->Init(reduced_profits, reduced_weights, reduced_capacities);
1633 return additional_profit_ +
1634 ((is_problem_solved_)
1636 : solver_->Solve(time_limit_.get(), time_limit_seconds_,
1637 &is_solution_optimal_));
1641 const int mapped_item_id =
1642 (use_reduction_) ? mapping_reduced_item_id_[item_id] : item_id;
1643 return (use_reduction_ && known_value_[item_id])
1644 ? best_solution_[item_id]
1645 : solver_->best_solution(mapped_item_id);
1658 *
upper_bound = std::numeric_limits<int64_t>::max();
virtual void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64_t *lower_bound, int64_t *upper_bound)
--— BaseKnapsackSolver --—
virtual std::string GetName() const
Knapsack64ItemsSolver(absl::string_view solver_name)
--— Knapsack64ItemsSolver --—
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
int64_t Solve(TimeLimit *time_limit, double time_limit_in_second, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
Initializes the solver and enters the problem to be solved.
KnapsackBruteForceSolver(absl::string_view solver_name)
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
Initializes the solver and enters the problem to be solved.
KnapsackBruteForceSolver & operator=(const KnapsackBruteForceSolver &)=delete
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
KnapsackBruteForceSolver(const KnapsackBruteForceSolver &)=delete
This type is neither copyable nor movable.
int64_t Solve(TimeLimit *time_limit, double time_limit_in_second, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const override
~KnapsackCapacityPropagator() override
KnapsackCapacityPropagator(const KnapsackState &state, int64_t capacity)
--— KnapsackCapacityPropagator --—
void InitPropagator() override
bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment) override
Returns false when the propagator fails.
void ComputeProfitBounds() override
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
Initializes the solver and enters the problem to be solved.
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
KnapsackCpSat(absl::string_view solver_name)
int64_t Solve(TimeLimit *time_limit, double time_limit_in_seconds, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
int64_t Solve(TimeLimit *time_limit, double time_limit_in_second, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
Initializes the solver and enters the problem to be solved.
KnapsackDivideAndConquerSolver(absl::string_view solver_name)
--— KnapsackDivideAndConquerSolver --—
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
KnapsackDynamicProgrammingSolver(absl::string_view solver_name)
--— KnapsackDynamicProgrammingSolver --—
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
Initializes the solver and enters the problem to be solved.
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
int64_t Solve(TimeLimit *time_limit, double time_limit_in_second, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64_t *lower_bound, int64_t *upper_bound) override
--— BaseKnapsackSolver --—
~KnapsackGenericSolver() override
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
Initializes the solver and enters the problem to be solved.
KnapsackGenericSolver(const std::string &solver_name)
--— KnapsackGenericSolver --—
int64_t Solve(TimeLimit *time_limit, double time_limit_in_seconds, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
--— KnapsackMIPSolver --—
KnapsackMIPSolver(MPSolver::OptimizationProblemType problem_type, absl::string_view solver_name)
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
Initializes the solver and enters the problem to be solved.
int64_t Solve(TimeLimit *time_limit, double time_limit_in_second, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
void set_profit_upper_bound(int64_t profit)
void CopyCurrentStateToSolution(bool has_one_propagator, std::vector< bool > *solution) const
int64_t profit_lower_bound() const
bool Update(bool revert, const KnapsackAssignment &assignment)
void set_profit_lower_bound(int64_t profit)
void Init(const std::vector< int64_t > &profits, const std::vector< int64_t > &weights)
Initializes data structure and then calls InitPropagator.
const KnapsackState & state() const
KnapsackPropagator(const KnapsackState &state)
--— KnapsackPropagator --—
virtual ~KnapsackPropagator()
virtual void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const =0
virtual bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment)=0
int64_t profit_upper_bound() const
const std::vector< KnapsackItemPtr > & items() const
virtual void InitPropagator()=0
int64_t current_profit() const
KnapsackSearchNode(const KnapsackSearchNode *parent, const KnapsackAssignment &assignment)
--— KnapsackSearchNode --—
void set_current_profit(int64_t profit)
void set_next_item_id(int id)
const KnapsackSearchNode * parent() const
void set_profit_upper_bound(int64_t profit)
KnapsackSearchPath(const KnapsackSearchNode &from, const KnapsackSearchNode &to)
--— KnapsackSearchPath --—
const KnapsackSearchNode * MoveUpToDepth(const KnapsackSearchNode &node, int depth) const
bool BestSolutionContains(int item_id) const
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities)
KnapsackSolver(const std::string &solver_name)
--— KnapsackSolver --—
@ KNAPSACK_DIVIDE_AND_CONQUER_SOLVER
@ KNAPSACK_BRUTE_FORCE_SOLVER
@ KNAPSACK_MULTIDIMENSION_XPRESS_MIP_SOLVER
@ KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER
@ KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
@ KNAPSACK_64ITEMS_SOLVER
@ KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER
@ KNAPSACK_MULTIDIMENSION_CPLEX_MIP_SOLVER
@ KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER
@ KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER
virtual ~KnapsackSolver()
std::string GetName() const
bool UpdateState(bool revert, const KnapsackAssignment &assignment)
Returns false when the state is invalid.
void Init(int number_of_items)
Initializes vectors with number_of_items set to false (i.e. not bound yet).
bool is_bound(int id) const
KnapsackState()
--— KnapsackState --—
A class to express a linear objective.
void SetCoefficient(const MPVariable *var, double coeff)
void SetMinimization()
Sets the optimization direction to minimize.
@ FEASIBLE
feasible, or stopped by limit.
ResultStatus Solve()
Solves the problem using the default parameter values.
@ SCIP_MIXED_INTEGER_PROGRAMMING
Recommended default value for MIP problems.
@ XPRESS_MIXED_INTEGER_PROGRAMMING
@ CBC_MIXED_INTEGER_PROGRAMMING
@ CPLEX_MIXED_INTEGER_PROGRAMMING
void SetTimeLimit(absl::Duration time_limit)
MPObjective * MutableObjective()
Returns the mutable objective object.
void SuppressOutput()
Suppresses solver logging.
MPConstraint * MakeRowConstraint(double lb, double ub)
void MakeBoolVarArray(int nb, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of boolean variables.
void SetName(absl::string_view name)
Sets the name of the model.
MPSolver::OptimizationProblemType problem_type
void STLDeleteElements(T *container)
CpSolverResponse SolveWithParameters(const CpModelProto &model_proto, const SatParameters ¶ms)
Solves the given CpModelProto with the given parameters.
In SWIG mode, we don't want anything besides these top-level includes.
bool CompareKnapsackItemWithEfficiencyInDecreasingEfficiencyOrder(const KnapsackItemWithEfficiency &item1, const KnapsackItemWithEfficiency &item2)
Comparator used to sort item in decreasing efficiency order.
uint32_t OneBit32(int pos)
KnapsackItem * KnapsackItemPtr
uint64_t OneBit64(int pos)
Returns a word with only bit pos set.
int MostSignificantBitPosition64(uint64_t n)
trees with all degrees equal w the current value of w
trees with all degrees equal to
KnapsackItemWithEfficiency(int _id, int64_t _profit, int64_t _weight, int64_t _profit_max)