# Routing Using Machine Learning

Routing Out by ML is available from Simul8 2022, allowing you to use machine learning algorithms to control routing rules in your simulation. Routing Out by ML is not available in Simul8 Online.

Note: your Machine Learning algorithm must return a positive integer greater than 0.

### Setup

To route work items using Machine Learning algorithms, you can either use R or Python.

If using R, the ML algorithm must be part of a function and the function saved as an .RDS file. The function must also be able to read in a dataframe, with the parameter name in column 1 and the parameter value in column 2.

If using Python, the algorithm must be saved in a .py file and the function must be called prediction. It also must take in two lists as the arguments.

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 we need to add the simulation parameters the algorithm needs.

Click Add, this will open a new dialog. Give you parameter a name and a value – the value will usually be a label, spreadsheet location or object property.

### Tutorial

In this tutorial we will show you how you can use Machine Learning to control routing decisions in a simulation.
What you will need to complete this tutorial:

#### Step 1: create your ML algorithm

We will use a Decision Tree to create a ML 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, then Run the script.

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

```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. Now 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”)```

#### Step 3: apply the ML 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 and find the GetRouteRF.rds 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.

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 ML algorithm we have created.

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