2

I want to minimize the following LPP: c=60x+40y+50z subject to 20x+10y+10z>=350 , 10x+10y+20z>=400, x,y,z>=0

my code snippet is the following(I'm using scipy package for the first time)

from scipy.optimize import linprog
c = [60, 40, 50]
A = [[20,10], [10,10],[10,20]]
b = [350,400]
res = linprog(c, A, b)
print(res)

The output is : screenshot of the output in Pycharm

1.Can someone explain the parameters of the linprog function in detail, especially how the bound will be calculated?

2.Have I written the parameters right?

I am naive with LPP basics, I think I am understanding the parameters wrong.

5
  • What have you tried to fix it? And what does the documentation say the parameters should be? My first guess would be pass in A.T rather than A, based on the error message. Commented Mar 21, 2018 at 14:07
  • What do you mean by "how the bound will be calculated"? Commented Mar 21, 2018 at 14:10
  • 2
    The error message is quite good IMHO. Commented Mar 21, 2018 at 14:48
  • The documentation says the 6th parameter of linprog() is "bounds=(min, max) pairs for each element in x, defining the bounds on that parameter." I am not sure how I'm going to calculate this value. Commented Mar 21, 2018 at 14:48
  • 1
    @ProteetiProva That is an optional parameter you can give as an input to the algorithm to specify that the variable values must be within certain bounds (by default the bounds are [0, +∞)). If your problem does not require specific bounds then you don't need to give it. Commented Mar 21, 2018 at 15:15

2 Answers 2

3

linprog expects A to have one row per inequation and one column per variable, and not the other way around. Try this:

from scipy.optimize import linprog
c = [60, 40, 50]
A = [[20, 10, 10], [10, 10, 20]]
b = [350, 400]
res = linprog(c, A, b)
print(res)

Output:

     fun: -0.0
 message: 'Optimization terminated successfully.'
     nit: 0
   slack: array([ 350.,  400.])
  status: 0
 success: True
       x: array([ 0.,  0.,  0.])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for being this much precise..really helped !
1

The message is telling you that your A_ub matrix has incorrect dimension. It is currently a 3x2 matrix which cannot left-multiply your 3x1 optimization variable x. You need to write:

A = [[20,10, 10], [10,10,20]]

which is a 2x3 matrix and can left multiply x.

1 Comment

I was really worried why the dimensions aren't matching. Thank you, now I can understand !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.