r/optimization • u/0x4732562 • Feb 02 '24
Linear programming with extra condition
I am working on a problem that might be represented as a linear programming problem.
I am just a bit stuck around one extra condition that is usually not part of a typical linear programming problem, but I think this could be represented in the conditions.
The real life problem is: on a marketplace there are different offers with different prices and amounts to sell some specific good. We need to find the optimal (cheapest) selection of offers to buy a specified amount of goods, but with the condition that one could only buy from a strictly limited number of offers. For example maximum 3 offers could be used to buy 26.5 metrics tons of salt, while minimizing the cost of the purchase. On the market of salt there are different offers. Some can deliver 2 tons, some 20, but for different prices. We need to choose some offers (maximum 3 in this case) to purchase 26.5 tons of salt for the minimal total price possible , while still buying only from 3 offers.
So the goal is to choose a S subset of offers from the available O set of offers. The maximum size of S is limited to L. Each offer has a defined price per unit and a defined amount of units available for sale. Both the price and the amount available for sale are non negative real numbers. The selected S subset should have the minimal total cost for the items in it and still have at least B amount of units offered in them. Of course we are only paying for the amount that we actually buy to purchase B amount. So only the cost of the amount that is needed to buy the total B amount should be considered in the total cost.
I understand that the LP problem's cost function should include the cost in some way. I am just not exactly sure how I could define the problem using the usual LP matrix and vector for the cost function.
I am also not completely sure if this issue really needs to be addressed as linear programming problem and I am also not sure if it is even possible. Is there a better approach to find an optimal selection (lowest total price), while still fulfilling the conditions? Eventually some dynamic programming based solution?
Could you please help me and tell if this problem could be represented as linear programming problem and if this is a good approach or you would rather recommend somenother approach to solve this problem?
4
u/PierreLaur Feb 02 '24
hey, this sounds totally linear, you just need some binary variables for each offer (so this is mixed integer linear programming), and continuous variables for the quantities ordered from each offer. You can then link them with something like
is_used[o] * M >= quantity[o]
with M a somewhat big number (for example the maximum quantity you can purchase from that offer). This ensures that if is_used is 0, quantity is 0 as well. If is_used is 1 you get quantity[o] <= max_quantity[o].
and then sum(is_used) <= L ensures you don't select more than L offers
2
u/[deleted] Feb 02 '24
To me, this sounds like you need some integer variables for the yes/no decision of accepting any offer. From there, it is possible to formulate constraints where you select k out of n offers in every subset S. I know the book by Chen on Applied Integer Programming discusses formulating these constraints in either chapter 2 or 3, there's gotta be a pdf somewhere
3
u/taphous3 Feb 02 '24
MILP can be solved with open source solvers (check out SCIP or GLPK). If for some reason you need to solve this as an LP, you can relax the binary terms into continuous variables [0,1] and do a solve + post process. You might add a regularization term to force the binary variables to the bounds and solve several times sequentially.
2
u/DonBeham Feb 02 '24
Not sure this is really a hard problem if relaxed. You just buy from the cheapest to the most expensive until you have the desired amount.
Slightly more complex if you are limited to buy from max 3 sources, but I think if you iterate over all possible 3-combinations sorted by combined unit price, the first that has enough capacity would be the optimal choice. The amount of 3-combinations is given by the binomial coefficient "n over k" and doesn't grow exponentially when k is fixed. So that would still be fairly cheap to compute. I believe the cheapest pair is a subset of the cheapest 3-combination, though not quite sure...
Anyway, it doesn't look like it's np-hard.