## Linear programming example with inequality constraints

Posted January 31, 2013 at 09:00 AM | categories: optimization, linear programming | tags:

Updated February 27, 2013 at 02:44 PM

adapted from http://www.matrixlab-examples.com/linear-programming.html which solves this problem with fminsearch.

Let us suppose that a merry farmer has 75 roods (4 roods = 1 acre) on which to plant two crops: wheat and corn. To produce these crops, it costs the farmer (for seed, water, fertilizer, etc. ) $120 per rood for the wheat, and $210 per rood for the corn. The farmer has $15,000 available for expenses, but after the harvest the farmer must store the crops while awaiting favorable or good market conditions. The farmer has storage space for 4,000 bushels. Each rood yields an average of 110 bushels of wheat or 30 bushels of corn. If the net profit per bushel of wheat (after all the expenses) is $1.30 and for corn is $2.00, how should the merry farmer plant the 75 roods to maximize profit?

Let \(x\) be the number of roods of wheat planted, and \(y\) be the number of roods of corn planted. The profit function is: \( P = (110)($1.3)x + (30)($2)y = 143x + 60y \)

There are some constraint inequalities, specified by the limits on expenses, storage and roodage. They are:

\(\$120x + \$210y <= \$15000\) (The total amount spent cannot exceed the amount the farm has)

\(110x + 30y <= 4000\) (The amount generated should not exceed storage space.)

\(x + y <= 75\) (We cannot plant more space than we have.)

\(0 <= x and 0 <= y \) (all amounts of planted land must be positive.)

To solve this problem, we cast it as a linear programming problem, which minimizes a function f(X) subject to some constraints. We create a proxy function for the negative of profit, which we seek to minimize.

f = -(143*x + 60*y)

from scipy.optimize import fmin_cobyla def objective(X): '''objective function to minimize. It is the negative of profit, which we seek to maximize.''' x, y = X return -(143*x + 60*y) def c1(X): 'Ensure 120x + 210y <= 15000' x,y = X return 15000 - 120 * x - 210*y def c2(X): 'ensure 110x + 30y <= 4000' x,y = X return 4000 - 110*x - 30 * y def c3(X): 'Ensure x + y is less than or equal to 75' x,y = X return 75 - x - y def c4(X): 'Ensure x >= 0' return X[0] def c5(X): 'Ensure y >= 0' return X[1] X = fmin_cobyla(objective, x0=[20, 30], cons=[c1, c2, c3, c4, c5]) print 'We should plant {0:1.2f} roods of wheat.'.format(X[0]) print 'We should plant {0:1.2f} roods of corn'.format(X[1]) print 'The maximum profit we can earn is ${0:1.2f}.'.format(-objective(X))

Normal return from subroutine COBYLA NFVALS = 40 F =-6.315625E+03 MAXCV = 4.547474E-13 X = 2.187500E+01 5.312500E+01 We should plant 21.88 roods of wheat. We should plant 53.12 roods of corn The maximum profit we can earn is $6315.62.

This code is not exactly the same as the original post, but we get to the same answer. The linear programming capability in scipy is currently somewhat limited in 0.10. It is a little better in 0.11, but probably not as advanced as Matlab. There are some external libraries available:

Copyright (C) 2013 by John Kitchin. See the License for information about copying.