1 4Opt-E
1 4Opt-E
DISCRETE MATHEMATICS
3
NỘI DUNG
4
Contents of part 1
5
Chapter 4. Combinatorial optimization problem
1. Introduction to problem
2. Brute force
3. Branch and bound
6
1. Introduction to problem
7
1.1. General problem
• In many practical application problems of combinatorics, each configuration is assigned to a value equal to
the rating of the worth using of the configuration for a particular use purpose.
• Then it appears the problem: Among possible combination configurations, determine the one that the worth
using is the best. Such kind of problems is called the combinatorial optimization problem.
8
1.1. General problem
9
1. Introduction to problem
10
1.2. Traveling salesman problem (TSP)
11
1.2. Traveling salesman problem
Denote:
- set of all permutations of n natural numbers 1, 2, ..., n.
12
1.2. Traveling salesman problem
• Then, the TSP could be stated as the following combinatorial optimization problem:
min { f() : }.
• One could see that the number of possible itineraries is n!, but there are only (n-1)! itineraries if the starting
city is fixed.
13
1. Introduction to problem
14
1.3. Knapsack problem
• Problem Definition
• Want to carry essential items in one bag
• Given a set of items, each has
• An weight (i.e., 12kg)
• A value (i.e., 4$)
• Goal
• To determine the # of each item to include in a collection so that
• The total weight is less than some given weight that the bag can carry
• And the total value is as large as possible
15
1.3. Knapsack problem
• Three Types:
• 0/1 Knapsack Problem
• restricts the number of each kind of item to zero or one
• Bounded Knapsack Problem
• restricts the number of each item to a specific value
• Unbounded Knapsack Problem
• places no bounds on the number of each item
• Complexity Analysis
• The general knapsack problem is known to be NP-hard
• No polynomial-time algorithm is known for this problem
16
1.3. Knapsack problem
• Three Types:
• 0/1 Knapsack Problem
• restricts the number of each kind of item to zero or one
• Bounded Knapsack Problem
• restricts the number of each item to a specific value
• Unbounded Knapsack Problem
• places no bounds on the number of each item
• Complexity Analysis
• The general knapsack problem is known to be NP-hard
• No polynomial-time algorithm is known for this problem
17
1. Introduction to problem
18
1.4. Bin backing problem
• Given n items: the weight of items are w1, w2, ..., wn. Need to find a way to place these n items into bins of
same size b such that the number of bins used is minimal.
19
1.4. Bin backing problem
sgn( x ) min,
j 1 i 1
ij Note: The signum function of a real number x is defined as follows:
x
j 1
ij 1, i 1, 2,..., n
w x
i 1
i ij b, j 1, 2,..., n;
20
Chapter 4. Combinatorial optimization problem
1. Introduction to problem
2. Brute force
3. Branch and bound
21
2. Brute force
• One of the most obvious methods to solve the combinatorial optimization problem is: On the basis of the
combinatorial enumeration algorithms, we go through each solution of the problem, and for each solution,
we calculate its value of objective function; then compare values of objective functions of all solutions to find
the optimal solution whose objective function is minimal (maximal).
• The approach based on such principles is called the brute force.
22
2. Brute force
j 1
23
2. Brute force
24
2. Brute force
25
2. Brute force
void BackTrack(int k)
{
int j, t;
if (bk>=w[k]) t=1 else t=0;
for (j= t; j--; j>=0)
{
x[k] = j;
bk= bk-w[k]*x[k];
fk= fk + v[k]*x[k];
if (k == n)
{
if (fk>fopt) {
xopt:=x; fopt:=fk;
}
}
else BackTrack(k+1);
bk = bk+w[k]*x[k];
fk = fk - v[k]*x[k];
}
}
26
2. Brute force
• Brute force is difficult to do even on the most morden super computer. Example to enumerate all
15! = 1 307 674 368 000
permutations on the machine with the calculation speed of 1 billions operations per second, and if to
enumerate one permutation requires 100 operations, then we need:
130767 seconds > 36 hours!
20! ===> 7645 years
27
2. Brute force
• However, it must be emphasized that in many cases (for example, in the traveling salesman problem, the
knapsack, bin backing problem), we have not found yet any effective methods other than the brute force.
• Then, a problem arises that in the process of enumerating all solutions, we need to make use of the found
information to eliminate solutions that are definitely not optimal.
• In the next section, we will look at such a search approach to solve the combinatorial optimization problems.
In literature, it is called Branch and bound algorithm.
28
Chapter 4. Combinatorial optimization problem
1. Introduction to problem
2. Brute force
3. Branch and bound
29
3. Branch and bound
30
3.1. General diagram
31
3.1. General diagram
• We will describe the idea of algorithm on the model of the following general combinatorial optimization
problem:
min { f(x) : x D },
where D is the finite set.
• Assume set D is described as following:
D = {x = (x1, x2, ..., xn) A1 A2 ... An:
x satisfies property P},
where A1, A2, ..., An are finite set, and P is property on the
Descartes product A1 A2 ... An.
32
3.1. General diagram
• The requirement about describe the set D is to be able to use the backtracking algorithm to enumerate all
solutions of the problem.
• Problem
max {f(x): x D}
is the equivalent of the problem
min {g(x): x D}, where g(x) = -f(x)
Therefore, we can limit to considering the minimize problem
33
3.1. General diagram
Branching procedure can implement by using backtracking:
() D
1 n1
a 1 a 2
1 ... a 1
1 n1
(a )1 (a12 ) . . . (a )
1
D(a11 ) D(a1 ) 2
D( a1n1 )
34
3.1. General diagram
• Branching can be described as following:
( ) D(a1,…,ak)
a1k 1 a 2 akp1
k 1 ...
D(a1 ,..., ak , a1k 1 )
p
We have partition: D(a1 ,..., ak ) D(a1 ,..., ak , aki 1 )
i 1
D(a1,..., ak)= { xD: xi = ai , i = 1,..., k } is a subset of solutions where the first k elements of solutions are already
known: x1 = a1 ,x2 = a2 , ...., xk = ak
35
Bounding
• We need to determine function g defined on the set of all partial solutions that satisfies the following
inequality:
The value of objective function of all solutions having the first
g(a1,..., ak) min{f(x): xD(a1, ..., ak)} (*) k elements as (a1, a2,…,ak)
For each k-level partial solution (a1, a2, ..., ak), k = 1, 2, ...
• The inequality (*) means that the value of g of partial solution (a1, a2, ..., ak) is not greater than the minimum
value of objective function of solution set D(a1, ..,ak)
D(a1,..., ak)= { xD: xi = ai , i = 1,..., k },
In other word, g(a1, a2, . . . , ak) is the lower bound of the value of objective function of solution set D(a1, a2,
..., ak).
36
Cut branch by using lower bound
• Assume we already have function g defined as above. We will use this function to reduce the amount of
searching during the process to consider all possible solutions in the backtracking algorithm.
• In the process to enumerate solutions, assume we already obtain some solutions. Thus, denote x* the
solution with objective function is minimum among all solutions obtained so far, and denote f* = f(x*)
• We call
• x* is the current best solution (optimal solution),
• f* is the current best value of objective function (optimal objective value).
37
Cut branch by using lower bound
If g(a1,…,ak-1,𝑎𝑘2 ) > 𝑓 ∗
f* is the current best objective value 𝑝
If g(a1,…,ak-1,𝑎𝑘 ) > 𝑓 ∗
(a1,…,ak-1)
1 2
a a akp
k k ...
If g(a1,…,ak-1,𝑎𝑘1 ) > 𝑓 ∗
2
1
a
k 1 a k 1 ... akq1
all solutions with first k elements as (a1,…,ak-1,𝑎1𝑘 ) certainly have the objective value >
f* we do not need to browse this branch
g(a1,..., ak) is lower bound of partial solution (a1,..., ak)
38
3.1. General diagram
void Branch(int k) {
//Construct xk from partial soluion (x1, x2, ..., xk-1)
for akAk
if (akSk )
{
xk = ak;
if (k == n) <Update Record>;
else if (g(x1,..., xk) f*) Branch(k+1);
}
}
void BranchAndBound ( ) {
f* = +;
//if you know any solution x* then set f* = f(x*)
Branch(1);
if (f* < +)
<f* is the optimal objective value, x* is optimal solution >
else < problem does not have any solutions >;
}
39
3.1. General diagram
The construction of g function depends on each specific combinatorial optimization problem. Usually we try to
build it so that:
• Calculating the value of g must be simpler than solving the combinatorial optimization problem on the right
side of (*).
• The value of g(a1, ..., ak) must be close to the value of the right side of (*).
Unfortunately, these two requirements are often contradictory in practice.
40
3. Branch and bound
41
3.2.1. Traveling salesman problem
42
3.2.1. Traveling salesman problem
Fix the starting city as city 1, the TSP leads to the problem:
• Determine the minimum value of
f(1,x2,..., xn) = c[1,x2]+c[x2,x3]+...+c[xn-1,xn] + c[xn,1]
where
(x2, x3, ..., xn) is permutation of natural numbers 2, ..., n.
43
3.2.1. Traveling salesman problem
44
3.2.1. Traveling salesman problem
1 2 3
1 u2 . . . uk-1 uk uk+1 uk+2 ….. un 1
We still need to go through n-k+1 segments, each segment with the cost at least cmin, thus the lower bound of
the partial solution (1, u2, ..., uk) can be calculated by the formula:
g(1, u2, ..., uk) = + (n-k+1) cmin
45
3.2.1. Traveling salesman problem
Give 5 cities {1, 2, 3, 4, 5}. Solve the TSP where the salesman starts from the city 1, and the cost matrix:
0 3 14 18 15
3 0 4 22 20
C= 17 9 0 16 4
9 20 7 0 18
9 15 11 5 0
46
3.2.1. Traveling salesman problem
• We have cmin = 3. The process executing the algorithm is described by the solution search tree.
• Information written in each box is the following in order:
• elements of partial solution,
• is the cost of partial solution,
• g – lower bound of partial solution.
47
3.2.1. Traveling salesman problem
Root, f* = +∞
(2) (4);
(3); (5);
= 3 = 14 = 18 = 15
g = 3 + 4*3 = 15 g = 14 +4*3 = 26 g = 18 +4*3 = 30 g = 15 +4*3 = 27
(2,3,4); (2,3,5);
=7 +16 = 23 =7 +4 = 11
g = 23 + 2*3 = 29 g = 11 + 2*3 = 17
These branches are eliminated because their lower bound g > f*=25
(2,3,4,5); (2,3,5,4);
=23 + 18 = 41 =11 + 5 = 16
49
3.2.1. Traveling salesman problem
void Branch(int k) {
for (int v = 2; v<=n;v++) {
if (visited[v] == FALSE) {
xk = v; visited[v] = TRUE;
f = f + c(xk−1,xk);
if (k == n) //Update record:
{ if (f + c(xn,x1) < f*) f∗ = f + c(xn,x1); }
else {
g = f + (n−k + 1)∗cmin; //calculate bound
if (g < f∗ ) Branch(k + 1);
}
}
f = f − c(xk−1,xk);
visited[v] = FALSE;
}
}
50
3.2.1. Traveling salesman problem
void BranchAndBound()
{
f* = +;
for (v = 1; v<=n;v++) visited[v] = FALSE;
f=0; x1 = 1; visited[x1] = TRUE;
Branch(2);
return f∗;
}
51
3. Branch and bound
52
3.2.2. Knapsack problem
53
3.2.2. Knapsack problem
• We have the variable
xj – number items type j loaded in the bag, j=1,2, ..., n
• Mathematical model of problem: Find
n n
f max { f ( x) p j x j : w j x j c, x j Z , j 1, 2,..., n }
*
j 1 j 1
54
3.2.2. Knapsack problem
• Assume we index the item in the order such that the following inequality is satisfied:
v1 /w1 v2 / w2 . . . vn / wn .
(it means items are ordered in descending order of profit per one unit of weight)
• To construct the upper bound function, we consider the following Knapsack continuous variables (KPC): Find
n n
g * max { f ( x) p j x j : w j x j c, x j 0, j 1, 2,..., n }
j 1 j 1
55
3.2.2. Knapsack problem
Proposition. The optimal solution to the KPC is vectơ (x* = 𝑥1∗ , 𝑥2∗ ,.., 𝑥𝑛∗ ) where elements are determined by the
formula:
𝑥1∗ = c/w1, 𝑥2∗ = 𝑥3∗ = ⋯ . = 𝑥𝑛∗ = 0
and the optimal value is g* = v1b /w1.
Proof. Consider x = (x1,..., xn) as a solution to the KPC. Then
pj (p1 / w1 ) wj , j = 1, 2, ..., n
as xj 0, we have
pj xj (p1 / w1 ) wj xj , j = 1, 2, ..., n.
• Therefore n n
p x ( p
j 1
j j
j 1
1 / w1 ) w j x j
n
( p1 / w1 ) w j x j
j 1
( p1 / w1 )c g *
Proposition is proved.
56
3.2.2. Knapsack problem
• Now we have the k-level partial solution: (u1, u2, ..., uk), then the profit of items currently loaded in the bag is
k = p1u1 + p2u2 + . . . + pkuk
and the remaining capacity of the bag is
ck = c – (w1u1 + w2u2 + . . . + wkuk)
• We have:
max{ f ( x) : x D, x j u j , j 1, 2,..., k}
n n
max { k
j k 1
pjxj : wx
j k 1
j j ck , x j Z , j k 1, k 2,..., n}
n n
k max { p j x j : wx j j ck , x j 0, j k 1, k 2,..., n}
j k 1 j k 1
k pkthe
• Thus, we can calculate 1ck upper
/ wk 1 bound for the partial solution (u , u , ..., u ) by formula g(u1, u2,..., uk) = k +
1 2 k
pk+1 ck / wk+1
57
3.2.2. Knapsack problem
• Note: When continuing build the (k+1)th element of solution, candidates for xk+1 are 0, 1, ..., [ck / wk+1 ]
• Using the result of the proposition, when selecting value for xk+1, we browse candidates for xk+1 in the
descending order: [ck / wk+1 ], [ck / wk+1 ]-1,…,1, 0
58
3.2.2. Knapsack problem
Example: Solve the knap sack problem using the branch and bound algorithm:
• Note that in this example, all four items are already sorted in descending order of profit on an unit weight
59
59
3.2.2. Knapsack problem
• The process executing the algorithm is described by the solution search tree.
• Information written in each box is the following in order:
• elements of partial solution,
• is the cost of partial solution (profit of items currently loaded in the bag),
• w : remaining capacity of the bag,
• g : upper bound of partial solution.
60
Root, f*=-∞ f(x) = 10 x1 + 5 x2 + 3 x3 + 6 x4 max,
5 x1 + 3 x2 + 2 x3 + 4 x4 8,
Select item 1: xj Z+ , j =1, 2, 3, 4.
8/5 = 1 x1 = 1 x1 = 0
(1) = 10 (0); = 0
w = 8-5=3; g = 10 +5*3/3 = 15 w = 8; g = 0 +5*8/3 = 40/3
x3 = 0 Select item 3:
0/2 = 0 Eliminate because upper bound g < f*=15
(1,1,0); = 15
w = 0; g = 15
• Finish algorithm, we obtain:
– Optimal solution: x* = (1, 1, 0, 0),
Select item 4:
x4 = 0 0/4 = 0 – Optimal objective value: f* = 15.
(1,1,0,0) We obtain a new solution
f*=15 Update record: f*=15 61
61
THANK YOU !
62