dodawanie obiektu do odpowiedniej listy (algorytm k - średnich)

0

Witajcie. Mam do zaimplementowania algorytm k-średnich. Chodzi o to , że dla każdego punktu z pliku (reprezentowanego za pomocą klasy Point) muszę obliczyć najbliższą odległość dla danego centroidu. Dla uproszczenia załóżmy , że mam dwa centrody:

        int min = -10;
		int max = 10;
		Centroid c1 = new Centroid("C1", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c2 = new Centroid("C2", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
      

Dodaję je do listy centroids. Obiekt typu Centroid posiada nazwę , oraz dwa pola typu double losujące wartości z przedziału od -10 do +10. Stworzyłem prostą metodę , która liczy taką odległość. Niestety, tu ugrzęzłem. Nie wiem w jaki sposób mogę dany punkt dodać do odpowiedniej listy :

        List<Point> c1_elements = new ArrayList<Point>();
		List<Point> c2_elements = new ArrayList<Point>();

            for(Point p: list)
			{
				
				Calculate.distance(c1.getX(), c1.getY(), p.getX(), p.getY());
				Calculate.distance(c2.getX(), c2.getY(), p.getX(), p.getY());
			} 

Mówiąc krótko, jeśli odległość punktu do c1 jest bliższa to dodaję taki element do listy c1_elements, jeśli odległość punktu od c2 jest mniejsza to wtedy do c2. PS. Dla dwóch list sprawa jest prosta, bo wystarczy zwykły if, ale już na przykład dla 7 centroidów sprawa ma się gorzej. W powyższym opisie podałem tylko dwa by nakreślić sedno problemu. Z góry dzięki za odpowiedź...

0

Wrzuć cały kod, ale jeśli dobrze zrozumiałem to dla każdego elementu z list sprawdzasz czy dany punkt leży bliżej c1 czy c2. Co w takim razie robi Calculate.distance?
Jeśli przyjmiemy, że Calculate.distance zwróci odległość od punktu do punktu to wystarczy je porównać.

for(Point p: list){
    if(Calculate.distance(c1.getX(), c1.getY(), p.getX(), p.getY()) < Calculate.distance(c2.getX(), c2.getY(), p.getX(), p.getY()))
        c1_elements.add(p);
    else
        c2_elements.add(p);
}

Jeszcze pozostaje przypadek, w którym są równe, ale tego nie sprecyzowałeś.

0

Tak, chodzi o to by sprawdzić czy każdy element ma bliżej do c1 czy c2. Distance rzeczywiście liczy odległość... Hmm, ale to o czym napisałeś dotyczy sytuacji sprzed edycji mojego posta - a co jeśli tych centroidów miałbym na przykład 7,8 lub 9?

 Centroid c1 = new Centroid("C1", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c2 = new Centroid("C2", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c3 = new Centroid("C3", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c4 = new Centroid("C4", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c5 = new Centroid("C5", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c6 = new Centroid("C6", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c7 = new Centroid("C7", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c8 = new Centroid("C8", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
		Centroid c9 = new Centroid("C9", new Random().nextInt(max - min + 1) + min, new Random().nextInt(max - min + 1) + min);
                 
List<Centroid> centroids = new ArrayList<Centroid>();
		List<Point> c1_elements = new ArrayList<Point>();
		List<Point> c2_elements = new ArrayList<Point>();
		List<Point> c3_elements = new ArrayList<Point>();
		List<Point> c4_elements = new ArrayList<Point>();
		List<Point> c5_elements = new ArrayList<Point>();
		List<Point> c6_elements = new ArrayList<Point>();
		List<Point> c7_elements = new ArrayList<Point>();
		List<Point> c8_elements = new ArrayList<Point>();
		List<Point> c9_elements = new ArrayList<Point>();
0

Przykład iteracji po punktach
http://www.dataonfocus.com/k-means-clustering-java-code/
plik KMeans.java linia 126

Dla każdego punktu i dla każdej centroidy obliczasz ich odległość i przypisujesz punkt do centroidy gdzie ich odległość jest najmniejsza

0

Chyba zostają jedynie ify.. W jednej pętli byś musiał obliczyć minimum odległość, a w drugiej porównać wynik i dodać do odpowiedniej listy

edit:
Oczywiście możesz trzymać je w liście, tak jak ktoś u góry wkleił, ale zwróć uwagę na indeksy

0

Hmm, ale to są dziesiątki jak nie setki ifów dla własnie 9 centroidów... Jakiś sposób musi być. reptille333 - to rozwiązanie mnie nie urządza. Klasy reprezentuję w inny sposób niż w tej , do której podałeś linka. Potrzebuję tutaj niestety konkretnego rozwiązania dla tego problemu , pomocy :( :P

0

Ten

new Random()

to raczej powinien być wywołany mniej razy...
Niby poprawnie, ale jakoś się kłóci z logiką.

0

Źle zrozumiałeś. W jednej pętli oblicz min z odległości do centroid. W drugiej sprawdź czy odległość == min, jeśli tak to dodaj do cx_elements, które możesz przechować w liście.
Pokaż więcej kodu.

0
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 List<Centroid> number_of_centroids(int x, List<? extends Point> list)
	{
		List<Centroid> centroids = new ArrayList<Centroid>();
		List<Point> c1_elements = new ArrayList<Point>();
		List<Point> c2_elements = new ArrayList<Point>();
		List<Point> c3_elements = new ArrayList<Point>();
		List<Point> c4_elements = new ArrayList<Point>();
		List<Point> c5_elements = new ArrayList<Point>();
		List<Point> c6_elements = new ArrayList<Point>();
		List<Point> c7_elements = new ArrayList<Point>();
		List<Point> c8_elements = new ArrayList<Point>();
		List<Point> c9_elements = new ArrayList<Point>();
		
		int min = -10;
		int max = 10;
		
		Centroid c1 = new Centroid("C1", Calculate.rand(), Calculate.rand());
		Centroid c2 = new Centroid("C2", Calculate.rand(), Calculate.rand());
		Centroid c3 = new Centroid("C3", Calculate.rand(), Calculate.rand());
		Centroid c4 = new Centroid("C4", Calculate.rand(), Calculate.rand());
		Centroid c5 = new Centroid("C5", Calculate.rand(), Calculate.rand());
		Centroid c6 = new Centroid("C6", Calculate.rand(), Calculate.rand());
		Centroid c7 = new Centroid("C7", Calculate.rand(), Calculate.rand());
		Centroid c8 = new Centroid("C8", Calculate.rand(), Calculate.rand());
		Centroid c9 = new Centroid("C9", Calculate.rand(), Calculate.rand());
		
		
		if (x == 2)
		{	
			centroids.add(c1);
			centroids.add(c2);
			for(Point p: list)
			{
				
		 if(Calculate.distance(c1.getX(), c1.getY(), p.getX(), p.getY()) < Calculate.distance(c2.getX(), c2.getY(), p.getX(), p.getY()))
				        c1_elements.add(p);
				    else
				        c2_elements.add(p);
				
			}
			
		}
		
		if (x==3)
		{
			centroids.add(c1);
			centroids.add(c2);
			centroids.add(c3);
		}
		
		
		if (x==4)
		{
			centroids.add(c1);
			centroids.add(c2);
			centroids.add(c3);
			centroids.add(c4);
		}
		
		
		if (x==5)
		{
			centroids.add(c1);
			centroids.add(c2);
			centroids.add(c3);
			centroids.add(c4);
			centroids.add(c5);
		}
		
		
		if (x==6)
		{
			centroids.add(c1);
			centroids.add(c2);
			centroids.add(c3);
			centroids.add(c4);
			centroids.add(c5);
			centroids.add(c6);
		}
		
		
		if (x==7)
		{
			centroids.add(c1);
			centroids.add(c2);
			centroids.add(c3);
			centroids.add(c4);
			centroids.add(c5);
			centroids.add(c6);
			centroids.add(c7);
		}
		

		
		if (x==8)
		{
			centroids.add(c1);
			centroids.add(c2);
			centroids.add(c3);
			centroids.add(c4);
			centroids.add(c5);
			centroids.add(c6);
			centroids.add(c7);
			centroids.add(c8);
		}
		
		
		if (x==9)
		{
			centroids.add(c1);
			centroids.add(c2);
			centroids.add(c3);
			centroids.add(c4);
			centroids.add(c5);
			centroids.add(c6);
			centroids.add(c7);
			centroids.add(c8);
			centroids.add(c9);
		}
		
		
		return centroids;
		
	}
	
0

Dokonałem jak najmniejszych przeróbek, tak aby "działało"

http://rextester.com/ELP82391

//Compiler version 1.8.0_72

import java.util.*;
import java.lang.*;

class Point {
    private double x, y;
    public Point(double x, double y) {
      this.x = x;
      this.y = y;
    }
    double getX() {
        return this.x;
    }
    double getY() {
        return this.x;
    }
    public String toString() {
      return String.format("Point[%f %f]", this.x, this.y);
    }
};

class Centroid {
    private String name;
    private double x, y;
    
    public Centroid(String name, double x, double y) {
      this.name = name;
      this.x = x;
      this.y = y;
    }
    double getX() {
        return this.x;
    }
    double getY() {
        return this.x;
    }
    public String toString() {
      return String.format("Centroid[%s %f %f]", this.name, this.x, this.y);
    }
};

class Calculate {

    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 List<Centroid> number_of_centroids(int x, List<? extends Point> list)
    {
        List<Centroid> centroids = new ArrayList<>();
        HashMap<Centroid, List<Point>> centroid_elements = new HashMap<Centroid, List<Point>>();
 
        int min = -10;
        int max = 10;
        
        Centroid c;
        for(int i = 1; i <= x; i++) {
          c = new Centroid(String.format("C%d", i), Calculate.rand(), Calculate.rand());
          centroids.add(c);
          centroid_elements.put(c, new ArrayList<>());
        }
        
        for(Point p: list)
        {
          double m = Double.MAX_VALUE;
          int cluster_number = -1;
          for(int j = 0; j < centroids.size(); j++) {
            double dist = Calculate.distance(centroids.get(j).getX(), centroids.get(j).getY(), p.getX(), p.getY());
            if(dist <= m) {
              m = dist;
              cluster_number = j;
            }
          }
          centroid_elements.get(centroids.get(cluster_number)).add(p);          
          System.out.println(String.format("%s assigned to %s", p, centroids.get(cluster_number)));
        }
        System.out.println(centroid_elements);
        
        return centroids;
    }
}

class Rextester
{  
    public static void main(String args[])
    {        
        List<Point> ps = new ArrayList<>();
        for(int i = 0; i < 10; i++) {
          ps.add(new Point(Calculate.rand(), Calculate.rand()));
        }        
        
        List<Centroid> cs = Calculate.number_of_centroids(5, ps);
        System.out.println(cs);
    }
}
0

Nie wiem ile może być punktów i czy wydajność kodu jest istotna. Warto zauważyć, że instrukcja

distance = Math.sqrt(s1+s2);

jest zbędna.

0

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:

  1. Przypisz punkty do najbliższych centroidów, których wartości są losowe - (zrobione - pierwsza iteracja)
  2. 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)
  3. Dokonaj przesunięcia centroidów i przypisz do nich nowe punkty.
  4. 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);
                    
                }
 
            }
 
        }
 

}
}

0

Musisz poćwiczyć wykorzystywanie kolekcji i pól klas. Poniżej przykład, w którym najbliższe punkty są przechowywane w klasie Centroid.

 
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Centroids 
{

	private static Map<String, Centroid> centroids; // name as unique key
	private static List<Point> pointsList;
	
	public static void main(String[] args)
	{
		// setup centroids
		centroids = setupNewCentroids(10);
		
		// setup points
		pointsList = setupNewPointsList(10);
		
		findClosestPoints();
		traceCentroids();
		
		// next iteration
		calculateMeanValuesAndMoveCentroid();
		resetCentroidClosestPoints();
		pointsList = setupNewPointsList(10);
		findClosestPoints();
		
	}
	
	private static void calculateMeanValuesAndMoveCentroid() 
	{
		// TODO Auto-generated method stub
	}

	private static void traceCentroids() 
	{
		for (Centroid c : centroids.values()) System.out.println(c);
	}

	private static void findClosestPoints() 
	{
		// iterate through all points and centroids > compare and find closest
		for (Point checkPoint : pointsList)
		{
			double closestDistance = -1;
			Centroid closestCentroid = null;
			
			for (Centroid c : centroids.values())
			{
				if (c.distance(checkPoint) < closestDistance || closestCentroid == null)  
				{
					closestDistance = c.distance(checkPoint);
					closestCentroid = c;
				}
			}
			closestCentroid.closestPoints.add(checkPoint);
		}
	}

	private static List<Point> setupNewPointsList(int pointsNum) 
	{
		List<Point> pointsList = new ArrayList<Point>();
		for (int i = 0; i < pointsNum; i++) 
		{
			pointsList.add(new Point(rand(), rand()));
		}
		return pointsList;
	}

	private static Map<String, Centroid> setupNewCentroids(int centroidsNum)
	{
		Map<String, Centroid> cMap = new HashMap<String, Centroid>();
		for (int i = 0; i < centroidsNum; i++) 
		{
			Centroid c = new Centroid();
			c.x = rand();
			c.y = rand();
			c.name = "C" + i;
			c.closestPoints = new ArrayList<Point>();
			cMap.put(c.name, c);
		}
		return cMap;
	}
	
	private static void resetCentroidClosestPoints()
	{
		for (Centroid c : centroids.values())
		{
			c.closestPoints = new ArrayList<Point>();
		}
	}
	
	
	private static int rand ()
    {
        int min = -10;
        int max = 10;
        return  new Random().nextInt(max - min + 1) + min;
    }
	
}


class Centroid extends Point 
{
	private static final long serialVersionUID = 1L;
	public String name;
    public List<Point> closestPoints;
    
    public String toString()
    {
    	String log = "--- " + this.name + " [" + this.x + "," + this.y + "]";
    	if (closestPoints != null && closestPoints.size() > 0)
    	{
    		for (Point p : closestPoints) log += "\n\t" + p + " distance: " + this.distance(p);
    	}
    	return log;
    }
};

1 użytkowników online, w tym zalogowanych: 0, gości: 1