package Experiment; import java.io.File; import java.io.IOException; import java.util.ArrayList; import Exceptions.UnsupportedFileTypeException; import GUI.Main; import Learners.BasicLearner; import Learners.BasicLearner2; import Learners.Learner; import Learners.kNearestNeighbors; import Preprocessors.BasicPreprocessor; import Preprocessors.BasicPreprocessor2; import Preprocessors.Preprocessor; public class SingleDataExperiment extends Thread implements Experiment { private Dataset dataset; private ArrayList preprocessors; private ArrayList learners; private int progress; private int id; public SingleDataExperiment(int id) { preprocessors = new ArrayList(); learners = new ArrayList(); progress = 0; this.id = id; } public SingleDataExperiment(int id, String dataSetFileName, String dataSetName) { preprocessors = new ArrayList(); learners = new ArrayList(); progress = 0; this.id = id; this.addDataset(dataSetFileName, dataSetName); } @Override public void build() { preprocessors = new ArrayList(); learners = new ArrayList(); learners.add(new kNearestNeighbors(1)); learners.add(new kNearestNeighbors(2)); learners.add(new kNearestNeighbors(4)); learners.add(new kNearestNeighbors(8)); learners.add(new kNearestNeighbors(16)); preprocessors.add(new BasicPreprocessor()); preprocessors.add(new BasicPreprocessor2()); } @Override public void run(String output) throws IOException, UnsupportedFileTypeException { this.runSingle("",output); } @Override public void runSingle(String datasetName, String output) throws IOException, UnsupportedFileTypeException { this.build(); // Temporary storage for preprocessed data String tempDirectory = "./temp/"; String trainingData = ""; String testingData = ""; String modifiedData = ""; String tempResults = tempDirectory + "results"; File temp = new File(tempDirectory); if (!temp.exists()) temp.mkdir(); ResultsTable results; Dataset d = dataset; if (d == null) return; if (d.isARFF()) { trainingData = tempDirectory + "trainingData.arff"; testingData = tempDirectory + "testingData.arff"; modifiedData = tempDirectory + "modifiedData.arff"; } else if (d.isCSV()) { trainingData = tempDirectory + "trainingData.csv"; testingData = tempDirectory + "testingData.csv"; modifiedData = tempDirectory + "modifiedData.csv"; } results = new ResultsTable(d.getName()); results.addHeader("Actual"); for (long i = 0; i < 10; i++) { d.splitThreeWay(i, trainingData, testingData); for (Preprocessor p : preprocessors) { p.preprocess(trainingData, testingData, modifiedData); for (Learner l : learners) { l.learn(modifiedData, testingData, tempResults); results.collectSplit(i, tempResults, p.getName() + "_" + l.getName()); progress++; this.setProgress(); } } } results.computeResults(output); } @Override public void addDataset(String filename) { Dataset d = new Dataset(filename); dataset = d; progress = 0; } @Override public void addDataset(String filename, String name) { Dataset d = new Dataset(filename, name); dataset = d; progress = 0; } @Override public void setProgress() { Main.setProgress(id, (int)((100.0 * (double) progress) / (10.0 * (double) learners.size() * (double) preprocessors.size()))); } @Override public int getID() { return id; } @Override public String getDataset() { return dataset.getName(); } }