Hacker News new | past | comments | ask | show | jobs | submit login

Why would it be better in one of those languages?

I'd be very surprised if it was as easy to set up the problem in R or Macsyma. In those languages, you'd probably need to write loops to generate your constraints (but maybe I'm wrong, I'm no R-expert).

But I'd love to see code proving me wrong.




Well, my experience is in mathematica. Which has operations research functions built right in. Point being, there are systems where this is already a solve problem, where you can get your answer in a minute or two.

So you'd simply do Minimize[function, {variables/constraints}] and then get on with your day.


It's true, glpk is not part of the haskell standard library. But solving a simple LP problem is easily done in a minute or two:

    lp = execLPM $ do	setDirection Max
			setObjective objFun
			leqTo (varSum ["x1", "x2", "x3"]) 100
			leqTo (10 *^ var "x1" ^+^ 4 *& "x2" ^+^ 5 *^ var "x3") 600
			leqTo (linCombination [(2, "x1"), (2, "x2"), (6, "x3")]) 300
			varGeq "x1" 0
			varBds "x2" 0 50
			varGeq "x3" 0
			setVarKind "x1" IntVar
			setVarKind "x2" ContVar

    main = print =<< glpSolveVars mipDefaults lp
The code in the blog post is just longer and more complicated because it's generating multiple constraints from code.

The only reason my code doesn't look as simple as the above is because it's doing a lot more. For example, this line generates 9 constraints:

    reservationConstraints loadPattern = [ linCombination [ (1.0, "reservation_" ++ k),
                                       (-1.0, "reserved_" ++ k ++ "_" ++ p) ], 0.0  |
                                       p <- (Map.keys loadPattern),
                                       k <- reservationTypes ]
E.g.:

    reservation_heavy - reserved_heavy_morning <= 0
    reservation_heavy - reserved_heavy_afternoon <= 0
    reservation_heavy - reserved_heavy_evening <= 0
    reservation_medium - reserved_medium_evening <= 0
    ....
If Mathematica is so easy, I'd love to see a significantly simpler version of the same problem written in it. But I have absolutely no idea how I'd write that simpler version.


Maximize[...], Minimize[...], LinearProgramming[...] thats it... But Mathematica costs money.

Example:

Minimize[{x + 2 y, -5 x + y == 7 && x + y >= 26 && x >= 3 && y >= 4}, {x, y}] LinearProgramming[{1, 2}, {{-5, 1}, {1, 1}}, {{7, 0}, {26, 1}}, {{3, Infinity}, {4, Infinity}}]


The vast majority of the code I wrote is setting up the problem (which has 15 vars and 27 constraints), not solving it. I.e., those list comprehensions are a way of writing 9 constraints on a single line.

To demonstrate mathematica is easier, you need to demonstrate that it's similarly easy to translate the problem from a verbal specification to an LP problem. Solving LP is the easy part.


There are packages in R that use GLPK api calls so it would be made simpler. See Rglpk http://cran.r-project.org/web/packages/Rglpk/index.html. But if one is originally coding in a particular langauge (in this case Haskell) then I would think coding within that language would be easier.


The blog post demonstrates a package in Haskell that uses GLPK API calls to make things simpler.


I do not know about the other languages, but in Octave there's a function called glpk that you can use and it accepts problems in the standard form.

I tend to use clp these days but glpk is a good LP solver as well.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: