Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *********************************// FORMS //**********************
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Algorytm_KSrednich
- {
- public partial class Form1 : Form
- {
- public static string nazwa_pliku_z_wartosciami;
- public static string nazwa_pliku_z_opisem_atr;
- public static List<List<string>> probki = new List<List<string>>();
- public static List<bool> czy_atr_symb = new List<bool>();
- public static List<string> nazwy_atr = new List<string>();
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- richTextBox1.Clear();
- chart1.ChartAreas.Clear();
- System.Windows.Forms.DataVisualization.Charting.ChartArea przestrzen;
- chart1.ChartAreas.Add("");
- przestrzen = chart1.ChartAreas[0];
- chart1.Series.Clear();
- chart1.Series.Add("Punkty K");
- System.Windows.Forms.DataVisualization.Charting.Series seria_akt;
- seria_akt = chart1.Series[0];
- seria_akt.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
- chart1.Series.Add("Punkt");
- System.Windows.Forms.DataVisualization.Charting.Series seria_akt2;
- seria_akt2 = chart1.Series[1];
- seria_akt2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
- seria_akt2.Color = Color.Red;
- List<KSrednieMetody.Punkt> punkty = new List<KSrednieMetody.Punkt>();
- for (int i = 0; i <= probki.Count - 1; i++)
- {
- punkty.Add(new KSrednieMetody.Punkt(Convert.ToDouble(probki[i][0]), Convert.ToDouble(probki[i][1])));
- }
- foreach (var item in punkty)
- {
- richTextBox1.Text += item.ToString() + "/n";
- }
- List<KSrednieMetody.PunktK> kpunkty = new List<KSrednieMetody.PunktK>();
- Random r = new Random();
- int K1 = r.Next(1, 101);
- int K2 = r.Next(1, 101);
- int K3 = r.Next(1, 101);
- kpunkty.Add(new KSrednieMetody.PunktK() { X1 = punkty[K1].X1, X2 = punkty[K1].X2, Grupa = 1 });
- kpunkty.Add(new KSrednieMetody.PunktK() { X1 = punkty[K2].X1, X2 = punkty[K2].X2, Grupa = 2 });
- kpunkty.Add(new KSrednieMetody.PunktK() { X1 = punkty[K3].X1, X2 = punkty[K3].X2, Grupa = 3 });
- for (int i = 0; i < kpunkty.Count; i++)
- {
- seria_akt.Color = Color.Green;
- seria_akt.Points.AddXY(kpunkty[i].X1, kpunkty[i].X2);
- }
- //wyświetlenie wszystkich punktów
- //for (int i = 0; i < punkty.Count; i++)
- //{
- // seria_akt2.Points.AddXY(Convert.ToDouble(punkty[i].X1), Convert.ToDouble(punkty[i].X2));
- //}
- int counter = 0;
- KSrednieMetody.AlgoStart(ref punkty, ref kpunkty, 100);
- richTextBox1.Clear();
- richTextBox1.Text = "";
- chart1.Series.Add("gr. 1");
- System.Windows.Forms.DataVisualization.Charting.Series seria_akt3;
- seria_akt3 = chart1.Series[2];
- seria_akt3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
- seria_akt3.Color = Color.Cyan;
- chart1.Series.Add("gr. 2");
- System.Windows.Forms.DataVisualization.Charting.Series seria_akt4;
- seria_akt4 = chart1.Series[3];
- seria_akt4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
- seria_akt4.Color = Color.Orange;
- chart1.Series.Add("gr. 3");
- System.Windows.Forms.DataVisualization.Charting.Series seria_akt5;
- seria_akt5 = chart1.Series[4];
- seria_akt5.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
- seria_akt5.Color = Color.Pink;
- chart1.Series.Add("Punkty K po zmianie");
- System.Windows.Forms.DataVisualization.Charting.Series seria_akt6;
- seria_akt6 = chart1.Series[5];
- seria_akt6.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
- seria_akt6.Color = Color.Black;
- foreach (var punkt in punkty)
- {
- richTextBox1.Text += ("\tX1: " + punkt.X1 + " X2 " + punkt.X2 + " Grupa " + punkt.Grupa + " \n");
- if (punkt.Grupa == 1)
- {
- seria_akt3.Points.AddXY(punkt.X1, punkt.X2);
- }
- if (punkt.Grupa == 2)
- {
- seria_akt4.Points.AddXY(punkt.X1, punkt.X2);
- }
- if (punkt.Grupa == 3)
- {
- seria_akt5.Points.AddXY(punkt.X1, punkt.X2);
- }
- }
- richTextBox1.Text += ("\nK punkty: \n");
- foreach (var punktK in kpunkty)
- {
- richTextBox1.Text += ("\tX1: " + punktK.X1 + " X2: " + punktK.X2 + " Grupa " + punktK.Grupa + " \n");
- seria_akt6.Points.AddXY(punktK.X1, punktK.X2);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileOpisAtr = new OpenFileDialog();
- openFileOpisAtr.Title = "Wczytywanie pliku z informacjami o atrybutach";
- openFileOpisAtr.Filter = "TXT files|*.txt";
- if (openFileOpisAtr.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- nazwa_pliku_z_opisem_atr = openFileOpisAtr.FileName;
- rtb_WczytajInformacjeOAtrybutach.LoadFile(nazwa_pliku_z_opisem_atr, RichTextBoxStreamType.PlainText);
- }
- using (StreamReader reader1 = new StreamReader(nazwa_pliku_z_opisem_atr))
- {
- try
- {
- string line;
- string[] lineChars;
- while ((line = reader1.ReadLine()) != null)
- {
- line = line.Replace("\t", " ");
- while (line.Contains(" ")) line = line.Replace(" ", " ");
- string linia = line.Trim();
- Console.WriteLine(line);
- if (linia != "")
- {
- lineChars = linia.Split(' ');
- probki.Add(lineChars.ToList());
- var x = "b";
- if (lineChars[0] != "")
- {
- nazwy_atr.Add(lineChars[0]);
- }
- if (lineChars[1] == "s")
- {
- czy_atr_symb.Add(true);
- }
- else czy_atr_symb.Add(false);
- }
- }
- }
- catch (Exception)
- {
- MessageBox.Show("Błąd!");
- }
- }
- }
- }
- }
- *********************************// KŚREDNIE METODY //**********************
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Algorytm_KSrednich;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- namespace Algorytm_KSrednich
- {
- class KSrednieMetody
- {
- [Serializable]
- public class Punkt
- {
- public double X1 { get; set; }
- public double X2 { get; set; }
- public int Grupa { get; set; }
- public Punkt(double x1, double x2)
- {
- this.X1 = x1;
- this.X2 = x2;
- }
- }
- [Serializable]
- public class PunktK
- {
- public double X1 { get; set; }
- public double X2 { get; set; }
- public int Grupa { get; set; }
- }
- public static object GlebokaKopia(object obj)
- {
- object objResult = null;
- using (MemoryStream ms = new MemoryStream())
- {
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(ms, obj);
- ms.Position = 0;
- objResult = bf.Deserialize(ms);
- }
- return objResult;
- }
- private static List<PunktK> memeory = new List<PunktK>();
- public static void ZapamietajOstatniePolozenieKPunktu(List<PunktK> kpunkty)
- {
- memeory = new List<PunktK>();
- memeory = (List<PunktK>)GlebokaKopia(kpunkty);
- }
- public static void ZerowanieGrup(ref List<Punkt> zbior)
- {
- foreach (var punkt2 in zbior)
- {
- punkt2.Grupa = -1;
- }
- }
- public static void NadanieGrupZbiorowi(ref List<Punkt> zbior, List<PunktK> kpunkty)
- {
- for (int i = 0; i < zbior.Count; i++)
- {
- Punkt punkt2 = zbior[i];
- NadanieGrupy(ref punkt2, kpunkty);
- }
- }
- public static double LiczenieOdleglosc(PunktK p1, Punkt p2)
- {
- var x = Math.Pow(p1.X1 - p2.X1, 2);
- var x2 = Math.Pow(p1.X2 - p2.X2, 2);
- double odleglosc = Math.Sqrt(x + x2);
- return odleglosc;
- }
- public static void NadanieGrupy(ref Punkt punkt, List<PunktK> kpunkty)
- {
- Dictionary<double, PunktK> d = new Dictionary<double, PunktK>();
- foreach (var kpunkt in kpunkty)
- {
- double od = LiczenieOdleglosc(kpunkt, punkt);
- while (d.ContainsKey(od))
- {
- od = od + 0.000001;
- }
- d.Add(od, kpunkt);
- }
- var g = d.First(k => k.Key == d.Keys.Min()).Value.Grupa;
- punkt.Grupa = g;
- }
- public static void PrzesunieciePunktuK(List<Punkt> zbior, ref List<PunktK> kpunkty)
- {
- foreach (var kpunkt in kpunkty)
- {
- List<Punkt> punkty = new List<Punkt>();
- foreach (var punkt in zbior)
- {
- if (punkt.Grupa == kpunkt.Grupa)
- {
- punkty.Add(punkt);
- }
- }
- double x1 = 0;
- double x2 = 0;
- if (punkty.Count > 0)
- {
- x1 = punkty.Average(i => i.X1);
- x2 = punkty.Average(i => i.X2);
- kpunkt.X1 = x1;
- kpunkt.X2 = x2;
- }
- }
- }
- public static void AlgoStart(ref List<Punkt> punkty, ref List<PunktK> kpunkty, int licznikStop)
- {
- int licznik = 0;
- while (licznik < licznikStop)
- {
- ZapamietajOstatniePolozenieKPunktu(kpunkty);
- ZerowanieGrup(ref punkty);
- NadanieGrupZbiorowi(ref punkty, kpunkty);
- PrzesunieciePunktuK(punkty, ref kpunkty);
- licznik++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement