Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This neat approach showed up recently as an answer to a FiveThirtyEight puzzle and of course I couldn’t help but throw it at dplyr
as soon as I could. Turns out that’s not a terrible idea. The question posed is
optimise
200a + 100b + 50c + 25d
under the constraints
400a + 400b + 150c + 50d ≤ 1000,
b ≤ a,
a ≤ 1,
c ≤ 8,
d ≤ 4,and (a,b,c,d) all non-negative integers.
Leaving aside any interpretations of wording of the original question (let’s just start with trying to solve this system of inequalities) the solution provided used 4 nested loops, which can definitely be avoided.
My approach was to create all possible combinations of the 4 variables (within the given constraints), filter out the ones that don’t meet the constraint criteria, then sort by the evaluating expression to find which one does best.
Not Found
I’m not suggesting that this is by any means always the best approach, but when the phase-space of possible solutions is so low (especially combinations of small integers) then this is pretty tidy (technically a single dplyr
chain).
Alternatively, one could set this up as an equation and use a linear solver. In that case, we want to optimise
subject to the constraints
where
Of course, the constraint that
Programming this is fairly straightforward, even with the constraint that these are integer solutions. limSolve::linp
is made for exactly these types of problems.
Not Found
which results in the same answer as our manual brute-force search.
One last thing to try is to plot the solution space and see how it looks. Sounds like a good opportunity to try out plotly.
Since this is technically a 5D plot (4 variables and a value) it’s a little difficult to visualise. I’ve reduced the dimensionality by treating each unique combination of
Going back to the expression that’s being optimised it’s pretty clear why it’s broken down into 4 planes when grouped this way (substitute different values of
Do you have another way to solve this? Drop a line or a link in the comments.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.