Routing via ML: Glass Bottle Sorting
In this example we will be using a machine learning algorithm to sort glass bottles on a bottling line conditional on some of their characteristics. These include their weight as well as condition, such as any cracks or bubbles present or not. Simul8 users will notice that this is essentially a Routing Out problem.
To follow along the example, download:
- R or Python
The model is a simple bottling line sorting out defected bottles before they get passed over to the filling stations. We show how to set this up in both R and Python in the two tutorials below.
Tutorial using R
Step 1
To integrate machine learning into the simulation, R studio was used to train the ML algorithm and write the routing formula for Simul8 to read. The data was imported into R and the model was trained using an ensemble machine learning method called random forest. We used the randomForest package in R. The model was then saved in R as shown in the script below.
#Load in the data set and packages
library(dplyr)
library(randomForest)
#Change this directory to one where you have saved GlassData.csv
Data = read.csv('C:\\Users\\YourName \\Downloads
GlassData.csv')
#Train the linear regression algorithm
DefectRF ← randomForest(formula = Defect ~ ., data = Data)
# Save the model for the function needed in Simul8 use the code below
saveRDS(DefectRF,“GetDefectRF.rds”)
For Simul8 to apply the ML algorithm for 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){
Weight = (df[1,2])
Cracks = (df[2,2])
Bubbles = (df[3,2])
algorithm = readRDS(C:\\Users\\YourName\\Downloads\\GetDefectRF.rds')
data = data.frame(Weight, Cracks, Bubbles)
return(predict(algorithm,data))
}
#The file you will use for Simul8
saveRDS(Routing,“ DefectRFRouting.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 Sorting Station Activity. The Activity then calls the algorithm which will feed back a numerical result to determine the route the work item follows.
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 with the Sorting Station Activity in Simul8, click on the Sorting Station Activity and open the Routing Out dialog in the Properties tab. Choose the By ML option.
In the Algorithm file location box select Browse and locate the routing function DefectRFRouting.rds. Then add the parameters used to train the algorithm and connect them with the values in the simulation such as labels or global variables. Click OK.
Note: make sure the correct value is connected to the correct bin (Queue). In this model a value of 2 should direct to the Faulty Glass bin Queue. You can use the Up and Down arrows to adjust this if necessary.
Reset the simulation and run. The routing out decision of the Sorting Station Activity will now use the machine learning algorithm.
Tutorial using Python
Step 1
The data was imported into Python and the model was trained using an ensemble machine learning method called random forest. This is loaded from the sklearn library in Python. 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 random forest regression
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
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'\GlassData.csv')
features = [' Weight', 'Cracks', 'Bubbles']
X = df[features].values
y = df['Defect']
# Then make the model based on the x and y data arrays in this case a Random forest
clf = RandomForestClassifier(max_depth=2, random_state=0)
GlassRF = clf.fit(X, y)
# Save the algorithm as a sav file which we will use in the prediction function
filename = filepath+r'\ GlassRF.sav'
pickle.dump(GlassRF,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 the scripts as a prediction.py file.
import pickle
import os
def prediction(list1,list2):
filepath = os.path.dirname(os.path.abspath(file))
filename = filepath+r'\ GlassRF.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 on the machine together with all necessary packages through the command prompt.
Step 2
Open the Glass Bottle example simulation. Click onto the Sorting Station Activity and go to the Routing Out section in the Properties tab. This will open a new dialog. In this dialog go to the Machine Learning option. Then click 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 1.
Click on Add and enter the parameters as in the R tutorial and click OK. After resetting, the Sorting Station Activity will now use the machine learning algorithm to make routing decisions.
Having trouble setting up Routing By ML? Check out our Machine Learning Troubleshooting page for more help.