2

I am playing around with a great simplex algorithm I have found here: https://github.com/JWally/jsLPSolver/

I have created a jsfiddle where I have set up a model and I solve the problem using the algorithm above. http://jsfiddle.net/Guill84/qds73u0f/

The model is basically a long array of variables and constraints. You can think of it as trying to find the cheapest means of transportation of passengers between different hubs (countries), where each country has a minimum demand for passengers, a maximum supply of passengers, and each connection has a price. I don't care where passengers go, I just want to find the cheapest way to distribute them. To achieve this I use the following minimising objective:

model = {
        "optimize": "cost",
            "opType": "min",
            "constraints": { \\etc... 

I am happy with the model and the answer provided by the algorithm ... but the latter takes a very long time to run (>15 seconds...) Is there any possible way I can speed up the calculation?

Kind regards and thank you. G.

Noobster
  • 1,024
  • 1
  • 13
  • 28

3 Answers3

4

It sounds as though you have a minimum-cost flow problem. There's a reasonable-looking TopCoder tutorial on min-cost flow by Zealint, who covers the cycle-canceling algorithm that would be my first recommendation (assuming that there's no quick optimization that can be done for your LP solver). If that's still too slow, there's a whole literature out there.

Since you're determined to solve this problem with an LP solver, my suggestion would be to write a simpler solver that is fast and greedy but suboptimal and use it as a starting point for the LP by expressing the LP in terms of difference from the starting point.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • Hello David! Yes, this is indeed a minimum-cost flow problem. I am not trying to reinvent the wheel or write the algo from scratch. In fact I wouldn't know how to begin, which is why I am using the simplex above. I like it because I can feed it my variables and constraints directly as an array. Is there a reason why it is taking so long to complete? As a starting point I would like to work on the above before I consider other options! – Noobster Apr 02 '15 at 15:59
  • @Noobster The LP solver you're using is a toy but glancing over it I couldn't see any glaring performance problems. I edited my suggestion into the answer. – David Eisenstat Apr 02 '15 at 16:18
3

@Noobster, I'm glad that someone other than me is getting use out of my simplex library. I went through, looked at it, and was getting around the same runtime as you (10 - 20 seconds). There was a piece of the code that was needlessly transposing array to turn the RHS into a 1d array from a 2d array. With your problem, this killed performance eating up 60ms every time it happened (for your problem, 137 times).

I've corrected this in the repo and am seeing runtimes around 2 seconds. There are probably a ton of code clean up optimizations like this that need to happen but the problem set I built this (http://mathfood.com) for are so small that I never knew this was an issue. Thanks!

For what its worth, I took the simplex algo out of a college textbook and turned it into code; the MILP piece came from wikipedia.

JWally
  • 582
  • 11
  • 18
2

Figured it out. The most expensive piece of the code was the pivoting operation; which it turns out was doing a lot of work to update the matrix by adding 0. Doing a little logic up front to prevent this dropped my run-time down on node from ~12 seconds to ~0.5.

    for (i = 0; i < length; i++) {
        if (i !== row) {
            pivot_row = tbl[i][col];
            for (j = 0; j < width; j++) {


                // No point in doing math if you're just adding
                // Zero to the thing


                if (pivot_row !== 0 && tbl[row][j] !== 0) {
                    tbl[i][j] += -pivot_row * tbl[row][j];
                }
            }
        }
    }
JWally
  • 582
  • 11
  • 18
  • Justin this is truly impressive. I very hope we get to work together someday, I have alot to learn from you. – Noobster Apr 15 '15 at 18:52