Pozwolę sobie wznowić temat. Przerobiłem program , według moich potrzeb - tak by ilość centroidów była podawana przez użytkownika. Doczytałem, że algorytm nie polega tylko na jednokrotnym przypisaniu punktów do centroidów. To tak na prawdę jedna z wielu i za razem pierwsza iteracja. W dalszych iteracjach chodzi o to, by dokonać przesunięcia centroidów i co za tym idzie dokonać ponownego przypisania do nich punktów. Po pierwszej iteracji współrzędne danego centroidu to po prostu średnia punktów (x,y) przypisanych do centroida. W skrócie:
- Przypisz punkty do najbliższych centroidów, których wartości są losowe - (zrobione - pierwsza iteracja)
- Oblicz nowe współrzędne centroidów - dla każdego centroida jest to średnia arytmetyczna punktów, które są do niego przypisane. (metoda meanYvalue, meanXvalue)
- Dokonaj przesunięcia centroidów i przypisz do nich nowe punkty.
- Punkty 2 i 3 wykonaj w zależności od ilości iteracji , które wprowadzi użytkownik.
Udało mi się dokonać pewnej modyfikacji i program wykonał drugą iterację - zrobiłem to jednak w sposób bardzo prymitywny - utworzylem kolejne listy, taką samą metodę przypisującą punkty, tylko z innymi wartościami i działa... ale to tylko dla drugiej iteracji. A jeśli chciałbym zrobić ich 20, albo i 100? Załączam poniżej "bazowy" kod , który wymaga wyżej wymienionych poprawek. Bez drugiej iteracji, tak by "nie zaśmiecać" kodu wyjścowego. Z góry dzięki za pomoc.
public class Calculate
{
static DecimalFormat df2 = new DecimalFormat("#.##");
static List<Centroid> centroids = new ArrayList<Centroid>();
static List<List<Point>> list_cx_elements = new ArrayList<>();
static List<List<Point>> list_cx_elements_2 = new ArrayList<>();
static List<Point> c1_elements = new ArrayList<Point>();
static List<Point> c2_elements = new ArrayList<Point>();
static List<Point> c3_elements = new ArrayList<Point>();
static List<Point> c4_elements = new ArrayList<Point>();
static List<Point> c5_elements = new ArrayList<Point>();
static List<Point> c6_elements = new ArrayList<Point>();
static List<Point> c7_elements = new ArrayList<Point>();
static List<Point> c8_elements = new ArrayList<Point>();
static List<Point> c9_elements = new ArrayList<Point>();
static List<Point> c10_elements = new ArrayList<Point>();
static Centroid c1 = new Centroid("C1", Calculate.rand(), Calculate.rand());
static Centroid c2 = new Centroid("C2", Calculate.rand(), Calculate.rand());
static Centroid c3 = new Centroid("C3", Calculate.rand(), Calculate.rand());
static Centroid c4 = new Centroid("C4", Calculate.rand(), Calculate.rand());
static Centroid c5 = new Centroid("C5", Calculate.rand(), Calculate.rand());
static Centroid c6 = new Centroid("C6", Calculate.rand(), Calculate.rand());
static Centroid c7 = new Centroid("C7", Calculate.rand(), Calculate.rand());
static Centroid c8 = new Centroid("C8", Calculate.rand(), Calculate.rand());
static Centroid c9 = new Centroid("C9", Calculate.rand(), Calculate.rand());
static Centroid c10 = new Centroid("C10", Calculate.rand(), Calculate.rand());
//http://itcraftsman.pl/algorytm-k-srednich-uczenie-nienadzorowane/
public static double ile(List<? extends Point> list)
{
return list.size();
}
public static double meanXvalue(List<? extends Point> list)
{
double suma =0 ;
for(Point p: list)
{
suma+=p.getX();
}
return suma / ile(list);
}
public static double meanYvalue(List<? extends Point> list)
{
double suma =0 ;
for(Point p: list)
{
suma+=p.getY() ;
}
return suma / ile(list);
}
public static double distance(double x1, double y1, double x2, double y2)
{
double distance =0 ;
double s1 = Math.pow((x2 - x1), 2);
double s2 = Math.pow((y2 - y1), 2);
distance = Math.sqrt(s1+s2);
return distance;
}
public static double rand ()
{
int min = -10;
int max = 10;
Random r = new Random();
double randomValue = min + (max - min) * r.nextDouble();
return randomValue;
}
public static void number_of_centroids(int x, List<? extends Point> list)
{
List<Centroid> temp = new ArrayList<>();
if (x==2)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
temp.add(c1);
temp.add(c2);
}
if (x==3)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
}
if (x==4)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
list_cx_elements.add(c4_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
temp.add(c4);
}
if (x==5)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
list_cx_elements.add(c4_elements);
list_cx_elements.add(c5_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
temp.add(c4);
temp.add(c5);
}
if (x==6)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
list_cx_elements.add(c4_elements);
list_cx_elements.add(c5_elements);
list_cx_elements.add(c6_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
temp.add(c4);
temp.add(c5);
temp.add(c6);
}
if (x==7)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
list_cx_elements.add(c4_elements);
list_cx_elements.add(c5_elements);
list_cx_elements.add(c6_elements);
list_cx_elements.add(c7_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
temp.add(c4);
temp.add(c5);
temp.add(c6);
temp.add(c7);
}
if (x==8)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
list_cx_elements.add(c4_elements);
list_cx_elements.add(c5_elements);
list_cx_elements.add(c6_elements);
list_cx_elements.add(c7_elements);
list_cx_elements.add(c8_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
temp.add(c4);
temp.add(c5);
temp.add(c6);
temp.add(c7);
temp.add(c8);
}
if (x==9)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
list_cx_elements.add(c4_elements);
list_cx_elements.add(c5_elements);
list_cx_elements.add(c6_elements);
list_cx_elements.add(c7_elements);
list_cx_elements.add(c8_elements);
list_cx_elements.add(c9_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
temp.add(c4);
temp.add(c5);
temp.add(c6);
temp.add(c7);
temp.add(c8);
temp.add(c9);
}
if (x==10)
{
list_cx_elements.add(c1_elements);
list_cx_elements.add(c2_elements);
list_cx_elements.add(c3_elements);
list_cx_elements.add(c4_elements);
list_cx_elements.add(c5_elements);
list_cx_elements.add(c6_elements);
list_cx_elements.add(c7_elements);
list_cx_elements.add(c8_elements);
list_cx_elements.add(c9_elements);
list_cx_elements.add(c10_elements);
temp.add(c1);
temp.add(c2);
temp.add(c3);
temp.add(c4);
temp.add(c5);
temp.add(c6);
temp.add(c7);
temp.add(c8);
temp.add(c9);
temp.add(c10);
}
for(Point p: list)
{
double min_temp = Double.MAX_VALUE - 1;
for(Centroid c: temp)
{
double distance = distance(p.getX(), p.getY(), c.getX(), c.getY());
if(distance < min_temp)
min_temp = distance;
}
for(int i = 0; i < temp.size(); i++)
{
if (distance(p.getX(), p.getY(), temp.get(i).getX(), temp.get(i).getY()) == min_temp)
{
list_cx_elements.get(i).add(p);
}
}
}
}
}