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.
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.
So you'd simply do Minimize[function, {variables/constraints}] and then get on with your day.
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.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}}]
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.
I tend to use clp these days but glpk is a good LP solver as well.