Nishanth Seshadri - 2 months ago 10

Python Question

I am trying to solve a problem with many variables using scipy and linear programming. I have a set of variables X which are real numbers between 0.5 and 3 and I have to solve the following equations :

`346 <= x0*C0 + x1*C1 + x2*C2 +......xN*CN <= 468`

25 <= x0*p0 + x1*p1 + x2*p2 +......xN*pN <= 33

12 <= x0*c0 + x1*c1 + x2*c2 +......xN*cN <= 17

22 <= x0*f0 + x1*f1 + x2*f2 +......xN*fN <= 30

the numbers C0...CN , p0...pN , c0...cN , f0...fN are already given to me. I tried to solve this in the following way:

`import numpy as np`

from scipy.optimize import linprog

from numpy.linalg import solve

A_ub = np.array([

[34, 56, 32, 21, 24, 16, 19, 22, 30, 27, 40, 33],

[2, 3, 2, 1.5, 3, 4, 1, 2, 2.5, 1, 1.2, 1.3],

[1, 2, 3, 1.2, 2, 3, 0.6, 1, 1, 1.2, 1.1, 0.8],

[0.5, 2, 2, 1, 3, 4, 1, 1, 1, 0.5, 0.3, 1.2],

[-34, -56, -32, -21, -24, -16, -19, -22, -30, -27, -40, -33],

[-2, -3, -2, -1.5, -3, -4, -1, -2, -2.5, -1, -1.2, -1.3],

[-1, -2, -3, -1.2, -2, -3, -0.6, -1, -1, -1.2, -1.1, -0.8],

[-0.5, -2, -2, -1, -3, -4, -1, -1, -1, -0.5, -0.3, -1.2]])

b_ub = np.array([468, 33, 17, 30, -346, -25, -12, -22])

c = np.array([34, 56, 32, 21, 24, 16, 19, 22, 30, 27, 40, 33])

res = linprog(c, A_eq=None, b_eq=None, A_ub=A_ub, b_ub=b_ub, bounds=(0.5, 3))

Explanation for the equations the first row of A_ub is the same as b_ub, as we are trying to maximize the equation as well as make sure it is within the given boundary limits i.e 468 and 346 meaning that I want to get the value as close as possible to the upper limit.

I put

`[-34, -56, -32, -21, -24, -16, -19, -22, -30, -27, -40, -33]`

`-346 > -(x0*C0 + x1*C1 + x2*C2 +......xN*CN)`

But I feel my approach is wrong as I get the answer as

`0.425`

`res.fun`

`nan`

`res.x`

The upper bound for x is 3 and the lower bound is 0.5

How do I define the problem as shown above in order to get a maximum value close to 468 while keeping in mind the upper bounds? How do I define lower bounds using scipy? I am working on linear programming for the first time so I may have missed out on ideas that can help me out.

I am also open to any other solutions.

<-----EDIT----->

To all the people who have answered this question above, thank you so much. I have one more question I need help with. is there any way I can formulate the following? :

`x0*C0 > x1*C1 > x2*C2 >...> xK*CK >...xN*CN > 0`

`x0*C0 >= 33/2`

please let me know asap. thanks

Answer

This system of inequalities is not feasible: there is no solution that satisfies all constraints. You can see that from `res`

:

```
fun: 0.42500000000000243
message: 'Optimization failed. Unable to find a feasible starting point.'
nit: 28
status: 2
success: False
x: nan
```

I believe this is a correct result (I verified this with another LP system).

Note: if you change the bounds to `(0,3)`

, you will get a feasible solution.

Source (Stackoverflow)

Comments