Monday, May 11, 2009

Shoulder Joint Pain When Pregnant

Dynamic Programming Knapsack Problem with

Let n objects fractional weights pi and bi benefits. The maximum you can take the backpack is C. We want to fill the bag

with objects, so as to maximize profit. Data * n objects with weights pi and bi benefits associated with each object.

* You can not split the objects (they are caught or not caught).
* We define a more general problem in terms of number of objects and the capacity C

bag: backpack (k, l, C)
* Solve the problem is to obtain
: backpack (1, n, C)
Resolution by Dynamic Programming To
solve the knapsack problem
need to perform certain actions:
* see that it meets the principle of optimality of Bellman.
* Search
recurrent equations for
problem. * Build a table of values \u200b\u200bfrom the equations.
Bellman optimality principle

* Any subsequence of optimal decisions in a sequence of decisions
solve a problem must also be optimal with respect to the subproblem is solved. * Be y1, ..., n an optimal sequence of 0-1 values \u200b\u200bfor x1, ..., xn.
or if y1 = 0, y2, ..., n form an optimal sequence for the knapsack problem
(2, n, C). or if y1 = 1, y2, ..., n form an optimal sequence for the knapsack problem

(2, n, C - p1).
* Proof: If there is a better solution for the corresponding
problem, then it is better for the backpack problem

(1, n, C), contrary to the hypothesis. The same is true at any stage decision.
recurrent equations for the problem

a) Equation forward
* Suppose gi (C) is the accumulated profit for the optimal solution to the knapsack problem
(j, n, C), then:
gj (C) = max {gj +1 (C), gj +1 (C-pj) + bj}
whose meaning: gj +1 (C) -> no object is caught gj j +1 (C-pj) + bj -> you take the object j

* The trivial case is when j it n +1, and in this case: gn +1 (C) = 0 Then

* calculating backpack (1, n, C) is reduced to the application of equations:

gj (C) = max {gj +1 (C), gj +1 (C-pj) + bj} if j gj n +1 (C) = 0 if j = n +1




b) Equation
back

* Suppose gj (C) is the accumulated profit for the optimal solution to the knapsack problem

(1, j, C), then:

gj (C) = max {gj-1 (C), gj-1 (C-pj) + bj}
whose meaning:
gj-1 (C) -> no object is caught
j gj-1 (C-pj) + bj -> you take the object j <>
* The trivial case is when j is 0, and in this case:

g0 (C) = 0

* After calculating of backpack (1, n, C) is reduced to the implementation of the equations:

gj (C) = max {gj-1 (C), gj-1 (C-pj) + bj} if j 0 gj (C) = 0 if j = 0



Implementation

* If we consider the equation backwards, we will be implementing:


backpack proc (p, b: array [1 .. n] nat, cap: nat, g: array [0 .. n, 0 .. cap] nat)
var c, j: nat;
for c = 0 to cap to g [0, c] = 0 fto;
for j = 1 to n do g [j, 0] = 0 fto;
for j = 1 to n do for c = 1 to make cap if c less than p [j] then
g [j, c] = g [j-1, c]; Otherwise
<> if g [j-1, c] greater than g [j-1, cp [j]] + b [j] then
g [j, c] = g [j-1, c]; otherwise

g [j, c] = g [j-1, cp [j]] + b [j]; fsi; fsi;
fto, fto
;
fproc;
 



function Java knapsack problem




public int [] [] Backpack (int [] weights, int [] benefits, int capacity) {

/ / Create the array of returns
int [] [] matriz_mochila = new int [pesos.length +1] [capacity +1];

/ / Fill the 1 st row of zeros
for(int i = 0; i
matriz_mochila[0][i] = 0;

//Rellenamos la 1ยช columna de ceros
for(int i = 0; i
matriz_mochila[i][0] = 0;

for(int j = 1; j for(int c = 1; c
if(c
matriz_mochila[j][c] = matriz_mochila[j-1][c];
}else{
               if(matriz_mochila[j-1][c] > matriz_mochila[j-1][c-pesos[j-1]]+ beneficios[j-1]){ 
matriz_mochila[j][c] = matriz_mochila[j-1][c];
}else{
matriz_mochila[j][c] = matriz_mochila[j-1][c-pesos[j-1]]+beneficios[j-1];

}}}
matriz_mochila
return;} <= capacidad; i++)



<= pesos.length; i++)

0 comments:

Post a Comment