-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaskySparkReducerJob
More file actions
387 lines (311 loc) · 16.3 KB
/
GaskySparkReducerJob
File metadata and controls
387 lines (311 loc) · 16.3 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;
import java.util.Collections;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
public class GaskySparkJob {
private static final int GRID_SIZE = 8;
public static void main(String[] args) {
// Create a Spark context
SparkConf conf = new SparkConf().setAppName("GaskySparkJob");
JavaSparkContext sparkContext = new JavaSparkContext(conf);
// Load input data
JavaRDD<String> inputData = sparkContext.textFile(args[0]);
// Debug: Print the input data
System.out.println("Debug: Input Data:");
inputData.foreach(line -> System.out.println(line));
// Process the input data and create key-value pairs
JavaPairRDD<Tuple2<String, Integer>, Iterable<Tuple2<Integer, Double>>> result = inputData
.flatMapToPair(line -> parseInputData(line))
.groupByKey();
// Collect the result and sort it
List<Tuple2<Tuple2<String, Integer>, Iterable<Tuple2<Integer, Double>>>> resultList = result.collect();
// Create a new list with the sorted elements
List<Tuple2<Tuple2<String, Integer>, Iterable<Tuple2<Integer, Double>>>> sortedResultList = new ArrayList<>(resultList);
sortedResultList.sort(Comparator.comparing(
(Tuple2<Tuple2<String, Integer>, Iterable<Tuple2<Integer, Double>>> tuple) -> tuple._1(),
Tuple2Comparator.INSTANCE
));
// Separate keys and values from the sorted result
List<Tuple2<String, Integer>> keysList = new ArrayList<>();
List<Iterable<Tuple2<Integer, Double>>> valuesList = new ArrayList<>();
for (Tuple2<Tuple2<String, Integer>, Iterable<Tuple2<Integer, Double>>> tuple : sortedResultList) {
keysList.add(tuple._1());
valuesList.add(tuple._2());
}
// Example: Print keys and values
System.out.println("Keys:");
keysList.forEach(key -> System.out.println(key));
System.out.println("Values:");
valuesList.forEach(values -> {
values.forEach(value -> System.out.print(" " + value));
System.out.println("");
});
// Call the new method to apply gaskyAlgorithm
applyGaskyAlgorithm(sparkContext,keysList, valuesList);
// Stop the Spark context
sparkContext.stop();
}
private static void applyGaskyAlgorithm(JavaSparkContext sparkContext, List<Tuple2<String, Integer>> keysList, List<Iterable<Tuple2<Integer, Double>>> valuesList) {
System.out.println("valuesList size" + valuesList.size());
// Create JavaRDDs for keys and values
JavaRDD<Tuple2<String, Integer>> keysRDD = sparkContext.parallelize(keysList);
JavaRDD<Iterable<Tuple2<Integer, Double>>> valuesRDD = sparkContext.parallelize(valuesList);
// Zip the two RDDs to create a PairRDD
JavaPairRDD<Tuple2<String, Integer>, Iterable<Tuple2<Integer, Double>>> zippedRDD = keysRDD.zip(valuesRDD);
// Filter out points with Double.MAX_VALUE distance
JavaPairRDD<Tuple2<String, Integer>, Iterable<Tuple2<Integer, Double>>> filteredRDD = zippedRDD.mapValues(values ->
filterUnfavorablePoints(values)
);
// Debug: Print the filtered points
System.out.println("Remaining Points after filtering:");
filteredRDD.collect().forEach(tuple -> {
Tuple2<String, Integer> keysTuple = tuple._1();
int colNumber = keysTuple._2();
Iterable<Tuple2<Integer, Double>> remainingPoints = tuple._2();
System.out.println("Column number: " + colNumber);
System.out.println("Remaining Points: " + remainingPoints);
});
// Apply gaskyAlgorithm to the remaining points
filteredRDD.foreach(tuple -> {
Tuple2<String, Integer> keysTuple = tuple._1();
int colNumber = keysTuple._2();
Iterable<Tuple2<Integer, Double>> remainingPoints = tuple._2();
System.out.println("Applying gaskyAlgorithm for Column number: " + colNumber);
// Assuming gaskyAlgorithm takes an Iterable<Tuple2<Integer, Double>> and a Tuple2<String, Integer>
gaskyAlgorithm(remainingPoints, colNumber);
});
}
private static Iterable<Tuple2<Integer, Double>> filterUnfavorablePoints(Iterable<Tuple2<Integer, Double>> values) {
List<Tuple2<Integer, Double>> filteredValues = new ArrayList<>();
for (Tuple2<Integer, Double> value : values) {
if (!value._2().equals(Double.MAX_VALUE)) {
filteredValues.add(value);
}
}
return filteredValues;
}
private static void gaskyAlgorithm(Iterable<Tuple2<Integer, Double>> remainingPoints, int colNumber) {
int totalRemainingPoints = Iterables.size(remainingPoints);
List<Tuple2<Double, Double>> points = new ArrayList<>();
// Convert remainingPoints to Tuple2<Double, Double> objects
for (Tuple2<Integer, Double> point : remainingPoints) {
points.add(new Tuple2<>(point._1().doubleValue(), point._2()));
}
// Debug: Print initial points
System.out.println("Initial Points for Column number: " + colNumber);
points.forEach(System.out::println);
// Filtering based on dominance
int currentWindowStart = 1;
while (points.size() >= 3 && currentWindowStart <= points.size() - 2) {
Tuple2<Double, Double> ii = points.get(currentWindowStart - 1);
Tuple2<Double, Double> jj = points.get(currentWindowStart);
Tuple2<Double, Double> kk = points.get(currentWindowStart + 1);
if (ii != null && jj != null && kk != null) {
double xij = calcBisectorProjections(ii._1(), ii._2(), jj._1(), jj._2())._1();
double xjk = calcBisectorProjections(jj._1(), jj._2(), kk._1(), kk._2())._1();
// Debug: Print xij and xjk
System.out.println("xij: " + xij + ", xjk: " + xjk);
if (xij > xjk) {
// Debug: Print the removed point
System.out.println("Removed point: " + jj);
// Remove the middle point
points.remove(currentWindowStart);
} else {
// Move to the next window
currentWindowStart++;
}
}
}
// Debug: Print final points after filtering
System.out.println("Filtered Points after Gasky Algorithm for Column number: " + colNumber);
points.forEach(System.out::println);
// Call the method to find proximal points
List<double[]> proximityProjectionsPoints = findProximityPoints(points, totalRemainingPoints, GRID_SIZE);
// Debug: Print proximal points
System.out.println("Proximal Points for Column number: " + colNumber);
proximityProjectionsPoints.forEach(point -> System.out.println(Arrays.toString(point)));
int unDominatedPointsSize = points.size();
int proximityIntervals = proximityProjectionsPoints.size() - 1;
int dominatedCoordinatesDistances = 0;
List<Double> distances = new ArrayList<>(Collections.nCopies(GRID_SIZE, Double.MAX_VALUE));
//update distance implementation
for (int interval = 0; interval < proximityIntervals; interval++) {
double[] currentInterval = proximityProjectionsPoints.get(interval);
Tuple2<Double,Double> dominantPoint = points.get(dominatedCoordinatesDistances);
int start = (int) currentInterval[0];
int end = (int) currentInterval[1];
for (int xCord = start; xCord <= end; xCord++) {
// if (xCord >= 1 && xCord <= distances.size()) {
// double currentDistance = distances.get(xCord - 1);
// double newDistance = findEuclideanDistance(xCord, 0, currentInterval[2], currentInterval[3]);
// if (currentDistance != Double.MAX_VALUE) {
// newDistance = Math.min(newDistance, currentDistance);
// }
// distances.set(xCord - 1, newDistance);
// }
// }
// dominatedCoordinatesDistances++;
if (distances.get(xCord - 1) != Double.MAX_VALUE){
distances.set(
xCord - 1,
Double.min(
findEuclideanDistance(xCord, 0, dominantPoint._1(), dominantPoint._2()),
distances.get(xCord - 1)
)
);
}else
distances.set(
xCord - 1,
findEuclideanDistance(xCord, 0, dominantPoint._1(), dominantPoint._2())
);
}
dominatedCoordinatesDistances++;
}
// Debug: Print updated distances
System.out.println("Updated Distances:");
distances.forEach(System.out::println);
}
private static List<double[]> findProximityPoints(List<Tuple2<Double, Double>> unDominatedPoints, final int totalPoints, final int gridSize) {
List<Tuple2<Double, Double>> intervals = new ArrayList<>();
System.out.println("unDominatedPoints size: " + unDominatedPoints.size());
// Calculate intervals based on the unDominatedPoints
for (int i = 1; i < unDominatedPoints.size(); i++) {
Tuple2<Double, Double> point1 = unDominatedPoints.get(i - 1);
Tuple2<Double, Double> point2 = unDominatedPoints.get(i);
intervals.add(new Tuple2<>((point1._1() + point2._1()) / 2, 0.0));
}
// Combine intervals using a frame
List<double[]> mergedInterval = new ArrayList<>(intervals.size());
mergedInterval.add(new double[]{1, intervals.get(0)._1()});
for (int i = 1; i < intervals.size(); i++) {
mergedInterval.add(new double[]{intervals.get(i - 1)._1(), intervals.get(i)._1()});
}
mergedInterval.add(new double[]{intervals.get(intervals.size() - 1)._1(), GRID_SIZE});
return mergedInterval;
}
private static double findEuclideanDistance(int x, int y, int x1, int y1) {
return Math.sqrt((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y));
}
private static double findEuclideanDistance(double x, double y, double x1, double y1){
return Math.sqrt((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y));
}
// FlatMap function to process input data and generate key-value pairs
private static Iterator<Tuple2<Tuple2<String, Integer>, Tuple2<Integer, Double>>> parseInputData(String line) {
System.out.println("Debug: Processing Line: " + line);
String[] distFavArray = line.split("\\s+");
List<Tuple2<Tuple2<String, Integer>, Tuple2<Integer, Double>>> result = new ArrayList<>();
if (distFavArray.length > 0) {
String facilityName = distFavArray[0];
int matrixRowNumber = Integer.parseInt(distFavArray[1]);
// Convert the strings to a list of strings
List<String> binMatrixValues = Arrays.asList(Arrays.copyOfRange(distFavArray, 2, distFavArray.length));
double[] leftDistance = new double[binMatrixValues.get(0).length()];
double[] rightDistance = new double[binMatrixValues.get(0).length()];
Arrays.fill(leftDistance, Double.MAX_VALUE);
Arrays.fill(rightDistance, Double.MAX_VALUE);
leftDistance = getLeftDistance(leftDistance, binMatrixValues);
rightDistance = getRightDistance(rightDistance, binMatrixValues);
for (int i = 0; i < binMatrixValues.get(0).length(); i++) {
result.add(new Tuple2<>(new Tuple2<>(facilityName, i + 1),
new Tuple2<>(matrixRowNumber, Double.min(leftDistance[i], rightDistance[i]))));
}
}
// Debug: Print the generated key-value pairs
System.out.println("Debug: Generated Key-Value Pairs:");
result.forEach(tuple -> System.out.println(tuple));
return result.iterator();
}
private static double[] getLeftDistance(double[] leftDistance, List<String> gridRows) {
boolean isFavlFound = false;
for (int i = 0; i < gridRows.get(0).length(); i++) {
if (gridRows.get(0).charAt(i) == '1') {
leftDistance[i] = 0;
isFavlFound = true;
} else if (isFavlFound) {
leftDistance[i] = leftDistance[i - 1] + 1;
}
}
return leftDistance;
}
private static double[] getRightDistance(double[] rightDistance, List<String> gridRows) {
boolean isFavrFound = false;
for (int i = gridRows.get(0).length() - 1; i >= 0; --i) {
if (gridRows.get(0).charAt(i) == '1') {
rightDistance[i] = 0;
isFavrFound = true;
} else if (isFavrFound) {
rightDistance[i] = rightDistance[i + 1] + 1;
}
}
return rightDistance;
}
// Comparator for Tuple2<String, Integer>
static class Tuple2Comparator implements Comparator<Tuple2<String, Integer>>, Serializable {
static final Tuple2Comparator INSTANCE = new Tuple2Comparator();
@Override
public int compare(Tuple2<String, Integer> tuple1, Tuple2<String, Integer> tuple2) {
int compareResult = tuple1._1().compareTo(tuple2._1());
if (compareResult == 0) {
// If the first elements are equal, compare the second elements
compareResult = Integer.compare(tuple1._2(), tuple2._2());
}
return compareResult;
}
}
private static Tuple2<Double, Double> calcBisectorProjections(double x, double y, double x1, double y1) {
double xx = ((y1 * y1) - (y * y) + (x1 * x1) - (x * x)) / (2 * (x1 - x));
double yy = 0;
return new Tuple2<>(xx, yy);
}
// private static List<double[]> findProximityPoints(JavaSparkContext sparkContext,
// List<Tuple2<Double, Double>> unDominatedPoints,
// final int totalPoints) {
// // Create a JavaRDD from the list of unDominatedPoints
// JavaRDD<Tuple2<Double, Double>> unDominatedPointsRDD = sparkContext.parallelize(unDominatedPoints);
// // Calculate intervals between consecutive points
// JavaRDD<Tuple2<Double, Double>> intervalsRDD = unDominatedPointsRDD
// .zipWithIndex()
// .filter(tuple -> tuple._2() > 0) // Exclude the first point
// .map(tuple -> {
// Tuple2<Double, Double> point1 = unDominatedPoints.get(tuple._2().intValue() - 1);
// Tuple2<Double, Double> point2 = tuple._1();
// return new Tuple2<>(
// (point1._1() + point2._1()) / 2,
// 0.0 // point lying on with intersection on X axis
// );
// });
// // Combine intervals
// JavaPairRDD<Double, Double> mergedIntervalRDD = intervalsRDD
// .mapToPair(interval -> new Tuple2<>(interval._1(), interval._1()))
// .reduceByKey((interval1, interval2) -> new Tuple2<>(interval1, interval2));
// // Collect the results back to the driver
// List<Tuple2<Double, Double>> mergedIntervals = mergedIntervalRDD.collect();
// // Convert List<Tuple2<Double, Double>> to List<double[]>
// List<double[]> mergedIntervalList = new ArrayList<>();
// for (Tuple2<Double, Double> interval : mergedIntervals) {
// mergedIntervalList.add(new double[]{interval._1(), totalPoints});
// }
// return mergedIntervalList;
// }
// Add this method to your class
private static <T> Iterable<T> iterableToJava(scala.collection.Iterable<T> scalaIterable) {
List<T> javaList = new ArrayList<>();
scala.collection.Iterator<T> scalaIterator = scalaIterable.iterator();
while (scalaIterator.hasNext()) {
javaList.add(scalaIterator.next());
}
return javaList;
}
}