Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

May 5, 2024

Latest: MapReduce Algorithm

MATHEMATICS OPTIMIZATION TECHNIQUES PYTHON

Solving Cargo Loading Problem using


Integer Programming in Python
February 3, 2022 Avinash Navlani

Learn how to use Python PuLP to solve Cargo loading problems and Knapsack Problems using
Integer Programming.
Linear programming deals with non-integer solutions but in certain scenarios, we need integer
solutions such as the number of products to manufacture, number of apartments to construct,
number of trees to plan. In this approach, we optimize the linear function and set of linear
constraints on integer variables. Integer programming is widely utilized in the area of job
scheduling, inventory management, transportation, computer science, and production planning.

In this tutorial, we are going to cover the following topics:

Contents [ hide ]
1 Types of Integer Programming
2 Knapsack Problem
3 Cargo Loading Problem
4 Understand the problem
5 Initialize LP Model
6 Define Decision Variable
7 Define Objective Function
8 Define the Constraints
9 Solve Model
10 Summary

Types of Integer Programming


There are three types of Integer programming problems:

1. Pure Integer Problems: In this type of problem, all the variables have integer
solution.
2. Mixed Integer Problems: In this type of problem, some of the variables have
integer and some of the variable have the continuous solution.
3. 0-1 Integer Problems: In this type of problem, all the varaibles requires value of 0
or 1.

Knapsack Problem
The knapsack problem is defined as how many units of each different kind of item or product to
put in a knapsack with a given capacity in order to maximize profit. It is also known as the fly-away
kit problem because a jet pilot decides the most valuable items to take abroad a jet. The knapsack
problem is a special case of integer programming where the objective function is maximized with a
single less than or equal to linear constraint. It has many applications in the real world.

Cargo Loading Problem


The cargo loading problem is a typical example of a knapsack problem. This type of problem solves
the capacity planning problem of a shipment. We load the items into a shipment with limited
capacity. The main objective is to load the most optimum items in the shipment.

Suppose there are n items 1,2,3 … n and the number of units of each item is mi. The weight per unit
of item i is wi, ri is the revenue per unit of item I, and W is the total capacity of cargo.

Understand the problem


One Cargo shipment of Shakti Pumps has a capacity of 10 tons. Shakti Pumps wants to ship three
types of pumps A, B, and C in this shipment.

A pump weight of 1 ton and profit of $12 (X1)


B pump weight of 2 tons and profit of $25 (X2)
C pump weight of 3 tons and profit of $38 (X3)

Objective Function:

Maximize Z = 12X1 + 25X2 + 38X3

Constraints:

12X1 + 25X2 + 38X3 ≤ 10 (Cargo storage capacity 10 ton)

Initialize LP Model
In this step, we will import all the classes and functions of pulp module and create a Maximization
LP problem using LpProblem class.
Python

1 # Import all classes of PuLP module


2 from pulp import *
3
4 # Create the problem variable to contain the problem data
5 model = LpProblem("Cargo-Loading-Problem", LpMaximize)

Define Decision Variable


In this step, we will define the decision variables. In our problem, we have three variables A, B, and
C. Let’s create them using LpVariable class. LpVariable will take the following four values:

First, arbitrary name of what this variable represents.


Second is the lower bound on this variable.
Third is the upper bound.
Fourth is essentially the type of data (discrete or continuous). The options for the
fourth parameter are LpContinuous or LpInteger.

Python

1 # Define Decision Variables


2 x1 = LpVariable("A", 0, None, LpInteger)
3 x2 = LpVariable("B", 0, None, LpInteger)
4 x3 = LpVariable("C", 0, None, LpInteger)

Define Objective Function


In this step, we will define the maximum objective function by adding it to the LpProblem object.

Python
1 # Define Objective
2 model += 12 * x1 + 25 * x2 + 38 * x3

Define the Constraints


In this step, we will add only 1 constraint defined in the problem by adding them to the
LpProblem object.
Python

1 # Define Constraints
2 model += x1 + 2*x2 + 3*x3 <= 10 # Cargo storage capacity 10 ton ; pump A is 1 ton, pump B is 2 to

Solve Model
In this step, we will solve the LP problem by calling solve() method. We can print the final value by
using the following for loop.

Python

1 # The problem is solved using PuLP's choice of Solver


2 model.solve()
3
4 # Print the variables optimized value
5 for v in model.variables():
6 print(v.name, "=", v.varValue)
7
8 # The optimised objective function value is printed to the screen
9 print("Value of Objective Function = ", value(model.objective))

Output:
A = 1.0
B = 0.0
C = 3.0
Value of Objective Function = 126.0

Summary
In this article, we have learned about Integer Programming, Knapsack Problems, Cargo Loading
Problems, Problem Formulation, and implementation in python using the PuLp library. We have
solved the cargo loading problem using an integer programming problem in Python. Of course, this
is just a simple case study, we can add more constraints to it and make it more complicated. In
upcoming articles, we will write more on different optimization problems and its solution using
Python. You can revise the basics of mathematical concepts in this article and learn about Linear
Programming in this article.
Solving Staff Scheduling Problem using Linear Programming

Solving Transportation Problem using Linear Programming in Python

 You May Also Like

Sensitivity Analysis in Python


March 24, 2022
Pandas Series
October 4, 2020

Dimensionality Reduction using PCA


October 27, 2020

Latest Posts

MapReduce Algorithm

Linear Programming using Pyomo

Networking and Professional Development for Machine Learning Careers in the USA

Predicting Employee Churn in Python

Airflow Operators

MLOps Tutorial

Python Decorators

Python Generators

Python Iterators Examples


Big Data Interview Questions and Answers

About Us

We love Data Science and we are here to provide you Knowledge on Machine Learning, Text Analytics,
NLP, Statistics, Python, and Big Data. We focus on simple, elegant, and easy to learn tutorials.

Resources

AWS

Big Data
Business Analytics

Data Engineering

Deep Learning

Essentials Skills

Interview

Julia

Machine Learning

Mathematics

MLOps

NLP

Optimization Techniques

Python

pandas

Recommender System

Statistics

Text Analytics

Archives

April 2024

September 2023

July 2023

March 2023

February 2023

January 2023
November 2022

June 2022

May 2022

April 2022

March 2022

February 2022

January 2022

December 2021

September 2021

July 2021

June 2021

May 2021

April 2021

March 2021

February 2021

January 2021

December 2020

November 2020

October 2020

September 2020

Data Science Deals

DataCamp

UpGrad
Edureka Data Science

Dataquest

Privacy Policy

Copyright © 2024 Machine Learning Geek. All rights reserved.


Theme: ColorMag by ThemeGrill. Powered by WordPress.

You might also like