public void CopyPoint(ref MyGrouping <int, Grade> a, IGrouping <int, Grade> b) //Copy cac gia tri trong variable dang reference type sang 1 variable khac { for (int j = 0; j < this.SubjectAmount; j++) { a.Add(new Grade { }); } int i = 0; foreach (var c in b) { a.ElementAt(i).Mark = c.Mark; i++; } }
public void KmeanPlusPlus(ref List <MyGrouping <int, Grade> > CentralPoint, System.Collections.Generic.List <IGrouping <int, Grade> > pointList) { Random ran = new Random(); // Khoi tao Student ID bat dau va ket thuc trong danh sach de random vi tri khoi tao int startingStudentID = pointList.First().Key; int endingStudentID = pointList.Last().Key; var temporaryVariable = new MyGrouping <int, Grade>(); // Khoi tao central point dau tien bang cach random CentralPoint.Add(temporaryVariable); int kmeanPlusPlusInitializePoint = ran.Next(startingStudentID, endingStudentID); var TheFisrtPoint = CentralPoint[0]; CopyPoint(ref TheFisrtPoint, pointList.Single(c => c.Key == kmeanPlusPlusInitializePoint)); int Count = 1; // Da ton tai san 1 central point while (true) // Bat dau tim cac central point con lai { if (Count == ClusterAmount) // Thoa so luong cluster yeu cau thi break { break; } List <MyGrouping <double?, Grade> > MinDistanceList = new List <MyGrouping <double?, Grade> >(); // Khoi tao List chua khoang cach be nhat den central point gan nhat cua tat cac cac point foreach (var point in pointList) { List <double?> Result = new List <double?>(); // List chua khoang cach den tung central point cua 1 diem for (int i = 0; i < Count; i++) { var temp = 0; Result.Add(temp); } for (int i = 0; i < this.SubjectAmount; i++) // Cong thuc tinh khoang cach, nhung khong tinh cang bac 2 { for (int j = 0; j < Count; j++) { Result[j] += (point.ElementAt(i).Mark - CentralPoint[j].ElementAt(i).Mark) * (point.ElementAt(i).Mark - CentralPoint[j].ElementAt(i).Mark); } } double?minResult = Result[0]; // Tim ra khoang cach be nhat for (int i = 0; i < Count; i++) { if (Result[i] < minResult) { minResult = Result[i]; } } MyGrouping <double?, Grade> temporaryCentralPoint = new MyGrouping <double?, Grade>(); // khoi tao 1 instance dai dien cho 1 point voi key la khoang cach ngan nhat toi central point gan nhat cua point do temporaryCentralPoint.Key = minResult; for (int j = 0; j < this.SubjectAmount; j++) { temporaryCentralPoint.Add(new Grade { }); } int elementCount = 0; foreach (var c in temporaryCentralPoint) { temporaryCentralPoint.ElementAt(elementCount).Mark = point.ElementAt(elementCount).Mark; elementCount++; } MinDistanceList.Add(temporaryCentralPoint); } IGrouping <double?, Grade> MaxPoint = MinDistanceList.First(); // Sau khi da co duoc ket qua khoang cach cua tung point den central point gan nhat cua no, ta se duyet het tat ca cac khoang cach nay va tim ra khoang cach lon nhat, point tuong ung se tro thanh point tiep theo foreach (var Point in MinDistanceList) // key o day chinh la khoang cach giua 1 point va central point gan nhat cua no { if (Point.Key > MaxPoint.Key) { MaxPoint = Point; } } var TemporaryPoint = new MyGrouping <int, Grade>(); // Copy point cac thanh phan trong 1 reference type variable sang 1 variable khac CentralPoint.Add(TemporaryPoint); for (int j = 0; j < this.SubjectAmount; j++) { CentralPoint[Count].Add(new Grade { }); } int gradeCount = 0; foreach (var c in CentralPoint[Count]) { CentralPoint[Count].ElementAt(gradeCount).Mark = MaxPoint.ElementAt(gradeCount).Mark; gradeCount++; } Count++; // Them thanh cong 1 central point } return; }