Ejemplo n.º 1
0
        private void LoadDataButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FileExtensionMethods.wczytaj_baze_probek_z_tekstem(_valuesFile, _descriptionFile, out values, out areSymbols, out names);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                CleanProgram();

                return;
            }

            values.ForEach(row =>
            {
                for (int i = 0; i < row.Count; i++)
                {
                    if (areSymbols[i])
                    {
                        row[i] = $"\"{row[i]}\"";
                    }
                }
            });

            //ZMIANA
            for (int i = 0; i < values.Count; i++)
            {
                for (int j = 0; j < values[i].Count; j++)
                {
                    if (!areSymbols[j])
                    {
                        values[i][j] = (double.Parse(values[i][j], CultureInfo.InvariantCulture) + i).ToString();
                    }
                }
            }

            DataTable data = new DataTable();

            foreach (var name in names)
            {
                data.Columns.Add(name);
            }

            foreach (var row in values)
            {
                DataRow dataRow = data.NewRow();
                for (int i = 0; i < names.Count; i++)
                {
                    dataRow[i] = row[i];
                }
                data.Rows.Add(dataRow);
            }


            LoadedDataGrid.ItemsSource = data.DefaultView;

            CleanProgram();
        }
Ejemplo n.º 2
0
 private void KMedoidButton_Click(object sender, EventArgs e)
 {
     FileExtensionMethods.wczytaj_baze_probek_z_tekstem(_valuesFile, _descriptionFile, out List <List <string> > values, out List <bool> areSymbols, out List <string> names);
 }
Ejemplo n.º 3
0
        private void KMeanButton_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(GroupsCount.Text, out int groupsCount))
            {
                MessageBox.Show("Niepoprawna liczba grup!");
                return;
            }
            else
            {
                if (groupsCount < 1)
                {
                    MessageBox.Show("Niepoprawna liczba iteracji!");
                    return;
                }
            }

            if (!int.TryParse(IterationCount.Text, out int iterationsCount))
            {
                MessageBox.Show("Niepoprawna liczba iteracji!");
                return;
            }
            else
            {
                if (iterationsCount < 1)
                {
                    MessageBox.Show("Niepoprawna liczba iteracji!");
                    return;
                }
            }

            wykres_czysc();

            FileExtensionMethods.wczytaj_baze_probek_z_tekstem(_valuesFile, _descriptionFile, out List <List <string> > values, out List <bool> areSymbols, out List <string> names);

            MainChart.ChartAreas.Add("chart");

            var numery_atr = new List <int>();

            probki_str_na_liczby(values, numery_atr, out List <List <double> > probki_num);

            var points = new List <DoublePoint>();

            for (int i = 0; i < probki_num[0].Count; i++)
            {
                var point = new DoublePoint()
                {
                    X = probki_num[0][i],
                    Y = probki_num[1][i]
                };

                points.Add(point);
            }

            List <DoublePoint> groupCenters = new List <DoublePoint>();

            var rand = new Random();

            for (int i = 0; i < groupsCount; i++)
            {
                var centerPoint = points[rand.Next(0, points.Count - 1)];
                if (groupCenters.Any(dp => dp.X == centerPoint.X && dp.Y == centerPoint.Y))
                {
                    i--;
                    continue;
                }

                groupCenters.Add(centerPoint);
            }

            Dictionary <DoublePoint, List <DoublePoint> > groups = new Dictionary <DoublePoint, List <DoublePoint> >();

            foreach (var groupCenter in groupCenters)
            {
                groups.Add(groupCenter, new List <DoublePoint>());
            }

            for (int i = 0; i < iterationsCount; i++)
            {
                foreach (var point in points)
                {
                    int minimalDestinationGroupIndex = -1;

                    double minimalDistance = -1;

                    for (int j = 0; j < groupCenters.Count; j++)
                    {
                        if (minimalDestinationGroupIndex == -1)
                        {
                            minimalDestinationGroupIndex = 0;
                            minimalDistance = CountEuclideanDistanceOfPoints(point, groupCenters[j]);
                            continue;
                        }

                        var distanceNow = CountEuclideanDistanceOfPoints(point, groupCenters[j]);

                        if (distanceNow < minimalDistance)
                        {
                            minimalDestinationGroupIndex = j;
                            minimalDistance = distanceNow;
                        }
                    }

                    groups[groupCenters[minimalDestinationGroupIndex]].Add(point);
                }

                List <DoublePoint> newGroupCenters = new List <DoublePoint>();
                Dictionary <DoublePoint, List <DoublePoint> > newGroups = new Dictionary <DoublePoint, List <DoublePoint> >();

                for (int j = 0; j < groups.Count; j++)
                {
                    double xSum = 0;
                    double ySum = 0;

                    for (int k = 0; k < groups[groupCenters[j]].Count; k++)
                    {
                        xSum += groups[groupCenters[j]][k].X;
                        ySum += groups[groupCenters[j]][k].Y;
                    }

                    var newCenterPoint = new DoublePoint()
                    {
                        X = xSum != 0 ? xSum / (double)groups[groupCenters[j]].Count : xSum,
                        Y = ySum != 0 ? ySum / (double)groups[groupCenters[j]].Count : ySum
                    };

                    newGroupCenters.Add(newCenterPoint);
                    newGroups.Add(newCenterPoint, new List <DoublePoint>());
                }

                //ZMIANA
                double minimalCountGroup = double.MaxValue;
                double maximalCountGroup = double.MinValue;

                foreach (var group in groups)
                {
                    if (group.Value.Count < minimalCountGroup)
                    {
                        minimalCountGroup = group.Value.Count;
                    }

                    if (group.Value.Count > maximalCountGroup)
                    {
                        maximalCountGroup = group.Value.Count;
                    }
                }

                if (1.1 * minimalCountGroup > maximalCountGroup)
                {
                    break;
                }

                if (i == iterationsCount - 1)
                {
                    break;
                }

                groupCenters = newGroupCenters;
                groups       = newGroups;
            }

            foreach (var group in groups)
            {
                List <double> xGroup = new List <double>();
                List <double> yGroup = new List <double>();

                foreach (var point in group.Value)
                {
                    xGroup.Add(point.X);
                    yGroup.Add(point.Y);
                }

                wykres_punkty_rysuj(xGroup, yGroup);
            }

            foreach (var groupCenter in groupCenters)
            {
                wykres_punkty_rysuj(new List <double>()
                {
                    groupCenter.X
                }, new List <double>()
                {
                    groupCenter.Y
                });
            }

            for (int i = MainChart.Series.Count / 2; i < MainChart.Series.Count; i++)
            {
                MainChart.Series[i].MarkerSize = 20;
            }
        }
Ejemplo n.º 4
0
        private void FCMButton_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(GroupsCount.Text, out int groupsCount))
            {
                MessageBox.Show("Niepoprawna liczba grup!");
                return;
            }
            else
            {
                if (groupsCount < 1)
                {
                    MessageBox.Show("Niepoprawna liczba iteracji!");
                    return;
                }
            }

            if (!int.TryParse(IterationCount.Text, out int iterationsCount))
            {
                MessageBox.Show("Niepoprawna liczba iteracji!");
                return;
            }
            else
            {
                if (iterationsCount < 1)
                {
                    MessageBox.Show("Niepoprawna liczba iteracji!");
                    return;
                }
            }

            if (!double.TryParse(FuzzyValue.Text, out double fuzziness))
            {
                MessageBox.Show("Niepoprawny stopień rozmycia!");
                return;
            }
            else
            {
                if (fuzziness < 1)
                {
                    MessageBox.Show("Niepoprawny stopień rozmycia!");
                    return;
                }
            }

            wykres_czysc();

            FileExtensionMethods.wczytaj_baze_probek_z_tekstem(_valuesFile, _descriptionFile, out List <List <string> > values, out List <bool> areSymbols, out List <string> names);

            MainChart.ChartAreas.Add("chart");

            var numery_atr = new List <int>();

            probki_str_na_liczby(values, numery_atr, out List <List <double> > probki_num);

            var points = new List <DoublePoint>();

            for (int i = 0; i < probki_num[0].Count; i++)
            {
                var point = new DoublePoint()
                {
                    X = probki_num[0][i],
                    Y = probki_num[1][i]
                };

                points.Add(point);
            }

            List <DoublePoint> groupCenters = new List <DoublePoint>();

            var rand = new Random();

            for (int i = 0; i < groupsCount; i++)
            {
                var centerPoint = points[rand.Next(0, points.Count - 1)];
                if (groupCenters.Any(dp => dp.X == centerPoint.X && dp.Y == centerPoint.Y))
                {
                    i--;
                    continue;
                }

                groupCenters.Add(centerPoint);
            }

            for (int i = 0; i < iterationsCount; i++)
            {
                foreach (var point in points)
                {
                    List <double> destinations = new List <double>();

                    for (int j = 0; j < groupCenters.Count; j++)
                    {
                        destinations.Add(CountEuclideanDistanceOfPoints(point, groupCenters[j]));
                    }

                    double sumOfAffiliation = 0;

                    foreach (var destination in destinations)
                    {
                        if (destination != 0)
                        {
                            sumOfAffiliation += Math.Pow(destination, 1 - fuzziness);
                        }
                    }

                    foreach (var destination in destinations)
                    {
                        //ZMIANA
                        var affiliation = rand.NextDouble();

                        point.Affiliation.Add(affiliation);
                        //if (destination == 0)
                        //{
                        //    point.Affiliation.Add(0);
                        //}
                        //else
                        //{
                        //    point.Affiliation.Add(Math.Pow(destination, 1 - fuzziness) / sumOfAffiliation);
                        //}
                    }

                    double affSum = 0;

                    for (int a = 0; a < point.Affiliation.Count; a++)
                    {
                        affSum += point.Affiliation[a];
                    }

                    for (int a = 0; a < point.Affiliation.Count; a++)
                    {
                        point.Affiliation[a] /= affSum;
                    }
                }

                List <DoublePoint> newGroupCenters = new List <DoublePoint>();

                for (int j = 0; j < groupCenters.Count; j++)
                {
                    double affiliationSum = 0;

                    foreach (var point in points)
                    {
                        affiliationSum += Math.Round(Math.Pow(point.Affiliation[j], fuzziness), 9);
                    }

                    double newCenterX = 0;

                    foreach (var point in points)
                    {
                        newCenterX += Math.Round(point.X * Math.Pow(point.Affiliation[j], fuzziness), 9);
                    }

                    newCenterX = newCenterX / affiliationSum;

                    double newCenterY = 0;

                    foreach (var point in points)
                    {
                        newCenterY += Math.Round(point.Y * Math.Pow(point.Affiliation[j], fuzziness), 9);
                    }

                    newCenterY = newCenterY / affiliationSum;

                    var newCenterPoint = new DoublePoint()
                    {
                        X = newCenterX,
                        Y = newCenterY
                    };

                    newGroupCenters.Add(newCenterPoint);
                }

                if (i == iterationsCount - 1)
                {
                    break;
                }

                groupCenters = newGroupCenters;
            }

            List <double> xGroup = new List <double>();
            List <double> yGroup = new List <double>();

            foreach (var point in points)
            {
                xGroup.Add(point.X);
                yGroup.Add(point.Y);
            }

            wykres_punkty_rysuj(xGroup, yGroup);

            foreach (var groupCenter in groupCenters)
            {
                wykres_punkty_rysuj(new List <double>()
                {
                    groupCenter.X
                }, new List <double>()
                {
                    groupCenter.Y
                });
            }

            for (int i = MainChart.Series.Count / 2; i < MainChart.Series.Count; i++)
            {
                MainChart.Series[i].MarkerSize = 20;
            }
        }
        private void IrysLoadButton_Click(object sender, EventArgs e)
        {
            wykres_czysc();

            MainChart.ChartAreas.Add(new ChartArea()
            {
                Name     = "TopLeft",
                Position = new ElementPosition(0, 0, 45, 50)
            });
            MainChart.ChartAreas.Add(new ChartArea()
            {
                Name     = "TopRight",
                Position = new ElementPosition(45, 0, 45, 50)
            });
            MainChart.ChartAreas.Add(new ChartArea()
            {
                Name     = "BottomLeft",
                Position = new ElementPosition(0, 50, 45, 50)
            });
            MainChart.ChartAreas.Add(new ChartArea()
            {
                Name     = "BottomRight",
                Position = new ElementPosition(45, 50, 45, 50)
            });

            try
            {
                FileExtensionMethods.wczytaj_baze_probek_z_tekstem(_valuesFile, _descriptionFile, out List <List <string> > values, out List <bool> areSymbols, out List <string> names);

                List <List <double> > attributes = new List <List <double> >();

                for (int i = 0; i < values[0].Count - 1; i++)
                {
                    attributes.Add(new List <double>());
                }

                List <string> classes = new List <string>();

                foreach (var row in values)
                {
                    for (int i = 0; i < row.Count - 1; i++)
                    {
                        double number;
                        try
                        {
                            number = double.Parse(row[i], CultureInfo.InvariantCulture);
                            attributes[i].Add(number);
                        }
                        catch
                        {
                            throw new Exception("Dane z pliku nie są w formacie double.");
                        }
                    }


                    if (!classes.Contains(row[row.Count - 1]))
                    {
                        classes.Add(row[row.Count - 1]);
                    }
                }

                Dictionary <string, List <int> > indexesPerClass = new Dictionary <string, List <int> >();

                foreach (var classe in classes)
                {
                    List <int> indexes = new List <int>();
                    for (int i = 0; i < values.Count; i++)
                    {
                        if (values[i][values[i].Count - 1] == classe)
                        {
                            indexes.Add(i);
                        }
                    }
                    indexesPerClass.Add(classe, indexes);
                }

                PaintArea(indexesPerClass, attributes, 2, 3);
                PaintArea(indexesPerClass, attributes, 1, 3);
                PaintArea(indexesPerClass, attributes, 0, 3);
                PaintArea(indexesPerClass, attributes, 1, 2);

                for (int i = 0; i < MainChart.Series.Count; i++)
                {
                    MainChart.Series[i].ChartArea = MainChart.ChartAreas[i / 3].Name;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                CleanProgram();

                return;
            }
        }