Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code a linear programming exercise by hand

I have been doing linear programming problems in my class by graphing them but I would like to know how to write a program for a particular problem to solve it for me. If there are too many variables or constraints I could never do this by graphing.

Example problem, maximize 5x + 3y with constraints:

  • 5x - 2y >= 0
  • x + y <= 7
  • x <= 5
  • x >= 0
  • y >= 0

I graphed this and got a visible region with 3 corners. x=5 y=2 is the optimal point.

How do I turn this into code? I know of the simplex method. And very importantly, will all LP problems be coded in the same structure? Would brute force work?

like image 471
user1676469 Avatar asked Dec 05 '12 06:12

user1676469


1 Answers

There are quite a number of Simplex Implementations that you will find if you search.

In addition to the one mentioned in the comment (Numerical Recipes in C), you can also find:

  1. Google's own Simplex-Solver
  2. Then there's COIN-OR
  3. GNU has its own GLPK
  4. If you want a C++ implementation, this one in Google Code is actually accessible.
  5. There are many implementations in R including the boot package. (In R, you can see the implementation of a function by typing it without the parenthesis.)

To address your other two questions:

  1. Will all LPs be coded the same way? Yes, a generic LP solver can be written to load and solve any LP. (There are industry standard formats for reading LP's like mps and .lp

  2. Would brute force work? Keep in mind that many companies and big organizations spend a long time on fine tuning the solvers. There are LP's that have interesting properties that many solvers will try to exploit. Also, certain computations can be solved in parallel. The algorithm is exponential, so at some large number of variables/constraints, brute force won't work.

Hope that helps.

like image 145
Ram Narasimhan Avatar answered Sep 21 '22 07:09

Ram Narasimhan