Routing Using Machine Learning

Routing Out by ML is available from Simul8 2022 onwards, allowing you to use machine learning algorithms to control routing rules in your simulation. It can be accessed through the Routing Out dialog as shown below:

Simul8 Machine Learning Routing Out

Setup

To route work items using machine learning algorithms, Simul8 connects your simulation to an external program. You can either use R or Python.

If you use R, the machine learning algorithm must be part of a function which is saved as an .RDS file. It must also be able to read in a dataframe, with the parameter name in column 1 and the parameter value in column 2.

If you use Python, the algorithm must be saved in a .py file and the function must be called prediction. The prediction function takes in a set of values and uses the algorithm to tell Simul8 the route to send the Work Item. This prediction function must also take in two lists as the arguments.

Note: in both cases, your machine learning algorithm must return a positive integer greater than 0. This means that the variable the algorithm is trained to return, must have positive integers over 0 as its label values.


To use machine learning for routing, click on an Activity > Routing Out > By ML. By default, Simul8 will use R. If you want to use Python, click on Advanced Settings. In the Setup tab, click on Browse and select the file which contains your algorithm.

Then add the simulation parameters the algorithm needs. Click Add, this will open a new dialog. Give your parameter a name and a value – the value will usually be a label, spreadsheet location or object property. The name refers to the variable that is used to train the algorithm. It is important that the spelling is the same as when training the algorithm.

Finally, click OK, reset and run your simulation. The routing decision on the chosen Activity will now follow the machine learning algorithm created in accordance with the parameter values in the simulation.

Tutorial using R

In this tutorial we will show you how you can use machine learning to control routing decisions in a simulation with R.


What you will need to complete this tutorial:

Step 1: Create your Machine Learning algorithm

We will use a Decision Tree to create a machine learning algorithm, based on the data in the DT_Data file.

Open R and copy and paste the script below in your R console, making sure you update the directory to where you saved the DT_Data.xlsx file. Run the script.

Note: make sure each folder in your directory is separated by two backslashes (\\)

# Remember to install the packages before loading them

library(readxl)
library(rpart)
library(rpart.plot)
#change this directory to one where you have saved DT_Data.xlsx
directory = “C:\\Users\\yourname\\Downloads”
path = (paste(directory,“\\DT_Data.xlsx”,sep = “”))
DTData = as.data.frame(read_excel(path,sheet = “Sheet1”))
set.seed(1234)


tree = rpart(Route ~., data = DTData)
rpart.plot(tree)
path = (paste(directory,“\\GetRouteDT.rds”,sep = “”))
saveRDS(tree,path)

Step 2: Create a prediction function

Now open a new R Script, copy and paste the below script into the console, and update the directories. Then, run the script.

Routing = function(df){
Priority = (df[1,2])
Product = (df[2,2])
#change this rds file to the same .RDS you have just created
algorithm = readRDS(“C:\\Users\\yourname\\Downloads\\GetRouteDT.rds”)
data = data.frame(Priority,Product)
return(predict(algorithm,data))
}

#Change this directory to a location on your machine. This is the file you will use for Simul8
saveRDS(Routing,“C:\\Users\\yourname\\Desktop\\GetRouteRF.rds”)

The .rds file has now been created.

Step 3: Apply the machine learning algorithm to the simulation

Open the Mail_Sorting simulation, click on the Sorting Activity and open the Routing Out dialog. Select By ML. Click on Browse to find the GetRouteRF.rds file you saved in step 2.

Simul8 Route by Machine Learning

Click on Add and enter the parameters. Type the Name (Priority), then click on the Value field and on the button to its right – this will open the Formula Editor. Choose Labels and double-click on lblPriority, then click OK. lblPriority is one of the labels assigned to the Work Item at the Start Point and is used by the algorithm to assign a route, this is also the case for lblProductType.

Simul8 Route by Machine Learning

Now do the same for ProductType. Enter the name as ProductType, open the Formula Editor from the Value field, choose Labels and double-click on lblProductType. Click OK.


Reset and run your simulation. The routing decision on the Sort Activity will now follow the machine learning algorithm we have created.

Tutorial using Python

In this tutorial we will show you how you can use machine learning to control routing decisions in a simulation with Python.


What you will need to complete this tutorial:

Step 1: Create your machine learning algorithm

Open your preferred Python interface, such as Jupiter notebook or Visual studio, in this example we will use Jupiter. We will use a Decision Tree to create a ML algorithm based on the data in the CheckOut_Data file. Copy and paste the script below into your notebook.

#First, we need to load in the packages needed for a decision tree

import pandas as pd

from sklearn import tree

from sklearn.tree import DecisionTreeClassifier

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'\dt_data.csv')

features = ['Product','Priority']

X = df[features].values

y = df['Route']

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

from sklearn.svm import SVC

from sklearn.pipeline import make_pipeline

from sklearn.preprocessing import StandardScaler

dtree=clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))

dtree=clf.fit(X, y)

dtree = DecisionTreeClassifier(max_depth = 2,max_leaf_nodes=10)

dtree = dtree.fit(X,y)

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

filename = filepath+r'\MailDT.sav'

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

Step 2: Create a prediction function

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 the scripts as a .py file.

import pickle

import os

def prediction(list1,list2):

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

filename = filepath+r'\MailDT.sav'

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

result = loaded_model.predict([list2])

return result[0]

You can see that with Python, the script for building a prediction model is shorter than in R. However, it is necessary that Python is installed directly on your machine together with all necessary packages through the Command Prompt.

Step 3: Apply the Machine Learning algorithm in the simulation

Open the Mail_Sorting simulation, click on the Sorting Activity and open the Routing Out dialog. Select By ML. Then select the Advanced Settings tab and select Python, go back to Setup. Click on Browse, and find the prediction.py script file you saved in step 2.


Click on Add and enter the parameters. Type the Name (Priority), then click on the Value field and on the button to its right – this will open the Formula Editor. Choose Labels and double-click on lblPriority, then click OK. lblPriority is one of the labels assigned to the Work Item at the Start Point and is used by the algorithm to assign a route, this is also the case for lblProductType.

Simul8 Route by Machine Learning

Now do the same for ProductType. Enter the name as ProductType, open the Formula Editor from the Value field, choose Labels and double-click on lblProductType. Click OK.

After resetting, the Sorting Activity now uses your Python-trained machine learning algorithm to make routing decisions.

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

Note creating a distribution using ML is not available in Simul8 Online.

See Also