3 ms·
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 setDirect
by yummyfajitas 14y ago
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.
- nukedukem 14y agoMaximize[...], 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}}]
- yummyfajitas 14y agoThe 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.