Sample application 5 used in IEEE Transactions on Intelligent Transportation Systems is added.
This commit is contained in:
1
scripts/sample_app5/ai_trainer/.gitignore
vendored
Normal file
1
scripts/sample_app5/ai_trainer/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.class
|
23
scripts/sample_app5/ai_trainer/README.md
Normal file
23
scripts/sample_app5/ai_trainer/README.md
Normal file
@ -0,0 +1,23 @@
|
||||
# Configure Simulation Settings
|
||||
|
||||
Firstly, edit config.json file in a way to declare the configuration of the simulation process used to collect training data
|
||||
|
||||
# Preparing Training Data
|
||||
|
||||
Invoke following command to convert collected data format for weka models
|
||||
|
||||
```
|
||||
./generate_training_data.sh
|
||||
```
|
||||
|
||||
This command creates *.arff files under the simulation results folder
|
||||
|
||||
# Generating Classification and Regression Models
|
||||
|
||||
Invoke following command to generate related weka models
|
||||
|
||||
```
|
||||
./generate_weka_models.sh
|
||||
```
|
||||
|
||||
This script creates weka model files under the simulation results folder. When you are done with training, you can move these files to ../config/weka/ folder
|
208
scripts/sample_app5/ai_trainer/WekaModelCreator.java
Normal file
208
scripts/sample_app5/ai_trainer/WekaModelCreator.java
Normal file
@ -0,0 +1,208 @@
|
||||
import java.io.FileReader;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
import weka.classifiers.Evaluation;
|
||||
import weka.classifiers.bayes.NaiveBayes;
|
||||
import weka.classifiers.functions.LinearRegression;
|
||||
import weka.classifiers.functions.MultilayerPerceptron;
|
||||
import weka.classifiers.functions.SMO;
|
||||
import weka.classifiers.functions.SMOreg;
|
||||
import weka.core.Instances;
|
||||
import weka.core.converters.ConverterUtils.DataSource;
|
||||
|
||||
public class WekaModelCreator {
|
||||
private static final String[] targets = {"edge","cloud_rsu","cloud_gsm"};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String dataPath = "";
|
||||
String classifier = "";
|
||||
String regressor = "";
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
try
|
||||
{
|
||||
Object object = parser.parse(new FileReader(args[0]));
|
||||
|
||||
//convert Object to JSONObject
|
||||
JSONObject jsonObject = (JSONObject)object;
|
||||
|
||||
//Reading the String
|
||||
dataPath = (String) jsonObject.get("sim_result_folder");
|
||||
classifier = (String) jsonObject.get("classifier");
|
||||
regressor = (String) jsonObject.get("regressor");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
System.out.println("######### TRAINING FOR " + dataPath + " #########");
|
||||
for(int i=0; i<targets.length; i++) {
|
||||
handleClassify("train", targets[i], classifier, dataPath);
|
||||
handleRegression("train", targets[i], regressor, dataPath);
|
||||
}
|
||||
|
||||
System.out.println("######### EVALUATION FOR " + dataPath + " #########");
|
||||
for(int i=0; i<targets.length; i++) {
|
||||
handleClassify("evaluate", targets[i], classifier, dataPath);
|
||||
handleRegression("evaluate", targets[i], regressor, dataPath);
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleRegression(String action, String target, String method, String dataFolder) throws Exception {
|
||||
if(action.equals("train")) {
|
||||
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
||||
Date startDate = Calendar.getInstance().getTime();
|
||||
String now = df.format(startDate);
|
||||
System.out.println("Training " + method + " for " + target + " started at " + now);
|
||||
|
||||
DataSource edgeRegressionSource = new DataSource(dataFolder + "/" + target + "_regression_train.arff");
|
||||
Instances edgeRegressionDataset = edgeRegressionSource.getDataSet();
|
||||
edgeRegressionDataset.setClassIndex(edgeRegressionDataset.numAttributes()-1);
|
||||
|
||||
if(method.equals("LinearRegression")) {
|
||||
LinearRegression lr = new LinearRegression();
|
||||
lr.buildClassifier(edgeRegressionDataset);
|
||||
weka.core.SerializationHelper.write(dataFolder + "/lr_" + target + ".model", lr);
|
||||
}
|
||||
else if(method.equals("SMOreg")) {
|
||||
SMOreg smoreg = new SMOreg();
|
||||
smoreg.buildClassifier(edgeRegressionDataset);
|
||||
weka.core.SerializationHelper.write(dataFolder + "/smoreg_" + target + ".model", smoreg);
|
||||
}
|
||||
|
||||
Date endDate = Calendar.getInstance().getTime();
|
||||
now = df.format(endDate);
|
||||
System.out.println("Training " + method + " for " + target + " fisished at " + now + ". It took " + getTimeDifference(startDate, endDate));
|
||||
}
|
||||
else if(action.equals("evaluate")) {
|
||||
System.out.println("Evaluation " + method + " for " + target + " started");
|
||||
|
||||
DataSource edgeRegressionSource = new DataSource(dataFolder + "/" + target + "_regression_test.arff");
|
||||
Instances edgeRegressionDataset = edgeRegressionSource.getDataSet();
|
||||
edgeRegressionDataset.setClassIndex(edgeRegressionDataset.numAttributes()-1);
|
||||
|
||||
if(method.equals("LinearRegression")) {
|
||||
LinearRegression lr = (LinearRegression) weka.core.SerializationHelper.read(dataFolder + "/lr_" + target + ".model");
|
||||
Evaluation lrEval = new Evaluation(edgeRegressionDataset);
|
||||
lrEval.evaluateModel(lr, edgeRegressionDataset);
|
||||
System.out.println("LinearRegression");
|
||||
System.out.println(lrEval.toSummaryString());
|
||||
}
|
||||
else if(method.equals("SMOreg")) {
|
||||
SMOreg smoreg = (SMOreg) weka.core.SerializationHelper.read(dataFolder + "/smoreg_" + target + ".model");
|
||||
Evaluation svmregEval = new Evaluation(edgeRegressionDataset);
|
||||
svmregEval.evaluateModel(smoreg, edgeRegressionDataset);
|
||||
System.out.println("SMOreg");
|
||||
System.out.println(svmregEval.toSummaryString());
|
||||
}
|
||||
|
||||
System.out.println("Evaluation " + method + " for " + target + " fisished");
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleClassify(String action, String target, String method, String dataFolder) throws Exception {
|
||||
if(action.equals("train")) {
|
||||
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
||||
Date startDate = Calendar.getInstance().getTime();
|
||||
String now = df.format(startDate);
|
||||
System.out.println("Training " + method + " for " + target + " started at " + now);
|
||||
|
||||
DataSource classifierSource = new DataSource(dataFolder + "/" + target + "_classifier_train.arff");
|
||||
Instances classifierDataset = classifierSource.getDataSet();
|
||||
classifierDataset.setClassIndex(classifierDataset.numAttributes()-1);
|
||||
|
||||
if(method.equals("NaiveBayes")) {
|
||||
NaiveBayes nb = new NaiveBayes();
|
||||
nb.buildClassifier(classifierDataset);
|
||||
weka.core.SerializationHelper.write(dataFolder + "/nb_" + target + ".model", nb);
|
||||
}
|
||||
else if(method.equals("SMO")) {
|
||||
SMO smo = new SMO();
|
||||
smo.buildClassifier(classifierDataset);
|
||||
weka.core.SerializationHelper.write(dataFolder + "/smo_" + target + ".model", smo);
|
||||
}
|
||||
else if(method.equals("MultilayerPerceptron")) {
|
||||
MultilayerPerceptron mlp = new MultilayerPerceptron();
|
||||
mlp.setLearningRate(0.1);
|
||||
//mlp.setMomentum(0.2);
|
||||
mlp.setTrainingTime(1000);
|
||||
//mlp.setHiddenLayers("3");
|
||||
mlp.buildClassifier(classifierDataset);
|
||||
weka.core.SerializationHelper.write(dataFolder + "/mlp_" + target + ".model", mlp);
|
||||
}
|
||||
|
||||
Date endDate = Calendar.getInstance().getTime();
|
||||
now = df.format(endDate);
|
||||
System.out.println("Training " + method + " for " + target + " fisished at " + now + ". It took " + getTimeDifference(startDate, endDate));
|
||||
}
|
||||
else if(action.equals("evaluate")) {
|
||||
System.out.println("Evaluation " + method + " for " + target + " started");
|
||||
|
||||
DataSource edgeClassifierSource = new DataSource(dataFolder + "/" + target + "_classifier_test.arff");
|
||||
Instances classifierDataset = edgeClassifierSource.getDataSet();
|
||||
classifierDataset.setClassIndex(classifierDataset.numAttributes()-1);
|
||||
|
||||
if(method.equals("NaiveBayes")) {
|
||||
NaiveBayes nb = (NaiveBayes) weka.core.SerializationHelper.read(dataFolder + "/nb_" + target + ".model");
|
||||
Evaluation nbEval = new Evaluation(classifierDataset);
|
||||
nbEval.evaluateModel(nb, classifierDataset);
|
||||
System.out.println(nbEval.toSummaryString());
|
||||
System.out.println(nbEval.toMatrixString());
|
||||
System.out.println(nbEval.toClassDetailsString());
|
||||
}
|
||||
else if(method.equals("SMO")) {
|
||||
SMO smo = (SMO) weka.core.SerializationHelper.read(dataFolder + "/smo_" + target + ".model");
|
||||
Evaluation smoEval = new Evaluation(classifierDataset);
|
||||
smoEval.evaluateModel(smo, classifierDataset);
|
||||
System.out.println(smoEval.toSummaryString());
|
||||
System.out.println(smoEval.toMatrixString());
|
||||
System.out.println(smoEval.toClassDetailsString());
|
||||
}
|
||||
else if(method.equals("MultilayerPerceptron")) {
|
||||
MultilayerPerceptron mlp = (MultilayerPerceptron) weka.core.SerializationHelper.read(dataFolder + "/mlp_" + target + ".model");
|
||||
Evaluation mlpEval = new Evaluation(classifierDataset);
|
||||
mlpEval.evaluateModel(mlp, classifierDataset);
|
||||
System.out.println(mlpEval.toSummaryString());
|
||||
System.out.println(mlpEval.toMatrixString());
|
||||
System.out.println(mlpEval.toClassDetailsString());
|
||||
}
|
||||
|
||||
System.out.println("Evaluation " + method + " for " + target + " fisished");
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
private static String getTimeDifference(Date startDate, Date endDate){
|
||||
String result = "";
|
||||
long duration = endDate.getTime() - startDate.getTime();
|
||||
|
||||
long diffInMilli = TimeUnit.MILLISECONDS.toMillis(duration);
|
||||
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
|
||||
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
|
||||
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);
|
||||
long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);
|
||||
|
||||
if(diffInDays>0)
|
||||
result += diffInDays + ((diffInDays>1 == true) ? " Days " : " Day ");
|
||||
if(diffInHours>0)
|
||||
result += diffInHours % 24 + ((diffInHours>1 == true) ? " Hours " : " Hour ");
|
||||
if(diffInMinutes>0)
|
||||
result += diffInMinutes % 60 + ((diffInMinutes>1 == true) ? " Minutes " : " Minute ");
|
||||
if(diffInSeconds>0)
|
||||
result += diffInSeconds % 60 + ((diffInSeconds>1 == true) ? " Seconds" : " Second");
|
||||
if(diffInMilli>0 && result.isEmpty())
|
||||
result += diffInMilli + ((diffInMilli>1 == true) ? " Milli Seconds" : " Milli Second");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
10
scripts/sample_app5/ai_trainer/config.json
Normal file
10
scripts/sample_app5/ai_trainer/config.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"sim_result_folder": "/home/cagatay/Projects/git-repos/EdgeCloudSim/scripts/sample_app5/output/18-10-2019_21-16/default_config",
|
||||
"num_iterations": 10,
|
||||
"train_data_ratio": 80,
|
||||
"min_vehicle": 100,
|
||||
"max_vehicle": 2200,
|
||||
"vehicle_step_size": 100,
|
||||
"classifier": "MultilayerPerceptron",
|
||||
"regressor": "LinearRegression"
|
||||
}
|
133
scripts/sample_app5/ai_trainer/data_convertor.py
Normal file
133
scripts/sample_app5/ai_trainer/data_convertor.py
Normal file
@ -0,0 +1,133 @@
|
||||
import pandas as pd
|
||||
import json
|
||||
import sys
|
||||
|
||||
if len (sys.argv) != 5:
|
||||
print('invalid arguments. Usage:')
|
||||
print('python data_conventor.py config.json [edge|cloud_rsu|cloud_gsm] [classifier|regression] [train|test]')
|
||||
sys.exit(1)
|
||||
|
||||
with open(sys.argv[1]) as json_data_file:
|
||||
data = json.load(json_data_file)
|
||||
|
||||
target = sys.argv[2]
|
||||
method = sys.argv[3]
|
||||
datatype = sys.argv[4]
|
||||
|
||||
print("conversion started with args " + target + ", " + method + ", " + datatype)
|
||||
|
||||
sim_result_folder = data["sim_result_folder"]
|
||||
num_iterations = data["num_iterations"]
|
||||
train_data_ratio = data["train_data_ratio"]
|
||||
min_vehicle = data["min_vehicle"]
|
||||
max_vehicle = data["max_vehicle"]
|
||||
vehicle_step_size = data["vehicle_step_size"]
|
||||
|
||||
def getDecisionColumnName(target):
|
||||
if target == "edge":
|
||||
COLUMN_NAME = "EDGE"
|
||||
elif target == "cloud_rsu":
|
||||
COLUMN_NAME = "CLOUD_VIA_RSU"
|
||||
elif target == "cloud_gsm":
|
||||
COLUMN_NAME = "CLOUD_VIA_GSM"
|
||||
return COLUMN_NAME
|
||||
|
||||
def getClassifierColumns(target):
|
||||
if target == "edge":
|
||||
result = ["NumOffloadedTask", "TaskLength", "WLANUploadDelay", "WLANDownloadDelay", "AvgEdgeUtilization", "Result"]
|
||||
elif target == "cloud_rsu":
|
||||
result = ["NumOffloadedTask", "WANUploadDelay", "WANDownloadDelay", "Result"]
|
||||
elif target == "cloud_gsm":
|
||||
result = ["NumOffloadedTask", "GSMUploadDelay", "GSMDownloadDelay", "Result"]
|
||||
return result
|
||||
|
||||
def getRegressionColumns(target):
|
||||
if target == "edge":
|
||||
result = ["TaskLength", "AvgEdgeUtilization", "ServiceTime"]
|
||||
elif target == "cloud_rsu":
|
||||
result = ["TaskLength", "WANUploadDelay", "WANDownloadDelay", "ServiceTime"]
|
||||
elif target == "cloud_gsm":
|
||||
result = ["TaskLength", "GSMUploadDelay", "GSMDownloadDelay", "ServiceTime"]
|
||||
return result
|
||||
|
||||
def znorm(column):
|
||||
column = (column - column.mean()) / column.std()
|
||||
return column
|
||||
|
||||
data_set = []
|
||||
|
||||
testDataStartIndex = (train_data_ratio * num_iterations) / 100
|
||||
|
||||
for ite in range(num_iterations):
|
||||
for vehicle in range(min_vehicle, max_vehicle+1, vehicle_step_size):
|
||||
if (datatype == "train" and ite < testDataStartIndex) or (datatype == "test" and ite >= testDataStartIndex):
|
||||
file_name = sim_result_folder + "/ite" + str(ite + 1) + "/" + str(vehicle) + "_learnerOutputFile.cvs"
|
||||
df = [pd.read_csv(file_name, na_values = "?", comment='\t', sep=",")]
|
||||
df[0]['VehicleCount'] = vehicle
|
||||
#print(file_name)
|
||||
data_set += df
|
||||
|
||||
data_set = pd.concat(data_set, ignore_index=True)
|
||||
data_set = data_set[data_set['Decision'] == getDecisionColumnName(target)]
|
||||
|
||||
if method == "classifier":
|
||||
targetColumns = getClassifierColumns(target)
|
||||
else:
|
||||
targetColumns= getRegressionColumns(target)
|
||||
|
||||
if datatype == "train":
|
||||
print ("##############################################################")
|
||||
print ("Stats for " + target + " - " + method)
|
||||
print ("Please use relevant information from below table in java side:")
|
||||
train_stats = data_set[targetColumns].describe()
|
||||
train_stats = train_stats.transpose()
|
||||
print(train_stats)
|
||||
print ("##############################################################")
|
||||
|
||||
#print("balancing " + target + " for " + method)
|
||||
|
||||
#BALANCE DATA SET
|
||||
if method == "classifier":
|
||||
df0 = data_set[data_set['Result']=="fail"]
|
||||
df1 = data_set[data_set['Result']=="success"]
|
||||
|
||||
#size = min(len(df0[df0['VehicleCount']==max_vehicle]), len(df1[df1['VehicleCount']==min_vehicle]))
|
||||
|
||||
size = len(df0[df0['VehicleCount']==max_vehicle]) // 2
|
||||
|
||||
df1 = df1.groupby('VehicleCount').apply(lambda x: x if len(x) < size else x.sample(size))
|
||||
df0 = df0.groupby('VehicleCount').apply(lambda x: x if len(x) < size else x.sample(size))
|
||||
|
||||
data_set = pd.concat([df0, df1], ignore_index=True)
|
||||
else:
|
||||
data_set = data_set[data_set['Result'] == 'success']
|
||||
|
||||
#size = min(len(data_set[data_set['VehicleCount']==min_vehicle]), len(data_set[data_set['VehicleCount']==max_vehicle]))
|
||||
|
||||
size = len(data_set[data_set['VehicleCount']==max_vehicle]) // 3
|
||||
data_set = data_set.groupby('VehicleCount').apply(lambda x: x if len(x.index) < size else x.sample(size))
|
||||
|
||||
#EXTRACT RELATED ATTRIBUTES
|
||||
df = pd.DataFrame(columns=targetColumns)
|
||||
for column in targetColumns:
|
||||
if column == 'Result' or column == 'ServiceTime':
|
||||
df[column] = data_set[column]
|
||||
else:
|
||||
df[column] = znorm(data_set[column])
|
||||
|
||||
f = open(sim_result_folder + "/" + target + "_" + method + "_" + datatype + ".arff", 'w')
|
||||
f.write('@relation ' + target + '\n\n')
|
||||
for column in targetColumns:
|
||||
if column == 'Result':
|
||||
f.write('@attribute class {fail,success}\n')
|
||||
else:
|
||||
f.write('@attribute ' + column + ' REAL\n')
|
||||
f.write('\n@data\n')
|
||||
df.to_csv(f, header=False, index=False)
|
||||
f.close()
|
||||
|
||||
print ("##############################################################")
|
||||
print ("Operation completed!")
|
||||
print (".arff file is generated for weka.")
|
||||
print ("##############################################################")
|
||||
|
14
scripts/sample_app5/ai_trainer/generate_training_data.sh
Normal file
14
scripts/sample_app5/ai_trainer/generate_training_data.sh
Normal file
@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
|
||||
python data_convertor.py config.json edge classifier train
|
||||
python data_convertor.py config.json edge classifier test
|
||||
python data_convertor.py config.json edge regression train
|
||||
python data_convertor.py config.json edge regression test
|
||||
python data_convertor.py config.json cloud_rsu classifier train
|
||||
python data_convertor.py config.json cloud_rsu classifier test
|
||||
python data_convertor.py config.json cloud_rsu regression train
|
||||
python data_convertor.py config.json cloud_rsu regression test
|
||||
python data_convertor.py config.json cloud_gsm classifier train
|
||||
python data_convertor.py config.json cloud_gsm classifier test
|
||||
python data_convertor.py config.json cloud_gsm regression train
|
||||
python data_convertor.py config.json cloud_gsm regression test
|
4
scripts/sample_app5/ai_trainer/generate_weka_models.sh
Normal file
4
scripts/sample_app5/ai_trainer/generate_weka_models.sh
Normal file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
javac -classpath "./json-simple-1.1.1.jar:../../../lib/weka.jar:../../../lib/mtj-1.0.4.jar" WekaModelCreator.java
|
||||
java -classpath ".:./json-simple-1.1.1.jar:../../../lib/weka.jar:../../../lib/mtj-1.0.4.jar" WekaModelCreator config.json
|
BIN
scripts/sample_app5/ai_trainer/json-simple-1.1.1.jar
Normal file
BIN
scripts/sample_app5/ai_trainer/json-simple-1.1.1.jar
Normal file
Binary file not shown.
Reference in New Issue
Block a user