Skip to content

Commit

Permalink
Fiexed file exportation features
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoBackman committed Oct 2, 2021
1 parent acb7997 commit 74c817f
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 38 deletions.
1 change: 1 addition & 0 deletions data/dataProfile.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10,20,[5, 5],[5, 6],1,2.439443E9,1.155171E9,[5, 6],1,7.537026E9,6.1135E7,[5, 6],1,7.537026E9,3.9387E7,
1 change: 1 addition & 0 deletions data/dataProfile.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10/20/[5, 5]/[5, 6]/1/8.0038E8/3.76755E8/[5, 6]/1/2.989229E9/3.6919E7/[5, 6]/1/2.989229E9/2.7689E7/
2 changes: 1 addition & 1 deletion data/dataProfile.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
data!
10,20,[5, 5],[5, 6],1,2.373717E9,1.207405E9,[5, 6],1,4.960301E9,5.4018E7,[5, 6],1,4.960301E9,4.4572E7,
1 change: 1 addition & 0 deletions data/dataProfile.xlsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10/20/[30, 30]/[31, 29]/1/9.05712E8/5.12317E8/[31, 29]/1/3.294872E9/5.5752E7/[31, 29]/1/3.294872E9/1.29866E8/
65 changes: 32 additions & 33 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import src.utils.CoordinateReader;
import src.utils.ExcelExport;
import src.utils.ProcessTimeRecorder;
import src.utils.ProfileContainer;

/*
* @author: Marco Backman
Expand All @@ -19,7 +20,7 @@
class Main {
//Range over 4 produces integer representation overflow
final String INPUT_FILE_NAME = "./data/SMALL_DATA_CASE5_40";
final String EXCEL_FILE_NAME = "./data/dataProfile.txt";
final String EXCEL_FILE_NAME = "./data/dataProfile.xlsx";
File EXCEL_FILE = new File(EXCEL_FILE_NAME);
ExcelExport excelExport = new ExcelExport();

Expand All @@ -36,73 +37,71 @@ public static void main (String[] args) {
}
sc.close();
//
mainInst.executeAlgorithm(inputList);
ProfileContainer singleCaseResult = mainInst.executeAlgorithm(inputList);
//export data to Excel file
mainInst.exportDataToExcel(singleCaseResult);
}

public void executeAlgorithm(ArrayList<Integer> targetPoints) {
public ProfileContainer executeAlgorithm(ArrayList<Integer> targetPoints) {
//read input files
CoordinateReader coordinateReader
= new CoordinateReader();
coordinateReader.readPointsFromFile(INPUT_FILE_NAME);
ArrayList<ArrayList<Integer>> matrix
= coordinateReader.getEntireMatrix();

ProfileContainer singleProfile = new ProfileContainer(1, 10, 20, targetPoints);

//construct sequential
SequentialAlgorithm sequentialAlgorithm = new SequentialAlgorithm();
sequentialAlgorithm.buildList(matrix);
//import construction time
singleProfile.setSeqBuildTime(ProcessTimeRecorder.getInSeconds(1));

//sequential search - match points
/*
IntegerNode node = sequentialAlgorithm.findMatch(targetPoints);
if (node == null) {
System.out.println("No match found");
} else {
System.out.println("Match found");
}
sequentialAlgorithm.print(targetPoints);
*/

//nearest distance
//search nearest distance
DataCarrier result = sequentialAlgorithm.findCloestDistance(targetPoints);
String seqResultInString = result.toString();
singleProfile.setSeqSearchTime(ProcessTimeRecorder.getInSeconds(2));
singleProfile.setSeqResultPoints(result.getPoints());
singleProfile.setSeqDistance((long)result.getDistance());
System.out.println("Sequential search: " + seqResultInString.toString());

//construct kd tree
KDTreeIntegers kdTreeInt = new KDTreeIntegers(matrix);
long constructionTimeStart = System.nanoTime();
long kdConstructionTimeStart = System.nanoTime();
kdTreeInt.buildKDTree();
ProcessTimeRecorder.KDTreeConstructionTime += System.nanoTime() - constructionTimeStart;
ProcessTimeRecorder.KDTreeConstructionTime += System.nanoTime() - kdConstructionTimeStart;
singleProfile.setKdBuildTime(ProcessTimeRecorder.getInSeconds(3));

//find point in kd tree
long searchTimeStart = System.nanoTime();
long kdSearchTimeStart = System.nanoTime();
DataCarrier kdResult = kdTreeInt.findKDTreeNormal(targetPoints);
ProcessTimeRecorder.KDTreeSearchTime += System.nanoTime() - searchTimeStart;
ProcessTimeRecorder.KDTreeSearchTime += System.nanoTime() - kdSearchTimeStart;
System.out.println("KDTree search: " + kdResult.toString());
singleProfile.setKdSearchTime(ProcessTimeRecorder.getInSeconds(4));
singleProfile.setKdResultPoints(kdResult.getPoints());
singleProfile.setKdDistance((long)kdResult.getDistance());

//KNN algorithm
long knnSearchTimeStart = System.nanoTime();
kdTreeInt.findKNNAlgorithm(targetPoints,
kdResult.getTreeNode());

ProcessTimeRecorder.KDTreeKNNSearchTime += System.nanoTime() - knnSearchTimeStart;
singleProfile.setKnnSearchTime(ProcessTimeRecorder.getInSeconds(5));
singleProfile.setKnnResultPoints(kdResult.getPoints());
singleProfile.setKnnDistance((long)kdResult.getDistance());
System.out.println("KDTree with KNN search: " + kdTreeInt.KNN_result.toString());

//kdTreeInt.printTree();
//export data to Excel file
return singleProfile;
}

private void exportDataToExcel(ProfileContainer data) {
try { //reset first
excelExport.resetContnet(EXCEL_FILE);
String content = excelExport.generateSingleLine(data);
excelExport.appendContent(content, EXCEL_FILE);
} catch (Exception e) {
e.printStackTrace();
}

//append data
exportDataToExcel("data!");
}

private void exportDataToExcel(String data) {
try {
excelExport.appendContent(data, EXCEL_FILE);
} catch(Exception e) {
System.out.println("Data export error");
}
}
}
2 changes: 0 additions & 2 deletions src/datastructure/SequentialAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public boolean compareTwoPoints(ArrayList<Integer> a, ArrayList<Integer> b) {
}

public IntegerNode findMatch(ArrayList<Integer> targetPoints) {
long searchTimeStart = System.nanoTime();
IntegerNode resultNode = null;
IntegerNode walk = start;
//dimension mismatch!
Expand All @@ -83,7 +82,6 @@ public IntegerNode findMatch(ArrayList<Integer> targetPoints) {
}
walk = walk.getNext();
}
ProcessTimeRecorder.seqExactSearchTime += System.nanoTime() - searchTimeStart;
return resultNode;
}

Expand Down
84 changes: 84 additions & 0 deletions src/utils/ExcelExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*
* @author: Marco Backman
* @email : roni2006@hanmail.net
* @Todo: consider implementing Apache POI API for better functionalities
*/


public class ExcelExport {

final char COMMA = 47;

public void appendContent(String contents, File file) throws IOException {
FileWriter fw = new FileWriter(file, true); //true - append
BufferedWriter bw = new BufferedWriter(fw);
Expand Down Expand Up @@ -77,4 +81,84 @@ private void createFile (File file) {
System.out.println("Error on file creation" + e);
}
}

/*
* 1: number range specs,
* 2: matrix length
* 3: user's desired point
* 4: sequential algorithm
* 4-1: point found
* 4-2: shortest distance found
* 4-3: construction time
* 4-4: search time
* 5: kd-tree algorithm
* 5-1: point found
* 5-2: shortest distance found
* 5-3: construction time
* 5-4: search time
* 6: knn algorithm
* 6-1: point found
* 6-2: shortest distance found
* 6-3: construction time - same as 5-3
* 6-4: search time
*/
public String generateSingleLine(ProfileContainer dataSet) {
StringBuilder sb = new StringBuilder();

//common data
sb.append(dataSet.getNumberRange());
sb.append(COMMA);
sb.append(dataSet.getMatrixLength());
sb.append(COMMA);
sb.append(dataSet.getTargetPoints().toString().trim());
sb.append(COMMA);

//sequential algorithm
sb.append(dataSet.getSeqResultPoints().toString().trim());
sb.append(COMMA);
sb.append(dataSet.getSeqDistance());
sb.append(COMMA);
sb.append(dataSet.getSeqBuildTime());
sb.append(COMMA);
sb.append(dataSet.getSeqSearchTime());
sb.append(COMMA);
//kd-tree algorithm
sb.append(dataSet.getKdResultPoints()).toString().trim();
sb.append(COMMA);
sb.append(dataSet.getKdDistance());
sb.append(COMMA);
sb.append(dataSet.getKdBuildTime());
sb.append(COMMA);
sb.append(dataSet.getKdSearchTime());
sb.append(COMMA);
//knn algorithm
sb.append(dataSet.getKnnResultPoints().toString().trim());
sb.append(COMMA);
sb.append(dataSet.getKnnDistance());
sb.append(COMMA);
sb.append(dataSet.getKdBuildTime());
sb.append(COMMA);
sb.append(dataSet.getKnnSearchTime());
sb.append(COMMA);
return sb.toString();
}

/*
* Lists of datasets
*/
public String returnEntireExcelContent(ArrayList<ProfileContainer> dataSetLists) {
if (dataSetLists == null) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append(dataSetLists.get(0).caseNumber);
sb.append("\n");
for (ProfileContainer dataSet : dataSetLists) {
sb.append(generateSingleLine(dataSet));
sb.append("\n");
}
return sb.toString();
}

}
7 changes: 5 additions & 2 deletions src/utils/ProcessTimeRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
public class ProcessTimeRecorder {
//in milliseconds
public static long sequentialConstructionTime = 0;
public static long seqExactSearchTime = 0;
public static long sequentialSearchTime = 0;
public static long KDTreeConstructionTime = 0;
public static long KDTreeSearchTime = 0;
public static long KDTreeKNNSearchTime = 0;

public static long sequentialSize = 0;
public static long KDTreeSize = 0;
Expand All @@ -15,7 +15,7 @@ public class ProcessTimeRecorder {

/*
* 1: sequential construction, 2: sequential search,
* 3: kdtree construction, 4: kdtree search
* 3: kdtree construction, 4: kdtree search 5: knn search(kdtree excluded)
*/
public static double getInSeconds(int index) {
switch(index) {
Expand All @@ -27,6 +27,8 @@ public static double getInSeconds(int index) {

case 4: return KDTreeSearchTime * Math.pow(10, 3);

case 5: return KDTreeKNNSearchTime * Math.pow(10, 3);

default: return 0;
}
}
Expand All @@ -46,6 +48,7 @@ public static void reset() {
sequentialSearchTime = 0;
KDTreeConstructionTime = 0;
KDTreeSearchTime = 0;
KDTreeKNNSearchTime = 0;
sequentialSize = 0;
KDTreeSize = 0;
}
Expand Down
Loading

0 comments on commit 74c817f

Please sign in to comment.