<div>
<p>Today I’m extraordinarily pleased because today I solved an <em>actuall real world problem</em> using R. Sure, I’ve solved many esoteric <em>statistical</em> problems with R, but I’m not sure if any of those solutions have escaped the digital world and made some impact <em>ex silico</em>. </p>
<p>It is now summer and in Sweden that means that many people tend to overhaul and rebuild their wooden decks as you need somewhere to sit during those precious few weeks of +20°C (70° F) weather. And so, we also decided to rebuild our algae ridden, half-rotten deck and everything went well until we got to the point where we had to construct the last steps leading into the house. As we had been slightly sloppy when buying planks we only had five left, and when naïvely measuring out the lengths we needed it seemed that the planks were not long enough. Now the problem was this: Was there some way we could saw the planks into the lengths we needed or did we have to go all the way to the lumber yard to get more planks?</p>
<p>These were the planks we had (in centimeters):</p>
<pre><code><div><pre>planks_we_have <span>&lt;-</span> c(<span>120</span>, <span>137</span>, <span>220</span>, <span>420</span>, <span>480</span>)
</pre>
</div>
<p></p>
<p><img alt="" src="http://i2.wp.com/www.sumsar.net/images/posts/2016-06-12-how-to-cut-your-planks-with-r/planks_before.jpeg?w=456" data-recalc-dims="1"></p>
<p></p>
<p>And these were the planks lengths we wanted (again in cm):</p>
<pre><code><div><pre>planks_we_want <span>&lt;-</span> c(<span>19</span>, <span>19</span>, <span>19</span>, <span>19</span>, <span>79</span>, <span>79</span>, <span>79</span>, <span>103</span>, <span>103</span>,
                    <span>103</span>, <span>135</span>, <span>135</span>, <span>135</span>, <span>135</span>, <span>160</span>)
</pre>
</div>
<p></p>
<p>If you just naïvely saw the smallest planks into the smallest plank lengths you’ll end up sawing the following: </p>
<pre><code><div><pre><span>120</span> <span>-></span> <span>19</span>, <span>19</span>, <span>19</span>, <span>19</span>
<span>137</span> <span>-></span> <span>79</span>
<span>220</span> <span>-></span> <span>79</span>, <span>79</span>
<span>420</span> <span>-></span> <span>103</span>, <span>103</span>, <span>103</span>
<span>480</span> <span>-></span> <span>135</span>, <span>135</span>, <span>135</span>
</pre>
</div>
<p></p>
<p>But using this “algorithm” we end up lacking material for the 135 cm and the 160 cm plank! However, it <em>could</em> be the case that if we just saw the plank lengths in a smarter way the planks we have would suffice. At this point I could have exclaimed “Ah, but isn’t this problem just a special case of the multiple knapsack problem?! Finally my course in Algorithm theory will pay off!” (but I really didn’t).</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Knapsack_problem">The knapsack problem</a> is a famous problem in computer science where the objective is to find the combination of items that has the highest total value under the constraint that their total weight is less than a given weight. My mental image of this problem is that of a thief trying to stuff a knapsack with the most valuable goods under the constraint that s/he can only carry a certain weight. The multiple knapsack problem is just the generalization where the are more than one knapsack. In our case each plank can be seen as a “knapsack” where each plank length is an “item”” we can allocate to a plank. If we set the value of each plank length to its actual length (so the 79 cm plank length is worth 79), and solve the multiple knapsack problem, we’ll end up getting the most sawed plank pieces possible given the material we have!</p>
<p>Now, we could code up a brute force solution in R but, as we wanted the deck built sooner than later, I first did a search on CRAN and was happy to find the <a target="_blank" href="https://cran.r-project.org/package=adagio">adagio package</a> which contains “Discrete and Global Optimization Routines”. Among these we find the <code>mknapsack</code> function which given vectors of item values, item weights and knapsack capacities solves the multiple knapsack problem. It returns a list containing the vector <code>ksack</code> that indicates what knapsack each item should go into. Here is now the R code to solve our plank sawing problem:</p>
<pre><code><div><pre>library(adagio)
<span># mknapsack calling signature is: mknapsack(values, weights, capacities)</span>
solution <span>&lt;-</span> mknapsack(planks_we_want, planks_we_want <span>+</span> <span>1</span>, planks_we_have)
<span># Above I added +1 cm to each length to compensate for the loss when sawing.</span>
solution<span>$</span>ksack
</pre>
</div>
<p></p>
<pre><code><div><pre><span>## [1] 1 4 4 1 1 3 4 5 5 5 5 4 2 3 4</span>
</pre>
</div>
<p></p>
<pre><code><div><pre><span># That is, cut plank length 1 from plank 1, plank length 2 from plank 4, etc.</span>

<span># Now pretty printing what to cut so that we don't make mistakes...</span>
assignment <span>&lt;-</span> data.frame(
  cut_this <span>=</span> planks_we_have[solution<span>$</span>ksack],
  into_this <span>=</span> planks_we_want)
t(assignment[order(assignment[,<span>1</span>]), ])
</pre>
</div>
<p></p>
<pre><code><div><pre><span>## 1 4 5 13 6 14 2 3 7 12 15 8 9 10 11</span>
<span>## cut_this 120 120 120 137 220 220 420 420 420 420 420 480 480 480 480</span>
<span>## into_this 19 19 79 135 79 135 19 19 79 135 160 103 103 103 135</span>
</pre>
</div>
<p></p>
<p>Tada! Turns out our existing planks were long enough to saw into the pieces we wanted after all. I guess we could have figured this out by ourselves, but thanks to the <a target="_blank" href="https://cran.r-project.org/package=adagio">adagio package</a> the R solution was pretty painless. Here are the planks <em>post saw</em>:</p>
<p><img alt="" src="http://i0.wp.com/www.sumsar.net/images/posts/2016-06-12-how-to-cut-your-planks-with-r/planks_after.jpeg?w=456" data-recalc-dims="1"></p>
<p>And here is the final result:</p>
<p><img alt="" src="http://i0.wp.com/www.sumsar.net/images/posts/2016-06-12-how-to-cut-your-planks-with-r/finished_result.jpeg?w=456" data-recalc-dims="1"></p>
<p>So, I guess it was a good thing that I studied that algorithms course and that I took a break from C++ to learn R. Now I’m just waiting for the R-package that does all the sawing and hammering for you…</p>
<p></p>

<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/how-to-cut-your-planks-with-r/"></div></div><div><a data-url="http://www.r-bloggers.com/how-to-cut-your-planks-with-r/" data-counturl="http://www.r-bloggers.com/how-to-cut-your-planks-with-r/" data-count="vertical" data-text="How to Cut Your Planks with R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>

<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.sumsar.net/blog/2016/06/how-to-cut-your-planks-with-r/"> Publishable Stuff</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/idkYgqVSckk"></a></div></code></pre></code></pre></code></pre></code></pre></code></pre></code></pre></code></pre></div>