Tuesday, July 10, 2018

Linear Programming

Linear programming (LP) is a technique for the optimization of a linear objective function. Integer programming (IP) has the additional constraint of the unknown variables being integers. I vaguely remember these from school, but they are terms I see every so often so I wanted to code something up and get reacquainted.

Many LP solvers exist and a quick search for a Python library turned me onto PuLP. They already had a Sudoku example, which is what I wanted to do, so I've turned that into a Jupyter notebook (getting one of these embedded in a post is something else I've been wanting to try) to make it a bit easier to follow.

LP has polynomial time solutions (see Karmarkar's algorithm), while IP is NP-hard (see branch-and-bound). With Sudoku being a relatively small problem it doesn't seem to matter. These puzzles are easily solvable in under 1 second.

The linear programming I remember was graphing several lines and getting a solution where they intersect. Applying the technique to Sudoku is way more fun. And there are obviously other more useful applications like portfolio optimization, airline crew scheduling, or vehicle routing.

While working on this I also came across Google Optimization Tools which I haven't heard of before. I don't know how popular PuLP is, so it could be another, perhaps better supported, alternative.

UPDATE:

CVXPY also looks promising as demonstrated in Optimization with Python: How to make the most amount of money with the least amount of risk.

Integer vs Linear Programming in Python has a nice comparison of ML to linear programming that will help you decide when to use each.