411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Land Cultivation
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Land Cultivation Population Growth Big Bang
Cultivating a field by determining the area to grow various cereals and the amount of fertilizers and pesticides to use, in order to maximize profit, is an example of linear programming that in its standard form is expressed in three parts:
A linear function to be maximized:
Constraint problem of the following form:
Non-negative variables
The problem is usually expressed in matrix form as follows:
Example calculation with Julia
Suppose a farmer has a plot of farmland, 10 km2, to grow wheat or barley, or a combination of both. The farmer has limited amounts of fertilizer (48 kilograms) and pesticides (32 kilograms). Each square kilometer of wheat requires 3 kilograms of fertilizer and 4 kilograms of pesticides, while each square kilometer of barley requires 6 kilograms of fertilizer and 2 kilograms of pesticides. Let the selling price of wheat per square kilometer be €4 and the selling price of barley be €3. If we denote the area of land cultivated with wheat and barley by x1 and x2, respectively, then profit can be maximized by choosing optimal values for x1 and x2. This problem can be expressed with the following linear programming problem in standard form.
First, you need to install the JuMP and HiGHS packages.
From the REPL, type:
using Pkg
Pkg.add("HiGHS")
using Pkg
Pkg.add("JuMP")
From Jupyter, in two code cells, type:
] add HiGHS
] add JuMP
And then click Run. Then run the code to create the linear programming model:
using JuMP
using HiGHS
model = Model(HiGHS.Optimizer)
@variable(model, x1 >= 0)
@variable(model, x2 >= 0)
@objective(model, Max, 4*x1 +3*x2)
@constraint(model, c1, 3*x1+6*x2<=48)
@constraint(model, c2, 4*x1+2*x2<=32)
@constraint(model, c3, x1+x2<=10)
print(model)
optimize!(model)
max 4𝑥1+3𝑥2
Subject to:
3𝑥1+6𝑥2≤48
4𝑥1+2𝑥2≤32
𝑥1+𝑥2≤10
𝑥1≥0
𝑥2≥0
Running HiGHS 1.5.3 [date: 1970-01-01, git hash: 45a127b78]
Copyright (c) 2023 HiGHS under MIT licence terms
Presolving model
3 rows, 2 cols, 6 nonzeros
3 rows, 2 cols, 6 nonzeros
Presolve : Reductions: rows 3(-0); columns 2(-0); elements 6(-0) - Not reduced
Problem not reduced by presolve: solving the LP
Using EKK dual simplex solver - serial
Iteration Objective Infeasibilities num(sum)
0 -3.4999947561e+00 Ph1: 3(7.25); Du: 2(3.49999) 0s
3 3.6000000000e+01 Pr: 0(0) 0s
Model status : Optimal
Simplex iterations: 3
Objective value : 3.6000000000e+01
HiGHS run time : 0.02
println("termination_status = ",termination_status(model))
println("primal_status = ",primal_status(model))
println("dual_status = ",dual_status(model))
println("objective_value = ",objective_value(model))
println("x1 = ",value(x1))
println("x2 = ",value(x2))
termination_status = OPTIMAL
primal_status = FEASIBLE_POINT
dual_status = FEASIBLE_POINT
objective_value = 36.0
x1 = 6.0
x2 = 4.0
You therefore earn a maximum profit of €36 by growing 6 km2 of wheat and 4 km2 of barley. Each square kilometer of wheat requires 3 kilograms of fertilizer and 4 kilograms of pesticides, while each square kilometer of barley requires 6 kilograms of fertilizer and 2 kilograms of pesticides.