'SciPy optimize with constraints Python

If I have a problem where I have 75 units of something, and I want to maximise the total selling price I can get for them within the constraints I have, how do I set this up in SciPy (or another better method I am not aware about)?

If I can sell my product to four different locations:

  1. Location A @ $130, but a max of 40 can be sold here
  2. Location B @ $134, but a max of 20 can be sold here
  3. Location C @ $135, but a max of 30 can be sold here
  4. Location D @ $138, but a max of 24 can be sold here

The way I interpret the documentation is that I need to have four constraints:

 1. A - 40 >= 0
 2. B - 20 >= 0
 3. C - 30 >= 0
 4. D - 24 >= 0

And then I would need to set a goal to minimize a function which multiplies the arrays (i.e. price * volume, but taking into account a negative parameter as I want to maximize rather than minimize).

The code should then find the optimized solution within the constraints is:

  1. D : 24 * $138
  2. C : 30 * $135
  3. B : 20 * $134
  4. A : 1 * $130

Thus selling all 75 units, leading to a total of $10,172.

How do I set this up within the SciPy (or better) framework?



Solution 1:[1]

Seems like a linear programming problem, for which try scipy.optimize.linprog.

if x_i = quantity sold at location i, p_i = price at location i, and the prices are fixed, but the quantities can vary, you have

minimize -(p_1*x_1 + p_2*x_2 + p_3*x_3 + p_4*x_4)

s.t.

p_1*x_1 <= 130*40

p_2*x_2 <= 134*20

p_3*x_3 <= 135*30

p_4*x_4 <= 138*24

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paisanco