Live data from Hacker News

Linear Programming in Python (2023)

slama.dev

41–50 of 56 posts

Re: Linear Programming in Python (2023)

#41
post #35
post #26

Earlier quoted context omitted.

Your response makes me wonder if the idea is not that RL can give more optimal solutions, but perhaps more robust ones? This is giving a ton of charity to the opening post, but fun for me to consider. As you say, given a model, if it is bounded than LP is an obvious route to get optimal. However, models being what they are, there are often times when the prescribed solution is unrealizable for reasons. Reinforcement…

A linear programming (LP) problem is essentially just, for positive integers m and n and real or rational matrices 1 by n c n by 1 x m by n A m by 1 b 1 by 1 z, maximize z = cx subject to Ax = b, x >= 0 That is, an LP is a fully precisely stated problem in math. There is no randomness , ambiguity, flexibility, uncertainty, etc. Now when move to the real world and have a problem, e.g., in mixing animal feed, cracking…

Right, I get what an LP is. As I said in the other thread, I'm mainly trying to use charity to the opening post to find a way it can be meaningful. With an idea that mayhap you could use RL to try and explore a solution space that you don't know the penalty for non-compliance to a solution.

Specifically, use LP to craft a plan in an iteration of scenarios that is varying a cost function through iterations. I suspect I'm fumbling in the multi period planning realm more so than the RL one, here.

I further suspect, though, that it is more "if you can formalize your problem such that LP solves it, use LP." That is, the opening post was largely just nonsensical?

Re: Linear Programming in Python (2023)

#42
post #3
post #2

I have often started modeling problems with pulp. As the problem size gets bigger, pulp will fail to work —- that is, pulp handles writing out large problems to disk (nlogn complexity) worse than gurobi handles reading and solving them (expn complexity). Then I end up writing out the problems into text files on python „by hand“, which is very cumbersome. I wish python and it’s libraries wasn’t so inefficient.

There is also https://www.python-mip.com/ which claims to be more efficient, but it supports fewer solvers than pulp, and I have not had opportunity to test it. Would anyone here have experience on how well their efficiency claims hold in practice?

I've used python-mip extensively, though only on problems with up to ~10k variables, and only using basic LP features (no advanced solver callbacks).

python-mip is substantially faster than cvxpy, pulp, and pyomo for constructing _and solving_ a very simple LP with 10k variables. It's on par even with cylp using vectorisation (maybe I haven't used cylp as well as I could (either way cylp is dreadful to use)). I say _and solving_ because for my use case it's the time to construct and solve relatively simple & small problems (like, But keep in mind these kinds of benchmarks can depend a lot on how you're using the library. Like, are you spending almost all time actually solving the model? Then it probably doesn't matter what library you use to construct the model.

python-mip in general is pretty nice. It supports advanced solver features if you're into that sorta stuff. But their typing is a bit whacky (no py.typed file, use numbers.Real for typing which makes type checking a bit whacky). And it relies on a funky pre-compiled CBC binary (I had to compile it from scratch at a very specific commit to get it working on M1). python-mip creates/modifies variables in the underlying solver library (CBC/Gurobi) as you create/modify variables through the python interface (I believe this is an uncommon approach, but good). The internal data model of variables & linear expressions is quite clean and tidy.

Overall would recommend python-mip if you just need to solve MILP problems.

From my scrappy benchmark constructing and solving a very simple LP with 10k variables:

'benchmark_python_mip' took: 40.33 ms

'benchmark_cvxpy' took: 738.11 ms

'benchmark_pulp' took: 152.68 ms

'benchmark_pyomo' took: 191.66 ms

'benchmark_cylp' took: 38.91 ms

Re: Linear Programming in Python (2023)

#43
post #19

Earlier quoted context omitted.

"reinforcement learning offers objectively better optimization" I don't follow. Linear programming (with the simplex method) finds the optimal solution set for any linear program eventually. How can reinforcement learning be "better" apart from efficiency?

It can't. They're talking out of their backend.

Speaking from years of experience comparing RL results to those of LP done with tools like Gurobi on industrial operations and supply chain optimization problems while at my former startup, which was acquired two years ago, so I am not shilling anything.

There are many problems that are hard to represent such that LP can compute them. That process alone is long, and hard to adjust even though clients need to make those adjustments.

These problems include multi-echelon inventory optimizations, and load-balancing across multiple assembly lines in factories when using hundreds of delivery vehicles, among others. These are properly multi-agent problems that RL solves well if given a valid simulation of the environment. LP often cannot solve it at all.

In cases where we went head to head with well known LP software vendors, we were able to move the Pareto frontier by double digit percentages.

We open sourced our software here after the acquihire. Depends on Gym among other repositories. Does something similar to Microsoft’s Bonsai. All are welcome to use.

https://github.com/PathmindAI

Re: Linear Programming in Python (2023)

#44
post #43
post #19

Earlier quoted context omitted.

It can't. They're talking out of their backend.

Speaking from years of experience comparing RL results to those of LP done with tools like Gurobi on industrial operations and supply chain optimization problems while at my former startup, which was acquired two years ago, so I am not shilling anything. There are many problems that are hard to represent such that LP can compute them. That process alone is long, and hard to adjust even though clients need to make tho…

In operations and supply chains it is not hard to find *A* solution. Operators literally make these decisions manually every day without the need for any computer.

The main challenge is to optimize these decisions. With optimality guarantees. Consistently. Day in, day out, with various uncertainties.

That is what math optimization has been doing for decades.

And no, RL is nowhere to be seen in production, except from some consulting projects that are abandoned.

Re: Linear Programming in Python (2023)

#45
post #42
post #3

Earlier quoted context omitted.

There is also https://www.python-mip.com/ which claims to be more efficient, but it supports fewer solvers than pulp, and I have not had opportunity to test it. Would anyone here have experience on how well their efficiency claims hold in practice?

I've used python-mip extensively, though only on problems with up to ~10k variables, and only using basic LP features (no advanced solver callbacks). python-mip is substantially faster than cvxpy, pulp, and pyomo for constructing _and solving_ a very simple LP with 10k variables. It's on par even with cylp using vectorisation (maybe I haven't used cylp as well as I could (either way cylp is dreadful to use)). I say _…

I tend to build mip problems that take days to make a good approximation (using gurobi). Those can not be modeled using pulp, because I run out of memory.

I’ll check out python-mip, thanks for the tip.

Re: Linear Programming in Python (2023)

#46
post #26
post #21

Earlier quoted context omitted.

I studied LP (linear programming) from some of the most advanced researchers in optimization, including LP, in the world, led the class in a Ph.D. qualifying exam mostly on LP, wrote a Ph.D. dissertation in optimization, taught linear programing in college and graduate school, and applied it in business. From that background, given a problem in LP, there are three cases, the problem is (1) infeasible, (2) feasible an…

Your response makes me wonder if the idea is not that RL can give more optimal solutions, but perhaps more robust ones? This is giving a ton of charity to the opening post, but fun for me to consider. As you say, given a model, if it is bounded than LP is an obvious route to get optimal. However, models being what they are, there are often times when the prescribed solution is unrealizable for reasons. Reinforcement…

> Your response makes me wonder if the idea is not that RL can give more optimal solutions, but perhaps more robust ones?

LP (linear programming), given an optimal solution, has some sensitivity analysis that nicely addresses robustness.

In the LP find x to solve

maximize z = cx

subject to Ax = b, x >= 0,

small changes in c, A, and b can result in large changes in x (below I explain some of how). But, still, due to some continuity in the linear algebra, the changes in z should (I am avoiding doing the derivations) be small, and continuing to use the optimal x from before the changes should (again, some derivations are needed) be not far off (omitting details).

The feasible region is much like a diamond with flat sides and sharp points (the extreme points).

Uh, let set F be the feasible region, i.e.,

F = {x | Ax = b, x >= 0}

Then the feasible region is convex, i.e., for x_1 and x_2 in the feasible region and t in [0,1],

tx_1 + (1 - t)x_2

(intuitively the points on the line between the two points x_1 and x_2) is also in the feasible region F.

So, easily there can be two extreme points x_1 and x_2 in F such that

z = cx_1 = cx_2

and both are optimal.

Then all points on the line between x_1 and x_2 will be feasible and optimal. So, now have infinitely many optimal points.

And it can be that x_1 is the only optimal point but after some small changes both x_1 and x_2 are optimal. And x_2 can be a long way from x_1.

Sooooo, the small changes resulted in a big change in the optimal points. And maybe after the change, x_2 is optimal but x_1 no longer is.

In this case, maybe LP does not look "robust", but that's usually not a big problem since after the small changes, likely in practice can still get by using the old optimal x_1.

I.e., just by continuity, the "small changes" will keep the changes in the feasible region F and the optimal value z small.

Ah, here is the good news: If raising 40,000 chickens (my father in law used to do that) and there is a small change in the prices of some of the feeds, after the small change might change suppliers (big change in x) for a savings in prices. Intuitively, the changes just showed that you are not very dependent on your old suppliers but have some alternatives that involve some big changes in x (maybe drop one supplier for another one) for about the same z. That big change in x looks not "robust" but is good news, right?

Re: Linear Programming in Python (2023)

#47
post #46
post #26

Earlier quoted context omitted.

Your response makes me wonder if the idea is not that RL can give more optimal solutions, but perhaps more robust ones? This is giving a ton of charity to the opening post, but fun for me to consider. As you say, given a model, if it is bounded than LP is an obvious route to get optimal. However, models being what they are, there are often times when the prescribed solution is unrealizable for reasons. Reinforcement…

> Your response makes me wonder if the idea is not that RL can give more optimal solutions, but perhaps more robust ones? LP (linear programming), given an optimal solution, has some sensitivity analysis that nicely addresses robustness . In the LP find x to solve maximize z = cx subject to Ax = b, x >= 0, small changes in c, A, and b can result in large changes in x (below I explain some of how). But, still, due to…

Ish? Sensitivity analysis measures, by definition, sensitivity to inputs. I suppose you can model it so that some extrinsic factors are themselves written as inputs, but I haven't seen this explored with the idea of robustness, much. (Granted I may just be ignorant in that area.)

What you are pointing to, seems to be degrees of freedom on an input? The more options you have in an input, the better. Robust systems also have to deal with the unknown adversaries of the system that will work against it, though? Not just, "we were sensitive to the costs of feed changing, but there is also now an avian flu going around and we have to kill our flock." A simulation is easy to imagine that does this and forces you to see what is next. Is that common in LP formulations?

Re: Linear Programming in Python (2023)

#48
post #43

Earlier quoted context omitted.

Speaking from years of experience comparing RL results to those of LP done with tools like Gurobi on industrial operations and supply chain optimization problems while at my former startup, which was acquired two years ago, so I am not shilling anything. There are many problems that are hard to represent such that LP can compute them. That process alone is long, and hard to adjust even though clients need to make tho…

In operations and supply chains it is not hard to find *A* solution. Operators literally make these decisions manually every day without the need for any computer. The main challenge is to optimize these decisions. With optimality guarantees. Consistently. Day in, day out, with various uncertainties. That is what math optimization has been doing for decades. And no, RL is nowhere to be seen in production, except from…

Our work was precisely in optimization over massive numbers of possible scenarios amid multi-dimensional uncertainties. Again, significantly outperforms LP. Not surprised that you are an advocate of math optimization. Most people working on optimization in industrial operations and supply chains have an OR background, and this sort of conservatism is common. But there are lots of situations that LP can't optimize while RL can.

Re: Linear Programming in Python (2023)

#49
post #37
post #23

Earlier quoted context omitted.

If you have a working LP solution without any glaring compromises in the problem formulation, then I'm not sure why one would want to throw out a perfectly good working solution... algorithms are the means, not the ends :)

I'm pretty sure the main reason for throwing out old tools that still function in favor of the new hotness is developer boredom.

This is why my teams always provide explicit opportunities and spaces for professional development. People should have the opportunity to stretch and grow; if you don't provide those opportunities explicitly, then your most motivated employees will find them implicitly. And you can't afford to not keep your most motivated employees, so you'll end up paying with tech debt instead of Engineeer prof development time.

Re: Linear Programming in Python (2023)

#50
post #48

Earlier quoted context omitted.

In operations and supply chains it is not hard to find *A* solution. Operators literally make these decisions manually every day without the need for any computer. The main challenge is to optimize these decisions. With optimality guarantees. Consistently. Day in, day out, with various uncertainties. That is what math optimization has been doing for decades. And no, RL is nowhere to be seen in production, except from…

Our work was precisely in optimization over massive numbers of possible scenarios amid multi-dimensional uncertainties. Again, significantly outperforms LP. Not surprised that you are an advocate of math optimization. Most people working on optimization in industrial operations and supply chains have an OR background, and this sort of conservatism is common. But there are lots of situations that LP can't optimize whi…

"Again, significantly outperforms LP."

No, this is mathematically impossible. You cannot out-optimize a mathematical global optimization solver. Whether that is LP, MILP, or NLP. Exactly because they provide a certificate of optimality.

It is like stating that you found a non-negative number that is smaller than 0. It’s not progressive. It’s flat out wrong.

"But there are lots of situations that LP can't optimize while RL can"

I will be satisfied if you just show us 1.

Stay in your lane, and state that you built a fast heuristic that works (finds good solutions but not optimal) in the problems you tried. Nobody can dispute this. But don't make claims you cannot support.

Post reply on HN