forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knapsack_solver.cc
1527 lines (1351 loc) · 54.4 KB
/
knapsack_solver.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/algorithms/knapsack_solver.h"
#include <algorithm>
#include <cstdint>
#include <limits>
#include <memory>
#include <queue>
#include <string>
#include <vector>
#include "ortools/base/stl_util.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/util/bitset.h"
#include "ortools/util/time_limit.h"
namespace operations_research {
namespace {
const int kNoSelection = -1;
const int kPrimaryPropagatorId = 0;
const int kMaxNumberOfBruteForceItems = 30;
const int kMaxNumberOf64Items = 64;
// Comparator used to sort item in decreasing efficiency order
// (see KnapsackCapacityPropagator).
struct CompareKnapsackItemsInDecreasingEfficiencyOrder {
explicit CompareKnapsackItemsInDecreasingEfficiencyOrder(int64_t _profit_max)
: profit_max(_profit_max) {}
bool operator()(const KnapsackItemPtr& item1,
const KnapsackItemPtr& item2) const {
return item1->GetEfficiency(profit_max) > item2->GetEfficiency(profit_max);
}
const int64_t profit_max;
};
// Comparator used to sort search nodes in the priority queue in order
// to pop first the node with the highest profit upper bound
// (see KnapsackSearchNode). When two nodes have the same upper bound, we
// prefer the one with the highest current profit, ie. usually the one closer
// to a leaf. In practice, the main advantage is to have smaller path.
struct CompareKnapsackSearchNodePtrInDecreasingUpperBoundOrder {
bool operator()(const KnapsackSearchNode* node_1,
const KnapsackSearchNode* node_2) const {
const int64_t profit_upper_bound_1 = node_1->profit_upper_bound();
const int64_t profit_upper_bound_2 = node_2->profit_upper_bound();
if (profit_upper_bound_1 == profit_upper_bound_2) {
return node_1->current_profit() < node_2->current_profit();
}
return profit_upper_bound_1 < profit_upper_bound_2;
}
};
typedef std::priority_queue<
KnapsackSearchNode*, std::vector<KnapsackSearchNode*>,
CompareKnapsackSearchNodePtrInDecreasingUpperBoundOrder>
SearchQueue;
// Returns true when value_1 * value_2 may overflow int64_t.
inline bool WillProductOverflow(int64_t value_1, int64_t value_2) {
const int MostSignificantBitPosition1 = MostSignificantBitPosition64(value_1);
const int MostSignificantBitPosition2 = MostSignificantBitPosition64(value_2);
// The sum should be less than 61 to be safe as we are only considering the
// most significant bit and dealing with int64_t instead of uint64_t.
const int kOverflow = 61;
return MostSignificantBitPosition1 + MostSignificantBitPosition2 > kOverflow;
}
// Returns an upper bound of (numerator_1 * numerator_2) / denominator
int64_t UpperBoundOfRatio(int64_t numerator_1, int64_t numerator_2,
int64_t denominator) {
DCHECK_GT(denominator, int64_t{0});
if (!WillProductOverflow(numerator_1, numerator_2)) {
const int64_t numerator = numerator_1 * numerator_2;
// Round to zero.
const int64_t result = numerator / denominator;
return result;
} else {
const double ratio =
(static_cast<double>(numerator_1) * static_cast<double>(numerator_2)) /
static_cast<double>(denominator);
// Round near.
const int64_t result = static_cast<int64_t>(floor(ratio + 0.5));
return result;
}
}
} // namespace
// ----- KnapsackSearchNode -----
KnapsackSearchNode::KnapsackSearchNode(const KnapsackSearchNode* const parent,
const KnapsackAssignment& assignment)
: depth_((parent == nullptr) ? 0 : parent->depth() + 1),
parent_(parent),
assignment_(assignment),
current_profit_(0),
profit_upper_bound_(std::numeric_limits<int64_t>::max()),
next_item_id_(kNoSelection) {}
// ----- KnapsackSearchPath -----
KnapsackSearchPath::KnapsackSearchPath(const KnapsackSearchNode& from,
const KnapsackSearchNode& to)
: from_(from), via_(nullptr), to_(to) {}
void KnapsackSearchPath::Init() {
const KnapsackSearchNode* node_from = MoveUpToDepth(from_, to_.depth());
const KnapsackSearchNode* node_to = MoveUpToDepth(to_, from_.depth());
CHECK_EQ(node_from->depth(), node_to->depth());
// Find common parent.
while (node_from != node_to) {
node_from = node_from->parent();
node_to = node_to->parent();
}
via_ = node_from;
}
const KnapsackSearchNode* KnapsackSearchPath::MoveUpToDepth(
const KnapsackSearchNode& node, int depth) const {
const KnapsackSearchNode* current_node = &node;
while (current_node->depth() > depth) {
current_node = current_node->parent();
}
return current_node;
}
// ----- KnapsackState -----
KnapsackState::KnapsackState() : is_bound_(), is_in_() {}
void KnapsackState::Init(int number_of_items) {
is_bound_.assign(number_of_items, false);
is_in_.assign(number_of_items, false);
}
// Returns false when the state is invalid.
bool KnapsackState::UpdateState(bool revert,
const KnapsackAssignment& assignment) {
if (revert) {
is_bound_[assignment.item_id] = false;
} else {
if (is_bound_[assignment.item_id] &&
is_in_[assignment.item_id] != assignment.is_in) {
return false;
}
is_bound_[assignment.item_id] = true;
is_in_[assignment.item_id] = assignment.is_in;
}
return true;
}
// ----- KnapsackPropagator -----
KnapsackPropagator::KnapsackPropagator(const KnapsackState& state)
: items_(),
current_profit_(0),
profit_lower_bound_(0),
profit_upper_bound_(std::numeric_limits<int64_t>::max()),
state_(state) {}
KnapsackPropagator::~KnapsackPropagator() { gtl::STLDeleteElements(&items_); }
void KnapsackPropagator::Init(const std::vector<int64_t>& profits,
const std::vector<int64_t>& weights) {
const int number_of_items = profits.size();
items_.assign(number_of_items, static_cast<KnapsackItemPtr>(nullptr));
for (int i = 0; i < number_of_items; ++i) {
items_[i] = new KnapsackItem(i, weights[i], profits[i]);
}
current_profit_ = 0;
profit_lower_bound_ = std::numeric_limits<int64_t>::min();
profit_upper_bound_ = std::numeric_limits<int64_t>::max();
InitPropagator();
}
bool KnapsackPropagator::Update(bool revert,
const KnapsackAssignment& assignment) {
if (assignment.is_in) {
if (revert) {
current_profit_ -= items_[assignment.item_id]->profit;
} else {
current_profit_ += items_[assignment.item_id]->profit;
}
}
return UpdatePropagator(revert, assignment);
}
void KnapsackPropagator::CopyCurrentStateToSolution(
bool has_one_propagator, std::vector<bool>* solution) const {
CHECK(solution != nullptr);
for (const KnapsackItem* const item : items_) {
const int item_id = item->id;
(*solution)[item_id] = state_.is_bound(item_id) && state_.is_in(item_id);
}
if (has_one_propagator) {
CopyCurrentStateToSolutionPropagator(solution);
}
}
// ----- KnapsackCapacityPropagator -----
KnapsackCapacityPropagator::KnapsackCapacityPropagator(
const KnapsackState& state, int64_t capacity)
: KnapsackPropagator(state),
capacity_(capacity),
consumed_capacity_(0),
break_item_id_(kNoSelection),
sorted_items_(),
profit_max_(0) {}
KnapsackCapacityPropagator::~KnapsackCapacityPropagator() {}
// TODO(user): Make it more incremental, by saving the break item in a
// search node for instance.
void KnapsackCapacityPropagator::ComputeProfitBounds() {
set_profit_lower_bound(current_profit());
break_item_id_ = kNoSelection;
int64_t remaining_capacity = capacity_ - consumed_capacity_;
int break_sorted_item_id = kNoSelection;
const int number_of_sorted_items = sorted_items_.size();
for (int sorted_id = 0; sorted_id < number_of_sorted_items; ++sorted_id) {
const KnapsackItem* const item = sorted_items_[sorted_id];
if (!state().is_bound(item->id)) {
break_item_id_ = item->id;
if (remaining_capacity >= item->weight) {
remaining_capacity -= item->weight;
set_profit_lower_bound(profit_lower_bound() + item->profit);
} else {
break_sorted_item_id = sorted_id;
break;
}
}
}
set_profit_upper_bound(profit_lower_bound());
if (break_sorted_item_id != kNoSelection) {
const int64_t additional_profit =
GetAdditionalProfit(remaining_capacity, break_sorted_item_id);
set_profit_upper_bound(profit_upper_bound() + additional_profit);
}
}
void KnapsackCapacityPropagator::InitPropagator() {
consumed_capacity_ = 0;
break_item_id_ = kNoSelection;
sorted_items_ = items();
profit_max_ = 0;
for (const KnapsackItem* const item : sorted_items_) {
profit_max_ = std::max(profit_max_, item->profit);
}
++profit_max_;
CompareKnapsackItemsInDecreasingEfficiencyOrder compare_object(profit_max_);
std::stable_sort(sorted_items_.begin(), sorted_items_.end(), compare_object);
}
// Returns false when the propagator fails.
bool KnapsackCapacityPropagator::UpdatePropagator(
bool revert, const KnapsackAssignment& assignment) {
if (assignment.is_in) {
if (revert) {
consumed_capacity_ -= items()[assignment.item_id]->weight;
} else {
consumed_capacity_ += items()[assignment.item_id]->weight;
if (consumed_capacity_ > capacity_) {
return false;
}
}
}
return true;
}
void KnapsackCapacityPropagator::CopyCurrentStateToSolutionPropagator(
std::vector<bool>* solution) const {
CHECK(solution != nullptr);
int64_t remaining_capacity = capacity_ - consumed_capacity_;
for (const KnapsackItem* const item : sorted_items_) {
if (!state().is_bound(item->id)) {
if (remaining_capacity >= item->weight) {
remaining_capacity -= item->weight;
(*solution)[item->id] = true;
} else {
return;
}
}
}
}
int64_t KnapsackCapacityPropagator::GetAdditionalProfit(
int64_t remaining_capacity, int break_item_id) const {
const int after_break_item_id = break_item_id + 1;
int64_t additional_profit_when_no_break_item = 0;
if (after_break_item_id < sorted_items_.size()) {
// As items are sorted by decreasing profit / weight ratio, and the current
// weight is non-zero, the next_weight is non-zero too.
const int64_t next_weight = sorted_items_[after_break_item_id]->weight;
const int64_t next_profit = sorted_items_[after_break_item_id]->profit;
additional_profit_when_no_break_item =
UpperBoundOfRatio(remaining_capacity, next_profit, next_weight);
}
const int before_break_item_id = break_item_id - 1;
int64_t additional_profit_when_break_item = 0;
if (before_break_item_id >= 0) {
const int64_t previous_weight = sorted_items_[before_break_item_id]->weight;
// Having previous_weight == 0 means the total capacity is smaller than
// the weight of the current item. In such a case the item cannot be part
// of a solution of the local one dimension problem.
if (previous_weight != 0) {
const int64_t previous_profit =
sorted_items_[before_break_item_id]->profit;
const int64_t overused_capacity =
sorted_items_[break_item_id]->weight - remaining_capacity;
const int64_t ratio = UpperBoundOfRatio(overused_capacity,
previous_profit, previous_weight);
additional_profit_when_break_item =
sorted_items_[break_item_id]->profit - ratio;
}
}
const int64_t additional_profit = std::max(
additional_profit_when_no_break_item, additional_profit_when_break_item);
CHECK_GE(additional_profit, 0);
return additional_profit;
}
// ----- KnapsackGenericSolver -----
KnapsackGenericSolver::KnapsackGenericSolver(const std::string& solver_name)
: BaseKnapsackSolver(solver_name),
propagators_(),
primary_propagator_id_(kPrimaryPropagatorId),
search_nodes_(),
state_(),
best_solution_profit_(0),
best_solution_() {}
KnapsackGenericSolver::~KnapsackGenericSolver() { Clear(); }
void KnapsackGenericSolver::Init(
const std::vector<int64_t>& profits,
const std::vector<std::vector<int64_t>>& weights,
const std::vector<int64_t>& capacities) {
CHECK_EQ(capacities.size(), weights.size());
Clear();
const int number_of_items = profits.size();
const int number_of_dimensions = weights.size();
state_.Init(number_of_items);
best_solution_.assign(number_of_items, false);
for (int i = 0; i < number_of_dimensions; ++i) {
CHECK_EQ(number_of_items, weights[i].size());
KnapsackCapacityPropagator* propagator =
new KnapsackCapacityPropagator(state_, capacities[i]);
propagator->Init(profits, weights[i]);
propagators_.push_back(propagator);
}
primary_propagator_id_ = kPrimaryPropagatorId;
}
void KnapsackGenericSolver::GetLowerAndUpperBoundWhenItem(
int item_id, bool is_item_in, int64_t* lower_bound, int64_t* upper_bound) {
CHECK(lower_bound != nullptr);
CHECK(upper_bound != nullptr);
KnapsackAssignment assignment(item_id, is_item_in);
const bool fail = !IncrementalUpdate(false, assignment);
if (fail) {
*lower_bound = 0LL;
*upper_bound = 0LL;
} else {
*lower_bound =
(HasOnePropagator())
? propagators_[primary_propagator_id_]->profit_lower_bound()
: 0LL;
*upper_bound = GetAggregatedProfitUpperBound();
}
const bool fail_revert = !IncrementalUpdate(true, assignment);
if (fail_revert) {
*lower_bound = 0LL;
*upper_bound = 0LL;
}
}
int64_t KnapsackGenericSolver::Solve(TimeLimit* time_limit,
bool* is_solution_optimal) {
DCHECK(time_limit != nullptr);
DCHECK(is_solution_optimal != nullptr);
best_solution_profit_ = 0LL;
*is_solution_optimal = true;
SearchQueue search_queue;
const KnapsackAssignment assignment(kNoSelection, true);
KnapsackSearchNode* root_node = new KnapsackSearchNode(nullptr, assignment);
root_node->set_current_profit(GetCurrentProfit());
root_node->set_profit_upper_bound(GetAggregatedProfitUpperBound());
root_node->set_next_item_id(GetNextItemId());
search_nodes_.push_back(root_node);
if (MakeNewNode(*root_node, false)) {
search_queue.push(search_nodes_.back());
}
if (MakeNewNode(*root_node, true)) {
search_queue.push(search_nodes_.back());
}
KnapsackSearchNode* current_node = root_node;
while (!search_queue.empty() &&
search_queue.top()->profit_upper_bound() > best_solution_profit_) {
if (time_limit->LimitReached()) {
*is_solution_optimal = false;
break;
}
KnapsackSearchNode* const node = search_queue.top();
search_queue.pop();
if (node != current_node) {
KnapsackSearchPath path(*current_node, *node);
path.Init();
const bool no_fail = UpdatePropagators(path);
current_node = node;
CHECK_EQ(no_fail, true);
}
if (MakeNewNode(*node, false)) {
search_queue.push(search_nodes_.back());
}
if (MakeNewNode(*node, true)) {
search_queue.push(search_nodes_.back());
}
}
return best_solution_profit_;
}
void KnapsackGenericSolver::Clear() {
gtl::STLDeleteElements(&propagators_);
gtl::STLDeleteElements(&search_nodes_);
}
// Returns false when at least one propagator fails.
bool KnapsackGenericSolver::UpdatePropagators(const KnapsackSearchPath& path) {
bool no_fail = true;
// Revert previous changes.
const KnapsackSearchNode* node = &path.from();
const KnapsackSearchNode* via = &path.via();
while (node != via) {
no_fail = IncrementalUpdate(true, node->assignment()) && no_fail;
node = node->parent();
}
// Apply current changes.
node = &path.to();
while (node != via) {
no_fail = IncrementalUpdate(false, node->assignment()) && no_fail;
node = node->parent();
}
return no_fail;
}
int64_t KnapsackGenericSolver::GetAggregatedProfitUpperBound() const {
int64_t upper_bound = std::numeric_limits<int64_t>::max();
for (KnapsackPropagator* const prop : propagators_) {
prop->ComputeProfitBounds();
const int64_t propagator_upper_bound = prop->profit_upper_bound();
upper_bound = std::min(upper_bound, propagator_upper_bound);
}
return upper_bound;
}
bool KnapsackGenericSolver::MakeNewNode(const KnapsackSearchNode& node,
bool is_in) {
if (node.next_item_id() == kNoSelection) {
return false;
}
KnapsackAssignment assignment(node.next_item_id(), is_in);
KnapsackSearchNode new_node(&node, assignment);
KnapsackSearchPath path(node, new_node);
path.Init();
const bool no_fail = UpdatePropagators(path);
if (no_fail) {
new_node.set_current_profit(GetCurrentProfit());
new_node.set_profit_upper_bound(GetAggregatedProfitUpperBound());
new_node.set_next_item_id(GetNextItemId());
UpdateBestSolution();
}
// Revert to be able to create another node from parent.
KnapsackSearchPath revert_path(new_node, node);
revert_path.Init();
UpdatePropagators(revert_path);
if (!no_fail || new_node.profit_upper_bound() < best_solution_profit_) {
return false;
}
// The node is relevant.
KnapsackSearchNode* relevant_node = new KnapsackSearchNode(&node, assignment);
relevant_node->set_current_profit(new_node.current_profit());
relevant_node->set_profit_upper_bound(new_node.profit_upper_bound());
relevant_node->set_next_item_id(new_node.next_item_id());
search_nodes_.push_back(relevant_node);
return true;
}
bool KnapsackGenericSolver::IncrementalUpdate(
bool revert, const KnapsackAssignment& assignment) {
// Do not stop on a failure: To be able to be incremental on the update,
// partial solution (state) and propagators must all be in the same state.
bool no_fail = state_.UpdateState(revert, assignment);
for (KnapsackPropagator* const prop : propagators_) {
no_fail = prop->Update(revert, assignment) && no_fail;
}
return no_fail;
}
void KnapsackGenericSolver::UpdateBestSolution() {
const int64_t profit_lower_bound =
(HasOnePropagator())
? propagators_[primary_propagator_id_]->profit_lower_bound()
: propagators_[primary_propagator_id_]->current_profit();
if (best_solution_profit_ < profit_lower_bound) {
best_solution_profit_ = profit_lower_bound;
propagators_[primary_propagator_id_]->CopyCurrentStateToSolution(
HasOnePropagator(), &best_solution_);
}
}
// ----- KnapsackBruteForceSolver -----
// KnapsackBruteForceSolver solves the 0-1 knapsack problem when the number of
// items is less or equal to 30 with brute force, ie. explores all states.
// Experiments show better results than KnapsackGenericSolver when the
// number of items is less than 15.
class KnapsackBruteForceSolver : public BaseKnapsackSolver {
public:
explicit KnapsackBruteForceSolver(const std::string& solver_name);
// Initializes the solver and enters the problem to be solved.
void Init(const std::vector<int64_t>& profits,
const std::vector<std::vector<int64_t>>& weights,
const std::vector<int64_t>& capacities) override;
// Solves the problem and returns the profit of the optimal solution.
int64_t Solve(TimeLimit* time_limit, bool* is_solution_optimal) override;
// Returns true if the item 'item_id' is packed in the optimal knapsack.
bool best_solution(int item_id) const override {
return (best_solution_ & OneBit32(item_id)) != 0U;
}
private:
int num_items_;
int64_t profits_weights_[kMaxNumberOfBruteForceItems * 2];
int64_t capacity_;
int64_t best_solution_profit_;
uint32_t best_solution_;
DISALLOW_COPY_AND_ASSIGN(KnapsackBruteForceSolver);
};
KnapsackBruteForceSolver::KnapsackBruteForceSolver(
const std::string& solver_name)
: BaseKnapsackSolver(solver_name),
num_items_(0),
capacity_(0LL),
best_solution_profit_(0LL),
best_solution_(0U) {}
void KnapsackBruteForceSolver::Init(
const std::vector<int64_t>& profits,
const std::vector<std::vector<int64_t>>& weights,
const std::vector<int64_t>& capacities) {
// TODO(user): Implement multi-dimensional brute force solver.
CHECK_EQ(weights.size(), 1)
<< "Brute force solver only works with one dimension.";
CHECK_EQ(capacities.size(), weights.size());
num_items_ = profits.size();
CHECK_EQ(num_items_, weights.at(0).size());
CHECK_LE(num_items_, kMaxNumberOfBruteForceItems)
<< "To use KnapsackBruteForceSolver the number of items should be "
<< "less than " << kMaxNumberOfBruteForceItems
<< ". Current value: " << num_items_ << ".";
for (int i = 0; i < num_items_; ++i) {
profits_weights_[i * 2] = profits.at(i);
profits_weights_[i * 2 + 1] = weights.at(0).at(i);
}
capacity_ = capacities.at(0);
}
int64_t KnapsackBruteForceSolver::Solve(TimeLimit* /*time_limit*/,
bool* is_solution_optimal) {
DCHECK(is_solution_optimal != nullptr);
*is_solution_optimal = true;
best_solution_profit_ = 0LL;
best_solution_ = 0U;
const uint32_t num_states = OneBit32(num_items_);
uint32_t prev_state = 0U;
uint64_t sum_profit = 0ULL;
uint64_t sum_weight = 0ULL;
uint32_t diff_state = 0U;
uint32_t local_state = 0U;
int item_id = 0;
// This loop starts at 1, because state = 0 was already considered previously,
// ie. when no items are in, sum_profit = 0.
for (uint32_t state = 1U; state < num_states; ++state, ++prev_state) {
diff_state = state ^ prev_state;
local_state = state;
item_id = 0;
while (diff_state) {
if (diff_state & 1U) { // There is a diff.
if (local_state & 1U) { // This item is now in the knapsack.
sum_profit += profits_weights_[item_id];
sum_weight += profits_weights_[item_id + 1];
CHECK_LT(item_id + 1, 2 * num_items_);
} else { // This item has been removed of the knapsack.
sum_profit -= profits_weights_[item_id];
sum_weight -= profits_weights_[item_id + 1];
CHECK_LT(item_id + 1, 2 * num_items_);
}
}
item_id += 2;
local_state = local_state >> 1;
diff_state = diff_state >> 1;
}
if (sum_weight <= capacity_ && best_solution_profit_ < sum_profit) {
best_solution_profit_ = sum_profit;
best_solution_ = state;
}
}
return best_solution_profit_;
}
// ----- KnapsackItemWithEfficiency -----
// KnapsackItem is a small struct to pair an item weight with its
// corresponding profit.
// This struct is used by Knapsack64ItemsSolver. As this solver deals only
// with one dimension, that's more efficient to store 'efficiency' than
// computing it on the fly.
struct KnapsackItemWithEfficiency {
KnapsackItemWithEfficiency(int _id, int64_t _profit, int64_t _weight,
int64_t _profit_max)
: id(_id),
profit(_profit),
weight(_weight),
efficiency((weight > 0) ? static_cast<double>(_profit) /
static_cast<double>(_weight)
: static_cast<double>(_profit_max)) {}
int id;
int64_t profit;
int64_t weight;
double efficiency;
};
// ----- Knapsack64ItemsSolver -----
// Knapsack64ItemsSolver solves the 0-1 knapsack problem when the number of
// items is less or equal to 64. This implementation is about 4 times faster
// than KnapsackGenericSolver.
class Knapsack64ItemsSolver : public BaseKnapsackSolver {
public:
explicit Knapsack64ItemsSolver(const std::string& solver_name);
// Initializes the solver and enters the problem to be solved.
void Init(const std::vector<int64_t>& profits,
const std::vector<std::vector<int64_t>>& weights,
const std::vector<int64_t>& capacities) override;
// Solves the problem and returns the profit of the optimal solution.
int64_t Solve(TimeLimit* time_limit, bool* is_solution_optimal) override;
// Returns true if the item 'item_id' is packed in the optimal knapsack.
bool best_solution(int item_id) const override {
return (best_solution_ & OneBit64(item_id)) != 0ULL;
}
private:
int GetBreakItemId(int64_t capacity) const;
void GetLowerAndUpperBound(int64_t* lower_bound, int64_t* upper_bound) const;
void GoToNextState(bool has_failed);
void BuildBestSolution();
std::vector<KnapsackItemWithEfficiency> sorted_items_;
std::vector<int64_t> sum_profits_;
std::vector<int64_t> sum_weights_;
int64_t capacity_;
uint64_t state_;
int state_depth_;
int64_t best_solution_profit_;
uint64_t best_solution_;
int best_solution_depth_;
// Sum of weights of included item in state.
int64_t state_weight_;
// Sum of profits of non included items in state.
int64_t rejected_items_profit_;
// Sum of weights of non included items in state.
int64_t rejected_items_weight_;
};
// Comparator used to sort item in decreasing efficiency order
bool CompareKnapsackItemWithEfficiencyInDecreasingEfficiencyOrder(
const KnapsackItemWithEfficiency& item1,
const KnapsackItemWithEfficiency& item2) {
return item1.efficiency > item2.efficiency;
}
// ----- Knapsack64ItemsSolver -----
Knapsack64ItemsSolver::Knapsack64ItemsSolver(const std::string& solver_name)
: BaseKnapsackSolver(solver_name),
sorted_items_(),
sum_profits_(),
sum_weights_(),
capacity_(0LL),
state_(0ULL),
state_depth_(0),
best_solution_profit_(0LL),
best_solution_(0ULL),
best_solution_depth_(0),
state_weight_(0LL),
rejected_items_profit_(0LL),
rejected_items_weight_(0LL) {}
void Knapsack64ItemsSolver::Init(
const std::vector<int64_t>& profits,
const std::vector<std::vector<int64_t>>& weights,
const std::vector<int64_t>& capacities) {
CHECK_EQ(weights.size(), 1)
<< "Brute force solver only works with one dimension.";
CHECK_EQ(capacities.size(), weights.size());
sorted_items_.clear();
sum_profits_.clear();
sum_weights_.clear();
capacity_ = capacities[0];
const int num_items = profits.size();
CHECK_LE(num_items, kMaxNumberOf64Items)
<< "To use Knapsack64ItemsSolver the number of items should be "
<< "less than " << kMaxNumberOf64Items << ". Current value: " << num_items
<< ".";
int64_t profit_max = *std::max_element(profits.begin(), profits.end());
for (int i = 0; i < num_items; ++i) {
sorted_items_.push_back(
KnapsackItemWithEfficiency(i, profits[i], weights[0][i], profit_max));
}
std::sort(sorted_items_.begin(), sorted_items_.end(),
CompareKnapsackItemWithEfficiencyInDecreasingEfficiencyOrder);
int64_t sum_profit = 0;
int64_t sum_weight = 0;
sum_profits_.push_back(sum_profit);
sum_weights_.push_back(sum_weight);
for (int i = 0; i < num_items; ++i) {
sum_profit += sorted_items_[i].profit;
sum_weight += sorted_items_[i].weight;
sum_profits_.push_back(sum_profit);
sum_weights_.push_back(sum_weight);
}
}
int64_t Knapsack64ItemsSolver::Solve(TimeLimit* /*time_limit*/,
bool* is_solution_optimal) {
DCHECK(is_solution_optimal != nullptr);
*is_solution_optimal = true;
const int num_items = sorted_items_.size();
state_ = 1ULL;
state_depth_ = 0;
state_weight_ = sorted_items_[0].weight;
rejected_items_profit_ = 0LL;
rejected_items_weight_ = 0LL;
best_solution_profit_ = 0LL;
best_solution_ = 0ULL;
best_solution_depth_ = 0;
int64_t lower_bound = 0LL;
int64_t upper_bound = 0LL;
bool fail = false;
while (state_depth_ >= 0) {
fail = false;
if (state_weight_ > capacity_ || state_depth_ >= num_items) {
fail = true;
} else {
GetLowerAndUpperBound(&lower_bound, &upper_bound);
if (best_solution_profit_ < lower_bound) {
best_solution_profit_ = lower_bound;
best_solution_ = state_;
best_solution_depth_ = state_depth_;
}
}
fail = fail || best_solution_profit_ >= upper_bound;
GoToNextState(fail);
}
BuildBestSolution();
return best_solution_profit_;
}
int Knapsack64ItemsSolver::GetBreakItemId(int64_t capacity) const {
std::vector<int64_t>::const_iterator binary_search_iterator =
std::upper_bound(sum_weights_.begin(), sum_weights_.end(), capacity);
return static_cast<int>(binary_search_iterator - sum_weights_.begin()) - 1;
}
// This method is called for each possible state.
// Lower and upper bounds can be equal from one state to another.
// For instance state 1010???? and state 101011?? have exactly the same
// bounds. So it sounds like a good idea to cache those bounds.
// Unfortunately, experiments show equivalent results with or without this
// code optimization (only 1/7 of calls can be reused).
// In order to simplify the code, this optimization is not implemented.
void Knapsack64ItemsSolver::GetLowerAndUpperBound(int64_t* lower_bound,
int64_t* upper_bound) const {
const int64_t available_capacity = capacity_ + rejected_items_weight_;
const int break_item_id = GetBreakItemId(available_capacity);
const int num_items = sorted_items_.size();
if (break_item_id >= num_items) {
*lower_bound = sum_profits_[num_items] - rejected_items_profit_;
*upper_bound = *lower_bound;
return;
}
*lower_bound = sum_profits_[break_item_id] - rejected_items_profit_;
*upper_bound = *lower_bound;
const int64_t consumed_capacity = sum_weights_[break_item_id];
const int64_t remaining_capacity = available_capacity - consumed_capacity;
const double efficiency = sorted_items_[break_item_id].efficiency;
const int64_t additional_profit =
static_cast<int64_t>(remaining_capacity * efficiency);
*upper_bound += additional_profit;
}
// As state_depth_ is the position of the most significant bit on state_
// it is possible to remove the loop and so be in O(1) instead of O(depth).
// In such a case rejected_items_profit_ is computed using sum_profits_ array.
// Unfortunately experiments show smaller computation time using the 'while'
// (10% speed-up). That's the reason why the loop version is implemented.
void Knapsack64ItemsSolver::GoToNextState(bool has_failed) {
uint64_t mask = OneBit64(state_depth_);
if (!has_failed) { // Go to next item.
++state_depth_;
state_ = state_ | (mask << 1);
state_weight_ += sorted_items_[state_depth_].weight;
} else {
// Backtrack to last item in.
while ((state_ & mask) == 0ULL && state_depth_ >= 0) {
const KnapsackItemWithEfficiency& item = sorted_items_[state_depth_];
rejected_items_profit_ -= item.profit;
rejected_items_weight_ -= item.weight;
--state_depth_;
mask = mask >> 1ULL;
}
if (state_ & mask) { // Item was in, remove it.
state_ = state_ & ~mask;
const KnapsackItemWithEfficiency& item = sorted_items_[state_depth_];
rejected_items_profit_ += item.profit;
rejected_items_weight_ += item.weight;
state_weight_ -= item.weight;
}
}
}
void Knapsack64ItemsSolver::BuildBestSolution() {
int64_t remaining_capacity = capacity_;
int64_t check_profit = 0LL;
// Compute remaining capacity at best_solution_depth_ to be able to redo
// the GetLowerAndUpperBound computation.
for (int i = 0; i <= best_solution_depth_; ++i) {
if (best_solution_ & OneBit64(i)) {
remaining_capacity -= sorted_items_[i].weight;
check_profit += sorted_items_[i].profit;
}
}
// Add all items till the break item.
const int num_items = sorted_items_.size();
for (int i = best_solution_depth_ + 1; i < num_items; ++i) {
int64_t weight = sorted_items_[i].weight;
if (remaining_capacity >= weight) {
remaining_capacity -= weight;
check_profit += sorted_items_[i].profit;
best_solution_ = best_solution_ | OneBit64(i);
} else {
best_solution_ = best_solution_ & ~OneBit64(i);
}
}
CHECK_EQ(best_solution_profit_, check_profit);
// Items were sorted by efficiency, solution should be unsorted to be
// in user order.
// Note that best_solution_ will not be in the same order than other data
// structures anymore.
uint64_t tmp_solution = 0ULL;
for (int i = 0; i < num_items; ++i) {
if (best_solution_ & OneBit64(i)) {
const int original_id = sorted_items_[i].id;
tmp_solution = tmp_solution | OneBit64(original_id);
}
}
best_solution_ = tmp_solution;
}
// ----- KnapsackDynamicProgrammingSolver -----
// KnapsackDynamicProgrammingSolver solves the 0-1 knapsack problem
// using dynamic programming. This algorithm is pseudo-polynomial because it
// depends on capacity, ie. the time and space complexity is
// O(capacity * number_of_items).
// The implemented algorithm is 'DP-3' in "Knapsack problems", Hans Kellerer,
// Ulrich Pferschy and David Pisinger, Springer book (ISBN 978-3540402862).
class KnapsackDynamicProgrammingSolver : public BaseKnapsackSolver {
public:
explicit KnapsackDynamicProgrammingSolver(const std::string& solver_name);
// Initializes the solver and enters the problem to be solved.
void Init(const std::vector<int64_t>& profits,
const std::vector<std::vector<int64_t>>& weights,
const std::vector<int64_t>& capacities) override;
// Solves the problem and returns the profit of the optimal solution.
int64_t Solve(TimeLimit* time_limit, bool* is_solution_optimal) override;
// Returns true if the item 'item_id' is packed in the optimal knapsack.
bool best_solution(int item_id) const override {
return best_solution_.at(item_id);
}
private:
int64_t SolveSubProblem(int64_t capacity, int num_items);
std::vector<int64_t> profits_;
std::vector<int64_t> weights_;
int64_t capacity_;
std::vector<int64_t> computed_profits_;
std::vector<int> selected_item_ids_;
std::vector<bool> best_solution_;
};
// ----- KnapsackDynamicProgrammingSolver -----
KnapsackDynamicProgrammingSolver::KnapsackDynamicProgrammingSolver(
const std::string& solver_name)
: BaseKnapsackSolver(solver_name),
profits_(),
weights_(),
capacity_(0),
computed_profits_(),
selected_item_ids_(),
best_solution_() {}
void KnapsackDynamicProgrammingSolver::Init(
const std::vector<int64_t>& profits,
const std::vector<std::vector<int64_t>>& weights,
const std::vector<int64_t>& capacities) {
CHECK_EQ(weights.size(), 1)
<< "Current implementation of the dynamic programming solver only deals"
<< " with one dimension.";
CHECK_EQ(capacities.size(), weights.size());
profits_ = profits;
weights_ = weights[0];
capacity_ = capacities[0];
}
int64_t KnapsackDynamicProgrammingSolver::SolveSubProblem(int64_t capacity,
int num_items) {
const int64_t capacity_plus_1 = capacity + 1;
std::fill_n(selected_item_ids_.begin(), capacity_plus_1, 0);
std::fill_n(computed_profits_.begin(), capacity_plus_1, int64_t{0});
for (int item_id = 0; item_id < num_items; ++item_id) {
const int64_t item_weight = weights_[item_id];
const int64_t item_profit = profits_[item_id];
for (int64_t used_capacity = capacity; used_capacity >= item_weight;
--used_capacity) {
if (computed_profits_[used_capacity - item_weight] + item_profit >
computed_profits_[used_capacity]) {
computed_profits_[used_capacity] =
computed_profits_[used_capacity - item_weight] + item_profit;
selected_item_ids_[used_capacity] = item_id;