Exemple #1
0
 public void clasifyKnnThread(KNN knn, int from, int to)
 {
     if (to > dataValues.Count)
     {
         to = dataValues.Count;
     }
     for (int i = from; i < to; i++)
     {
         knn.setClass(dataValues[i]);
     }
 }
Exemple #2
0
        private void runKnnInThreads(KNN knn, 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() {
                    runKnn(data, knn, (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() {
                runKnn(data, knn, from, data.DataValues.Count);
            });
            tasks.Add(lastThread);
            lastThread.Start();

            foreach (var temp in tasks)
            {
                temp.Join();
            }
        }
Exemple #3
0
        public void KNNClassification(string trainingPath, string trainingName, string dataPath, string dataName, char separator = ';', int k = 3)
        {
            DataSet trainingSet = new DataSet(trainingPath + "\\" + trainingName + ".txt", separator, true);
            DataSet dataSet     = new DataSet(dataPath + "\\" + dataName + ".txt", separator, true);

            Globals.stopWatch.Start();
            KNN knn = new KNN(trainingSet, k);

            Globals.stopWatch.Stop();
            Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Training data set");
            Globals.stopWatch.Start();
            runKnnInThreads(knn, dataSet);
            //foreach (var v in dataSet.DataValues) knn.setClass(v);
            Globals.stopWatch.Stop();
            Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Test data set");
            Globals.stopWatch.Start();
            dataSet.saveToFile(dataPath, dataName, separator);
            Globals.stopWatch.Stop();
            Globals.printElapsedTimeResetWatch(Globals.stopWatch.Elapsed, "Saving result to file");
        }
Exemple #4
0
 private void runKnn(DataSet data, KNN knn, int from, int to)
 {
     data.clasifyKnnThread(knn, from, to);
 }