private void runTreeInThreads(DecisionTree tree, DataSet data) { int tuplesInThread = data.DataValues.Count / MAX_THREADS; int from = 0; List <int> froms = new List <int>(); froms.Add(0); for (int i = 1; i < MAX_THREADS; i++) { froms.Add(froms[i - 1] + tuplesInThread); } foreach (var v in froms) { Console.WriteLine(v); } List <System.Threading.Thread> tasks = new List <System.Threading.Thread>(); for (int i = 0; i < MAX_THREADS - 1; i++) { System.Threading.Monitor.Enter(semaphore); Object fr = new object(); fr = from; System.Threading.Thread thread = new System.Threading.Thread(delegate() { runTree(data, tree, (int)fr, (int)fr + tuplesInThread); }); tasks.Add(thread); thread.Start(); from += tuplesInThread; System.Threading.Monitor.Exit(semaphore); } System.Threading.Thread lastThread = new System.Threading.Thread(delegate() { runTree(data, tree, from, data.DataValues.Count); }); tasks.Add(lastThread); lastThread.Start(); foreach (var temp in tasks) { temp.Join(); } }
public void SprintClassification(string treePath, string treeName, string dataPath, string dataName, char separator = ';') { DecisionTree tree = new DecisionTree(); Globals.stopWatch.Start(); tree.loadFromTxt(treePath + "\\" + treeName + ".txt"); Globals.stopWatch.Stop(); Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Loading tree"); DataSet dataSet = new DataSet(dataPath + "\\" + dataName + ".txt", separator, true); Globals.stopWatch.Start(); runTreeInThreads(tree, dataSet); //foreach (var v in dataSet.DataValues) tree.classifyTuple(v); Globals.stopWatch.Stop(); Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Classifying"); Globals.stopWatch.Start(); dataSet.saveToFile(dataPath, dataName, separator); Globals.stopWatch.Stop(); Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Saving result to file"); }
public void loadFromTxt(string path) { try { using (var sr = new StreamReader(path)) { string newLine = sr.ReadLine(); var attributes = newLine.Split(';'); nodeAttribute = Int32.Parse(attributes[0]); nodeOperator = attributes[1]; nodeValue = attributes[2]; while (true) { newLine = sr.ReadLine(); if (newLine == null) { break; } attributes = newLine.Split(';'); DecisionTree toAdd; if (attributes.Count() == 1) { toAdd = new DecisionTree(0, "-1", "-1", attributes[0]); } else { toAdd = new DecisionTree(Int32.Parse(attributes[0]), attributes[1], attributes[2]); } addChild(toAdd); } } } catch (IOException e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }
public void createTree(string trainingPath, string trainingName, char separator) { Globals.stopWatch.Start(); DataSet trainingSet = new DataSet(trainingPath + "\\" + trainingName + ".txt", separator, true); Globals.stopWatch.Stop(); Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Training data set"); Globals.stopWatch.Start(); Sprint sprint = new Sprint(trainingSet); Globals.stopWatch.Stop(); Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Sprint created"); Globals.stopWatch.Start(); DecisionTree tree = sprint.createTree(); Globals.stopWatch.Stop(); Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Tree created"); Globals.stopWatch.Start(); tree.saveToTxt(trainingPath, "Decision_Tree_" + trainingName); Globals.stopWatch.Stop(); Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Saving tree to file"); }
private bool addChild(DecisionTree toAdd) { if (leftChild == null) { leftChild = toAdd; return(true); } else if (leftChild.Leaf == null) { bool b = leftChild.addChild(toAdd); if (b) { return(true); } if (rightChild == null) { rightChild = toAdd; return(true); } if (rightChild.Leaf != null) { return(false); } return(rightChild.addChild(toAdd)); } else if (rightChild == null) { rightChild = toAdd; return(true); } else if (rightChild.Leaf == null) { return(rightChild.addChild(toAdd)); } return(false); }
private void runTree(DataSet data, DecisionTree tree, int from, int to) { data.clasifyTreeThread(tree, from, to); }