/* * Complete Experiment 1 * * CSI2510 Algorithmes et Structures de Donnees * www.uottawa.ca * * Robert Laganiere, 2022 * */ /* * Name - Anisaftab Saiyed * ID- 300259073 * CSI PA2 */ import java.util.List; import java.util.ArrayList; import java.io.*; import java.util.Scanner; /* * Exp1 tests the KDtree with different points to see if it gives * similar results to linear arraylists or not. */ public class Exp1 { /** reads a csv file of 3D points and turns into a list of 3dpoints * @param filename the csv file which needs to be read. * @return List Arraylist of all 3d points from the csv file * @throws FileNotFoundException,Exception */ public static List read(String filename) throws Exception { List points = new ArrayList(); try { File myObj = new File(filename); //java forces a try catch since scanner throws file not found errors Scanner reader = new Scanner(myObj); //first line should have column names reader.nextLine(); //scan through file while (reader.hasNextLine()) { //get next line and split into x,y,z String[] tokens = reader.nextLine().split(","); //make into point Point3D p = new Point3D( Double.parseDouble(tokens[0]), Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]) ); //add point to list points.add(p); } reader.close(); } catch(FileNotFoundException f){ f.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return points; } /** save method saves the neigbors of the given point in a txt file. * @param list list of neighbors of the given point * @param filename output file name * @throws FileNotFoundException * @throws UnsupportedEncodingException */ public static void save(List list, String filename) throws FileNotFoundException, UnsupportedEncodingException{ //initialize writer and set header PrintWriter writer = new PrintWriter(filename, "UTF-8"); //write each point with its corresponding color for(Point3D p: list){ String point = p.toString(); writer.println(point); } writer.close(); } /** This method checks to see if both the linear search and kd tree search are giving the same * results. * @param listNeighbors list of neighbors from linear search * @param kdNeighbors list of neighbors from kd search * @return boolean value saying if both the lists are same or not */ public static boolean checkNeighborsPresent(List listNeighbors, List kdNeighbors){ for(Point3D p : listNeighbors){ if( ! kdNeighbors.contains(p)){ return false; } } return true; } /** * @param args args[0] = type of search, args[1] = eps value, args[2] = csv file name, args[3,4,5] = x,y &z coordinates of a point whose neighbors we are finding. * @throws Exception */ public static void main(String[] args) throws Exception { String type = args[0]; // not reading args[0] double eps= Double.parseDouble(args[1]); // reads the csv file List points= Exp1.read(args[2]); Point3D query= new Point3D(Double.parseDouble(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5])); // creates the NearestNeighbor instance NearestNeighbors nn= new NearestNeighbors(points); List neighbors= nn.rangeQuery(query,eps); NearestNeighborsKD nnKD = new NearestNeighborsKD(points); List neighbors2 = nnKD.getNeighbors(query, eps) ; if ( type.equals("lin")){ save(neighbors, "pt_lin.txt"); System.out.println("Number of neighbors from linear search= "+neighbors.size()); System.out.println(neighbors); } else if ( type.equals("kd")){ save(neighbors2, "pt_kd.txt"); System.out.println("Number of neighbors from kdtree search= "+neighbors2.size()); System.out.println(neighbors2); } else{ save(neighbors, "pt_lin.txt"); save(neighbors2, "pt_kd.txt"); System.out.println("Number of neighbors from linear search= "+neighbors.size()); System.out.println(neighbors); System.out.println("Number of neighbors from kdtree search= "+neighbors2.size()); System.out.println(neighbors2); System.out.println("Both linear and kd search are giving same neighbors or not: " + Exp1.checkNeighborsPresent(neighbors, neighbors2)); } } }