0% found this document useful (0 votes)
62 views25 pages

Optimization Technique Lecture Note

For full knowledge on optimization techniques

Uploaded by

ogheneovoaefe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views25 pages

Optimization Technique Lecture Note

For full knowledge on optimization techniques

Uploaded by

ogheneovoaefe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CSC 405 OPTIMIZATION TECHNIQUES

DEPARTMENT OF COMPUTER SCIENCE

NIGER DELTA UNIVERSITY

LECTURE NOTE

MATRIC NO:

FACULTY:

DEPT:

CLASS ID:

SUBMISSION DATE:

1
Chapter One
Optimization Techniques
1.1 Introduction
Optimization techniques are methods used to find the best possible solution or outcome for a given problem
within a defined set of constraints. These techniques are widely used in various fields such as engineering,
economics, operations research, and artificial intelligence. Optimization is the process of making something as
effective or functional as possible. Basic types include: Linear Optimization, Nonlinear Optimization, Integer
Optimization, Combinatorial Optimization, Stochastic Optimization and Multi-Objective Optimization.

Linear Optimization also referred to as Linear Programming (LP) is concerned with the following:
a. Objective Function: A linear function that needs to be maximized or minimized.
b. Constraints: A set of linear inequalities or equations.
c. Feasible Region: The set of all possible points that satisfy the constraints.
d. Simplex Method: An iterative method to find the optimal solution.

Nonlinear Optimization also referred to as Nonlinear Programming (NLP) is concerned with the following:
a. Objective Function: A nonlinear function.
b. Constraints: Can be linear or nonlinear.
c. Methods: - Gradient Descent, - Newton's Method, - Conjugate Gradient Method

Integer Optimization also referred to as Integer Programming (IP) is concerned with the following:
a. Definition: Optimization where some or all of the decision variables are restricted to integer values.
b. Methods: - Branch and Bound, - Cutting Planes

Combinatorial Optimization is concerned with the following:


a. Definition: Optimization over a discrete set of possible solutions; examples are Traveling Salesman
Problem, Knapsack Problem.
b. Methods: - Greedy Algorithms, - Dynamic Programming, - Genetic Algorithms

Stochastic Optimization is concerned with the following:


a. Definition: Optimization involving randomness or uncertainty.
b. Methods: - Simulated Annealing, - Particle Swarm Optimization, - Ant Colony Optimization

Multi-Objective Optimization is concerned with the following:


a. Definition: Optimization involving more than one objective function.
b. Methods: - Pareto Optimality, - Weighted Sum Method, - e-Constraint Method

2
Chapter Two
Linear Optimization
2.1 Widespread Methodology
The methods of linear programming were originally developed between 1945 and 1955 by American
mathematicians to solve problems arising in industry and economic planning. Many such problems
involve constraints on the size of the workforce, the quantities of raw materials available, and the number
of machines available and so on. The problems that will be solved usually have two variables in them
and can be solved graphically, but problems occurring in industry have many more variables and have to
be solved by computer. For example, in oil refineries, problems arise with hundreds of variables and tens
of thousands of constraints. Another application is in determining the best diet for farm animals such as
pigs. In order to maximize the profit a pig farmer needs to ensure that the pigs are fed appropriate food
and sufficient quantities of it to produce lean meat. The pigs require a daily allocation of carbohydrate,
protein, amino acids, minerals and vitamins. Each involves various components. For example, the
mineral content includes calcium, phosphorus, salt, potassium, iron, magnesium, zinc, copper, manganese,
iodine, and selenium. All these dietary constituents should be present, in correct amounts. Undoubtedly
linear programming is one of the most widespread methods used to solve management and economic
problems, and has been applied in a wide variety of situations and contexts.

2.2 Formation of Linear Programming Problems


You are now in a position to use your knowledge of inequalities from the previous chapter to illustrate
linear programming with the following case study. Suppose a manufacturer of printed circuits has a
stock of 200 resistors, 120 transistors and 150 capacitors and is required to produce two types of circuits.
Type A requires 20 resistors, 10 transistors and 10 capacitors. Type B requires 10 resistors, 20 transistors
and 30 capacitors. If the profit on type A circuits is £5 and that on type B circuits is £12, how many of
each circuit should be produced in order to maximise the profit? You will not actually solve this problem
yet, but show how it can be formulated as a linear programming problem. There are three vital stages in
the formulation, namely:
a. What are the unknowns?
b. What are the constraints?
c. What is the profit/cost to be maximised/minimised?
For this problem,
a. What are the unknowns?
Clearly the number of type A and type B circuits produced; so we define
x = number of type A circuits produced
y = number of type B circuits produced
b. What are the constraints?
There are constraints associated with the total number of resistors, transistors and capacitors available.
Resistors Since each type A requires 20 resistors, and each type B requires 10 resistors, then
20 x + 10 y ≤ 200, as there is a total of 200 resistors available.
Transistors Similarly
10 x + 20 y ≤ 120
Capacitors Similarly
10 x + 30 y ≤ 150
Finally you must state the obvious (but nevertheless important) inequalities
x ≥ 0, y ≥ 0 .
c. What is the profit?
Since each type A gives £5 profit and each type B gives £12 profit, the total profit is £P, where
P = 5x + 12 y .
You can now summarize the problem as:
maximize P = 5x + 12 y
subject to
3
20 x + 10 y ≤ 200
10 x + 20 y ≤ 120
10 x + 30 y ≤ 150
x≥0
y ≥ 0.
This is called a linear programming problem since both the objective function P and the constraints are all
linear in x and y. In this particular example you should be aware that x and y can only be integers since it is
not sensible to consider fractions of a printed circuit. In all linear programming problems you need to
consider if the variables are integers.

Example
A small firm builds two types of garden shed. Type A requires 2 hours of machine time and 5 hours of
craftsman time. Type B requires 3 hours of machine time and 5 hours of craftsman time.
Each day there are 30 hours of machine time available and 60 hours of craftsman time.
The profit on each type A shed is £60 and on each type B shed is £84. Formulate the appropriate linear
programming problem.
Solution
(a) Unknowns
Define
x = number of Type A sheds produced each day,
y = number of Type B sheds produced each day.

(b) Constraints

Machine time: 2 x + 3y ≤ 30

Craftsman time: 5x + 5y ≤ 60

and
x ≥ 0, y ≥ 0

(c) Profit

P = 60 x + 84 y So, in summary, the linear programming


problem is maximize P = 60 x + 84 y
subject to 2 x + 3y ≤ 30
x + y ≤ 12
x ≥0
y ≥0

2.3 Linear Inequalities


In mathematics, inequality occurs when a non-equal comparison is made between two mathematical expressions
or two numbers. In general, inequalities can be either numerical inequality or algebraic inequality or a
combination of both. Linear inequalities are inequalities that involve at least one linear algebraic expression,
that is, a polynomial of degree 1 is compared with another algebraic expression of degree less than or equal to 1.
There are several ways to represent various kinds of linear inequalities. Linear inequalities are defined as
expressions in which two linear expressions are compared using the inequality symbols. The five symbols that
are used to represent the linear inequalities are listed below:

4
Symbol Name Symbol Example

Not equal ≠ x≠3

Less than (<) x + 7 < √2

Greater than (>) 1 + 10x > 2 + 16x

Less than or equal to (≤) y≤4

Greater than or equal to (≥) -3 - √3x ≥ 10

We need to note that if, p < q, then p is some number that is strictly less than q. If p ≤ q, then it means that p is
some number that is either strictly less than q or is exactly equal to q. Likewise, the same applies to the
remaining two inequalities > (greater than) and ≥ (greater than or equal to).
Now, Let's say we have a linear inequality, 3x - 4 < 20. In this case LHS < RHS. We can see that the expression
on the left-hand side, that is, 3x - 4 is in fact lesser than the number on the right-hand side, which is 20. We can
represent this inequality pictorially on a weighing scale as:

2.4 Rules of Linear Inequalities


The 4 types of operations that are done on linear inequalities are addition, subtraction, multiplication, and
division. Linear inequalities with the same solution are called equivalent inequality. There are rules for both
equality and inequality. All the rules mentioned below are also true for inequalities involving lesser than or equal
to (≤), and greater than or equal to (≥). Before learning how to solve linear inequalities, let's look at some of the
important rules of inequality for all these operations.

Addition Rule of Linear Inequalities: As per the addition rule of linear inequalities, adding the same number to each
side of the inequality produces an equivalent inequality, that is the inequality symbol does not change.
If x > y, then x + a > y + a and if x < y, then x + a < y + a.

Subtraction Rule of Linear Inequalities: As per the subtraction rule of linear inequalities, subtracting the same
number from each side of the inequality produces an equivalent inequality, that is the inequality symbol does not
change. If x > y, then x − a > y − a and if x < y, then x − a < y − a.

Multiplication Rule of Linear Inequalities: As per the multiplication rule of linear inequalities, multiplication on both
sides of an inequality with a positive number always produces an equivalent inequality, that is the inequality
symbol does not change. If x > y and a > 0, then x × a > y × a and if x < y and a > 0, then x × a < y × a, Here,
× is used as the multiplication symbol. On the other hand, multiplication on both sides of the inequality with a
negative number does not produce an equivalent inequality unless we also reverse the direction of the inequality
symbol. If x > y and a < 0, then x × a < y × a and if x < y and a < 0, then x × a > y × a.

Division Rule of Linear Inequalities: As per the division rule of linear inequalities, division of both sides of an
inequality with a positive number produces an equivalent inequality, that is the inequality symbol does not
change. If x > y and a > 0, then (x/a) > (y/a) and if x < y and a > 0, then (x/a) < (y/a). On the other hand, the
division of both sides of an inequality with a negative number produces an equivalent inequality if the inequality
symbol is reversed. If x > y and a < 0, then (x/a) < (y/a) and if x < y and a < 0, then (x/a) > (y/a)
5
Chapter Three
Solving System of Linear Inequalities
3.1 Introduction
Solving linear inequalities in multi-step one variable is the same as solving multi-step linear equations; begin by
isolating the variable from the constants. As per the rules of inequalities, while we are solving multi-step linear
inequalities, it is important for us to not forget to reverse the inequality sign when multiplying or dividing with
negative numbers.
Step 1: Simplify the inequality on both sides - on LHS as well as RHS as per the rules of inequality.
Step 2: When the value is obtained, if the inequality is a strict inequality, the solution for x is less than or greater
than the value obtained as defined in the question. And, if the inequality is not a strict inequality, then the
solution for x is less than or equal to or greater than or equal to the value obtained as defined in the question.
Now, let's try solving linear inequalities with an example, to understand the concept. 2x + 3 > 7
To solve this linear inequality, we would follow the steps: 2x > 7 - 3 ⇒ 2x > 4 ⇒ x > 2
The solution to this inequality will be the set of all values of x for which this inequality x > 2 is satisfied, that is,
all real numbers strictly greater than 2.

3.1.1 Solving Linear Inequalities with Variable on Both Sides


Let us try solving linear inequalities with one variable by applying the concept we learned. Consider the
following example. 3x - 15 > 2x + 11
We proceed as follows: -15 - 11 > 2x - 3x ⇒ - 26 > - x ⇒ x > 26

3.1.2 Solving System of Linear Inequalities by Graphing


The system of two-variable linear inequalities is of the form ax + by > c or ax + by ≤ c. The signs of inequalities
can change as per the set of inequalities given. To solve a system of two-variable linear inequalities, we must
have at least two inequalities. Now, to solve a system of linear inequalities in two variables, let us consider an
example.
2y - x > 1 and y - 2x < -1
First, we will plot the given inequalities on the graph. To do that, follow the given steps:
a. Replace the inequality sign with equal to =, that is, we have 2y - x = 1 and y - 2x = -1. Since the linear
inequality is strict, we draw dotted lines on the graph.
b. Check if the origin (0, 0) satisfies the given linear inequalities. If it does, then shade the region on one side
of the line which includes the origin. If the origin does not satisfy the linear inequality, shade the region on
one side of the line which does not include the origin. For 2y - x > 1, substitute (0, 0) we have: 2 × 0 - 0 > 1
⇒ 0 > 1 which is not true. Hence, shade the side of the line 2y - x = 1 which does not include origin.
Similarly, for y - 2x < -1, substituting (0, 0), we have: 0 - 2 × 0 < -1 ⇒ 0 < -1 which is not true. Hence,
shade the side of the line y - 2x = -1 which does not include origin.
c. The common shaded will be the feasible region that forms the solution of the system of linear inequalities.
If there is no common shaded region, then the solution does not exist. The violet-colored region in the
graph given below shows the solution of the given system of linear inequalities.

6
Linear inequalities with one variable are plotted on a number line, as the output gives the solution of one
variable. Hence, graphing linear inequalities in one variable is done using a number line only. On the contrary,
linear inequalities with two variables are plotted on a two-dimensional x and y axis graph, as the output gives the
solution of two variables. Hence, graphing of two-variable linear inequalities is done using a graph.
Let's consider the below example. 4x > -3x + 21
The solution in this case is simple. 4x + 3x > 21 ⇒ 7x > 21 ⇒ x > 3
This can be plotted on a number line as:

Any point lying on the blue part of the number line will satisfy this inequality. Note that in this case, we have
drawn a hollow dot at point 3. This is to indicate that 3 is not a part of the solution set (this is because the given
inequality has a strict inequality). As per the solution obtained, the blue part of the number line satisfies the
inequality. Let's take another example of linear inequalities: 3x + 1 ≤ 7
We proceed as follows: 3x ≤ 7 - 1 ⇒ 3x ≤ 6 ⇒ x ≤ 2
We want to represent this solution set on a number line. Thus, we highlight that part of the number line lying to
the left of 2

We see that any number lying on the red part of the number line will satisfy this inequality and so it is a part of
the solution set for this inequality. Note that we have drawn a solid dot exactly at point 2. This is to indicate that
2 is also a part of the solution set.

3.2 Inequalities in Two Variables


When one expression is given to be greater than or less than another expression, we have an inequality. Linear
inequalities are defined as expressions where two values are compared using inequality symbols. Linear

7
inequalities in two variables represent the inequal relationship between two algebraic expressions which
includes two distinct variables. A linear inequality in two variables is formed when symbols other than equal
to, such as greater than or less than are used to relate two expressions, and two variables are involved.
Here are some examples of linear inequalities in two variables:
2x<3y+2
7x−2y>8
3x+4y+3≤2y−5
y+x≥0

The solution of a linear inequality in two variables, like Ax + By > C, is an ordered pair (x, y) that produces a
true statement when the values of x and y are substituted into the inequality. Solving linear inequalities is the
same as solving linear equations; the difference it holds is of inequality symbol. We solve linear inequalities in
the same way as linear equations.
Step 1: Simplify the inequality on both sides, on LHS as well as RHS as per the rules of inequality.
Step 2: Once the value is obtained, we have:
a. strict inequalities, in which the two sides of the inequalities cannot be equal to each other.
b. non-strict inequalities, in which the two sides of the inequalities can also be equal.
Linear inequalities in two variables have infinite sets or infinitely many ordered pair solutions.
These ordered pairs or the solution sets can be graphed in the appropriate half of a rectangular coordinate plane.
In order to graph inequalities with two variables,
1. Identify the type of inequality (greater than, less than, greater than or equal to, less than or equal to).
2. Graph the boundary line - a dashed (in case of strict inequality) or a solid line (in case of non-strict
inequalities).
3. Choose a test point, most probably (0, 0) or any other point which is not on the boundary.
4. Shade the region accordingly. If the test point solves the inequality, shade the region that contains
it. Otherwise, shade the opposite side of the boundary line.
5. Verify with more number of test points in and out of the region.
Example: Graph the linear equality 2x+3y>7
a. Plot the straight line corresponding to the linear equation 2x+3y=72x+3y=7.
b. Determine any two points (solutions) for this equation: two possible points on the graph can be taken
as A(−1,3),B(2,1)A(−1,3),B(2,1) and plot them on the graph.
c. Determine some specific solutions for the linear inequality 2x+3y>7, which can be as follows
(2,3),(3,1),(4.5,0),(0,3),(1.5,2)
d. Plot these five points on the same graph.

8
All the five points (corresponding to the five solutions) lie above the line.
a. Take any point which lies above the line. Its coordinates, say (x0,y0),(x0,y0),, will satisfy the inequality:
2x0+3y0>7
b. This means that the solution set for the inequality consists of all points lying above the line.
c. Put x = 0, y = 0, which gives 2(0) + 3(0) > 7, which further gives 0 > 7.This doesn't holds true for the
given inequality. So, shade the half plane which doesn't include the point (0, 0).

Example 1
Golden's mother hands over $7 to him for chocolates. She tells him to spend only $7 or less than that.
A milk chocolate costs $2 while a nuts chocolate costs $3. Let x be the number of milk chocolates and y be the
number of nuts chocolates. Form an inequality corresponding to the above situation and graph the inequality.

Solution
2x+3y≤72x+3y≤7will be the inequality corresponding to the above situation.
In this case, we will plot a solid line as boundary joining the points which satisfy the linear equation 2x+3y=7
For 2x+3y=7
x 2 5 -7
y 1 -1 7
For inequality2x+3y≤7
a. Determine some specific solutions for the linear inequality 2x+3y≤72x+3y≤7 which can be as
follows: (0,1),(−4,0),(1,0),(−5,1),(2,−1)
b. Plot these points. These will lie below the solid line.
Now, put x = 0, y = 0. This gives, 2(0) + 3(0) ≤ 7, which satisfies the inequality. So, shade the half plane in the
linear inequality graph below, which includes the point (0,0).

9
Example 2 Graph the solution set for y>−5x+2
Solution: y>−5x+2 is an inequality in slope intercept form; slope=-5, intercept=2. In this case, we will plot a
dashed line because of a less than or equal to equality, as boundary joining the points which satisfy the linear
equation y=−5x+2. For y=−5x+2

x 0 1 2 -1
y 2 -3 -8 7
For the inequality y>−5x+2
a. Determine some specific solutions for the linear inequality y>−5x+2y>−5x+2 which can be as
follows: (1,2),(3,−2),(4,3),(4,5),(3,6)(1,2),(3,−2),(4,3),(4,5),(3,6)
b. Plot these points. These will lie above the dotted line.
For y>−5x+2 put x=0, y=0. This gives 0>-5(0)+2, which further gives 0>2
This doesn't hold true for the given inequality. So, shade the half plane in the linear inequality graph below,
which doesn't includes the point (0,0).

10
Chapter Four
Solved Examples
Question 1. A company is involved in the production of two items (X and Y). The resources needed to
produce X and Y are twofold, namely machine time for automatic processing and craftsman time for hand
finishing. The table below gives the number of minutes required for each item:
Machine time Craftsman time
Item X 13 20
Y 19 29
The company has 40 hours of machine time available in the next working week but only 35 hours of craftsman
time. Machine time is costed at £10 per hour worked and craftsman time is costed at £2 per hour worked. Both
machine and craftsman idle times incur no costs. The revenue received for each item produced (all production is
sold) is £20 for X and £30 for Y. The company has a specific contract to produce 10 items of X per week for a
particular customer.
a. Formulate the problem of deciding how much to produce per week as a linear program.
b. Solve this linear program graphically.
11
Solution
Let
a. x be the number of items of X
b. y be the number of items of Y
then the LP is: maximize
a. 20x + 30y - 10(machine time worked) - 2(craftsman time worked)
subject to:
a. 13x + 19y <= 40(60) machine time
b. 20x + 29y <= 35(60) craftsman time
c. x >= 10 contract
d. x,y >= 0
so that the objective function becomes
maximise
a. 20x + 30y - 10(13x + 19y)/60 - 2(20x + 29y)/60
i.e. maximise
a. 17.1667x + 25.8667y
subject to:
a. 13x + 19y <= 2400
b. 20x + 29y <= 2100
c. x >= 10
d. x,y >= 0
It is plain from the diagram below that the maximum occurs at the intersection of x=10 and 20x + 29y <= 2100
Solving simultaneously, rather than by reading values off the graph, we have that x=10 and y=65.52 with the
value of the objective function being £1866.5

Question 2. A company manufactures two products (A and B) and the profit per unit sold is £3 and £5
respectively. Each product has to be assembled on a particular machine, each unit of product A taking 12
12
minutes of assembly time and each unit of product B 25 minutes of assembly time. The company estimates that
the machine used for assembly has an effective working week of only 30 hours (due to
maintenance/breakdown).
Technological constraints mean that for every five units of product A produced at least two units of product B
must be produced.
a. Formulate the problem of how much of each product to produce as a linear program.
b. Solve this linear program graphically.
c. The company has been offered the chance to hire an extra machine, thereby doubling the effective
assembly time available. What is the maximum amount you would be prepared to pay (per week) for the
hire of this machine and why?
Solution
Let
xA = number of units of A produced
xB = number of units of B produced
then the constraints are:
12xA + 25xB <= 30(60) (assembly time)
xB >= 2(xA/5)
i.e. xB - 0.4xA >= 0
i.e. 5xB >= 2xA (technological)
where xA, xB >= 0
and the objective is
maximise 3xA + 5xB
It is plain from the diagram below that the maximum occurs at the intersection of 12xA + 25xB = 1800 and xB -
0.4xA = 0

13
Solving simultaneously, rather than by reading values off the graph, we have that:
xA= (1800/22) = 81.8
xB= 0.4xA = 32.7
with the value of the objective function being £408.9
Doubling the assembly time available means that the assembly time constraint (currently 12xA + 25xB <= 1800)
becomes 12xA + 25xB <= 2(1800) This new constraint will be parallel to the existing assembly time constraint
so that the new optimal solution will lie at the intersection of 12xA + 25xB = 3600 and xB - 0.4xA = 0
i.e. at xA = (3600/22) = 163.6
xB= 0.4xA = 65.4
with the value of the objective function being £817.8
Hence we have made an additional profit of £(817.8-408.9) = £408.9 and this is the maximum amount we would
be prepared to pay for the hire of the machine for doubling the assembly time. This is because if we pay more
than this amount then we will reduce our maximum profit below the £408.9 we would have made without the
new machine.

Question 3. Solve
minimise
4a + 5b + 6c
subject to
a + b >= 11
a - b <= 5
c-a-b=0
14
7a >= 35 - 12b
a >= 0 b >= 0 c >= 0
Solution
To solve this LP we use the equation c-a-b=0 to put c=a+b (>= 0 as a >= 0 and b >= 0) and so the LP is
reduced to
minimise
4a + 5b + 6(a + b) = 10a + 11b
subject to
a + b >= 11
a - b <= 5
7a + 12b >= 35
a >= 0 b >= 0
From the diagram below the minimum occurs at the intersection of a - b = 5 and a + b = 11
i.e. a = 8 and b = 3 with c (= a + b) = 11 and the value of the objective function 10a + 11b = 80 + 33 = 113.

Question 4. Solve the following linear program:


maximise 5x1 + 6x2
subject to
x1 + x2 <= 10
x1 - x2 >= 3
5x1 + 4x2 <= 35
x1 >= 0
x2 >= 0
Solution
It is plain from the diagram below that the maximum occurs at the intersection of
15
5x1 + 4x2 = 35 and
x1 - x2 = 3
Solving simultaneously, rather than by reading values off the graph, we have that
5(3 + x2) + 4x2 = 35
i.e. 15 + 9x2 = 35
i.e. x2 = (20/9) = 2.222 and
x1 = 3 + x2 = (47/9) = 5.222
The maximum value is 5(47/9) + 6(20/9) = (355/9) = 39.444

Question 5. A carpenter makes tables and chairs. Each table can be sold for a profit of £30 and each chair
for a profit of £10. The carpenter can afford to spend up to 40 hours per week working and takes six hours to
make a table and three hours to make a chair. Customer demand requires that he makes at least three times as
many chairs as tables. Tables take up four times as much storage space as chairs and there is room for at most
four tables each week.
Formulate this problem as a linear programming problem and solve it graphically.
Solution
Variables
Let
xT = number of tables made per week
xC = number of chairs made per week
Constraints
a. total work time
6xT + 3xC <= 40
a. customer demand
xC >= 3xT
a. storage space
16
(xC/4) + xT <= 4
a. all variables >= 0
Objective
maximise 30xT + 10xC
The graphical representation of the problem is given below and from that we have that the solution lies at the
intersection of
(xC/4) + xT = 4 and 6xT + 3xC = 40
Solving these two equations simultaneously we get xC = 10.667, xT = 1.333 and the corresponding profit =
£146.667

17
Chapter Five
Sample Python Programs
5.1 Introduction
A few Python project solution for solving linear optimization problems using the PuLP library are presented
here. The PuLP library is a popular tool for linear programming in Python.
Step 1: Install the PuLP Library. First, ensure you have the PuLP library installed. You can install it using pip:
pip install pulp
Step 2: Define the Linear Optimization Problems
We will solve two linear optimization problems as examples:
1. Maximization Problem (Graphical Method Example)
a. Objective: Maximize Z=3x1+
b. Constraints: x1+x2≤4
x1≤2
x2≤3
x1,x2≥0
2. Maximization Problem (Simplex Method Example)
a. Objective: Maximize Z=3x1+5x2
b. Constraints: x1+2x2≤8
3x1+2x2≤12
x1,x2≥0

Step 3: Solve the Problems Using PuLP


Here is the Python code to solve the above problems using the PuLP library:

# Import the PuLP library


import pulp

# Problem 1: Maximization Problem (Graphical Method Example)

# Create the problem variable to contain the problem data


problem1 = pulp.LpProblem("Maximize Z", pulp.LpMaximize)

# Decision variables
x1 = pulp.LpVariable('x1', lowBound=0, cat='Continuous')
x2 = pulp.LpVariable('x2', lowBound=0, cat='Continuous')

# Objective function
problem1 += 3 * x1 + 2 * x2, "Z"

# Constraints
problem1 += x1 + x2 <= 4
problem1 += x1 <= 2
problem1 += x2 <= 3

# Solve the problem


problem1.solve()

# Print the results


print("Problem 1 (Graphical Method Example):")
print("Status:", pulp.LpStatus[problem1.status])
18
print("Optimal Solution:")
print("x1 =", pulp.value(x1))
print("x2 =", pulp.value(x2))
print("Objective (Z) =", pulp.value(problem1.objective))

# Problem 2: Maximization Problem (Simplex Method Example)

# Create the problem variable to contain the problem data


problem2 = pulp.LpProblem("Maximize Z", pulp.LpMaximize)

# Decision variables
x1 = pulp.LpVariable('x1', lowBound=0, cat='Continuous')
x2 = pulp.LpVariable('x2', lowBound=0, cat='Continuous')

# Objective function
problem2 += 3 * x1 + 5 * x2, "Z"

# Constraints
problem2 += x1 + 2 * x2 <= 8
problem2 += 3 * x1 + 2 * x2 <= 12

# Solve the problem


problem2.solve()

# Print the results


print("\nProblem 2 (Simplex Method Example):")
print("Status:", pulp.LpStatus[problem2.status])
print("Optimal Solution:")
print("x1 =", pulp.value(x1))
print("x2 =", pulp.value(x2))
print("Objective (Z) =", pulp.value(problem2.objective))

Explanation of the Code


1. Importing PuLP: We start by importing the pulp library.
2. Problem Definition:
a. We define the optimization problems using pulp.LpProblem, specifying the problem name and
the objective (maximization or minimization).
b. We define the decision variables using pulp.LpVariable, setting their lower bounds (in this case,
0).
c. We add the objective function to the problem using +=.
d. We add the constraints to the problem using +=.
3. Solving the Problem:
a. We solve the problem using the solve method.
b. We print the status, optimal solution values for decision variables, and the optimal objective
function value.
Output
When you run the above code, you should see the optimal solutions for both linear optimization problems:

Problem 1 (Graphical Method Example):


Status: Optimal
Optimal Solution:
19
x1 = 2.0
x2 = 2.0
Objective (Z) = 10.0

Problem 2 (Simplex Method Example):


Status: Optimal
Optimal Solution:
x1 = 2.0
x2 = 3.0
Objective (Z) = 21.0
This output indicates that both problems were solved optimally, and it provides the values of the decision
variables and the objective function at the optimal solution.

5.2 Solved Examples


Example 2: Minimization Problem
Problem: Minimize Z=4x1+3x2
Subject to:
2x1+x2≥8
x1+x2≥6
x1,x2≥0

Solution:
# Import the PuLP library
import pulp

# Create the problem variable to contain the problem data


Problem2 = pulp.LpProblem("Minimize Z", pulp.LpMinimize)

# Decision variables
x1 = pulp.LpVariable('x1', lowBound=0, cat='Continuous')
x2 = pulp.LpVariable('x2', lowBound=0, cat='Continuous')

# Objective function
Problem2 += 4 * x1 + 3 * x2, "Z"

# Constraints
Problem2 += 2 * x1 + x2 >= 8
Problem2 += x1 + x2 >= 6

# Solve the problem


Problem2.solve()

# Print the results


print("Problem 2 (Minimization Example):")
print("Status:", pulp.LpStatus[problem3.status])
print("Optimal Solution:")
print("x1 =", pulp.value(x1))
print("x2 =", pulp.value(x2))
print("Objective (Z) =", pulp.value(problem2.objective))

20
Example 3: Resource Allocation Problem
Problem: A company produces two products, P1 and P2. Each product requires different amounts of two
resources, R1 and R2. The objective is to maximize profit.
1. Profit for P1: $5 per unit
2. Profit for P2: $4 per unit
3. Availability of R1: 20 units
4. Availability of R2: 40 units
5. P1 requires 2 units of R1 and 1 unit of R2
6. P2 requires 1 unit of R1 and 2 units of R2
Formulation: Maximize Z=5x1+4x2
Subject to:
2x1+x2≤20
x1+2x2≤40
x1,x2≥0

Solution:
# Create the problem variable to contain the problem data
problem4 = pulp.LpProblem("Maximize Profit", pulp.LpMaximize)

# Decision variables
x1 = pulp.LpVariable('P1', lowBound=0, cat='Continuous')
x2 = pulp.LpVariable('P2', lowBound=0, cat='Continuous')

# Objective function
problem4 += 5 * x1 + 4 * x2, "Profit"

# Constraints
problem4 += 2 * x1 + x2 <= 20
problem4 += x1 + 2 * x2 <= 40

# Solve the problem


Problem3.solve()

# Print the results


print("\nProblem 3 (Resource Allocation Example):")
print("Status:", pulp.LpStatus[problem4.status])
print("Optimal Solution:")
print("P1 =", pulp.value(x1))
print("P2 =", pulp.value(x2))
print("Objective (Profit) =", pulp.value(problem4.objective))

Example 4: Diet Problem


Problem: Minimize the cost of a diet that meets the minimum daily requirements for protein, carbohydrates,
and fat. The diet consists of two foods, F1 and F2, with the following costs and nutritional content:
a. Cost of F1: $3 per unit
b. Cost of F2: $2 per unit
c. F1 contains: 2g protein, 1g carbohydrates, 3g fat per unit
d. F2 contains: 1g protein, 2g carbohydrates, 2g fat per unit
e. Minimum daily requirements: 8g protein, 10g carbohydrates, 15g fat
Formulation: Minimize Z=3x1+2x2
21
Subject to:
2x1+x2≥8
x1+2x2≥10
3x1+2x2≥15
x1,x2≥0

Solution:
# Create the problem variable to contain the problem data
Problem4 = pulp.LpProblem("Minimize Cost", pulp.LpMinimize)

# Decision variables
x1 = pulp.LpVariable('F1', lowBound=0, cat='Continuous')
x2 = pulp.LpVariable('F2', lowBound=0, cat='Continuous')

# Objective function
Problem4 += 3 * x1 + 2 * x2, "Cost"

# Constraints
Problem4 += 2 * x1 + x2 >= 8
Problem4 += x1 + 2 * x2 >= 10
Problem4 += 3 * x1 + 2 * x2 >= 15

# Solve the problem


Problem4.solve()

# Print the results


print("\nProblem 4 (Diet Example):")
print("Status:", pulp.LpStatus[problem5.status])
print("Optimal Solution:")
print("F1 =", pulp.value(x1))
print("F2 =", pulp.value(x2))
print("Objective (Cost) =", pulp.value(problem4.objective))

Example 5: Transportation Problem


Problem: A company needs to transport goods from two warehouses (W1 and W2) to three retail stores (S1,
S2, and S3). The supply at each warehouse and demand at each store, as well as the transportation costs, are
given. The objective is to minimize the total transportation cost.
a. Supply at W1: 30 units
b. Supply at W2: 20 units
c. Demand at S1: 10 units
d. Demand at S2: 20 units
e. Demand at S3: 20 units
f. Transportation costs (per unit):
a. W1 to S1: $2
b. W1 to S2: $3
c. W1 to S3: $1
d. W2 to S1: $4
e. W2 to S2: $2
f. W2 to S3: $5
Formulation: Minimize Z=2x11+3x12+1x13+4x21+2x22+5x23
Subject to:
22
x11+x12+x13≤30
x21+x22+x23≤20
x11+x21≥10
x12+x22≥20
x13+x23≥20
xij≥0

Solution:
# Create the problem variable to contain the problem data
Problem5 = pulp.LpProblem("Minimize Transportation Cost", pulp.LpMinimize)

# Decision variables
x11 = pulp.LpVariable('W1_to_S1', lowBound=0, cat='Continuous')
x12 = pulp.LpVariable('W1_to_S2', lowBound=0, cat='Continuous')
x13 = pulp.LpVariable('W1_to_S3', lowBound=0, cat='Continuous')
x21 = pulp.LpVariable('W2_to_S1', lowBound=0, cat='Continuous')
x22 = pulp.LpVariable('W2_to_S2', lowBound=0, cat='Continuous')
x23 = pulp.LpVariable('W2_to_S3', lowBound=0, cat='Continuous')

# Objective function
Problem5 += 2 * x11 + 3 * x12 + 1 * x13 + 4 * x21 + 2 * x22 + 5 * x23, "Transportation Cost"

# Constraints
Problem5 += x11 + x12 + x13 <= 30 # Supply at W1
Problem5 += x21 + x22 + x23 <= 20 # Supply at W2
Problem5 += x11 + x21 >= 10 # Demand at S1
Problem5 += x12 + x22 >= 20 # Demand at S2
Problem5 += x13 + x23 >= 20 # Demand at S3

# Solve the problem


Problem5.solve()

# Print the results


print("\nProblem 6 (Transportation Example):")
print("Status:", pulp.LpStatus[problem6.status])
print("Optimal Solution:")
print("W1 to S1 =", pulp.value(x11))
print("W1 to S2 =", pulp.value(x12))
print("W1 to S3 =", pulp.value(x13))
print("W2 to S1 =", pulp.value(x21))
print("W2 to S2 =", pulp.value(x22))
print("W2 to S3 =", pulp.value(x23))
print("Objective (Transportation Cost) =", pulp.value(problem6.objective))
These examples demonstrate how to solve various linear programming problems using Python and the PuLP
library. The approach includes defining the problem, setting up decision variables, objective functions, and
constraints, and then solving the problem to obtain optimal solutions. This output indicates that both problems
were solved optimally, and it provides the values of the decision variables and the objective function at the
optimal solution.

23
5.3 Study Questions and Answers
Question 1: What is the feasible region in a linear programming problem?
Answer: The feasible region is the set of all possible points that satisfy all the constraints of a linear
programming problem. It is typically a convex polygon or polyhedron in the decision variable space.

Question 2: How do you determine the optimal solution using the graphical method?
Answer: To determine the optimal solution using the graphical method:
1. Plot the constraints on a graph.
2. Identify the feasible region.
3. Draw the objective function and move it parallel until the last point in the feasible region is touched.
4. The coordinates of this point give the optimal solution.

Question 3: What is a slack variable, and why is it used in the simplex method?
Answer: A slack variable is added to a less-than-or-equal-to constraint to convert it into an equality. It
represents the unused portion of the resource. In the simplex method, slack variables are used to transform
inequalities into equalities, allowing the problem to be solved using matrix operations.

Question 4: Explain the significance of the pivot operation in the simplex method.
Answer: The pivot operation in the simplex method is used to move from one vertex of the feasible region to
an adjacent vertex. It involves selecting an entering variable (which will increase) and a leaving variable (which
will decrease to zero), updating the tableau accordingly. This operation continues iteratively until no further
improvements can be made, leading to the optimal solution.

Question 5: What is sensitivity analysis in the context of linear programming?


Answer: Sensitivity analysis in linear programming examines how the optimal solution changes as the
coefficients of the objective function or the right-hand side values of the constraints change. It helps in
understanding the robustness of the optimal solution and in decision-making under uncertainty.

Question 5: What is the objective function in linear programming?


Answer: The objective function in linear programming is a linear function that needs to be maximized or
minimized. It is typically represented in the form \( c^T x \), where \( c \) is a vector of coefficients and \( x \)
is a vector of decision variables.

Question 5: Explain the Simplex Method in linear programming.


Answer: The Simplex Method is an iterative algorithm used to solve linear programming problems. It works by
moving along the edges of the feasible region (defined by the constraints) to find the optimal solution. The
method starts at an initial feasible solution and moves to adjacent vertices with a higher (or lower for
minimization) value of the objective function until the optimal solution is reached.

Question 5: What is the difference between linear and nonlinear programming?


Answer: The main difference between linear and nonlinear programming is the form of the objective function
and constraints. In linear programming, both the objective function and the constraints are linear. In nonlinear
programming, either the objective function or the constraints (or both) are nonlinear.

Question 5: Describe the Gradient Descent method in nonlinear optimization.


Answer: Gradient Descent is an iterative optimization algorithm used to minimize a function. Starting from an
initial point, it moves in the direction of the negative gradient of the function at the current point to find the
local minimum. The step size (learning rate) determines how far to move in the direction of the gradient.

Question 5: What are the key features of integer programming?


Answer: Key features of integer programming include: Decision variables are restricted to integer values. Often
24
used for problems involving discrete choices. Solutions are found using methods like Branch and Bound or
Cutting Planes.

Question 5: Explain the concept of Pareto Optimality in multi-objective optimization.


Answer: Pareto Optimality in multi-objective optimization refers to a situation where no objective can be
improved without worsening at least one other objective. A solution is Pareto optimal if there is no other
solution that is better in all objectives.

Question 5: What is the Traveling Salesman Problem (TSP) and which category of optimization does it fall
under?
Answer: The Traveling Salesman Problem (TSP) is a combinatorial optimization problem where the goal is to
find the shortest possible route that visits a set of cities and returns to the origin city. It falls under the category
of combinatorial optimization.

25

You might also like