-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbowman-farm-python-code
More file actions
305 lines (305 loc) · 11.3 KB
/
Copy pathbowman-farm-python-code
File metadata and controls
305 lines (305 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
{
"cells": [
{
"cell_type": "code",
"execution_count": 114,
"id": "72ba00ac-cc24-403e-b8f9-d3589e610297",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: pulp in /opt/anaconda3/lib/python3.12/site-packages (3.1.1)\n"
]
}
],
"source": [
"!pip install pulp"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "47c0b1d9-0e52-4206-9b46-3ddc687a973d",
"metadata": {},
"outputs": [],
"source": [
"import pulp\n",
"\n",
"# Define the LP problem\n",
"model = pulp.LpProblem(\"Farm_Optimization\", pulp.LpMaximize)\n"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "f48c39c7-9628-4856-9fd5-5fe13d2b90bb",
"metadata": {},
"outputs": [],
"source": [
"# Decision Variable\n",
"\n",
"\n",
"Xc = pulp.LpVariable(\"2025_Buy_Cows\", lowBound=0, upBound=50, cat='Integer')\n",
"Xh = pulp.LpVariable(\"2025_Buy_Hens\", lowBound=0, upBound=1600, cat='Integer')\n",
"\n",
"Yc = pulp.LpVariable(\"2025_Sell_Cows\", lowBound=0, upBound=30, cat='Integer')\n",
"Yh = pulp.LpVariable(\"2025_Sell_Hens\", lowBound=0, upBound=400, cat='Integer')\n",
"\n",
"Ac = pulp.LpVariable('Acres_Corn', lowBound=0, cat='Continuous')\n",
"Aw = pulp.LpVariable('Acres_Wheat', lowBound=0, cat='Continuous')\n",
"Ar = pulp.LpVariable('Acres_Rapeseed', lowBound=0, cat='Continuous')\n",
"\n",
"Zcorn = pulp.LpVariable('2025_Sell_Corn', lowBound=0, cat='Continuous')\n",
"Zwheat = pulp.LpVariable('2025_Sell_Wheat', lowBound=0, cat='Continuous')\n",
"\n",
"Zrape = pulp.LpVariable('2026_Sell_Rapeseed', lowBound=0, cat='Continuous')\n",
"\n",
"Scorn = pulp.LpVariable('2025_Store_Corn', lowBound=0, cat='Continuous')\n",
"Swheat = pulp.LpVariable('2025_Store_Wheat', lowBound=0, cat='Continuous')\n",
"\n",
"Pcorn = pulp.LpVariable('2025_Purchase_Corn', lowBound=0, cat='Continuous')\n",
"Pwheat = pulp.LpVariable('2025_Purchase_Wheat', lowBound=0, cat='Continuous')\n",
"\n",
"Kinvest = pulp.LpVariable(\"Capital_Invested\", lowBound=0, cat='Continuous')\n"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "a4783ace-16d5-4e63-8ff8-da9505ee39cf",
"metadata": {},
"outputs": [],
"source": [
"# Parameters\n",
"\n",
"\n",
"initial_cows = 30\n",
"initial_hens = 400\n",
"usable_capital = 160000 - 40000 # Net after fixed costs\n",
"\n",
"# A dictionary storing various costs and prices for buying, selling, planting, feeding, and residual values\n",
"prices = {\n",
" 'corn_selling_2025': 200,\n",
" 'wheat_selling_2025': 195,\n",
"\n",
" 'corn_purchase_2025': 240,\n",
" 'wheat_purchase_2025': 220,\n",
" \n",
" 'corn_selling_2026': 210,\n",
" 'wheat_selling_2026': 205,\n",
" 'rape_selling_2026': 450,\n",
"\n",
" 'corn_plant_cost': 550,\n",
" 'wheat_plant_cost': 400,\n",
" 'rape_plant_cost': 244,\n",
" \n",
" 'cow_income': 1300,\n",
" 'hen_income': 155,\n",
" \n",
" 'cow_buy': 1040,\n",
" 'hen_buy': 17,\n",
" \n",
" 'cow_residual': 0.9 * 1040,\n",
" 'hen_residual': 0.85 * 17, \n",
" \n",
" 'interest_rate': 0.035,\n",
"}\n",
"\n",
"# Short aliases for current livestock numbers\n",
"Sc = initial_cows\n",
"Sh = initial_hens\n",
"\n",
"# Adds up all expenses, including: Buying animals, Planting crops, Purchasing feed, Capital investment\n",
"total_cost = (\n",
" prices['cow_buy'] * Xc +\n",
" prices['hen_buy'] * Xh +\n",
" \n",
" prices['corn_plant_cost'] * Ac +\n",
" prices['wheat_plant_cost'] * Aw +\n",
" prices['rape_plant_cost'] * Ar +\n",
" \n",
" prices['corn_purchase_2025'] * Pcorn +\n",
" prices['wheat_purchase_2025'] * Pwheat +\n",
" \n",
" Kinvest\n",
")\n",
"\n",
"# Calculates total revenue, from: Selling crops (current and next year), Livestock income (milk, eggs, etc.)\n",
"revenue = (\n",
" prices['corn_selling_2025'] * Zcorn +\n",
" prices['wheat_selling_2025'] * Zwheat +\n",
" prices['corn_selling_2026'] * Scorn +\n",
" prices['wheat_selling_2026'] * Swheat +\n",
" prices['rape_selling_2026'] * Zrape +\n",
" prices['cow_income'] * (Sc + Xc - Yc) +\n",
" prices['hen_income'] * (Sh + Xh - Yh)\n",
")\n",
"\n",
"# Accounts for value of remaining livestock (not sold by end of cycle), adjusted by residual (salvage) rates\n",
"residual_value = (\n",
" prices['cow_residual'] * (Sc + Xc - Yc) +\n",
" prices['hen_residual'] * (Sh + Xh - Yh)\n",
")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "7084e4c9-7cb1-487e-9951-a838636349da",
"metadata": {},
"outputs": [],
"source": [
"# Objective function\n",
"\n",
"\n",
"# Formula to maximize total expected wealth, combining: Revenue, Leftover livestock value, Interest on leftover cash, Subtracted by total expenses\n",
"expected_wealth = revenue + residual_value + prices['interest_rate'] * (usable_capital + revenue - total_cost) - total_cost\n",
"model += expected_wealth, \"Maximize_Expected_Wealth\"\n"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "45e18cea-4ef1-48a1-8bbc-4b2f95dbf85e",
"metadata": {},
"outputs": [],
"source": [
"# Constraints\n",
"\n",
"\n",
"# Limits the number of animals the farm can hold.\n",
"model += Sc + Xc - Yc <= 80, \"Max_Cows\"\n",
"model += Sh + Xh - Yh <= 2000, \"Max_Hens\"\n",
"\n",
"# Total land constraint, also accounting for land used by cows.\n",
"model += Ac + Aw + Ar + 2 * Xc + Sc - Yc + Sh - Yh <= 850, \"Land_Constraint\"\n",
"\n",
"\n",
"# Labour constraints (simplified Frost & Dry max of 7100 hours)\n",
"model += 3*(Sc + Xc) + 0.05*(Sh + Xh) + 2.0*Ac + 2.0*Aw + 3.0*Ar <= 7100, \"Labour_Constraint\"\n",
"\n",
"# Feed constraints\n",
"# Ensures animals have enough feed, via stored or purchased crops.\n",
"model += Scorn >= (Sc + Xc - Yc) * 1, \"Feed_Corn\"\n",
"model += Swheat >= (Sh + Xh - Yh) * 0.04, \"Feed_Wheat\"\n",
"\n",
"\n",
"# Yield constraints (in tons per acre)\n",
"model += Zcorn + Scorn <= 3.0 * Ac, \"Corn_Yield\"\n",
"model += Zwheat + Swheat <= 2.0 * Aw, \"Wheat_Yield\"\n",
"model += Zrape <= 3.2 * Ar, \"Rapeseed_Yield\"\n",
"\n",
"# Financial feasibility\n",
"model += total_cost <= usable_capital + revenue, \"Financial_Constraint\"\n",
"model += usable_capital + revenue - total_cost >= 0, \"NonNegative_Cash\"\n"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "7e858aed-d30d-4754-b4af-dffc0ceb6576",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the CBC MILP Solver \n",
"Version: 2.10.3 \n",
"Build Date: Dec 15 2019 \n",
"\n",
"command line - /opt/anaconda3/lib/python3.12/site-packages/pulp/apis/../solverdir/cbc/osx/i64/cbc /var/folders/km/kqnqtg953b76gw0hctk94d1m0000gn/T/baadbb91d5da4f9d8cd2e487394ba645-pulp.mps -max -timeMode elapsed -branch -printingOptions all -solution /var/folders/km/kqnqtg953b76gw0hctk94d1m0000gn/T/baadbb91d5da4f9d8cd2e487394ba645-pulp.sol (default strategy 1)\n",
"At line 2 NAME MODEL\n",
"At line 3 ROWS\n",
"At line 16 COLUMNS\n",
"At line 99 RHS\n",
"At line 111 BOUNDS\n",
"At line 116 ENDATA\n",
"Problem MODEL has 11 rows, 15 columns and 59 elements\n",
"Coin0008I MODEL read with 0 errors\n",
"Option for timeMode changed from cpu to elapsed\n",
"Continuous objective value is 1.14591e+06 - 0.00 seconds\n",
"Cgl0004I processed model has 4 rows, 8 columns (4 integer (0 of which binary)) and 19 elements\n",
"Cbc0012I Integer solution of -1145912.3 found by DiveCoefficient after 0 iterations and 0 nodes (0.03 seconds)\n",
"Cbc0038I Full problem 4 rows 8 columns, reduced to 2 rows 4 columns\n",
"Cbc0001I Search completed - best objective -1145912.28, took 0 iterations and 0 nodes (0.03 seconds)\n",
"Cbc0035I Maximum depth 0, 0 variables fixed on reduced cost\n",
"Cuts at root node changed objective from -1.14591e+06 to -1.14591e+06\n",
"Probing was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"Gomory was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"Knapsack was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"Clique was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"MixedIntegerRounding2 was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"FlowCover was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"TwoMirCuts was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"ZeroHalf was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n",
"\n",
"Result - Optimal solution found\n",
"\n",
"Objective value: 1145912.28000000\n",
"Enumerated nodes: 0\n",
"Total iterations: 0\n",
"Time (CPU seconds): 0.00\n",
"Time (Wallclock seconds): 0.03\n",
"\n",
"Option for printingOptions changed from normal to all\n",
"Total time (CPU seconds): 0.00 (Wallclock seconds): 0.04\n",
"\n",
"Status: Optimal\n",
"2025_Buy_Cows: 0.00\n",
"2025_Buy_Hens: 1600.00\n",
"2025_Purchase_Corn: 0.00\n",
"2025_Purchase_Wheat: 0.00\n",
"2025_Sell_Corn: 0.00\n",
"2025_Sell_Cows: 0.00\n",
"2025_Sell_Hens: 400.00\n",
"2025_Sell_Wheat: 0.00\n",
"2025_Store_Corn: 30.00\n",
"2025_Store_Wheat: 64.00\n",
"2026_Sell_Rapeseed: 2489.60\n",
"Acres_Corn: 10.00\n",
"Acres_Rapeseed: 778.00\n",
"Acres_Wheat: 32.00\n",
"Capital_Invested: 0.00\n",
"\n",
"Max Expected Wealth: £1,288,507.28\n"
]
}
],
"source": [
"# Solve the model\n",
"model.solve()\n",
"\n",
"# Print results\n",
"print(f\"Status: {pulp.LpStatus[model.status]}\")\n",
"for variable in model.variables():\n",
" print(f\"{variable.name}: {variable.varValue:.2f}\")\n",
"print(f\"\\nMax Expected Wealth: £{pulp.value(model.objective):,.2f}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}