Labels

Philosophy (28) Logic (22) Probability (21) Argumentation (20) Ramus (20) Literature (15) Assumptions (14) Handouts (11) Mathematics (11) Metaphors (11) Quotes (11) Matlab (10) Tropes (10) Method (9) Quintilian (9) Induction (8) Modeling (6) Book Reviews (5) Collingwood (5) Physics (5) Problem Structuring (5) Analogies (4) Historiography (4) System (4) Aphorisms (3) Classical (3) Evidence (3) Fallacies (3) People (3) Religion (3) Transitions (3) Decision Making (2) Dynamic Programming (2) GIS (2) Linear Programming (2) Poetry (2) Sayings (2) Toulmin (2) Writing (2) economics (2) Art (1) Bach (1) Policy (1) Regression (1) Risk (1)
Showing posts with label Dynamic Programming. Show all posts
Showing posts with label Dynamic Programming. Show all posts

Review of "Dynamics Programming: a practical introduction" by D.K.Smith

I checked out the book from my university library to learn a little bit about dynamic programming. The book is very attractive because it's very concise (156 pages).  For me the main merit of the book consists in introducing the reader to some of the most common problems tackled with dynamic programming. The main drawback is the notation, which is not straightforward.
Chapter 1 explains clearly what dynamic programming is about and when it is helpful.
Chapter 2 introduces the stagecoach problem and the general shortest route problem.
Chapter 3 explains the knapsack problem and some other problems derived from this one. I have written another post with an elementary code to solve a knapsack problem. See here.
Chapter 4: ....

Knapsack problem in Dynamic Programming - Matlab Code

Suppose we have knapsack whose maximum capacity C is 5 kilograms. We have many expensive items that we want to fit into the knapsack without exceeding the maximum capacity. So, our goal is that the value of the items inside the knapsack is maximum, without exceeding C. Here's a list of the items:

item #i 1 2 3 4
weight 2 3 4 5
benefit 3 4 5 6

The key of this problem lies in 1) determining the space of possible options to fit items inside the knapsack and 2) choose that whose benefit is maximum. For this problem there are three possible actions, determined by the current weight of the knapsack namely:

  1. if the addition of one i-th item makes the sack weight exceed its capacity (wi > w) then leave then remove the i-th item.
  2. if  (wi < w), then there are two options from which we have to choose that which provides the sack contents with a maximum benefit
    1. either remove one item and keep the weight constant
    2. add the i-th item and check what's the remaining allowed weight and the number of items allowed.

These rules (or actions) are expressed as:



Working the previous rules we arrive at the benefit table B:

i/W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7


where B(i,w) is the benefit of having item i that with weight w pounds allowed. 

B(0,0) to B(5,0) = 0; because there's space from 0 to 5, but they weight nothing.
B(0,0) to B(0,4) = 0; because there isn't any space into the knapsack
B(1,1) = 0; item 1 weights 2, but the allowed is 1 -> no item, no benefit
B(1,2) = 3; one item allowed (#1), wi = 2, w(allowed) = 2, , wi<w, so bi = 3;
B(1,3) = 3; one item allowed (#1), wi = 2, w(allowed) = 3, wi<w, so bi=3;
...
B(2,2) = 3; two items allowed (1&2), wi = 2,3; w(allowed) = 2, so w1 fits, therefore bi = b1 = 3;
B(2,3) = 4; two items allowed (any from 1 to 3), wi = 2,3; w(allowed) = 4, so w2 fits, therefore bi = b4 = 4;
...
B(2,5) = 7; two items allowed (any from 1 to 5); options are: 
  1. same weight but with one less item -> B(i-1,w) = B(1,5) = 3
  2. compute remaining weight (w-wi) and try to fit any more items (i-1) -> bi + B(i-1,w-wi) = 4 + B(1,5-3=2) = 4 + 3 = 7
  3. so what's better is the option #2 because it maximizes the Benefit in the knapsack.
There is another version for the recursion equation:


See the (definitely not shortest but-still-works) code below:

Matlab Code:
item = [1 2 3 4];
wis = [2 3 4 5];
bis = [3 4 5 6];
B = zeros(length(item),Cap);

for i = 1 : length(item)
    bi = bis(i); wi = wis(i);
     for wk = 1 : Cap % weight allowed in knapsack
         if i == 1 && wi > wk
            term1 = 0; term2 = 0; term3 = 0;
            B(i,wk) = 0;
         elseif i == 1 && wi == wk
            term1 = 0;
            term2 = bi;
            term3 = 0;
            B(i,wk) = max(term1,term2 + term3);
         elseif i == 1 && wk > wi
            term1 = 0;
            term2 = bi;
            term3 = B(i,wk-wi);
            B(i,wk) = max(term1,term2 + term3);
         elseif i > 1 && wk == wi
            term1 = B(i-1,wk);
            term2 = bi;
            term3 = 0;
            B(i,wk) = max(term1,term2 + term3);
         elseif i > 1 && wk > wi
            term1 = B(i-1,wk);
            term2 = bi;
            term3 = B(i,wk-wi);
            B(i,wk) = max(term1,term2 + term3);
        elseif i > 1 && wi > wk
            term1 = B(i-1,wk);
            term2 = 0;
            term3 = 0;
            B(i,wk) = max(term1,term2 + term3);
        end
    end
end


Resources

The knapsack problem manifests itself in many practical situations. Here's a list of some of them (click) and also here's a technical book (which I haven't read) with many algorithms (click) and another FREE! book (here).  There's also a nice explanation here.