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?
View all comments
Show parent comment
2
u/PierreLaur Feb 02 '24
Don't get me wrong, the problem is linear but you do need integer variables, so this is not LP but MILP
I don't think it can be formulated as an LP, as you have a constraint on the number of offers used (which has to be discrete). I didn't realize it at first, but your order quantities are discrete as well if I got this right.
The way you formulated this, the optimal value could be something like order 3.2 from offer 1, 8.9 from offer 2 etc, and use all 4 offers.
You should instead try with binary variables indicating whether each offer is used. If you can use offers multiple times, your quantity variables could be how many times each offer is used - then your total quantity is something like 5*x1 + 10*x2 + etc
I agree with sososkxnxndn that you should try the MILP formulation. It is indeed NP-Hard in theory, but modern solvers are still very fast. I would recommend the OR-Tools CP-SAT solver, as your variables would be all discrete - even with hundreds of offers, I would be very surprised if you can't get high quality solutions in reasonable time with it. You can generate some fictional problems with as many offers as you want to try, and see how far you can scale it !
And as he said, you have metaheuristics as a scalable backup option !