Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear programming with scipy.optimize.linprog

I've just check the simple linear programming problem with scipy.optimize.linprog:

1*x[1] + 2x[2] -> max

1*x[1] + 0*x[2] <= 5
0*x[1] + 1*x[2] <= 5
1*x[1] + 0*x[2] >= 1
0*x[1] + 1*x[2] >= 1
1*x[1] + 1*x[2] <= 6

And got the very strange result, I expected that x[1] will be 1 and x[2] will be 5, but:

>>> print optimize.linprog([1, 2], A_ub=[[1, 1]], b_ub=[6], bounds=(1, 5), method='simplex')
  status: 0
   slack: array([ 4.,  4.,  4.,  0.,  0.])
 success: True
     fun: 3.0
       x: array([ 1.,  1.])
 message: 'Optimization terminated successfully.'
     nit: 2

Can anyone explain, why I got this strange result?

like image 358
juise Avatar asked Jun 15 '15 16:06

juise


People also ask

How linear programming is used in optimization?

In Mathematics, linear programming is a method of optimising operations with some constraints. The main objective of linear programming is to maximize or minimize the numerical value. It consists of linear functions which are subjected to the constraints in the form of linear equations or in the form of inequalities.

Is optimization same as linear programming?

Linear programming (LP), also called linear optimization, is a method to achieve the best outcome (such as maximum profit or lowest cost) in a mathematical model whose requirements are represented by linear relationships.

Is linear programming an optimization problem?

Linear programming deals with a class of optimization problems, where both the objective function to be optimized and all the constraints, are linear in terms of the decision variables.

What is Linprog function?

x = linprog( f , A , b , Aeq , beq , lb , ub ) defines a set of lower and upper bounds on the design variables, x , so that the solution is always in the range lb ≤ x ≤ ub .


1 Answers

optimize.linprog always minimizes your target function. If you want to maximize instead, you can use that max(f(x)) == -min(-f(x))

from scipy import optimize

optimize.linprog(
    c = [-1, -2], 
    A_ub=[[1, 1]], 
    b_ub=[6],
    bounds=(1, 5),
    method='simplex'
)

This will give you your expected result, with the value -f(x) = -11.0

 slack: array([ 0.,  4.,  0.,  4.,  0.])
 message: 'Optimization terminated successfully.'
     nit: 3
       x: array([ 1.,  5.])
  status: 0
 success: True
     fun: -11.0
like image 88
cel Avatar answered Sep 20 '22 04:09

cel