Exemple #1
0
        public void Train(List <Point> points)
        {
            //double[][] initCentroids = new double[2][];
            //initCentroids[0] = new double[] { points.First().X, points.First().Y };
            //initCentroids[1] = new double[] { points.Last().X, points.Last().Y };
            //m_Settings.InitialCentroids = initCentroids;

            m_CurrentData = getDataFromPoints(points);
            KMeansFunctionRecognitonScore res = m_LearningApi.RunBatch() as KMeansFunctionRecognitonScore;
        }
 /// <summary>
 /// Trains the K-Means algorithm using coordinates.
 /// </summary>
 /// <param name="points">Coordinate Points</param>
 public void Train(List <Coordinates> points)
 {
     m_CurrentData = GetDataFromPoints(points);
     KMeansFunctionRecognitonScore res = m_LearningApi.RunBatch() as KMeansFunctionRecognitonScore;
 }
        public void Test_FunctionRecognition()
        {
            int numAttributes = 3;
            int numClusters   = 2;
            int maxCount      = 300;


            // a value in % representing the tolerance to possible outliers
            double tolerance = 0;
            // directory to load
            string loadDirectory = rootFolder + "Functions\\";
            // directory to save
            string saveDirectory = rootFolder + "Function Recognition\\";


            string TrainedFuncName = "SIN_SIN X";

            /*
             * // functions' paths
             * string[] FunctionPaths = new string[]
             * {
             *  loadDirectory + TrainedFuncName + "\\NRP5-10\\" + TrainedFuncName + " SimilarFunctions Normalized NRP5-10.csv",
             *  loadDirectory + "COS X\\NRP5-10\\COS X SimilarFunctions Normalized NRP5-10.csv",
             *  loadDirectory + "Triangular\\NRP5-10\\Triangular SimilarFunctions Normalized NRP5-10.csv",
             *  loadDirectory + "Square\\NRP5-10\\Square SimilarFunctions Normalized NRP5-10.csv",
             *  loadDirectory + "Line -X\\NRP5-10\\Line -X SimilarFunctions Normalized NRP5-10.csv"
             * };*/

            /*
             * // functions' paths
             * string[] FunctionPaths = new string[]
             * {
             *  loadDirectory + TrainedFuncName + "\\NRP5-10\\" + TrainedFuncName + " SimilarFunctions NRP5-10.csv",
             *  loadDirectory + "5 SIN 2X\\NRP5-10\\5 SIN 2X SimilarFunctions NRP5-10.csv"
             * };*/


            /*
             * // functions' paths
             * string[] FunctionPaths = new string[]
             * {
             *  loadDirectory + TrainedFuncName + "\\NRP5-10\\" + TrainedFuncName + " SimilarFunctions Normalized NRP5-10.csv",
             *  loadDirectory + "SIN 1.5X\\NRP5-10\\SIN 1.5X SimilarFunctions Normalized NRP5-10.csv",
             *  loadDirectory + "SIN 2X\\NRP5-10\\SIN 2X SimilarFunctions Normalized NRP5-10.csv"
             * };*/


            // functions' paths
            string[] FunctionPaths = new string[]
            {
                loadDirectory + TrainedFuncName + "\\NRP5-10\\" + TrainedFuncName + " SimilarFunctions Normalized NRP5-10.csv",
                loadDirectory + "SIN_COS X\\NRP5-10\\SIN_COS X SimilarFunctions Normalized NRP5-10.csv",
                loadDirectory + "COS_COS X\\NRP5-10\\COS_COS X SimilarFunctions Normalized NRP5-10.csv",
                loadDirectory + "COS_SIN X\\NRP5-10\\COS_SIN X SimilarFunctions Normalized NRP5-10.csv"
            };

            int numTrainFun = 800;
            int numTestFun  = 200;

            double[][] loadedSimFunctions = Helpers.LoadFunctionData(FunctionPaths[0]);

            int numLoadedFunc = 0;

            ClusteringSettings settings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 2);
            LearningApi        api      = new LearningApi();

            api.UseActionModule <object, double[][]>((funcData, ctx) =>
            {
                numLoadedFunc++;
                return(KMeansAlgorithm.transposeFunction(KMeansAlgorithm.selectFunction(loadedSimFunctions, numLoadedFunc, numAttributes)));
            });

            api.UseKMeansFunctionRecognitionModule(settings);

            KMeansFunctionRecognitonScore res = new KMeansFunctionRecognitonScore();

            //train
            for (int i = 0; i < numTrainFun; i++)
            {
                res = api.Run() as KMeansFunctionRecognitonScore;
            }

            // save the formed clusters (just for plotting the function recognition results)
            Helpers.Write2CSVFile(res.Centroids, saveDirectory + "Calculated Centroids.csv");
            double[][] tempMaxDistance = new double[1][];
            tempMaxDistance[0] = res.InClusterMaxDistance;
            Helpers.Write2CSVFile(tempMaxDistance, saveDirectory + "Calculated Max Distance.csv");

            // save the trained clusters in a persistant location (just for plotting the clusters)
            Helpers.Write2CSVFile(res.Centroids, saveDirectory + TrainedFuncName + "\\Calculated Centroids C" + numClusters + ".csv");
            Helpers.Write2CSVFile(tempMaxDistance, saveDirectory + TrainedFuncName + "\\Calculated Max Distance C" + numClusters + ".csv");

            // start testing for function recognition

            double[]   testingResults = new double[numTestFun * FunctionPaths.Length];
            double[][] data;
            for (int l = 0; l < FunctionPaths.Length; l++)
            {
                loadedSimFunctions = Helpers.LoadFunctionData(FunctionPaths[l]);
                for (int i = 0; i < numTestFun; i++)
                {
                    data = KMeansAlgorithm.transposeFunction(KMeansAlgorithm.selectFunction(loadedSimFunctions, numTrainFun + i + 1, numAttributes));

                    var predictionResult = api.Algorithm.Predict(data, null) as KMeansFunctionRecognitionResult;

                    if (predictionResult.Result)
                    {
                        testingResults[i + l * numTestFun] = 1;
                    }
                    else
                    {
                        testingResults[i + l * numTestFun] = 0;
                    }
                }
            }

            // save results
            double[][] tempFunResults = new double[1][];
            tempFunResults[0] = testingResults;
            Helpers.Write2CSVFile(tempFunResults, saveDirectory + "Results.csv");

            double[][] TFMat = createTrueFalseMatrix(testingResults, FunctionPaths.Length);
            Helpers.Write2CSVFile(TFMat, saveDirectory + "TrueFalseMatrix.csv");
        }