Upload
others
View
5
Download
1
Embed Size (px)
Citation preview
Optimisation Discrete
Solving the Knapsack Problem
Sonia Cafieri
ENAC
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 1 / 50
Outline
1 Introduction
2 Heuristics for the Knapsack Problem
3 Solving the Continuous Knapsack Problem
4 Exact solution of the KPBranch and Bound algorithms for IPBranch and Bound for the KPBranch and Bound for IP : Remarks
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 2 / 50
Knapsack problem (KP)
max
n∑i=1
xiui
s.t.n∑
i=1
xivi ≤ V
xi ∈ {0, 1} ∀i
V = volume of the knapsack
vi = volume of objecti
ui = value of objecti
xi = 1 if object i is selected, 0 otherwise
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 3 / 50
Solving the KP
There is no polynomial algorithm known that solves the problem to optimality.There is also no proof that such an algorithm does not exist.
How to solve the Knapsack problem?
Apply an algorithm with exponential running time to find anoptimal solution
Enumerate all possible solutions in a smart way to find anoptimal solution:branch-and-bound or dynamical programming
Apply a polynomial algorithm that constructs agood but possibly non-optimalsolution: heuristic approach.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 4 / 50
Heuristics for KP : Greedy algorithm
Greedy : algorithm that makes the choice that looks the best at the moment.
Idea :
Sort by most rewarding items with respect to a given criterionand take as many of the top most valuable items as will fit.
Heuristic 1 : sort ui i ∈ {1, . . . , n} by non-increasing order
Heuristic 2 : sort vi i ∈ {1, . . . , n} by increasing order
Heuristic 3 : sort ui/vi i ∈ {1, . . . , n} by non-increasing order
⇒ the most efficient greedy
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 5 / 50
Greedy algorithm for KP
Let i, j be indexes such thatj = 1, . . . , n correspond to items sorted with respect to a givencriterion
W← 0
for j = 1, . . . , n do
W← vi j + W
if (W≤ V) then
xi j ← 1
else
xi j ← 0
W←W− vi j
endif
endfor
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 6 / 50
0-1 Knapsack and Continuous Knapsack
0-1 Knapsack problem :
you can only choose to take all or nothing of a particular item(0-1)
Continuous (Fractional) Knapsack :
you can take any fractions of the items to put in the knapsack
⇒ continuous relaxationof 0-1 Knapsack
For both versions :
Optimal Substructure property:
an optimal solution contains within it optimal solutions tosubproblems.
If we remove itemi from an optimal solution, then the remaining items must be optimal forthe subproblem where the bag volume isV − vi and the available items do not include itemi.Otherwise, we would be able to construct an even better solution than the optimal one.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 7 / 50
Solving the CKP: Greedy (n.3)
Idea :
Sort by most rewarding items with respect to the ratio of value to size, and take as muchas possible of the most valuable items as will fit.
Steps :
1 compute ratios (densities)ui/vi
2 sort them by non-increasing order
3 take as much as possible of the densest item not already in thebag
Complexity :O(n logn) for sorting, O(n) for greedy choices
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 8 / 50
Solving the CKP: Greedy (n.3)Sort values/volumes by non-increasing order.Let i, j be indexes such thatui j/vi j with j = 1 is the maximum ratio:
ui1
vi1≥ ui2
vi2≥ · · · ≥ uin
vin
W← 0
for i ≤ n do
if (W + vi j ≤ V) then
take itemi :xi j ← 1W←W + vi j
else
take a fraction of itemi :
xih ←V −W
vihxi j ← 0 ∀j > h
endif
endforSonia Cafieri (ENAC) Knapsack Problem 2011-2012 9 / 50
Solving the Continuous Knapsack Problem
TheoremThe Greedy algorithm (n.3) is optimal for the Continuous Knapsack Problem (CKP)(continuous relaxation of 0-1 Knapsack)
Proof. 1
Note that the algorithm fills the knapsack completely.It may take a fraction of an item, which can only be the last selected item.
The greedy solution is such that there exists ah such that
1 = xi1 = xi2 = · · · = xih > xih+1 > xih+2 = · · · = xin = 0.
We compare this solution with another (feasible) solutionyi1, . . . , yin.
Let k be the smallest index such thatyik < 1, andlet ℓ be the smallest index such thatk < ℓ and yiℓ > 0.(such anℓ exists, otherwise they solution would be equal to thex solution).
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 10 / 50
Solving the Continuous Knapsack Problem
Proof. (cont.)
We increaseyik and decreaseyiℓ , while keeping all other values equal.Let ǫ = min{vik(1− yik), viℓyiℓ}.We increaseyik by
ǫ
vikand decreaseyiℓ by
ǫ
viℓ, obtaining a new solution.
This new solution is no worse than they solution.Moreover, eitheryik has become 1, oryiℓ has become 0.
Repeating the same argument we find the solutionx of the greedy.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 11 / 50
Solving the Continuous Knapsack Problem
TheoremThe Greedy algorithm (n.3) is optimal for the Continuous Knapsack Problem (CKP)(continuous relaxation of 0-1 Knapsack)
Proof. 2
Let O = {o1, o2, . . . , on} be the optimal solution andG = {g1, g2, . . . , gn} be thegreedy solution(where the items are ordered with respect to the greedy choice).We show that there exist an optimal solution such that it alsotakes the greedy choicein each iteration.
First, we show that an optimal solution selects (a fraction or 1 unit of) itemg1.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 12 / 50
Solving the Continuous Knapsack Problem
Proof. 2 (cont.)
Two cases:1 G takes 1 unit ofg1.
If O also takes 1 unit ofg1, done.If O doesn’t take 1 unit ofg1, then we take away volumevg1 from O and put 1unit of g1 to it ⇒ new solutionO′.Of courseO′ has the same volume constraintV asO.Moreover,g1 has maximum ratiou/v.⇒ O′ is as good asO ⇒ g1 ∈ O′ andO′ is an optimal solution.
2 G takes a fractionf of g1 (the volume added toO is f × vg1).If O also takes a fractionf of g1, done.If O takes less thanf of g1 (cannot take more becauseo optimal), then we takeawayf × vg1 from O and putf of g1 to it ⇒ new solutionO′.As before,O′ is as good asO (otherwise contradiction).
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 13 / 50
Solving the Continuous Knapsack Problem
Proof. 2 (cont.)
Second, we observe thatafter each greedy choice is made, we have a problem of the sameform asthe original one.
The problem exhibits the optimal substructure property⇒ G≡ O.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 14 / 50
Solving the Continuous Knapsack Problem
(Another) Alternative Proof:
The simplex algorithm for solving the CKP takes exactly the same steps as the greedy.
Let ih be such that :
∀ j ∈ {1, . . . , h} xi j ← 1
∀ j ∈ {h + 2, . . . , n} xi j ← 0
xih+1 ← V −∑hj=1 vi j
vih+1
that is,ih is such that:h∑
j=1
vi j ≤ V andh+1∑j=1
vi j > V
Let us apply the simplex algorithm with the dictionary representation.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 15 / 50
Solving the Continuous Knapsack Problem
(Another) Alternative Proof: (cont.)
xn+1 = 1− xi1
xn+2 = 1− xi2
...x2n = 1− xin
x2n+1 = V −∑nj=1 vi j xi j
z =∑n
j=1 ui j xi j
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 16 / 50
Solving the Continuous Knapsack Problem
(Another) Alternative Proof: (cont.)
pivot: xi1 ↔ xn+1
xi1 = 1− xn+1
xn+2 = 1− xi2
...x2n = 1− xin
x2n+1 = V −∑nj=2 vi j xi j − vi1(1− xn+1) = (V − vi1)−
∑nj=2 vi j xi j + vi1xn+1
z = ui1 +∑n
j=2 ui j xi j − ui1xn+1
We continue with following dictionaries.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 17 / 50
Solving the Continuous Knapsack Problem
(Another) Alternative Proof: (cont.)
Dictionaryh :pivot: xih↔ xn+h
xi1 = 1− xn+1
xi2 = 1− xn+2
...xih = 1− xn+h
xn+h+1 = 1− xih+1
x2n = 1− xin
x2n+1 = (V −∑hj=1 vi j )−
∑nj=h+1 vi j xi j +
∑hj=1 vi j xn+j
z =∑h
j=1 ui j +∑n
j=h+1 ui j xi j −∑h
j=1 ui1xn+j
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 18 / 50
Solving the Continuous Knapsack Problem(Another) Alternative Proof: (cont.)
Dictionaryh + 1 :pivot: xih+1 ↔ x2n+1
xi1 = 1− xn+1
xi2 = 1− xn+2
...xih = 1− xn+h
x2n = 1− xin
xih+1 =(V −∑h
j=1 vi j )vih+1
−∑n
j=h+2 vi j xi j
vih+1
+
∑hj=1 vi j xn+j
vih+1
− x2n+1
vih+1
xn+h+1 = 1− (expression ofxih+1)
z =∑h
j=1 ui j + uih+1(expression ofxih+1) +∑n
j=h+2 ui j xi j −∑h
j=1 ui j xn+j
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 19 / 50
Solving the Continuous Knapsack Problem
(Another) Alternative Proof: (cont.)
Dictionaryh + 1 :pivot: xih+1 ↔ x2n+1
z =h∑
j=1
ui j + uih+1
((V −∑h
j=1 vi j )vih+1
)
+n∑
j=h+2
(ui j − uih+1
vi j
vih+1
)xi j
+h∑
j=1
(vi j
vih+1
− ui j
)xn+j
−x2n+1
vih+1
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 20 / 50
Solving the Continuous Knapsack Problem
(Another) Alternative Proof: (cont.)
We check the signs of the coefficients of the variables :
∀j = h + 2, . . . , n sign
(ui j − uih+1
vi j
vih+1
)= sign
(ui j
vi j− uih+1
vih+1
)= −1
because j > h + 1
∀j = 1, . . . , h sign
(uih+1
vih+1
vi j − ui j
)= sign
(uih+1
vih+1
− ui j
vi j
)= −1
because j < h + 1
Hence the simplex algorithm stops.
⇒ the (feasible) solution found is also the optimal solution.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 21 / 50
Solving the KP exactly
Can we solve the KP exactly ?⇓
Branch-and-Bound (B&B)(séparation et évaluation)
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 22 / 50
Branch and Bound
Branch-and-Bound (B&B) is a general framework for solvingexactly
integer and discrete optimization problems.
Intelligentimplicit enumeration of all possible solutions
only potentially good solutions are enumerated,
solutions that cannnot lead to improve the current solutionare not explored
based on the observation that the enumeration of integer solutions has a
tree structure.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 24 / 50
Branch and Bound - search treeSearch Tree :
Nodes: represent stages of construction of the solution
Arcs : represent choices made to build the solution
For the KP :
Nodes : represent a step in whichsome objects have been placed in the bag,some others have been left out of the bag,and others for which no decision has yetbeen taken.
Arcs : indicate the action of puttingan object in the bag or not putting it in thebag
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 25 / 50
Branch and Bound - idea
The nodes of the search tree represent subsets of all of the possible solutions.In particular, the root node representsall solutions that can be generated bygrowing the tree.
The main idea is to build step by step the search tree avoidinggrowing the whole tree
as much as possible.
Only the most promising nodes are generated and explored at any stage.
The node that is the most promising is determined by estimating a bound on the bestvalue of the objective function that can be obtained by growing that node to later stages.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 26 / 50
Branch and Bound - main steps
Branching:
a node is selected for further growth and
two new subproblems are created
Bounding:
upper and lower bounds on the best value
attained by growing a node are estimated
Pruning:
nodes that are showed to be never either
feasible or optimal are permanently discarded
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 27 / 50
Branch and Bound - main steps
During the search, a node must be in one of the following states :
(i) has no feasible solution
(ii) is such that its associated solutionxS is feasible and(a) xS has a value better than the incumbent *
(b) xS has a value no better than the incumbent *
(iii) neither of states (i) and (ii) has yet been established.
A node in either state (i) or (ii) is said to befathomed.
A node that has not yet been fathomed is said to beactive.
A node in states (i) or (iib) cannot contain the optimum, it isprunedfrom the tree.
* incumbent= best integer solution found so far
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 28 / 50
Branch and Bound - main steps
The search continues, branching from the active nodes untileither :
(i) all nodes have been pruned from the tree
(in which case the problem has no feasible solutions)
(ii) all active nodes have bounds no greater than the value of the incumbent
(in which case the incumbent represents an optimal solution)
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 29 / 50
Branch and Bound for KP - principles
The solution set is partitioned into subsetsS1, . . . , SM
each subset corresponds to a partial (feasible) solution,
i.e. some of the variables have been fixed
When abranchingwith respect to a variablexj is done, 2 new subsets
are created fromSk :
S0k where we set xj = 0, S1
k where we set xj = 1
An upper boundUBk corresponding toSk can be computed by the
greedy algorithm on the CKP corresponding toSk (integer part)
A lower boundLBk corresponding toSk can be computed by
rounding down the fractional variable, if it exists.
If UBk = LBk the subproblemk is solved exactly.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 31 / 50
Branch and Bound for KP - principles
When we continue branching ?
Let z∗ be the value of the best feasible solution found so far:
current best solution: z∗ ≥ max{LBk, ∀k}
UBk > z∗ : the subsetSk may contain a feasible solution with value> z∗.
We continue working onSk. (Sk, UBk) is active ⇒ Branching
UBk ≤ z∗ : the subsetSk cannot contain a solution with value> z∗.
The subset is of no interest anymore.⇒ Fathoming
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 32 / 50
Branch and Bound for KP - steps
Initialization
construct a list L containing all active pairs(Sk, UBk) :
initially, the original problem
initial Upper BoundUB : solution of the CKP via the greedy algorithm
initial Lower BoundLB : a feasible solution
(rounding down the value of the fractional variable in the solution of CKP)
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 33 / 50
Branch and Bound for KP - steps
As long asL is not empty :
choose among the active pairs the one with the maximalUB : (Sk, UBk)
split Sk in S0k andS1
k according to a branching criterion
(ex. : the fractional variablexi in the continuous solution)
computeUB0k andUB1
k (greedy algorithm for CNP)
computeLB0k andLB1
k (feasible solution for KP)
if LB0k > z∗, then set z∗ ← LB0
k and updatex∗ accordingly
if LB1k > z∗, then set z∗ ← LB1
k and updatex∗ accordingly
if UB0k > z∗, then add (S0
k, UB0k) to L
if UB1k > z∗, then add (S1
k, UB1k) to L
updateL deleting the elements(Sh, UBh) such thatUBh < z∗
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 34 / 50
Branch and Bound for KP - algorithm
Sort values/volumes by non-increasing order.Let i j be indexes such thatui j /vi j with j = 1 is the maximum ratio
xi ← 2 ∀ik← 1 // iteration counter
L← (Sk, UBk) // original problem
z∗ ← LBk
while (L not empty) do
ν ← min{i j |j ∈ {1, ..n} ∧ xν = 2}x0ν ← 0, x1
ν ← 1 // two branches
for p = 0, 1 do
compute UBpk and LBp
k
if(∑ν
j=1 vi j ≤ V)
then
if (UBpk > z∗ and LBp
k 6= UBpk) then
add (Spk, UBp
k) to L by non-increasing order forUB
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 35 / 50
Branch and Bound for KP - algorithm (cont.)
if (LBpk > z∗) then
z∗ ← LBpk // current best solution
x∗ ← corresponding solution
end if
end for
L← L \ (Sk, UBk)k← k + 1
end while
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 36 / 50
Example
max z = 12x1 + 11x− 2 + 8x3 + 6x4
s.t. 8x1 + 8x2 + 6x3 + 5x4 ≤ 20
xi ∈ {0, 1} ∀i
u1
v1>
u2
v2>
u3
v3>
u4
v4
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 37 / 50
branching on the variable with lowest index www.win.tue.nl/ wscor/OW/2V300
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 38 / 50
branching on fractional variables www.win.tue.nl/ wscor/OW/2V300
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 39 / 50
Designing a Branch and Bound algorithm
Branch and Bound is a very general framework.
To completely specify how the process is to proceed, you alsoneed to define
How to obtain bounds?
How to branch?
In what order to examine nodes?
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 41 / 50
Branch and Bound: Obtaining bounds
For a maximization problem
- the (current) tightest lower bound, provided by the best solution we have found,is also referred to as theprimal bound.
- the upper bound on the profit obtainable within a given set of solutionsis also referred to as thedual bound.
To obtain adual (upper) bound:
one typically solves a relaxation of the subproblem :
LP relaxation (but also Lagrangian relaxation, etc..)
The bound can also be computed by solving thedual problem.
To obtain aprimal (lower) bound:
one typically computes a feasible solution of the subproblem.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 42 / 50
Dual Problem
For aLP problem in standardPrimalform:
(LP)
max cTx x∈ Rn, c ∈ Rn
s.t. Ax≤ b A∈ Rm×n, b ∈ Rm
x≥ 0
TheDual Problemis :
(D)
min bTy y∈ Rm
s.t. ATy≥ c
y≥ 0
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 43 / 50
Dual Problem
To obtain the Dual Problem:
- write the Lagrangian : L(x, y, s) = cTx− sTx + yT(b− Ax), with s∈ Rn, y ∈ Rm
(using nonnegative Lagrangian multipliers to add the constraints to the objective function)
- write the Lagrangian dual : min max(yb+ (cT − s− yA)x)
- apply the optimality conditions (KKT) to the Lagrangian dual
The solution of the dual problem provides an upper bound
to the solution of the primal problem
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 44 / 50
Dual Problem : ExampleKnapsack with 2 constraints, relaxed
maxn∑
i=1
xiui
s.t.n∑
i=1
xivi ≤ V
n∑i=1
xipi ≤ P
xi ≤ 1 ∀ixi ≥ 0 ∀i
A =
v1 . . . vn
p1 . . . pn
1 . . . 0...
...0 . . . 1
b =
VP1...1
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 45 / 50
Dual Problem : ExampleKnapsack with 2 constraints, relaxed
AT =
v1 p1 1 . . . 0...
... 0 . . . 0vn pn 0 . . . 1
(D)
min bTy =
n+2∑i=1
biyi
s.t. ATy≥ u ⇔ viy1 + piy2 + yi+2 ≥ ui ∀i ∈ {1, ..n}y≥ 0
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 46 / 50
Dual Problem : ExampleKnapsack with 2 constraints, relaxed
A (feasible) solution can be obtained by setting :
y1 = y2 = 0, yi+2 = ui ∀i
The solution is :n∑
i=1
ui . This gives an upper bound for the 0-1 KP.
Another solution can be obtained by setting :
y1 = y2 = 1, yi+2 = max{0, ui − pi − vi ∀i}The solution is :
V + P +n∑
i=1
max{0, ui − pi − vi}
that can be better than the previous one ifV + P <
n∑i=1
ui.
So, we can search for
minα∈R+
α(V + P) + max{0, ui − α(pi + vi)}
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 47 / 50
Branch and Bound: How to partition into subproblems
IP problems :
- choose xj /∈ Z- set Sj
k = Sk ∪ {x : xj ≤ ⌊xkj ⌋} and
Sj+1k = Sk ∪ {x : xj ≤ ⌊xk
j ⌋+ 1}How to choose the fractionary component? Example : choose the component with
fractionary value closest to 0.5
Binary IP problems :
- choose xj
- setS0k = Sk ∪ {x : xj = 0} and
S1k = Sk ∪ {x : xj = 1}
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 48 / 50
Branch and Bound: How to choose a node
The search tree can be explored using
Depth-first search
allows to find a feasible solution to the IP quickly
Breadth-first-search
may allow to prune more of the search tree sooner
Best-Node First
chooses the node with the best dual bound.
This might lead to finding a good incumbent quickly, thus eliminating the need
to evaluate the other subproblems
Otherheuristics
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 49 / 50
Bibliography I
L.R. Foulds.Combinatorial Optimization for UndergraduatesSpringer-Verlag, 1984.
C.H. Papadimitriou and K. Steiglitz.Combinatorial Optimization: Algorithms and Complexity.Dover, New York, 1998.
John W. Chinneck.Practical Optimization: a gentle introductionwww.sce.carleton.ca/faculty/chinneck/po.html, 2010.
Sonia Cafieri (ENAC) Knapsack Problem 2011-2012 50 / 50