private static HeightObject[] KmeanAlgorithm(HeightObject[] height_object, HeightObject[] centroid_object, TextBox log) { // Khởi tạo trọng tâm mới là rỗng HeightObject[] new_cent_object = new HeightObject[centroid_object.Length]; // Với mỗi điểm tính trọng tâm gần nó nhất và gán cụm foreach (var ho in height_object) { ho.Cluster = GetNearestCluster(ho, centroid_object); } // Tính lại trọng tâm for (int i = 0; i < centroid_object.Length; i++) { new_cent_object[i] = CalulateNewCentroid(i, height_object); } //Logging.Write(log, "process: ", new_cent_object); // Nếu trọng tâm chưa trùng nhau, tiếp tục tính trọng tâm // Không thì thôi if (!EqualsCentroid(centroid_object, new_cent_object)) { return(KmeanAlgorithm(height_object, new_cent_object, log)); } else { return(new_cent_object); } }
private static HeightObject[] GetHeightObjectFromFile(string input_path) { // Lấy ra độ cao của những điểm được gán nhãn là 2 // Khai báo var las2txt_file = Las2txt(input_path); var heightObjects = new List <HeightObject>(); // Đọc file FileStream fs = File.Open(las2txt_file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BufferedStream bs = new BufferedStream(fs); StreamReader sr = new StreamReader(bs); string line; while ((line = sr.ReadLine()) != null) { var line_split = line.Split(' '); if (line_split[3] == "2") { var height = float.Parse(line_split[2]); var Object = new HeightObject(height); heightObjects.Add(Object); } } return(heightObjects.ToArray()); }
private static int GetNearestCluster(HeightObject ho, HeightObject[] centroid_object) { var min_index = 0; for (int i = 1; i < centroid_object.Length; i++) { if (ho.GetDistance(centroid_object[i]) < ho.GetDistance(centroid_object[min_index])) { min_index = i; } } return(min_index); }
private static HeightObject[] GetRandomCentroid(HeightObject[] height_object, int number_of_cluster) { HeightObject[] centroid = new HeightObject[number_of_cluster]; Random random = new Random(); int random_value = random.Next(1, height_object.Length / (number_of_cluster + 1) - 1); for (int i = 0; i < number_of_cluster; i++) { centroid[i] = height_object[i * random_value + random_value]; } return(centroid); }
public float GetDistance(HeightObject that) { return(Math.Abs(this.Value - that.Value)); }