Commit a4660014 authored by Simon Trapp's avatar Simon Trapp

Implemented WEKA support in QPME.

parent d2d1bf56
......@@ -33,7 +33,8 @@ Require-Bundle: org.dom4j,
cern.colt;bundle-version="1.2.0",
edu.bonn.cs.net.jbarrier;bundle-version="1.0.0",
org.eclipse.equinox.concurrent;bundle-version="1.1.0",
org.eclipse.swt
org.eclipse.swt,
nz.ac.waikato.cms.weka;bundle-version="3.9.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.apache.commons.math.distribution
Automatic-Module-Name: qpme.simqpn.kernel
......@@ -64,7 +64,6 @@ public class MARS implements AbstractDistribution {
if (functions == null)
createFunctions(qplace.colors);
double result = constant;
System.out.println("START");
for (int i = 0; i < functions.length; i++) {
result += functions[i].calculate(qplace.getQueueTokenPop(), color);
}
......@@ -107,7 +106,8 @@ public class MARS implements AbstractDistribution {
public double calculate(int[] tokenNumbers, int mainColor) {
int tokenNumber = tokenNumbers[colorId];
if (colorId == mainColor)
if (colorId == mainColor) // TODO: this excludes the processed request from the concurrency -> change
// training data so this can be removed
tokenNumber = tokenNumber - 1;
if (tokenNumber >= knot)
return 0.0;
......@@ -135,7 +135,8 @@ public class MARS implements AbstractDistribution {
public double calculate(int[] tokenNumbers, int mainColor) {
int tokenNumber = tokenNumbers[colorId];
if (colorId == mainColor)
if (colorId == mainColor) // TODO: this excludes the processed request from the concurrency -> change
// training data so this can be removed
tokenNumber = tokenNumber - 1;
if (tokenNumber <= knot)
return 0.0;
......
......@@ -40,17 +40,67 @@
*/
package de.tud.cs.simqpn.kernel.loading.distributions;
import java.util.ArrayList;
import de.tud.cs.simqpn.kernel.entities.QPlace;
import weka.classifiers.Classifier;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
public class WEKA implements AbstractDistribution {
public WEKA() {
// TODO
Classifier wekaModel;
Instances dataStructure = null;
public WEKA(Classifier model) {
if (model == null)
throw new NullPointerException();
wekaModel = model;
}
@Override
public double nextDouble(QPlace qplace, int color) {
return 0.0; // TODO
int[] tokenNumbers = qplace.getQueueTokenPop(); // gets an array with the concurrencies for each WC
if (dataStructure == null)
initializeDataStructure(tokenNumbers.length);
Instance dataPoint = new DenseInstance(dataStructure.numAttributes());
dataPoint.setDataset(dataStructure);
for (int i = 0; i < tokenNumbers.length; i++) {
dataPoint.setValue(i + 1, tokenNumbers[i]);
}
try {
return wekaModel.classifyInstance(dataPoint);
} catch (Exception e) {
e.printStackTrace();
return 0.0;
}
}
/**
* initializes the data structure with slots for response time and the
* concurrencies for each workload class
*
* @param wc_count The amount of workload classes available at the queueing
* place to be predicted by the model
*/
private void initializeDataStructure(int wc_count) {
ArrayList<Attribute> attributes = new ArrayList<>();
final Attribute attr_RT = new Attribute("Response Time"); // first attribute is ALWAYS response time
attributes.add(attr_RT);
for (int i = 0; i < wc_count; i++) {
final Attribute attr_CC = new Attribute("Concurrency " + (i + 1)); // Concurrencies must be ordered the same
// in QPME and the weka model training
// data
attributes.add(attr_CC);
}
dataStructure = new Instances("Data Structure", attributes, 0);
dataStructure.setClass(attr_RT);
}
}
}
\ No newline at end of file
......@@ -41,51 +41,36 @@
*/
package de.tud.cs.simqpn.kernel.loading.distributions;
import java.util.LinkedList;
import java.util.List;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import de.tud.cs.simqpn.kernel.SimQPNException;
import weka.classifiers.Classifier;
public class WEKACreator extends DistributionCreator {
double constant;
List<Double> coefficients = new LinkedList<Double>();
List<Double> knots = new LinkedList<Double>();
List<Integer> sides = new LinkedList<Integer>();
List<String> colorIds = new LinkedList<String>();
String marsFilename;
String wekaFilename;
Classifier wekaModel;
@Override
protected void loadParams() throws SimQPNException {
marsFilename = this.loadStringParam("wekaFile"); // TODO: model in memory laden
// loadMARSModelFromFile(marsFilename);
}
wekaFilename = this.loadStringParam("wekaFile");
try {
FileInputStream fis = new FileInputStream(wekaFilename);
ObjectInputStream ois = new ObjectInputStream(fis);
wekaModel = (Classifier) ois.readObject();
ois.close();
fis.close();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
// private void loadMARSModelFromFile(String fileName) {
// coefficients.clear();
// knots.clear();
// sides.clear();
// colorIds.clear();
// try (BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))) {
// String line = br.readLine();
// constant = Double.valueOf(line);
// while ((line = br.readLine()) != null) {
// String[] segments = line.split(" ");
// coefficients.add(Double.valueOf(segments[0]));
// knots.add(Double.valueOf(segments[1]));
// sides.add(Integer.valueOf(segments[2]));
// colorIds.add(segments[3]);
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
@Override
public AbstractDistribution getDistribution() throws SimQPNException {
return new WEKA();
return new WEKA(wekaModel);
}
@Override
......@@ -95,7 +80,7 @@ public class WEKACreator extends DistributionCreator {
@Override
public String getConstructionText() {
return "(" + marsFilename + ")";
return "(" + wekaFilename + ")";
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment