Timing via ML: Airport Check-in Example

In this example we will be using machine learning to calculate the time passengers spend in an airport check-in desk conditional on some of their characteristics. These include how many children they may have with them or if they have a carry-on bag and so on.

To follow the example, download:

Machine Learning Example Simul8

The model is a simple simulation of an airport check-in desk section, using machine learning to predict the timings for each passenger group going through check-in.

In this example, both R and Python are used to set the timing predictions.

We explain both in more detail in the following two tutorial sections.

Tutorial using R

Step 1

To integrate machine learning into the simulation, R Studio was used to train the algorithm and write the routing formula for Simul8 to read. The data was imported into R and the model was trained using the preinstalled linear regression function lm(). This model was then saved in R as shown in the script below.

#Load in the data set and packages

library(dplyr)

#Change this directory to one where you have saved Airport_Data.csv

Data = read.csv('C:\\Users\\YourName \\Downloads
Airport_Data.csv')

#Train the linear regression algorithm

Timelm ← lm(Time ~., data = Data)

summary(Timelm)

# Save the model for the function needed in Simul8 use the code below

saveRDS(Timelm,“GetTimelm.rds”)

For Simul8 to read the algorithm and apply the routing, the below script is needed.

# Add routing function to read though Simul8

# These need to be the same exact names as in the data set used to train

the Machine Learning model

Routing = function(df){

Adults = (df[1,2])

Children = (df[2,2])

CarryOn = (df[3,2])

FrequentFlyer = (df[4,2])

algorithm = readRDS(C:\\Users\\YourName\\Downloads\\GetTimelm.rds')

data = data.frame(Adults, Children, CarryOn, FrequentFlyer)

return(predict(algorithm,data))

}

#The file you will use for Simul8

saveRDS(Routing,“CheckInTimmings.rds”)

The names from the dataset are used when training the algorithm and correspond to the Labels of work items in the simulation when these enter the check-in Activity. The simulation then calls the algorithm which will feed back a numerical results which is then used for setting the timing within in the check-in Activity.

Note: the directory should be changed in both scripts to reflect where the .RDS and Excel files have been saved on your machine.

Step 2

To connect the scripts to Simul8, go to Data & Rules and select Create Distribution. Then select the machine learning option as highlighted below.

Machine Learning Example Simul8

From there, select the routing script from the Browse section and add in the parameters used to train the algorithm, then connect them to their corresponding value in the simulation, usually a label or global variable. Next, save the distribution.

Machine Learning Example Simul8

In the Check-In Desk Activities go to the distribution section and scroll to select the distribution Timing_ML.

Machine Learning Example Simul8

Reset the simulation and run. The check-in time will now depend on the combination of Labels used.

Python tutorial

Step 1

Python can also be used to train the ML algorithm. To do so, open your preferred Python interface such as Jupiter notebook, Visual studio and so forth. In this example we will use Jupiter. Copy and paste the script below into your notebook.

#First, we need to load in the packages needed for a Regression algorithm

import numpy as np

import pandas as pd

from sklearn import preprocessing, svm

from sklearn.linear_model import LinearRegression

import pickle

#Then we lock in the file path, when saving this as a py file by using the download section remove the “” as this is only needed in Jupiter notebook

filepath = os.path.dirname(os.path.abspath(“file”))

# Read in the data and save as a name, in this case df for dataframe was used

df = pd.read_csv(filepath+r'\ Airport_Data.csv')

features = ['Adults', 'Children', 'CarryOn', 'FrequentFlyer']

X = df[features].values

y = df['Time']

# Then make the model based on the x and y data arrays in this case a Liniar regression

regr = LinearRegression() 

Timelm = regr.fit(X, y)

# Save the algorithm as a sav file which we will use in the prediction function

filename = filepath+r'\Timelm.sav'

pickle.dump(Timelm,open(filename,'wb'))

Now open a new Script, copy and paste the below script into the console, and run the script. Remember to change (file) to (“file”) if running in a Jupiter notebook. For Simul8 to be able to run it save your scripts as a .py file .

import pickle

import os

def prediction(list1,list2):

filepath = os.path.dirname(os.path.abspath(file))

filename = filepath+r'\Timelm.sav'

loaded_model = pickle.load(open(filename,'rb'))

result = loaded_model.predict([list2])

return result[0]

When using Python, the script used to predict the route value is shorter than in R. However, to work correctly it is necessary that Python is installed directly onto the machine together with all necessary packages through the Command Prompt.

Step 2

To connect the scripts to Simul8, go to Data & Rules and select Create Distribution. Then select the Machine Learning option as highlighted below.

Machine Learning Example Simul8

In the new dialog go into the Advanced Settings tab and select Python. Then go back to Setup, select the Python Routing script and add the names and values that the algorithm used in training. This set-up is the same as step 2 in the R code tutorial. Finally save the distribution.

Machine Learning Example Simul8

In the Check-In Desk Activities go into the distribution section and scroll to select the distribution Timing_ML for the Desks. Reset the simulation and run it. The check-in time will now depend on the combination of label values for the passengers when they enter the Desk activities.

Having trouble setting up Distribution By ML? Check out our Machine Learning Troubleshooting page for more help.

See Also