How to Schedule Flights in Python | by Khuyen Tran | Towards Data Science

Imagine you are an owner of an airline. Your airline needs to assign its airplanes based in New York to cover all the upcoming scheduled flights.
There are 10 flights and 8 sequences of flights. Each sequence of flights consists of multiple flights.
For example, a sequence of flights can consist of flights from New York to Buffalo, from Buffalo to Chicago, and from Chicago to New York.
Each flight sequence has a certain cost. We need to select a subset of flight sequences so that there is at least one airplane for each flight.
There are many possible ways to select a combination of flight sequences so that there is a plane available for each flight. One possible combination is like this:
Another possible combination is like this:
And many more.
Which combination of sequences should your airline choose so that the constraint is satisfied and the cost is minimized?
Many major airlines face this problem. American Airlines estimated that their mathematical programming-based system saves about $20 million per year.
In this article, you will learn how to use integer programming and Python to solve this problem.
Integer programming involves problems with objectives and constraints. Some or all of the variables are restricted to be integers.
CVXPY is a Python tool that provides interfaces to many integer programming solvers. To install CXVPY, type:
pip install cvxpy
Let’s identify the inputs, objectives, and constraints in this problem.
Start with downloading the data from Google Drive:
Get the schedule, cost, and hours of each flight sequence.
Turn all positive numbers into 1:
Define input parameters:
Define decision variables using cp.Variable :
There must be at least one airplane per flight. For example, to make sure that there is at least one airplane for the flight from New York → Buffalo, flight sequence 1, 4, or 7 must be chosen.
To make sure that there is at least one airplane for the flight from New York → Cincinnati, either flight sequence 2 or 5 must be chosen.
Thus for the flight from New York → Buffalo, we have the constraint:
Generalize this constraint for all 10 flights, we have:
We want to choose the flight sequences that minimize the total cost.
Now we have constraints and an objective, let’s solve the problem!
13.0
The value of the objective is 13. Let’s see what the values of y are.
[1. 1. 1. 0. 0. 0. 0. 0.]
Cool! Since the first 3 values of the y array are 1's, our airplane saves the most money by choosing sequences of flights 1, 2, and 3.
That means that the airplane should fly:
from New York → Buffalo → Chicago → New York
from New York → Cincinnati → Pittsburgh → New York
from New York → Chicago → Cincinnati → New York
Cool! What if our airplane wants the total hours of flights to be no more than 1700?
Let’s add that constraint to our problem and see what happens.
First we need to get the array representing the number of hours for each flight sequence:
Combine the first constraint with the new constraint:
Solve the problem:
20
The value of the objective with an additional constraint is 20, which is greater than 13. Why does the value of the objective change?
It is because different sequences of flights are chosen.
But why are the second and third sequences of flights are not chosen? If we take a close look at the array representing hours, we can see that the total hours of the first 3 flights exceed 1700.
However, the total hours of the first, fifth, and sixth sequence of flights are smaller than 1700, which satisfies the constraint.
And here is the visual representation of the new chosen flight sequences.
Congratulations! You have just learned how to choose sequences of flights while minimizing the cost. I hope this article will motivate you to solve similar problems using integer programming and Python.
Feel free to fork and play with the code for this article in this Github repo:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Chen, D.-S., Batson, R. G., & Dang, Y. (2010). Applied integer programming: modeling and solution. J. Wiley & Sons.