fixed!
This commit is contained in:
parent
dba545b114
commit
e0a99fd006
|
@ -11,12 +11,19 @@ public class Point implements Comparable<Point> {
|
|||
|
||||
private Double x;
|
||||
private Double y;
|
||||
private String id;
|
||||
|
||||
public Point(Double x, Double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Point(Double x, Double y, String id) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Double getX() {
|
||||
return x;
|
||||
}
|
||||
|
@ -47,4 +54,12 @@ public class Point implements Comparable<Point> {
|
|||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package Presenter.Algorithms;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -7,6 +9,27 @@ import java.util.List;
|
|||
*/
|
||||
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.
|
||||
|
@ -18,14 +41,14 @@ public class InversionCounter {
|
|||
* @param aux Groundtruth Ordnung um die Anzahl der Inversionen zu bestimmen.
|
||||
* @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) {
|
||||
return 0;
|
||||
}
|
||||
int invCount = 0;
|
||||
int mid = start + (end - start) / 2;
|
||||
int invCountLeft = run(a, start, mid, aux); // divide and conquer
|
||||
int invCountRight = run(a, mid + 1, end, aux); // divide and conquer
|
||||
int invCountLeft = countInversions(a, start, mid, aux); // divide and conquer
|
||||
int invCountRight = countInversions(a, mid + 1, end, aux); // divide and conquer
|
||||
invCount += (invCountLeft + invCountRight);
|
||||
for (int i = start; i <= end; i++) {
|
||||
aux.set(i, a.get(i));
|
||||
|
|
|
@ -146,16 +146,43 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
|
|||
|
||||
int numberOfInversions = 0;
|
||||
|
||||
ArrayList<Double> umin = new ArrayList<>();
|
||||
ArrayList<Double> umax = 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) {
|
||||
umin.add((slab.getLower() * p.getM()) + p.getB());
|
||||
umax.add((slab.getUpper() * p.getM()) + p.getB());
|
||||
umin.add(new Point(p.getM(), slab.getLower() * p.getM() + p.getB(),counter+""));
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -142,7 +142,7 @@ public class RepeatedMedianEstimator implements Algorithm {
|
|||
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);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -49,26 +49,23 @@ public class LeastMedianOfSquaresEstimatorTest {
|
|||
|
||||
@Test
|
||||
public void mergeSort() throws Exception {
|
||||
//double[] umin = {1,5,6,4,20};
|
||||
//double[] umax = {1,4,6,5,20};
|
||||
double[] umin = {1,2,3,4};
|
||||
double[] umax = {1,4,2,3};
|
||||
ArrayList<Double> a = new ArrayList<>();
|
||||
ArrayList<Double> b = new ArrayList<>();
|
||||
|
||||
double[] umin = {6,3,4,1,2,5};
|
||||
double[] umax = {3,5,2,6,1,4};
|
||||
// double[] umin = {3,1,4,2};
|
||||
// double[] umax = {1,4,2,3};
|
||||
ArrayList<Integer> a = new ArrayList<>();
|
||||
ArrayList<Integer> b = new ArrayList<>();
|
||||
|
||||
for (double d :umin) {
|
||||
a.add(d);
|
||||
a.add((int) d);
|
||||
}
|
||||
|
||||
for (double d :umax) {
|
||||
b.add(d);
|
||||
b.add((int) d);
|
||||
}
|
||||
/
|
||||
|
||||
int ret = InversionCounter.run(a,0,a.size()-1,b);
|
||||
|
||||
assertEquals(2, ret);
|
||||
int ret = InversionCounter.run(a, b);
|
||||
assertEquals(9d, ret, 0.001);
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue