Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Formulating a linear programming problem

After this lesson: I can write a decision as variables, an objective and constraints.

55 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

How much of each should the bakery make?

Operations research is the use of mathematics to make the best choice when resources are limited. A bakery with one oven and a sack of flour is a typical case.

Before anybody can find the best plan, the choice must be written down as a model. You will learn to write one from a short story.

By the end of this lesson

Note

I can write a decision as variables, an objective and constraints.

Key words

WordWhat it meansExample
Decision variablea number you are free to choosex = 4
Objectivethe quantity you want as large or as small as possible
Constrainta limit that every plan must respect
Feasible plana plan that breaks no constraint
Linear programa model whose objective and constraints are all straight-line sums

The cells students fill in are left blank in this preview.

A model has variables, an objective and constraints

The bakery makes batches of loaves and batches of cakes. Let x be the loaf batches and y the cake batches. These are the decision variables: the numbers the baker chooses.

The objective is the profit to make as large as possible. The constraints are the limits: oven hours and kilograms of flour. Nobody can bake a negative batch, so x and y are never below 0.

Worked example: the bakery in one table

ItemOne loaf batchOne cake batchAvailable
Oven hours128
Flour in kg3218
Profit in pounds2030make it large

The cells students fill in are left blank in this preview.

The table becomes three lines of algebra

Read the table row by row. The oven row says a loaf batch uses 1 hour and a cake batch uses 2. So the hours used are x + 2y, and this must not pass 8.

The flour row gives 3x + 2y <= 18. The profit row gives the objective: maximise 20x + 30y.

Check a plan against the model

Code: python
loaves = 4
cakes = 2

oven_used = loaves + 2 * cakes
flour_used = 3 * loaves + 2 * cakes
profit = 20 * loaves + 30 * cakes

print(oven_used, "of 8 oven hours")
print(flour_used, "of 18 kg of flour")
print(profit, "pounds")
# Change loaves to 5 and cakes to 2. Which limit does the plan now break?

A feasible plan breaks no constraint

A feasible plan meets every constraint at once. The plan of 4 loaf batches and 2 cake batches uses 8 oven hours and 16 kg of flour, so it is feasible.

The plan of 5 and 2 needs 9 oven hours, which is too many. It is infeasible, even though its profit looks good.

Test the plan of 4 and 2

Command
docker exec jupyter python3 -c "x = 4; y = 2; print(x + 2 * y <= 8, 3 * x + 2 * y <= 18, 20 * x + 30 * y)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Test the plan of 5 and 2

Command
docker exec jupyter python3 -c "x = 5; y = 2; print(x + 2 * y <= 8, 3 * x + 2 * y <= 18, 20 * x + 30 * y)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think every limit must be used up exactly, so they write an equals sign. A limit is a ceiling. A plan may leave oven time unused and still be feasible. Write <= for a ceiling and use = only when a quantity must be hit exactly.

A diet or timetable has the same parts

A diet problem chooses grams of two foods. The objective is the cost to make as small as possible, and the constraints are minimums of protein and energy, written with >=.

A timetable problem chooses how many hours each person works. The constraints say every shift is covered and nobody works too long. The method is the same: variables, objective, constraints.

Worked example: a cheap meal as a model

PartIn wordsIn symbols
VariablesPortions of rice and of beansr and b
ObjectiveSpend as little as possible: 40p and 60p a portionminimise 0.4r + 0.6b
ConstraintAt least 20 g of protein: 4 g and 10 g a portion4r + 10b >= 20
ConstraintNo negative portionsr >= 0 and b >= 0

The cells students fill in are left blank in this preview.

Going further

In the code block, try three plans of your own. Find one that is feasible and earns more than 140 pounds, if you can.

Then write the model of your own week. Choose two things to spend time on and name a limit on your hours. Say what you want as large as possible.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.