Practical data science and optimization: farmer’s problem


In this post we consider how to solve the following problem (from [1]).

 

Consider a farmer, who has A=500 acres of land, and raises grain, corn and sugar beet.

The farmer needs N1=200T of wheat and N2=240T of corn to feed his cattle. These amounts can be either raised by himself or bought from an external wholesaler. If more than this amount is produced, the exceeded part will be sold. Selling prices are SP1=170$ and SP2=150$ per ton of wheat and corn respectively.

The purchase prices are 40% more than the selling price. (PP1=1.4*SP1 ,PP2=1.4*SP2, PP3=1.4*SP3)

Sugar beet sells at SP3=36$ per ton. However, there is a quota on sugar beet production, any amount in

excess of the quota can be sold only at SP3Q=10$ per ton. The quota this year is Q3=6000T.

Based on past experience, the farmer knows that the mean yield on his land is roughly Y1=2.5T, Y2=3T and Y3=20T per acre for wheat, corn and sugar beets, respectively. And the planting costs are PC1=150$, PC2=230$ and PC3=260$ per acre respectively.

In winter he wants to decide how much land he should devote for each crop in order to get maximal profit in the next autumn.

 

Table 1: Data for the farmer’s problem

t1

Total available land : 500 acres

 

Let x1, x2, x3 be the acres of land devoted to wheat, corn and sugar beets respectively, x4, x5 be the tones of wheat and corn purchased, x6, x7 be the tons of wheat and corn sold, x8 be the tons of sugar beets sold at the favorable price, and x9 be the tons of sugar beets sold at the lower price. We formulate the problem as the following linear optimization program:

minimize:

z=150*x1+230*x2+260*x3+238*x4+210x5-170*x6-150*x7-36*x8-10*x9

such that: x1 + x2 + x3 ≤ 500,

-2.5*x1-x4 +x6 <=-200

-3*x2-x5+x7<=-240

-20*x3 +x8 +x9 <=0

x8<=6000

x1,..,x9 ≥ 0.

 

To solve this problem you need python and scipy installed on your computer.

If you do not know how to do it, see [2] for installing python, [3] for installing pip3, and [4] for installing scipy.

Assuming you have installed all that is required above, you need to copy the code below into a file farmer.py

from scipy.optimize import linprog

obj=[150,230,260,238,210,-170,-150,-36,-10]

lhs_ineq=[[1,1,1,0,0,0,0,0,0],

[-2.5,0,0,-1,0,1,0,0,0],

[0,-3,0,0,-1,0,1,0,0],

[0,0,-20,0,0,0,0,1,1],

[0,0,0,0,0,0,0,1,0]]

rhs_ineq=[500,-200,-240,0,6000]

opt=linprog(c=obj,A_ub=lhs_ineq,b_ub=rhs_ineq)

print(opt)

 

On the first line we imported linprog, on the second line we defined objective function. On the third line we defined the left side data in the inequalities. Then we defined the right side data in the inequalities. On the last two lines we found the optimal solution and displayed it on the screen.

To execute this code run this command: python3 farmer.py

r1

 

You will see the result on the screen, As you can see, the optimal solution is: x1=120 acres for wheat, x2=80 acres for corn, x3=300 acres for sugar beets. The overall expected profit is $118,600.

To solve this problem with different input parameters, change these parameters in the code and run it.

 

 

References

[1] Stochastic Linear/Integer Programming, Guest Lecturer: Xu, Huan

http://cgm.cs.mcgill.ca/~avis/courses/567/notes/stoch1.pdf

[2] How to install python

https://realpython.com/installing-python/

https://www.codecademy.com/article/install-python3

[3] How to install pip3

https://www.activestate.com/resources/quick-reads/how-to-install-and-use-pip3/

[4] How to install scipy

https://scipy.org/install/

 

 

 

 

 

 

 

 

How do you rate this article?

21


I_g_o_r
I_g_o_r

I am curious about science, technologies and their applications to solving real problems.


Practical data science & optimization in examples
Practical data science & optimization in examples

This blog gives readers practical examples of data science and optimization problems and their solutions using python scripts. These scripts can be used to solve real problems in business and life.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.