dbscan / Exp2.java
Exp2.java
Raw
/*
 * Name - Anisaftab Saiyed
 * ID- 300259073
 * CSI PA2
 */


import java.util.List;
import java.util.ArrayList;
import java.util.*;

import java.io.*;  
import java.util.Scanner;  

/*
 * Exp2 class tests the time taken by linear search and kd tree search to find 
 * neigbors.
 */

public class Exp2 {
  
    /** 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<Point3D> Arraylist of all 3d points from the csv file
   * @throws FileNotFoundException,Exception 
   */
    public static List<Point3D> read(String filename) throws Exception {
	 
        List<Point3D> points =  new ArrayList<Point3D>();

        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;
    }
    
    
    /** This method runs the actual Experiment2. It searches the neighbors of every 
     * point after step indexes and finds the time taken during the search.
     * @param list list containing all the points from the csv file
     * @param eps epsilon distance within which we will find nearby points
     * @param type the type of search which needs to be done, like lin or kd or both
     * @param step the number of indexes to skip in list and find its neighbors
     */
    public static void runExp2( List<Point3D> list, double eps, String type, int step ){

       
        if ( type.equals("lin")){
            NearestNeighbors nn= new NearestNeighbors(list);

            long startTime = System.currentTimeMillis();
            for(int i = 0; i <= list.size() - 1; i+=step  ){
                Point3D p = list.get(i);
                nn.rangeQuery(p, eps);
            }
            long endTime = System.currentTimeMillis();
            long duration = ( endTime - startTime) ;
            System.out.println( "Linear search takes " + duration + "ms");
        }
        else if ( type.equals("kd")){
            NearestNeighborsKD nnKD = new NearestNeighborsKD(list);

            long startTime = System.currentTimeMillis();
            for(int i = 0; i <= list.size() - 1; i+=step  ){
                Point3D p = list.get(i);
                nnKD.getNeighbors(p, eps);
            }
            long endTime = System.currentTimeMillis();
            long duration = ( endTime - startTime) ;
            System.out.println( "KDTree search takes " + duration + "ms");
        }
        else{
            NearestNeighbors nn= new NearestNeighbors(list);

            long startTime = System.currentTimeMillis();
            for(int i = 0; i <= list.size() - 1; i+=step  ){
                Point3D p = list.get(i);
                nn.rangeQuery(p, eps);
            }
            long endTime = System.currentTimeMillis();
            long duration = ( endTime - startTime) ;
            

            NearestNeighborsKD nnKD = new NearestNeighborsKD(list);

            long startTime2 = System.currentTimeMillis();
            for(int i = 0; i <= list.size() - 1; i+=step  ){
                Point3D p = list.get(i);
                nnKD.getNeighbors(p, eps);
            }
            long endTime2 = System.currentTimeMillis();
            long duration2 = ( endTime2 - startTime2) ;

            System.out.println( "Linear search takes " + duration + "ms");
            System.out.println( "KDTree search takes " + duration2 + "ms");
        
        }
    }
  
    
    /** 
     * @param args write args in this order: args[0]= type of search, args[1] = epsilon distance value within which we find the neigbors, args[2]= csv file with all the points
     * args[3]= step value to skip indexes in neighbor search
     * @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<Point3D> list = Exp2.read(args[2]);
        
        //step value
        int step = Integer.parseInt(args[3]) ;
        //runs the actual experiment
        Exp2.runExp2(list, eps, type, step);
        
    

    
    }   
}