Linear programming in Julia with GLPK and JuMP

By: Jaafar Ballout

Re-posted from: https://www.supplychaindataanalytics.com/linear-programming-in-julia-with-glpk-and-jump/

In previous posts we have covered various linear programming examples in both Python and R. We have e.g. introduced lpSolve in R, PuLP in Python, Gekko in Python, MIP in Python, ortools in Python as well as many other packages and modules for linear programming. In this post I will demonstrate how one can implement linear programming in Julia.

Julia is a flexible dynamic language that is appropriate for scientific and numerical computing. Besides, Julia is an open-source project. Its source code is available on GitHub. Using Julia provides access to many packages already developed for this language. For the linear programming example presented in this post I will use the Julia packages GLPK and JuMP.

Financial engineering example utilizing JuMP and GLPK in Julia

Logo of Julia programming language

The demonstrated example is about a bank loan policy. This simple example will help us understand the power of linear programming in solving most of the problems we face in supply chain management and the financial fields. Indeed, Julia will be incorporated to solve the optimization problem. The example is adapted from Hamdy Taha’s book available on Amazon.

Type of loan Interest rate Bad-debt ratio
Personal 0.14 0.1
Car 0.13 0.07
Home 0.12 0.03
Farm 0.125 0.05
Commercial 0.10 0.02

A bank is in the process of devising a loan policy that involves a maximum of $12 million. The table, shown above, provides the data about the available loans. It is important to note that bad debts are unrecoverable and produce no interest revenue. Competition with other financial institutions dictates the allocation of at least 40% of the funds to farm and commercial loans. To assist the housing industry in the region, home loans must equal at least 50% of the personal, car, and home loans. The bank limits the overall ratio of bad debts on all loans to at most 4%.

Developing a mathematical problem statement

The hardest part of any optimization problem is building the mathematical model. The steps to overcome this hardship are as follows:

  1. Define the decision variables
  2. Write down the objective function
  3. Formulate all the constraints

Decison variables

According to the given in the problem, we need to determine the amount of loan in million dollars. The table shows five categories or types of loans. Thus, we need five decision variables corresponding to each category.

  • x1 = personal loans
  • x2 = car loans
  • x3 = home loans
  • x4 = farm loans
  • x5 = commercial loans

Objective function

The objective function is to maximize profits (net return). The net return is the difference between the revenues, generated by interest rate, and losses due to the bad-debt ratio.

Then, the objective function is as follows:

Constraints

Total Loans should be less or equal to 12 million dollars

Farm and Commercial loans equal at least 40% of all loans

Home loans should equal at least 50% of the personal, car, and home loans

Bad debts should not exceed 4% of all loans

Non-negativity

Application of linear programming in Julia

The first step is to add the required packages to solve the linear program. This is done using the <Pkg> which is the built-in package manager in Julia. We can add the needed packages using the following commands in Julia REPL. If Julia is installed, typing Julia at the command line, in the computer command prompt, is enough to open REPL.

Julia REPL
using Pkg
Pkg.add("JuMP")
Pkg.add("GLPK")

The work presented below is done in a Jupyter notebook with a Julia kernel. JuMP is a modeling language for mathematical optimization in Julia while GLPK is the solver that we will use. After importing the packages in Jupyter, we defined the optimization problem by creating a variable BM that stands for Bank Model. The next step is setting the optimizer by declaring the solver (GLPK) and the model (BM). Moving on, we specified the decision variables, constraints, and objective function.

In [1]:

## Importing the necessary packages ## 
using JuMP
using GLPK

In [2]:

## Defining the model ##
BM = Model() # BM stands for Bank Model

## Setting the optimizer ##
set_optimizer(BM,GLPK.Optimizer)

## Define the variables ##
@variable(BM, x1>=0)
@variable(BM, x2>=0)
@variable(BM, x3>=0)
@variable(BM, x4>=0)
@variable(BM, x5>=0)

## Define the constraints ##
@constraint(BM, x1+x2+x3+x4+x5<=12)
@constraint(BM, 0.4x1+0.4x2+0.4x3-0.6x4-0.6x5<=0)
@constraint(BM, 0.5x1+0.5x2-0.5x3<=0)
@constraint(BM, 0.06x1+0.03x2-0.01x3+0.01x4-0.02x5<=0)

## Define the objective function ##
@objective(BM, Max, 0.026x1+0.0509x2+0.0864x3+0.06875x4+0.078x5)

## Run the optimization ##
optimize!(BM)

Declaring the objective function and constraints in Julia is easier because the algebraic equation can be inputted as it is (i.e. no need to show the multiplication operator between the variable and the constant). After running the optimization model, we can view the output of the model as follows:

In [3]:

BM

Out[3]:

A JuMP Model
Maximization problem with:
Variables: 5
Objective function type: AffExpr
`AffExpr`-in-`MathOptInterface.LessThan{Float64}`: 4 constraints
`VariableRef`-in-`MathOptInterface.GreaterThan{Float64}`: 5 constraints
Model mode: AUTOMATIC
CachingOptimizer state: ATTACHED_OPTIMIZER
Solver name: GLPK
Names registered in the model: x1, x2, x3, x4, x5

In [4]:

objective_value(BM)

Out[4]:

0.99648

In [5]:

objective_sense(BM)

Out[5]:

MAX_SENSE::OptimizationSense = 1

In [6]:

println("x1 = ", value.(x1), "\n","x2 = ",value.(x2), "\n", "x3 = ",value.(x3), "\n", "x4 = ",value.(x4), "\n", "x5 = ",value.(x5))
x1 = 0.0
x2 = 0.0
x3 = 7.199999999999999
x4 = 0.0
x5 = 4.8

Analyzing the output of the model, we can see that the bank should spend 7.2 million dollars on home loans and 4.8 million dollars on commercial loans. This distribution will help the bank maximize its net returns (i.e. profits) to 0.99648 million dollars.

Concluding remarks

In this post, we explored the power of linear programming in the banking sector and used Julia to solve the LP optimization problem. It is nice to see that open-source languages can solve these kinds of problems in an easy way without extensive hours of coding. In the future, we will have an insight into non-linear problems in supply chain management.

The post Linear programming in Julia with GLPK and JuMP appeared first on Supply Chain Data Analytics.