-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupQueue.java
More file actions
94 lines (72 loc) · 2.39 KB
/
GroupQueue.java
File metadata and controls
94 lines (72 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
public class GroupQueue {
public static final int minimalAcceptableDistance = 8;
//create a method that given array list I sort it and then iterate through x and y and add elements
//to a hash map (same key) if they are connected
//compare to all other elements in Hashmap (if connected to one)
public static ArrayList<DataPoint> identifyLongestQueue(ArrayList<DataPoint> array, int minimalAcceptableDistance) {
ArrayList<Integer> categories = new ArrayList<Integer>();
//Sort ArrayList
Collections.sort(array);
//first element label 1
int category_label = 1;
categories.add(category_label);
for(int i = 1; i < array.size(); i++) {
double minimalDistance = Double.MAX_VALUE;
boolean minimumFound = false;
int index = -1;
for(int j = i-1; j >= 0; j--) {
double distance_between_points = calculatedistance(array.get(i), array.get(j));
if(distance_between_points < minimalAcceptableDistance && distance_between_points < minimalDistance ) {
minimalDistance = distance_between_points;
index = j;
minimumFound = true;
}
}
if(minimumFound) {
categories.add(i, categories.get(index));
} else {
categories.add(i, category_label);
category_label++;
}
}
Map<Integer, Long> counted_categories = categories.stream().collect(Collectors.groupingBy(e->e, Collectors.counting()));
long max = 0;
int label = 0;
for(Map.Entry<Integer, Long> entry: counted_categories.entrySet()) {
if(entry.getValue() > max) {
max = entry.getValue();
label = entry.getKey();
}
}
ArrayList<DataPoint> returnArray = new ArrayList<DataPoint>();
for(int i = 0; i < categories.size(); i++) {
if(categories.get(i) == label) {
returnArray.add(array.get(i));
}
}
return returnArray;
}
public static double calculatedistance(DataPoint a, DataPoint b) {
return Math.sqrt(Math.pow((a.x[0] - b.x[0]), 2) + Math.pow((a.x[1] - b.x[1]), 2));
}
/**
* Returns training matrix to be used to classify
* @param a
* @return matrix
*/
public int[][] trainingmatrix(ArrayList<DataPoint> a) {
int[][] result = new int[100][100];
for(DataPoint data: a) {
if(data.label == 1) {
result[(int) data.x[0]][(int) data.x[1]] = 2;
} else {
result[(int) data.x[0]][(int) data.x[1]] = 1;
}
}
return result;
}
}