Esercizio 1
Dato il seguente problema di Knapsack 0-1
max 30x
1+ 36x
2+ 15x
3+ 11x
4+ 5x
5+ 3x
69x
1+ 12x
2+ 6x
3+ 5x
4+ 3x
5+ 2x
6< 17 x ∈ {0,1}
6risolvere il problema tramite l’algoritmo di Branch-and-Bound.
Esercizio 1
Poiché in corrispondenza di ogni nodo dell’albero dovremo risolvere un problema di Knapsack continuo, riordiniamo gli oggetti in modo tale
che
p
1a
1≥ p
2a
2≥.. .≥ p
na
np1/a1 = 30/9 = 3,333 ⇒ y1 p2/a2 = 36/12 = 3 ⇒ y2 p3/a3 = 15/6 = 2,5 ⇒ y3 p4/a4 = 11/5 = 2,2 ⇒ y4 p5/a5 = 5/3 = 1,666 ⇒ y5 p6/a6 = 3/2 = 1,5 ⇒ y6
Gli oggetti sono già ordinati ⇒ Conserviamo la formulazione iniziale
Esercizio 1
Sia P0 il rilassamento lineare associato al sottoproblema S0 del nodo radice
max 30x1 + 36x2 + 15x3 + 11x4 + 5x5 + 3x6 9x1 + 12x2 + 6x3 + 5x4 + 3x5 + 2x6 < 17 0 < xi < 1, i = 1, …,6
Calcoliamo la soluzione associata all’upper bound utilizzando l’algoritmo per il Knapsack continuo:
x0UB = [1, (17 – 9)/12, 0, 0, 0, 0] = [1, 2/3, 0, 0, 0, 0] ⇒ z0UB = 54
Per il lower bound, consideriamo una soluzione ammissibile data da x0LB = [1, 0, 0, 0, 0, 0] ⇒ z0LB = 30 = zLB.
S0
z0UB = 54
z0LB = 30 = zLB
Generiamo i due sottoproblemi S1 t.c. x2 = 0
S2 t.c. x2 = 1
S1 S2
x2 = 1 x2 = 0
Sia P1 il rilassamento lineare associato al sottoproblema S1 (x2 = 0) max 30x1 + 15x3 + 11x4 + 5x5 + 3x6
9x1 + 6x3 + 5x4 + 3x5 + 2x6 < 17 0 < xi < 1, i = 1, …,6
x1UB = [1, 0, 1, (17–15)/5, 0, 0] = [1, 0, 1, 2/5, 0, 0] ⇒ z1UB = 49,4 x1LB = [1, 0, 1, 0, 0, 0] = [1, 0, 1, 0, 0, 0] ⇒ z1LB = 45.
Poiché z1LB > zLB, aggiorniamo il valore del lower bound corrente zLB = 45
Esercizio 1
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
zLB = 45
z0UB = 54
z0LB = 30 = zLB
Sia P2 il rilassamento lineare associato al sottoproblema S2 (x2 = 1)
max 30x1 + 36 + 15x3 + 11x4 + 5x5 + 3x6
9x1 + 12 + 6x3 + 5x4 + 3x5 + 2x6 < 17⇒ 9x1 + 6x3 + 5x4 + 3x5 + 2x6 < 5 0 < xi < 1, i = 1, …,6
x2UB = [(17–12)/9, 1, 0, 0, 0, 0] = [5/9, 1, 0, 0, 0, 0] ⇒ z2UB = 52,6 x2LB = [0, 1, 0, 0, 0, 0] ⇒ z2LB = 36.
z2UB = 52.6 z2LB = 36
Esercizio 1
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
zLB = 45
z0UB = 54
z0LB = 30 = zLB
z2UB = 52.6 z2LB = 36
Da S2, generiamo i due sottoproblemi S3 t.c. x1 = 0
S4 t.c. x1 = 1
Sia P3 il rilassamento lineare associato al sottoproblema S3 (x2 = 1, x1=0) max 36 + 15x3 + 11x4 + 5x5 + 3x6
6x3 + 5x4 + 3x5 + 2x6 < 5 0 < xi < 1, i = 1, …,6 x3UB = [0, 1, 5/6, 0, 0, 0] ⇒ z3UB = 48,5 x3LB = [0, 1, 0, 0, 0, 0] ⇒ z3LB = 36.
S3 S4
x1 = 0 x1 = 1
z3UB = 48.5 z3LB = 36
Esercizio 1
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
z0UB = 54
z0LB = 30 = zLB
z2UB = 52.6 z2LB = 36
Da S2, generiamo i due sottoproblemi S3 t.c. x1 = 0
S4 t.c. x1 = 1
Sia P4 il rilassamento lineare associato al sottoproblema S4 (x2 = x1=1) max 30 + 36 + 15x3 + 11x4 + 5x5 + 3x6
9 + 12 + 6x3 + 5x4 + 3x5 + 2x6 < 17 ⇒ 6x3+5x4+3x5+2x6 < 17– 21 0 < xi < 1, i = 1, …,6
Poiché P4 è inammissibile, il nodo S4 può essere chiuso per inammissibilità.
S3 S4
x1 = 0 x1 = 1
z3UB = 48.5 z3LB = 36
Esercizio 1
×
inammissibilità
Esercizio 1
Da S1, generiamo i due sottoproblemi S5 t.c. x4 = 0
S6 t.c. x4 = 1
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
z0UB = 54
z0LB = 30 = zLB
z2UB = 52.6 z2LB = 36
S3 S4
x1 = 0 x1 = 1
z3UB = 48.5
z3LB = 36 inammissibilità
×
zLB = 45
S5 S6
x4 = 0 x4 = 1
z5UB = 48.33 z6LB = 45
Sia P5 il rilassamento lineare associato al sottoproblema S1 (x2 =x4 = 0) max 30 x1 + 15x3 + 5x5 + 3x6
9x1 + 6x3 + 3x5 + 2x6 < 17 0 < xi < 1, i = 1, …,6
x5UB = [1, 0, 1, 0, (17–15)/3, 0] = [1, 0, 1, 0, 2/3, 0] ⇒ z5UB = 48,33 x5LB = [1, 0, 1, 0, 0, 0] ⇒ z5LB = 45.
Esercizio 1
Da S1, generiamo i due sottoproblemi S5 t.c. x4 = 0
S6 t.c. x4 = 1
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
z0UB = 54
z0LB = 30 = zLB
z2UB = 52.6 z2LB = 36
S3 S4
x1 = 0 x1 = 1
z3UB = 48.5
z3LB = 36 inammissibilità
×
zLB = 45
S5 S6
x4 = 0 x4 = 1
z5UB = 48.33 z6LB = 45
Sia P6 il rilassamento lineare associato al sottoproblema S6 (x2 = 0, x4=1) max 30 x1 + 15x3 + 11 + 5x5 + 3x6
9x1 + 6x3 + 5 + 3x5 + 2x6 < 17 ⇒ 9x1 + 6x3 + 3x5 + 2x6 < 12 0 < xi < 1, i = 1, …,6
x6UB = [1, 0, (12–9)/6 , 1, 0, 0] = [1, 0, 1/2, 1, 0, 0] ⇒ z6UB = 48,5 x6LB = [1, 0, 0, 1, 0, 0] ⇒ z6LB = 41.
z6UB = 48.5 z6LB = 41
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
zLB = 45
z0UB = 54
z0LB = 30 = zLB
z2UB = 52.6 z2LB = 36
Da S3, generiamo i due sottoproblemi S7 t.c. x5 = 0
S8 t.c. x5 = 1
S5 S6
x4 = 0 x4 = 1
z5UB = 48.33 z5LB = 45
z6UB = 48.5
z6LB = 41 S3 S4
x1 = 0 x1 = 1
z3UB = 48.5
z3LB = 36 inammissibilità
×
S7 S8
x5 = 0 x5 = 1 Sia P7 il rilassamento lineare associato al sottoproblema S7 (x2=x4=x5=0)
max 30x1 + 15x3 + 3x6 9x1 + 6x3 + 2x6 < 17 0 < xi < 1, i = 1, …,6 x7UB = [1, 0, 1, 0, 0, 1] ⇒ z7UB = 48
x7LB = [1, 0, 1, 0, 0, 1] ⇒ z7LB = 48 ⇒ zLB = 48.
S7
z7UB = 48 z7LB = 48
×
zLB = 48
OPT .
Esercizio 1
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
zLB = 45
z0UB = 54
z0LB = 30 = zLB
z2UB = 52.6 z2LB = 36
Da S3, generiamo i due sottoproblemi S7 t.c. x5 = 0
S8 t.c. x5 = 1
S5 S6
x4 = 0 x4 = 1
z5UB = 48.33 z5LB = 45
z6UB = 48.5
z6LB = 41 S3 S4
x1 = 0 x1 = 1
z3UB = 48.5
z3LB = 36 inammissibilità
×
S7 S8
x5 = 0 x5 = 1 Sia P8 il rilassamento lineare associato al sottoproblema S8 (x2=x4=0, x5=1)
max 30x1 + 15x3 + 5 + 3x6
9x1 + 6x3 + 3 + 2x6 < 17 ⇒ 9x1+6x3+2x6 < 14 0 < xi < 1, i = 1, …,6
x8UB = [1, 0, (14–9)/6, 0, 1, 0]=[1,0,5/6,0,1,0] ⇒ z8UB = 47,5 x8LB = [1, 0, 0, 0, 1, 0] ⇒ z8LB = 35
S7
z7UB = 48 z7LB = 48
×
zLB = 48
OPT
Poiché z8UB = 47,5 < zLB, il nodo S8 può essere chiuso per bound.
z8UB = 47.5 z8LB = 35
bound.
×
Esercizio 1
Per lo stesso motivo, anche i nodi S3 ed S6 possono essere chiusi per bound.
×
bound. bound.
×
Per determinare il valore finale ottimo del problema, consideriamo tutti i nodi che sono stati chiusi per ottimalità e prendiamo la soluzione che da’ il massimo valore della funzione obiettivo.
Nel nostro caso il valore ottimo è dato da z*=48 e corrisponde alla soluzione x* = [1,0,1,0,0,1] associata al nodo S
7.
Esercizio 1
S0
S1 S2
z1UB = 49.4 z1LB = 45
x2 = 0 x2 = 1
zLB = 45
z0UB = 54
z0LB = 30 = zLB
z2UB = 52.6 z2LB = 36
S5 S6
x4 = 0 x4 = 1
z5UB = 48.33 z5LB = 45
z6UB = 48.5
z6LB = 41 S3 S4
x1 = 0 x1 = 1
z3UB = 48.5
z3LB = 36 inammissibilità
×
S7 S8
x5 = 0 x5 = 1
S7
z7UB = 48 z7LB = 48
×
zLB = 48
OPT
z8UB = 47.5 z8LB = 35
bound.
×
Esercizio 1
×
bound. bound.
×
Esercizio 2
Dato il seguente problema di Knapsack
max 6 x1 + 10 x2 + 12 x3
x1 + 2x2 + 3 x3 < 5 (KP) x ∈ {0,1}3
risolvere il problema tramite l’algoritmo di programmazione dinamica.
Esercizio 2
max 6 x
1+ 10 x
2+ 12 x
3x
1+ 2x
2+ 3 x
3< 5 (KP) x ∈ {0,1}
3Calcoliamo la formula di ricorsione per r = 1:
z
1(0) = 0 ⇒ x
1= 0
z
1(1) = 6 ⇒ x
1= 1
z
1(2) = 6 ⇒ x
1= 1
z
1(3) = 6 ⇒ x
1= 1
z
1(4) = 6 ⇒ x
1= 1
z
1(5) = 6 ⇒ x
1= 1
d
r 0 1 2 3 4 5
1
(0,*,*)
0
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
2 z
2(0) z
2(1) z
2(2) z
2(3) z
2(4) z
2(5) 3 z
3(0) z
3(1) z
3(2) z
3(3) z
3(4) z
3(5)
Rappresentiamo questi valori in una tabella di dimensione n × b = 3 × 5, in cui ogni elemento contiene z
r(d).
Il simbolo * nella soluzione indica che la variabile non è stata ancora fissata
Esercizio 2
Calcoliamo ora z
2(d):
Per d = {0, 1}
z
2(0) = z
1(0) = 0 ⇒ x
2= 0 z
2(1) = z
1(1) = 6 ⇒ x
2= 0 Per d = {2, 3, 4, 5}
z
2(2) = max {z
1(2), z
1(0) + c
2} = max {6, 10} = 10 ⇒ x
2= 1 z
2(3) = max {z
1(3), z
1(1) + c
2} = max {6, 16} = 16 ⇒ x
2= 1 z
2(4) = max {z
1(4), z
1(2) + c
2} = max {6, 16} = 16 ⇒ x
2= 1 z
2(5) = max {z
1(5), z
1(3) + c
2} = max {6, 16} = 16 ⇒ x
2= 1 Riportando questi valori in tabella si ha:
Esercizio 2
d
r 0 1 2 3 4 5
1
(0,*,*)
0
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
2
(0,0,*)
0
(1,0,*)
6
(0,1,*)
10
(1,1,*)
16
(1,1,*)
16
(1,1,*)
16
3 z
3(0) z
3(1) z
3(2) z
3(3) z
3(4) z
3(5)
Esercizio 2
Calcoliamo ora z
3(d):
Per d = {0, 1, 2}
z
3(0) = z
2(0) ⇒ x
3= 0 z
3(1) = z
2(1) ⇒ x
3= 0 z
3(2) = z
2(2) ⇒ x
3= 0 Per d = {3, 4, 5}
z
3(3) = max {z
2(3), z
2(0) + c
3} = max {16, 12} = 16 ⇒ x
3= 0 z
3(4) = max {z
2(4), z
2(1) + c
3} = max {16, 18} = 18 ⇒ x
3= 1 z
3(5) = max {z
2(5), z
2(2) + c
3} = max {16, 22} = 22 ⇒ x
3= 1 Riportando questi valori in tabella si ha:
Esercizio 2
d
r 0 1 2 3 4 5
1
(0,*,*)
0
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
(1,*,*)
6
2
(0,0,*)
0
(1,0,*)
6
(0,1,*)
10
(1,1,*)
16
(1,1,*)
16
(1,1,*)
16
3
(0,0,0)
0
(1,0,0)
6
(0,1,0)
10
(1,1,0)
16
(1,0,1)
18
(0,1,1)
22
Pertanto, la soluzione ottima è x
*= (0, 1, 1) di valore 22.
Esercizio 2
Esercizio 3
Dato il seguente problema di Knapsack 0-1
max 22x1 + 8x2 – 12x3 + 16x4 7x1 + 3x2 – 4x3 + 5x4 < 10
x ∈ {0,1}4
risolvere il problema tramite l’algoritmo di Branch-and-Bound.
Esercizio 3
Poiché la variabile x
3ha coefficiente negativo, applichiamo il seguente cambio di variabile x'
3= 1 – x
3per ricondurci ad un problema di Knapsack 0-1 in cui i coefficienti del vincolo e della funzione obiettivo sono tutti non negativi.
Il problema diventa
– 12 + max 22x
1+ 8x
2+ x'
3+ 16x
47x
1+ 3x
2+ 4x'
3+ 5x
4< 14
x ∈ {0,1}
4Possiamo momentaneamente trascurare la costante –12 ricordandoci
di aggiungerla al valore finale della soluzione di branch & bound.
Esercizio 3
Riordiniamo ora gli oggetti in modo tale che
p
1a
1≥ p
2a
2≥.. .≥ p
na
np1/a1 = 22/7 = 3,14 ⇒ y2 p2/a2 = 8/3 = 2,66 ⇒ y4 p3/a3 = 12/4 = 3 ⇒ y3 p4/a4 = 16/5 = 3,2 ⇒ y1
Riscriviamo ora il problema nelle nuove variabili yi
max 16y1 + 22y2 + 12y3 + 8y4 5y1 + 7y2 + 4y3 + 3y4 < 14
y ∈ {0,1}4
Esercizio 3
Sia P0 il rilassamento lineare associato al sottoproblema S0 del nodo radice
max 16y1 + 22y2 + 12y3 + 8y4 5y1 + 7y2 + 4y3 + 3y4 < 14 0 < yi < 1, i = 1, …,4
Calcoliamo la soluzione associata all’upper bound utilizzando l’algoritmo per il Knapsack continuo:
y0UB = [1, 1, 1/2, 0] ⇒ z0UB = 44
Per il lower bound, consideriamo una soluzione ammissibile data da x0LB = [1, 1, 0, 0] ⇒ z0LB = 38 = zLB.
S0
z0UB = 44
z0LB = 38 = zLB
Generiamo i due sottoproblemi S1 t.c. y3 = 0
S2 t.c. y3 = 1
S1 S2
y3 = 1 y3 = 0
Sia P1 il rilassamento lineare associato al sottoproblema S1 (y3 = 0) max 16y1 + 22y2 + 8x4
5y1 + 7y2 + 3y4 < 14 0 < yi < 1, i = 1, …,4 y1UB = [1, 1, 0, 2/3] ⇒ z1UB = 43,33 z1LB = [1, 1, 0, 0] ⇒ z1LB = 38.
Esercizio 3
S0
S1 S2
z1UB = 43.33 z1LB = 38
y3 = 0 y3 = 1
z0UB = 44
z0LB = 38 = zLB
Sia P2 il rilassamento lineare associato al sottoproblema S2 (y3 = 1)
max 16y1 + 22y2 + 12 + 8y4
5y1 + 7y2 + 3y4 < 10 0 < yi < 1, i = 1, …,4
y2UB = [1, 5/7, 1, 0] ⇒ z2UB = 43,7 y2LB = [1, 0, 1, 0] ⇒ z2LB = 28.
z2UB = 43.7 z2LB = 28
Esercizio 3
Da S1, generiamo i due sottoproblemi S3 t.c. y4 = 0
S4 t.c. y4 = 1
S3 S4
Sia P3 il rilassamento lineare associato al sottoproblema S3 (y3 = y4 = 0) max 16y1 + 22y2
5y1 + 7y2 < 14
0 < yi < 1, i = 1, …,4 y3UB = [1, 1, 0, 0] ⇒ z3UB = 38
x3LB = [1, 1, 0, 0] ⇒ z3LB = 38.
z3UB = 38 z3LB = 38
Esercizio 3
S0
S1 S2
y3 = 0 y3 = 1
y4 = 0 y4 = 1
Chiudiamo il nodo per ottimalità.
×
OPT
z1UB = 43.33 z1LB = 38
z0UB = 44
z0LB = 38 = zLB
z2UB = 43.7 z2LB = 28
Da S1, generiamo i due sottoproblemi S3 t.c. x4 = 0
S4 t.c. x4 = 1
Sia P4 il rilassamento lineare associato al sottoproblema S4 (y3 = 0, y4=1) max 16y1 + 22y2 + 8
5y1 + 7y2 < 11
0 < yi < 1, i = 1, …,5 y4UB = [1, 6/7, 0, 1] ⇒ z4UB = 42,8 y4LB = [1, 0, 0, 1] ⇒ z4LB = 24.
Esercizio 3
S3 S4
S0
S1 S2
y3 = 0 y3 = 1
y4 = 0 y4 = 1
×
OPT
z3UB = 38 z3LB = 38
z1UB = 43.33 z1LB = 38
z0UB = 44
z0LB = 38 = zLB
z2UB = 43.7 z2LB = 28
z4UB = 42.8 z4LB = 24
Da S2, generiamo i due sottoproblemi S5 t.c. y2 = 0
S6 t.c. y2 = 1
Sia P5 il rilassamento lineare associato al sottoproblema S5 (y3 = 1, y2=0) max 16y1 + 12 + 8y4
5y1 + 3y4 < 10
0 < yi < 1, i = 1, …,4 y5UB = [1, 0, 1, 1] ⇒ z5UB = 36
y5LB = [1, 0, 1, 1] ⇒ z5LB = 36.
Esercizio 3
Chiudiamo il nodo per ottimalità o per bound.
S5 S6
y2 = 0 y2 = 1
z5UB = 36 z5LB = 36
S3 S4
S0
S1 S2
y3 = 0 y3 = 1
y4 = 0 y4 = 1
×
OPT
×
OPT/Boundz3UB = 38 z3LB = 38
z1UB = 43.33 z1LB = 38
z0UB = 44
z0LB = 38 = zLB
z2UB = 43.7 z2LB = 28
z4UB = 42.8 z4LB = 24
Da S2, generiamo i due sottoproblemi S5 t.c. x1 = 0
S6 t.c. x1 = 1
Sia P6 il rilassamento lineare associato al sottoproblema S6 (y3 = 0, y2=1) max 16y1 + 22 + 12 + 8y4
5y1+3y4 < 3 0 < yi < 1, i = 1, …,4
y6UB = [3/5, 1, 1, 0] ⇒ z6UB = 43,6 y6LB = [0, 1, 1, 1] ⇒ z6LB = 34.
Esercizio 3
S5 S6
y2 = 0 y2 = 1
z5UB = 36 z5LB = 36
S3 S4
S0
S1 S2
y3 = 0 y3 = 1
y4 = 0 y4 = 1
×
OPT
×
z3UB = 38 z3LB = 38
z1UB = 43.33 z1LB = 38
z0UB = 44
z0LB = 38 = zLB
z2UB = 43.7 z2LB = 28
z4UB = 42.8 z4LB = 24
z6UB = 43,6 z5LB = 34
OPT/Bound
Da S4, generiamo i due sottoproblemi S7 t.c. y2 = 0
S8 t.c. y2 = 1
S7 S8
y2 = 0 y2 = 1
S7
Esercizio 3
S5 S6
y2 = 0 y2 = 1
z5UB = 36 z5LB = 36
S3 S4
S0
S1 S2
y3 = 0 y3 = 1
y4 = 0 y4 = 1
×
OPT
×
z3UB = 38 z3LB = 38
z1UB = 43.33 z1LB = 38
z0UB = 44
z0LB = 38 = zLB
z2UB = 43.7 z2LB = 28
z4UB = 42.8 z4LB = 24
OPT/Bound
Sia P7 il rilassamento lineare associato al sottoproblema S7 (y3=y2=0,y4=1) max 16y1 + 8
5y1 < 11
0 < yi < 1, i = 1, …,4 y7UB = [1, 0,0, 1] ⇒ z7UB = 20
y7LB = [1, 0, 0, 1] ⇒ z7LB = 20
Poiché y7UB è intera il nodo S7 può essere chiuso per ottimalità.
Osserviamo che questo nodo potrebbe anche essere chiuso per bound in quanto z7UB < zLB.
Esercizio 3
Da S4, generiamo i due sottoproblemi S7 t.c. y2 = 0
S8 t.c. y2 = 1
S7 S8
y2 = 0 y2 = 1
S7
Esercizio 3
S5 S6
y2 = 0 y2 = 1
z5UB = 36 z5LB = 36
S3 S4
S0
S1 S2
y3 = 0 y3 = 1
y4 = 0 y4 = 1
×
OPT
×
z3UB = 38 z3LB = 38
z1UB = 43.33 z1LB = 38
z0UB = 44
z0LB = 38 = zLB
z2UB = 43.7 z2LB = 28
z4UB = 42.8 z4LB = 24
×
OPT/Bound
OPT/Bound
z7UB = 20 z7LB = 20