Ejemplo n.º 1
0
            /// <summary>
            /// Gets the vectors with a specific value count
            /// </summary>
            /// <param name="count">Count of element in the vector values</param>
            /// <returns>Vector collection</returns>
            public VectorCollection GetVectorsCountValues(int count)
            {
                var vc = new VectorCollection();

                if (count > GetMaxCountValues())
                {
                    return(vc);
                }
                foreach (var vec in this)
                {
                    if (vec.Values.Count == count)
                    {
                        vc.Add(vec);
                    }
                }
                return(vc);
            }
Ejemplo n.º 2
0
        public static void KnnMain()
        {
            Console.WriteLine("Using demo setup?[y/n]");
            string str = Console.ReadLine();

            if (str.ToLower().Contains("y") == false)
            {
                Console.WriteLine("Input node maximum value:(Default:10000)");
                NodesMaxValueSet = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input node count:(Default:150)");
                NodesSet = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input groups count for random points to divid into:(Default:3, Min:1)");
                DataGroupCount = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input fluctuation ratio:(Default:0.5, Min:0.1)");
                FluctuationRatio = Convert.ToSingle(Console.ReadLine());

                Console.WriteLine("Input new node count:(Default:10)");
                NewNodesCount = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input k set:(Default:7)");
                k = Convert.ToInt32(Console.ReadLine());
            }

            //Gen grouped random nodes
            Random2DPoints.OnGeneratePointGroups += Random2DPoints_OnGeneratePointGroups;
            var ClassifiedPoints = Random2DPoints.GenerateRandomPointsGroup(NodesMaxValueSet, NodesSet, DataGroupCount, FluctuationRatio);
            var Dataset          = new VectorCollection <Point2D>(ClassifiedPoints);

            Dataset.Print();

            var           knnTrainer = new KnnTrainer <Point2D>(Dataset);
            Knn <Point2D> knn        = new Knn <Point2D>(knnTrainer.Train());

            knn.OnClassify += Knn_OnClassify;

            while (Retry)
            {
                var NewNodes = Random2DPoints.GenerateRandomPoints(NodesMaxValueSet, NewNodesCount);

                foreach (var NewNode in NewNodes)
                {
                    knn.Classify(k, NewNode);
                }

                InputCommand();
            }
        }
Ejemplo n.º 3
0
        public static void KmeansMain()
        {
            Console.WriteLine("Using demo setup?[y/n]");
            string str = Console.ReadLine();

            if (str.ToLower().Contains("y") == false)
            {
                Console.WriteLine("Input node maximum value:(Default:10000 , Min:0)");
                NodesMaxValueSet = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input node count:(Default:10000 , Min:0)");
                NodesSet = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input groups count for random points to divid into:(Default:3 , Min:0)");
                DataGroupCount = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input fluctuation ratio:(Default:0.18 , Min:0 , Max:0.5)");
                FluctuationRatio = Convert.ToSingle(Console.ReadLine());

                Console.WriteLine("Input k:( Default:3 , Min:1)");
                k = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input iteration limit:(Default:100)");
                IterationLimit = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input convergence distance:(Default:5)");
                ConvDistance = Convert.ToInt32(Console.ReadLine());
            }

            //Gen grouped random nodes
            Random2DPoints.OnGeneratePointGroups += Random2DPoints_OnGeneratePointGroups;
            var RandomPoints = Random2DPoints.GenerateRandomPointsGroup(NodesMaxValueSet, NodesSet, DataGroupCount, FluctuationRatio);
            var Dataset      = new VectorCollection <Point2D>(RandomPoints);

            Dataset.Print();

            while (Retry)
            {
                KmeansTrainer <Point2D> kmeansTrainer = new KmeansTrainer <Point2D>(Dataset, k, ConvDistance, IterationLimit);
                kmeansTrainer.OnIteration += Kmeans_OnIteration;

                var TrainResult = kmeansTrainer.Train();

                //Kmeans<Point2D> kmeans = new Kmeans<Point2D>(TrainResult);

                InputCommand();
                kmeansTrainer.OnIteration -= Kmeans_OnIteration;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Simplifies and combines the matrix
        /// </summary>
        /// <returns>Result group collection instance</returns>
        public ResultGroupCollection Simplify()
        {
            var vectors = new VectorCollection();

            foreach (var elm in Matrix.Keys)
            {
                var vect = vectors.GetVectorByKey(elm.Name);
                if (vect == null)
                {
                    vect = new Vector {
                        Key = elm.Name
                    };
                    vectors.Add(vect);
                }
                vect.Values.Add(Matrix[elm]);
            }

            var maxValues    = vectors.GetMaxCountValues();
            var resultGroups = new ResultGroupCollection();

            while (maxValues > 0)
            {
                if (vectors.Count == 1)
                {
                    var rgroup = new ResultGroup();
                    rgroup.Keys.Add(vectors[0].Key);
                    rgroup.Values.AddRange(vectors[0].Values);
                    resultGroups.Add(rgroup);
                }
                else
                {
                    var rgroup = new ResultGroup();
                    for (var i = 0; i < vectors.Count; i++)
                    {
                        var vecX = vectors[i];

                        for (var j = i + 1; j < vectors.Count; j++)
                        {
                            var vecY = vectors[j];
                            var elementsCoincidentes = new List <string>();
                            var numIgualdades        = 0;
                            foreach (var elmX in vecX.Values)
                            {
                                foreach (var elmY in vecY.Values)
                                {
                                    if (elmX != elmY)
                                    {
                                        continue;
                                    }
                                    numIgualdades++;
                                    if (!elementsCoincidentes.Contains(elmX))
                                    {
                                        elementsCoincidentes.Add(elmX);
                                    }
                                }
                            }
                            if (numIgualdades == maxValues)
                            {
                                if (rgroup.Values.Count == 0)
                                {
                                    rgroup.Values.AddRange(elementsCoincidentes);
                                    rgroup.Keys.Add(vecX.Key);
                                    rgroup.Keys.Add(vecY.Key);
                                }
                                else
                                {
                                    var igualdadesgrupo = 0;
                                    foreach (var elmX in rgroup.Values)
                                    {
                                        foreach (var elmY in elementsCoincidentes)
                                        {
                                            if (elmX == elmY)
                                            {
                                                igualdadesgrupo++;
                                            }
                                        }
                                    }
                                    if (igualdadesgrupo != maxValues)
                                    {
                                        if (rgroup.Keys.Count > 0)
                                        {
                                            resultGroups.Add(rgroup);
                                        }
                                        rgroup = new ResultGroup();
                                        rgroup.Values.AddRange(elementsCoincidentes);
                                        rgroup.Keys.Add(vecX.Key);
                                        rgroup.Keys.Add(vecY.Key);
                                    }
                                    else if (igualdadesgrupo == maxValues)
                                    {
                                        var xAdded = false;
                                        foreach (var key in rgroup.Keys)
                                        {
                                            if (key == vecX.Key)
                                            {
                                                xAdded = true;
                                            }
                                        }
                                        if (!xAdded)
                                        {
                                            rgroup.Keys.Add(vecX.Key);
                                        }
                                        var yAdded = false;
                                        foreach (var key in rgroup.Keys)
                                        {
                                            if (key == vecY.Key)
                                            {
                                                yAdded = true;
                                            }
                                        }
                                        if (!yAdded)
                                        {
                                            rgroup.Keys.Add(vecY.Key);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (maxValues == vecX.Values.Count && maxValues > vecY.Values.Count)
                                {
                                    if (rgroup.Keys.Count > 0 && !resultGroups.ContainsRGroup(vecX))
                                    {
                                        resultGroups.Add(rgroup);
                                    }
                                    rgroup = new ResultGroup();
                                    rgroup.Values.AddRange(vecX.Values);
                                    rgroup.Keys.Add(vecX.Key);
                                }
                                if (maxValues == vecY.Values.Count && maxValues > vecX.Values.Count)
                                {
                                    if (rgroup.Keys.Count > 0 && !resultGroups.ContainsRGroup(vecY))
                                    {
                                        resultGroups.Add(rgroup);
                                    }
                                    rgroup = new ResultGroup();
                                    rgroup.Values.AddRange(vecY.Values);
                                    rgroup.Keys.Add(vecY.Key);
                                }
                            }
                        }
                    }
                    if (rgroup.Keys.Count > 0 && rgroup.Values.Count > 0)
                    {
                        resultGroups.Add(rgroup);
                    }
                }
                maxValues--;
            }
            return(resultGroups);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            VectorCollection <RawImage28x28> Dataset = null;
            Knn <RawImage28x28> knn = null;
            KmeansTrainResult <RawImage28x28> KmeansTrainResult = null;
            Kmeans <RawImage28x28>            kmeans            = null;

            //CheckFiles
            if (File.Exists(MnistPixelFilePath) == false)
            {
                MnistPixelFilePath = Environment.CurrentDirectory + "\\train-images.idx3-ubyte";
            }

            if (File.Exists(MnistLabelFilePath) == false)
            {
                MnistLabelFilePath = Environment.CurrentDirectory + "\\train-labels.idx1-ubyte";
            }

            if (File.Exists(KmeansTrainResultFilePath) == false)
            {
                KmeansTrainResultFilePath = Environment.CurrentDirectory + "\\KmeansTrainResult.json";
            }


            //LoadFiles
            if (File.Exists(MnistPixelFilePath) && File.Exists(MnistLabelFilePath))
            {
                var MnistDataSet = MnistDataSetLoader.LoadData(MnistPixelFilePath, MnistLabelFilePath);
                Dataset = new VectorCollection <RawImage28x28>(MnistDataSet);
            }

            if (File.Exists(KmeansTrainResultFilePath))
            {
                KmeansTrainResult = new KmeansTrainResult <RawImage28x28>(KmeansTrainResultFilePath);
            }

            if (Dataset != null)
            {
                var knnTrainer = new KnnTrainer <RawImage28x28>(Dataset);
                knn = new Knn <RawImage28x28>(knnTrainer.Train());

                if (KmeansTrainResult == null)
                {
                    Console.WriteLine("Can't find Kmeans train data.");
                    KmeansTrainResult = AskKmeansTrainData(false);
                }
                else if (KmeansTrainResult != null)
                {
                    Console.WriteLine("Found Kmeans train data.");
                    Console.WriteLine("[U]se trained data or [t]rain again? [u/t]");
                    if (Console.ReadLine().ToLower().Contains('t'))
                    {
                        KmeansTrainResult = null;
                        KmeansTrainResult = AskKmeansTrainData(true);
                    }
                }
            }
            else
            {
                Console.WriteLine("Can't find Knn train data, ignored");
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }

            if (KmeansTrainResult != null)
            {
                kmeans = new Kmeans <RawImage28x28>(KmeansTrainResult);
            }

            if (kmeans == null && knn == null)
            {
                Console.WriteLine("Fatal error: Can not find any trained data.");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }

            MainForm form = new MainForm(knn, kmeans);

            Application.EnableVisualStyles();
            Application.Run(form);
        }