Ejemplo n.º 1
0
        public static void ReadFromXLS(string path) // Function for reading data from the file .xls
        {
            HSSFWorkbook hssfwb;

            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))  // Data2
            {
                hssfwb = new HSSFWorkbook(file);
            }
            ISheet        sheet = hssfwb.GetSheet("FirstList");
            List <string> Elements = new List <string>();
            int           column = 1, Row = 1;

            countColumnData = 0;
            Elements.Clear();

            for (column = 1; sheet.GetRow(0).GetCell(column) != null; column++) // подсчет количества колонок в файле, а также запись названия ЛП
            {
                List <Term> t = new List <Term>();
                NameOfLinguisticVariables.Add(string.Format("{0: 0.0}", sheet.GetRow(0).GetCell(column)));
                LinguisticVariable LP = new LinguisticVariable(new Guid(), string.Format("{0: 0.0}", sheet.GetRow(0).GetCell(column)), t, 0, 1);
                //FKB.ListVar.Add(LP);
                countColumnData += 1;
            }

            if (counterFoRowDataFromFile == 0)
            {
                for (Row = 1; sheet.GetRow(Row) != null && sheet.GetRow(Row).GetCell(0) != null; Row++)  // подсчет количества строк в файле
                {
                    counterFoRowDataFromFile++;
                }
            }
            column         = 1;
            ElementsMatrix = new double[counterFoRowDataFromFile, countColumnData];
            for (int row = 1; row <= counterFoRowDataFromFile; row++)  // запись построчно с файла данных в список ElementsMulti -MultiDimensionalVector-
            {
                MultiDimensionalVector h = new MultiDimensionalVector();
                while (sheet.GetRow(row).GetCell(column) != null)
                {
                    Elements.Add(string.Format("{0: 0.0}", sheet.GetRow(row).GetCell(column)));
                    column += 1;
                }
                List <double> result  = Elements.Select(x => double.Parse(x)).ToList();
                int           integer = 0;
                foreach (double x in result)
                {
                    var newVector = x;
                    h.Add(newVector);
                    ElementsMatrix[row - 1, integer] = x;
                    integer++;
                }
                ElementsMulti.Add(h);
                column = 1;
                Elements.Clear();
            }
            ClusterCount = (counterFoRowDataFromFile / 2) + 3;
            if (ClusterCount > 10)
            {
                ClusterCount = 7;
            }
        }
Ejemplo n.º 2
0
        public MultiDimensionalVector FindClosestCentroid(MultiDimensionalVector element)  //нахождение ближайшего центроида для элемента
        {
            int    indexElement         = Elements.IndexOf(element);
            int    indexClosestCentroid = 0;
            double min = double.MaxValue;

            for (int j = 0; j < Centroids.Count; ++j)
            {
                if (DistanceMatrix[indexElement, j] < min)
                {
                    min = DistanceMatrix[indexElement, j];
                    indexClosestCentroid = j;
                }
            }
            MultiDimensionalVector ClosestCentroid = Centroids.ElementAt(indexClosestCentroid);

            return(ClosestCentroid);
        }
Ejemplo n.º 3
0
        public bool EqualPrevAndNewCentroids(double epsilon)  //сравнение новых вычисленных центроидов для каждого кластера Cluster.Centroid с имеющимися центроидами (Centroids), вычисление с погрешностью epsilon
        {
            bool equivalent = true;

            for (int i = 0; i < Centroids.Count; ++i)
            {
                //if (Centroids.ElementAt(i) == Clusters.ElementAt(i).Centroid)
                MultiDimensionalVector differentVector = Centroids.ElementAt(i) - Clusters.ElementAt(i).Centroid;
                foreach (var coord in differentVector)
                {
                    if (Math.Abs(coord) > epsilon)
                    {
                        equivalent = false;
                        break;
                    }
                }
            }
            return(equivalent);
        }
Ejemplo n.º 4
0
        public MultiDimensionalVector CalculateCentroid(Cluster cluster)  //вычисление центроида для кластера и обновление значения центроида в кластере
        {
            MultiDimensionalVector centroid = new MultiDimensionalVector();

            for (int i = 0; i < cluster.Centroid.Count; ++i)
            {
                centroid.Add(0.0);
            }

            if (cluster.Elements.Count > 0)
            {
                foreach (var e in cluster.Elements)
                {
                    centroid = centroid + e;
                }
                centroid        /= cluster.Elements.Count;
                cluster.Centroid = centroid;
            }
            else
            {
                centroid = this.Elements.ElementAt(rand.Next(Elements.Count - 1));
            }
            return(centroid);
        }