This commit is contained in:
Armin Wolf 2017-06-18 18:24:30 +02:00
parent dba545b114
commit e0a99fd006
5 changed files with 84 additions and 22 deletions

View File

@ -11,12 +11,19 @@ public class Point implements Comparable<Point> {
private Double x; private Double x;
private Double y; private Double y;
private String id;
public Point(Double x, Double y) { public Point(Double x, Double y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public Point(Double x, Double y, String id) {
this.x = x;
this.y = y;
this.id = id;
}
public Double getX() { public Double getX() {
return x; return x;
} }
@ -47,4 +54,12 @@ public class Point implements Comparable<Point> {
return 1; return 1;
} }
} }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
} }

View File

@ -1,5 +1,7 @@
package Presenter.Algorithms; package Presenter.Algorithms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
/** /**
@ -7,6 +9,27 @@ import java.util.List;
*/ */
public class InversionCounter { public class InversionCounter {
public static int run(List<Integer> a, List<Integer> b){
HashMap<Integer, Integer> indexesA = new HashMap<>();
ArrayList<Integer> substituted = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
temp.addAll(a);
for (int i=0;i<a.size();i++){
indexesA.put(a.get(i), (i+1));
}
for (int j=0;j<b.size();j++){
substituted.add(indexesA.get(b.get(j)));
}
return countInversions(substituted, 0, substituted.size()-1, temp);
}
/** /**
* Angepasster Merge-Sort Algorithmus. * Angepasster Merge-Sort Algorithmus.
@ -18,14 +41,14 @@ public class InversionCounter {
* @param aux Groundtruth Ordnung um die Anzahl der Inversionen zu bestimmen. * @param aux Groundtruth Ordnung um die Anzahl der Inversionen zu bestimmen.
* @return Anzahl der inversionen zwischen a und aux. * @return Anzahl der inversionen zwischen a und aux.
*/ */
public static int run(List<Double> a, int start, int end, List<Double> aux) { public static int countInversions(List<Integer> a, int start, int end, List<Integer> aux) {
if (start >= end) { if (start >= end) {
return 0; return 0;
} }
int invCount = 0; int invCount = 0;
int mid = start + (end - start) / 2; int mid = start + (end - start) / 2;
int invCountLeft = run(a, start, mid, aux); // divide and conquer int invCountLeft = countInversions(a, start, mid, aux); // divide and conquer
int invCountRight = run(a, mid + 1, end, aux); // divide and conquer int invCountRight = countInversions(a, mid + 1, end, aux); // divide and conquer
invCount += (invCountLeft + invCountRight); invCount += (invCountLeft + invCountRight);
for (int i = start; i <= end; i++) { for (int i = start; i <= end; i++) {
aux.set(i, a.get(i)); aux.set(i, a.get(i));

View File

@ -146,16 +146,43 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
int numberOfInversions = 0; int numberOfInversions = 0;
ArrayList<Double> umin = new ArrayList<>();
ArrayList<Double> umax = new ArrayList<>();
ArrayList<Double> randomIntersection = new ArrayList<>(); ArrayList<Double> randomIntersection = new ArrayList<>();
ArrayList<Point> umin = new ArrayList<>();
ArrayList<Point> umax = new ArrayList<>();
HashMap<Point, Integer> secondaryIndex = new HashMap<>();
ArrayList<Integer> listA = new ArrayList<>();
ArrayList<Integer> listB = new ArrayList<>();
int counter = 0;
for (Line p : set) { for (Line p : set) {
umin.add((slab.getLower() * p.getM()) + p.getB()); umin.add(new Point(p.getM(), slab.getLower() * p.getM() + p.getB(),counter+""));
umax.add((slab.getUpper() * p.getM()) + p.getB()); umax.add(new Point(p.getM(), slab.getUpper() * p.getM() + p.getB(),counter+""));
counter++;
} }
numberOfInversions = InversionCounter.run(umin, 0, umin.size() - 1, umax); Collections.sort(umin);
Collections.sort(umax);
for (int i=0; i<umin.size();i++){
int id = Integer.parseInt(umin.get(i).getId());
secondaryIndex.put(umin.get(i), id);
}
for (Point p : umax){
int x = Integer.parseInt(p.getId());
listB.add(secondaryIndex.get(x));
}
for (Point q : umin){
int x = Integer.parseInt(q.getId());
listA.add(secondaryIndex.get(x));
}
numberOfInversions = InversionCounter.run(listA, listB);
return numberOfInversions; return numberOfInversions;
} }

View File

@ -142,7 +142,7 @@ public class RepeatedMedianEstimator implements Algorithm {
high.add(interval.getUpper() * line.getM() + line.getB()); high.add(interval.getUpper() * line.getM() + line.getB());
} }
int inversions = InversionCounter.run(low, 0, low.size()-1, high); //int inversions = InversionCounter.countInversions(low, 0, low.size()-1, high);
} }

View File

@ -49,26 +49,23 @@ public class LeastMedianOfSquaresEstimatorTest {
@Test @Test
public void mergeSort() throws Exception { public void mergeSort() throws Exception {
//double[] umin = {1,5,6,4,20}; double[] umin = {6,3,4,1,2,5};
//double[] umax = {1,4,6,5,20}; double[] umax = {3,5,2,6,1,4};
double[] umin = {1,2,3,4}; // double[] umin = {3,1,4,2};
double[] umax = {1,4,2,3}; // double[] umax = {1,4,2,3};
ArrayList<Double> a = new ArrayList<>(); ArrayList<Integer> a = new ArrayList<>();
ArrayList<Double> b = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>();
for (double d :umin) { for (double d :umin) {
a.add(d); a.add((int) d);
} }
for (double d :umax) { for (double d :umax) {
b.add(d); b.add((int) d);
} }
/
int ret = InversionCounter.run(a,0,a.size()-1,b); int ret = InversionCounter.run(a, b);
assertEquals(9d, ret, 0.001);
assertEquals(2, ret);
} }