Programming for the 0/1 Knapsack Problem Nirmal Prajapati Sanjay - - PowerPoint PPT Presentation

β–Ά
programming for the 0 1 knapsack
SMART_READER_LITE
LIVE PREVIEW

Programming for the 0/1 Knapsack Problem Nirmal Prajapati Sanjay - - PowerPoint PPT Presentation

Revisiting Sparse Dynamic Programming for the 0/1 Knapsack Problem Nirmal Prajapati Sanjay Rajopadhye Tarequl Islam Sifat Tarequl.Sifat@colostate.edu prajapati@lanl.gov Sanjay.Rajopadhye@colostate.edu Los Alamos National Laboratory


slide-1
SLIDE 1

Revisiting Sparse Dynamic Programming for the 0/1 Knapsack Problem

Tarequl Islam Sifat Tarequl.Sifat@colostate.edu Corespeq Inc Fort Collins, Colorado, USA Nirmal Prajapati prajapati@lanl.gov Los Alamos National Laboratory Los Alamos, New Mexico, USA Sanjay Rajopadhye Sanjay.Rajopadhye@colostate.edu Department of Computer Science Colorado State University Fort Collins, Colorado, USA

slide-2
SLIDE 2

0/1 Knapsack Problem Statement

β—Ό Given a set of 𝑂 items numbered from 1 up to 𝑂, each with a weight

π‘₯𝑗 and a profit π‘žπ‘—, along with maximum capacity 𝐷, we must 𝑛𝑏𝑦𝑗𝑛𝑗𝑨𝑓 ෍

𝑗=1 𝑂+1

π‘žπ‘—π‘¦π‘— π‘‘π‘£π‘π‘˜π‘“π‘‘π‘’ 𝑒𝑝 ෍

𝑗=1 𝑂+1

π‘₯𝑗𝑦𝑗 < 𝐷 π‘π‘œπ‘’ 𝑦𝑗 ∈ {0,1}

2

slide-3
SLIDE 3

Sparse DP Algorithm for solving 0/1- Knapsack Problem

β—Ό A β€œsparse” KPDP algorithm (SKPDP) has been known for a while. β—Ό Conventional KPDP algorithm generates a DP table that contains

many repeated values.

β—Ό SKPDP does not calculate the repeated values in the DP table. β—Ό So far there has been no quantitative analysis of its benefits.

3

slide-4
SLIDE 4

Contributions

β—Ό Quantitative analysis of Sequential SKPDP β—Ό Exploration of two parallelization techniques for SKPDP and their

performance analysis

β—Ό Comparison of SKPDP with Branch-and-Bound

4

slide-5
SLIDE 5

Problem Instance Generation For Quantitative Analysis

β—Ό Uncorrelated : π‘žπ‘— = π‘ π‘π‘œπ‘’π‘π‘›(π‘ π‘π‘œπ‘•π‘“) β—Ό Weakly Correlated : π‘žπ‘— = 𝛽π‘₯𝑗 + π‘ π‘π‘œπ‘’π‘π‘›(π‘ π‘π‘œπ‘•π‘“) β—Ό Strongly Correlated: π‘žπ‘— = 𝛽π‘₯𝑗 + 𝛾 [Hardest Problem Instances] β—Ό Subset Sum: π‘žπ‘— = π‘₯𝑗

where 𝛽, 𝛾 are constants, π‘žπ‘— is profit and π‘₯𝑗 is weight of each item.

5

slide-6
SLIDE 6

Punch Line

β—Ό For KP instances with significantly large capacity than the number of

items (C >> N)

β—Ό If the problem instance is weakly correlated, the operation count of

SKPDP algorithm is invariant with respect to capacity (𝐷).

β—Ό If the problem instance is strongly correlated, the operation count of

SKPDP algorithm is exponentially less than KPDP.

6

slide-7
SLIDE 7

Punch Line

7

Weakly Correlated Instances Strongly Correlated Instances

slide-8
SLIDE 8

Dynamic Programming Solution

8

for ( k=1 ; k < N ; k++ ) { for ( c=0 ; c <= C ; c++ ) { if( c < weights[k] ) M[k,c] = M[k-1,c]; else M[k,c]=MAX(M[k-1,c], M[k-1,c-weights[k]]+profits[k]); } }

𝑁 𝑙, 𝑑 = α‰Š 𝑁 𝑙 βˆ’ 1, 𝑑 𝑗𝑔 π‘₯𝑙 > 𝑑 max 𝑁 𝑙 βˆ’ 1, 𝑑 , 𝑁 𝑙 βˆ’ 1, 𝑑 βˆ’ π‘₯𝑙 + π‘žπ‘™ π‘“π‘šπ‘‘π‘“

slide-9
SLIDE 9

Example Problem Instance

Item No. Profits Weights 1 1 1 2 6 2 3 18 5 4 22 6 5 28 7

9

𝑂 = 5 𝐷 = 11

slide-10
SLIDE 10

Dynamic Programming Table

10

𝑁 𝑙, 𝑑 = α‰Š 𝑁 𝑙 βˆ’ 1, 𝑑 𝑗𝑔 π‘₯𝑙 > 𝑑 max 𝑁 𝑙 βˆ’ 1, 𝑑 , 𝑁 𝑙 βˆ’ 1, 𝑑 βˆ’ π‘₯𝑙 + π‘žπ‘™ π‘“π‘šπ‘‘π‘“

Capacity

Item# (weight,profit)

1 2 3 4 5 6 7 8 9 10 11 1 (1,1) 1 1 1 1 1 1 1 1 1 1 1 2 (2,6) 1 6 7 7 7 7 7 7 7 7 7 3 (5,18) 1 6 7 7 18 19 24 25 25 25 25 4 (6,22) 1 6 7 7 18 22 24 28 29 29 40 5 (7,28) 1 6 7 7 18 22 28 29 34 35 40

c k

slide-11
SLIDE 11

Memory Efficient KPDP

β—Ό Only need the current row of the DP table to calculate the next

row

β—Ό The whole table does not have to be stored β—Ό This way we can find the optimal profit value β—Ό We can find the exact solution including which items are taken in

the optimal solution by using a divide-and-conquer strategy

β—Ό Divide-and-Conquer strategy doubles the number of

computations, 2𝑂𝐷

β—Ό Reduces memory requirement by a factor of 𝑂/2

11

slide-12
SLIDE 12

β€œSparsity” in the current context

12

slide-13
SLIDE 13

β€œSparsity” in the current context

Capacity

Item# (weight,profit)

1 2 3 4 5 6 7 8 9 10 11 1 (1,1) 1 1 1 1 1 1 1 1 1 1 1 2 (2,6) 1 6 7 7 7 7 7 7 7 7 7 3 (5,18) 1 6 7 7 18 19 24 25 25 25 25 4 (6,22) 1 6 7 7 18 22 24 28 29 29 40 5 (7,28) 1 6 7 7 18 22 28 29 34 35 40

13

Item# <0,0> 1 (1,1) <0,0>,<1,1> 2 (2,6) <0,0>,<1,1>,<2,6>,<3,7> 3 (5,18) <0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25> 4 (6,22) <0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,22>,<7,24>,<8,28>,<9,29>,<11,40> 5 (7,28) <0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,22>,<7,28>,<8,29>,<9,34>,<10,35>,<11,40>

slide-14
SLIDE 14

Building the Sparse Table Add-Merge-Kill

14

{1,2,3} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,19>, <7,24>, <8,25> {1,2,3,4} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,22>, <7,24>, <8,28>, <9,29>, <11,40>

When we include the 4th item (Weight: 6, Profit: 22) in our choice

slide-15
SLIDE 15

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

Building the Sparse Table Add-Merge-Kill

14

{1,2,3} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,19>, <7,24>, <8,25> {1,2,3,4} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,22>, <7,24>, <8,28>, <9,29>, <11,40>

When we include the 4th item (Weight: 6, Profit: 22) in our choice

slide-16
SLIDE 16

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

Building the Sparse Table Add-Merge-Kill

14

{1,2,3} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,19>, <7,24>, <8,25> {1,2,3,4} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,22>, <7,24>, <8,28>, <9,29>, <11,40>

When we include the 4th item (Weight: 6, Profit: 22) in our choice Add (6,22) to each pair,

<6,22>,<7,23>,<8,28>,<9,29>,<11,40>,<12,41>,<13,46>,<14,47>

slide-17
SLIDE 17

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

Building the Sparse Table Add-Merge-Kill

14

{1,2,3} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,19>, <7,24>, <8,25> {1,2,3,4} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,22>, <7,24>, <8,28>, <9,29>, <11,40>

When we include the 4th item (Weight: 6, Profit: 22) in our choice Add (6,22) to each pair,

<6,22>,<7,23>,<8,28>,<9,29>,<11,40>

slide-18
SLIDE 18

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

Building the Sparse Table Add-Merge-Kill

14

{1,2,3} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,19>, <7,24>, <8,25> {1,2,3,4} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,22>, <7,24>, <8,28>, <9,29>, <11,40>

When we include the 4th item (Weight: 6, Profit: 22) in our choice

<6,22>,<7,23>,<8,28>,<9,29>,<11,40>

slide-19
SLIDE 19

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

Building the Sparse Table Add-Merge-Kill

14

{1,2,3} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,19>, <7,24>, <8,25> {1,2,3,4} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,22>, <7,24>, <8,28>, <9,29>, <11,40>

When we include the 4th item (Weight: 6, Profit: 22) in our choice

<6,22>,<7,23>,<8,28>,<9,29>,<11,40>

slide-20
SLIDE 20

Building the Sparse Table Add-Merge-Kill

15

slide-21
SLIDE 21

Generation of Problem Instances

β—Ό The fraction of objects that can fit in the knapsack on average, 1/πœ‡

𝑋

𝑏𝑀𝑕 = πœ‡π· 𝑂

β—Ό The set of weights, π‘₯𝑗 is generated with a normal distribution that

has a mean of 𝑋

𝑏𝑀𝑕

β—Ό For weakly correlated problem instances, the correlation between

the weights and profits is controlled by a noise factor, 𝜏 π‘žπ‘— = 𝛽π‘₯𝑗 + π‘†π‘π‘œπ‘’π‘π‘› π½π‘œπ‘’π‘“π‘•π‘“π‘  𝑐𝑓𝑒π‘₯π‘“π‘“π‘œ [βˆ’πœπ‘‹

𝑏𝑀𝑕, πœπ‘‹ 𝑏𝑀𝑕]

β—Ό For strongly correlated problem instances 𝜏 is irrelevant,

π‘žπ‘— = 𝛽π‘₯𝑗 + 𝛾

16

slide-22
SLIDE 22

Gain

π»π‘π‘—π‘œ = 1 βˆ’ π½π‘’π‘“π‘ π‘π‘’π‘—π‘π‘œπ‘‘ π‘—π‘œ 𝑇𝐿𝑄𝐸𝑄 π½π‘’π‘“π‘ π‘π‘’π‘—π‘π‘œπ‘‘ π‘—π‘œ 𝐿𝑄𝐸𝑄 (= 2𝑂𝐷) The range of Gain is (-1, 1)

β—Ό A value of gain close to 1 means that the number of iterations

in SKPDP is insignificant compared to KPDP

β—Ό A value of gain close to -1 mean that we have the worst-case

scenario for SKPDP.

17

slide-23
SLIDE 23

Gain

18

πœ‡ = 2, 𝜏 = 0.1%

slide-24
SLIDE 24

SKPDP vs KPDP

19

Weakly Correlated Instances Strongly Correlated Instances πœ‡ = 2, 𝜏 = 0.1% 𝑂 = 256, πœ‡ = 8, 𝜏 = 0.1%

slide-25
SLIDE 25

Impact of 𝜏 on the sparsity

20

π‘ž = 𝛽π‘₯ + π‘†π‘π‘œπ‘’π‘π‘› π½π‘œπ‘’. π½π‘œ βˆ’πœπ‘‹

𝑏𝑀𝑕, πœπ‘‹ 𝑏𝑀𝑕 ; πœ‡ = 2

slide-26
SLIDE 26

Impact of πœ‡ on the sparsity

21

𝑋

𝑏𝑀𝑕 = πœ‡π·

𝑂 ; 𝜏 = 0.1%

slide-27
SLIDE 27

Impact of 𝜏 and πœ‡ on the sparsity

22

𝑂 = 210, 212 < C < 250

slide-28
SLIDE 28

Parallelization of SKPDP

β—Ό Fine-Grained Parallelization β—Ό Coarse-Grained Parallelization

23

slide-29
SLIDE 29

Fine-Grained Parallelization

24

slide-30
SLIDE 30

Fine-Grained Parallelization

Item# <0,0> 1 (1,1) <0,0>,<1,1> 2 (2,6) <0,0>,<1,1>,<2,6>,<3,7> 3 (5,18) <0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25> 4 (6,22) <0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,22>,<7,24>,<8,28>,<9,29>,<11,40> 5 (7,28) <0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,22>,<7,28>,<8,29>,<9,34>,<10,35>,<11,40>

25

{1,2,3} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,19>, <7,24>, <8,25> {1,2,3,4} <0,0>, <1,1>, <2,6>, <3,7>, <5,18>, <6,22>, <7,24>, <8,28>, <9,29>, <11,40>

When we include the 4th item (Weight: 6, Profit: 22) in our choice

Remember the example used to demonstrate Add-Merge-Kill?

slide-31
SLIDE 31

Fine-Grained Parallelization

26

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4th row:

slide-32
SLIDE 32

Fine-Grained Parallelization

26

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4th row:

<5,18>,<6,19>,<7,24>,<8,25> <0,0>,<1,1>,<2,6>,<3,7>

slide-33
SLIDE 33

Fine-Grained Parallelization

26

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4th row:

<5,18>,<6,19>,<7,24>,<8,25> <0,0>,<1,1>,<2,6>,<3,7> <0,0>,<1,1>,<2,6>,<3,7>,<6,22>,<7,23>,<8,28>,<9,29> <5,18>,<6,19>,<7,24>,<8,25>,<11,40>

slide-34
SLIDE 34

Fine-Grained Parallelization

26

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4th row:

<5,18>,<6,19>,<7,24>,<8,25> <0,0>,<1,1>,<2,6>,<3,7> <0,0>,<1,1>,<2,6>,<3,7>,<6,22>,<7,23>,<8,28>,<9,29> <5,18>,<6,19>,<7,24>,<8,25>,<11,40>

At most π‘₯𝑗 pairs from the suffix of the preceding part

slide-35
SLIDE 35

Fine-Grained Parallelization

26

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4th row:

<5,18>,<6,19>,<7,24>,<8,25> <0,0>,<1,1>,<2,6>,<3,7> <0,0>,<1,1>,<2,6>,<3,7>,<6,22>,<7,23>,<8,28>,<9,29> <5,18>,<6,19>,<7,24>,<8,25>,<11,40>

At most π‘₯𝑗 pairs from the suffix of the preceding part At most π‘₯𝑗 pairs from the prefix of the following part

slide-36
SLIDE 36

Fine-Grained Parallelization

26

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4th row:

<5,18>,<6,19>,<7,24>,<8,25> <0,0>,<1,1>,<2,6>,<3,7> <0,0>,<1,1>,<2,6>,<3,7>,<6,22>,<7,23>,<8,28>,<9,29> <5,18>,<6,19>,<7,24>,<8,25>,<11,40>

At most π‘₯𝑗 pairs from the suffix of the preceding part At most π‘₯𝑗 pairs from the prefix of the following part In the worst-case extra merge-kill we must do at each point of stitching is 2π‘₯𝑗

slide-37
SLIDE 37

Fine-Grained Parallelization

26

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4th row:

<5,18>,<6,19>,<7,24>,<8,25> <0,0>,<1,1>,<2,6>,<3,7> <0,0>,<1,1>,<2,6>,<3,7>,<6,22>,<7,23>,<8,28>,<9,29> <5,18>,<6,19>,<7,24>,<8,25>,<11,40> <0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,22>,<7,24>,<8,28>,<9,29>,<11,40>

slide-38
SLIDE 38

Coarse-Grained Parallelization

27

slide-39
SLIDE 39

Coarse-Grained Parallelization

β—Ό The computation occurs in batches of P rows. β—Ό Every π‘„π‘’β„Ž row of the DP table is stored (completely) in the

memory.

β—Ό Other 𝑄 βˆ’ 1 rows only stores a local buffer of size π‘₯𝑗

28

<0,0>,<1,1>,<2,6>,<3,7>,<5,18>,<6,19>,<7,24>,<8,25>

4rd row: k j At most π‘₯𝑗

slide-40
SLIDE 40

Coarse-Grained Parallelization

β—Ό The local buffers are basically a FIFO stack and implemented

with a rotating array.

β—Ό These rotating arrays have a length of 𝑋

𝑛𝑏𝑦 = max(π‘₯𝑗)

β—Ό Extra memory required by the implementation is 𝑄 βˆ— 𝑋

𝑛𝑏𝑦

29

slide-41
SLIDE 41

Performance of Coarse-Grained Parallelization

30

πœ‡ = 8, 𝜏 = 0.1%

slide-42
SLIDE 42

SKPDP vs BnB

31

slide-43
SLIDE 43

Future work

β—Ό A model to predict the performance gain by SKPDP β—Ό Ordering the items by weight values to maximize sparsity β—Ό Choosing the best input parameters for the coarse-grained

parallelization of SKPDP

32

slide-44
SLIDE 44

Conclusion

β—Ό SKPDP shows exponential performance improvement using

SKPDP over KPDP.

β—Ό Coarse-grained parallelization shows promise. β—Ό For most problem instances BnB with proper heuristics does

better than SKPDP.

β—Ό For some hard problem instances SKPDP performs better

than BnB.

33

slide-45
SLIDE 45

34

Thank you!