Professional Documents
Culture Documents
ENGG1400
David Rey
Research Center for Integrated Transport Innovation (rCITI)
School of Civil and Environmental Engineering
UNSW Australia
Knapsack Problem
Outline
Knapsack Problem
Logistics Problems
Logistics Problems
Knapsack Problem
Outline
Knapsack Problem
Logistics Problems
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
variable)
Bounded problem:
Each item is available in limited
quantity, we normalize the
quantity taken: xi {0, 1}
Unbounded problem:
Each item is available in
unlimited quantity, the quantity
taken is non-negative:
xi {0, 1, 2, . . .}
Knapsack Problem
Logistics Problems
Capacity constraint
The total weight of the items taken must not exceed the capacity
of the knapsack:
X
xi wi C
iN
Knapsack Problem
Logistics Problems
Bounded model
max V =
xi vi
Unbounded model
X
max V =
xi vi
iN
subject to:
X
xi wi C
iN
subject to:
X
iN
xi {0, 1},i N
xi wi C
iN
xi {0, 1, 2, . . .},i N
Knapsack Problem
Logistics Problems
v
20
30
66
40
70
v /w
2
1.5
2.2
1
1.4
(w = weight, v = value)
Knapsack Problem
Logistics Problems
v
20
30
66
40
70
v /w
2
1.5
2.2
1
1.4
(w = weight, v = value)
Knapsack Problem
Logistics Problems
Can we use the greedy algorithm for the 0-1 Problem? (1)
item
w
1 (rosemary) 10
2 (paprika)
20
3 (oregano)
30
4 (curry)
40
5 (saffron)
50
Capacity = 100
v
20
30
66
40
70
v /w
2
1.5
2.2
1
1.4
ratio
Take the highest v /w ratio
Knapsack Problem
Logistics Problems
Can we use the greedy algorithm for the 0-1 Problem? (1)
item
w
1 (rosemary) 10
2 (paprika)
20
3 (oregano)
30
4 (curry)
40
5 (saffron)
50
Capacity = 100
v
20
30
66
40
70
v /w
2
1.5
2.2
1
1.4
ratio
Take the highest v /w ratio
Knapsack Problem
Logistics Problems
Can we use the greedy algorithm for the 0-1 Problem? (1)
item
w
1 (rosemary) 10
2 (paprika)
20
3 (oregano)
30
4 (curry)
40
5 (saffron)
50
Capacity = 100
v
20
30
66
40
70
v /w
2
1.5
2.2
1
1.4
ratio
Take the highest v /w ratio
So is the greedy algorithm any good for the 0-1 knapsack problem?
Knapsack Problem
Logistics Problems
Can we use the greedy algorithm for the 0-1 Problem? (2)
Consider the following problem:
item w
1
5
2
100
Capacity =
v
50
100
100
v /w
10
1
Knapsack Problem
Logistics Problems
Can we use the greedy algorithm for the 0-1 Problem? (2)
Consider the following problem:
item w
1
5
2
100
Capacity =
v
50
100
100
v /w
10
1
Knapsack Problem
Outline
Knapsack Problem
Logistics Problems
Logistics Problems
Knapsack Problem
Logistics Problems
optimization problems
Requirement: need to be able to decompose the initial
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
if wi > c
if wi c
Knapsack Problem
Logistics Problems
w
1
2
3
4
5
m[i, c]
i =0
i =1
i =2
i =3
i =4
i =5
v
20
30
66
40
70
0
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
Knapsack Problem
Logistics Problems
w
1
2
3
4
5
m[i, c]
i =0
i =1
i =2
i =3
i =4
i =5
v
20
30
66
40
70
0
0
0
0
0
0
0
1
0
-
2
0
-
3
0
-
4
0
-
5
0
-
6
0
-
7
0
-
8
0
-
9
0
-
10
0
-
Knapsack Problem
Logistics Problems
0
0
0
0
0
0
0
w
1
2
3
4
5
v
20
30
66
40
70
1
0
20
-
2
0
20
-
3
0
20
-
4
0
20
-
5
0
20
-
6
0
20
-
7
0
20
-
8
0
20
-
9
0
20
-
10
0
20
-
Knapsack Problem
Logistics Problems
0
0
0
0
0
0
0
w
1
2
3
4
5
v
20
30
66
40
70
1
0
20
20
-
2
0
20
30
-
3
0
20
50
-
4
0
20
50
-
5
0
20
50
-
6
0
20
50
-
7
0
20
50
-
8
0
20
50
-
9
0
20
50
-
10
0
20
50
-
Knapsack Problem
Logistics Problems
0
0
0
0
0
0
0
w
1
2
3
4
5
v
20
30
66
40
70
1
0
20
20
20
-
2
0
20
30
30
-
3
0
20
50
66
-
4
0
20
50
86
-
5
0
20
50
96
-
6
0
20
50
116
-
7
0
20
50
116
-
8
0
20
50
116
-
9
0
20
50
116
-
10
0
20
50
116
-
Knapsack Problem
Logistics Problems
0
0
0
0
0
0
0
w
1
2
3
4
5
v
20
30
66
40
70
1
0
20
20
20
20
-
2
0
20
30
30
30
-
3
0
20
50
66
66
-
4
0
20
50
86
86
-
5
0
20
50
96
96
-
6
0
20
50
116
116
-
7
0
20
50
116
116
-
8
0
20
50
116
126
-
9
0
20
50
116
136
-
10
0
20
50
116
156
-
Knapsack Problem
Logistics Problems
0
0
0
0
0
0
0
w
1
2
3
4
5
v
20
30
66
40
70
1
0
20
20
20
20
20
2
0
20
30
30
30
30
3
0
20
50
66
66
66
4
0
20
50
86
86
86
5
0
20
50
96
96
96
6
0
20
50
116
116
116
7
0
20
50
116
116
116
8
0
20
50
116
126
136
9
0
20
50
116
136
156
10
0
20
50
116
156
166
Knapsack Problem
Logistics Problems
algorithm or a DP algorithm
0-1 Knapsack can also be solved with a DP algorithm
AMPL but you should be familiar with the greedy and the DP
algorithms
AMPL uses an optimization engine called CPLEX (works with
Knapsack Problem
Outline
Knapsack Problem
Logistics Problems
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Loading constraints:
Load balance
Vertical stability
Load bearing
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
S = {1, 2, 3, . . .}
each slot has a maximum capacity (in tons) that it can
support Bj , j S
Logistics Problems
Knapsack Problem
X
iN
yij wi Bj
j S
Logistics Problems
Knapsack Problem
xi vi
iN
subject to:
X
xi wi C
iN
yij = xi
i N
yij wi Bj
j S
xi {0, 1}
i N
yij {0, 1}
i N, j S
jS
X
iN
Logistics Problems
Knapsack Problem
Logistics Problems
Knapsack Problem
Logistics Problems
280
x? = [0, 1, 1, 1, 1, 0, 1, 1, 1, 1]
Capacity used: 35 tons
Allocation (y? ):
items 2 and 9 together
items 3 and 10 together
4 other items alone
6/8 slots used
Knapsack Problem
Logistics Problems
Summary
Knapsack models are very useful for project management: finance,
logistics, etc.
Fractional Knapsack used to represent problems with divisible