WIP: zwischen Ergebnisse sehen schonmal gut aus. Algorithmus terminiert noch nicht...

This commit is contained in:
Armin Wolf 2017-06-13 19:45:10 +02:00
parent be1d7e5432
commit b37b4564a5
2 changed files with 302 additions and 89 deletions

View File

@ -19,9 +19,9 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
private LinkedList<Point> intersections = new LinkedList<>(); private LinkedList<Point> intersections = new LinkedList<>();
private int n; private int n;
private double quantileError; private double quantileError;
private double kPlus; private int kPlus;
private double kMinus; private int kMinus;
private ArrayDeque<Slab> slabs; private PriorityQueue<Slab> slabs;
private Slab subSlabU1; private Slab subSlabU1;
private Slab subSlabU2; private Slab subSlabU2;
private Line sigmaMin; private Line sigmaMin;
@ -31,51 +31,78 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
public LeastMedianOfSquaresEstimator(LinkedList<Line> set, LinkedList<Point> intersections) { public LeastMedianOfSquaresEstimator(LinkedList<Line> set, LinkedList<Point> intersections) {
this.set = set; this.set = set;
this.intersections = intersections; this.intersections = intersections;
//(1.) Let n <- |S|; q+ <- q; q- <- q+ * (1 - quantileError);....
n = set.size();
double quantile = 0.5;
double qPlus = quantile;
double qMinus = qPlus * (1 - quantileError);
kMinus = (int) Math.ceil(n * qMinus);
kPlus = (int) Math.ceil(n * qPlus);
} }
public void printResult(){ public void printResult(){
System.out.println("RESULT: "+sigmaMin.getM()+"x +"+sigmaMin.getB()); System.out.println("RESULT: X1: "+sigmaMin.getX1() + ", X2: "+sigmaMin.getX2()+"\t Y1: "+sigmaMin.getY1()+", Y2: "+sigmaMin.getY2());
} }
/** /**
* *
*/ */
public void approximateLMS() { public void approximateLMS() {
//(1.) Let n <- |S|; q+ <- q; q- <- q+ * (1 - quantileError);....
n = set.size();
double quantile = 0.5;
double qPlus = quantile;
double qMinus = qPlus * (1 - quantileError);
kMinus = Math.ceil(n * qMinus);
kPlus = Math.ceil(n * qPlus);
//(2.) Let U <- (-inf, inf) be the initial active slabs... //(2.) Let U <- (-inf, inf) be the initial active slabs...
slabs = new ArrayDeque<>(); Comparator<Slab> comparator = new Comparator<Slab>() {
@Override
public int compare(Slab o1, Slab o2) {
if (o1.getDistance() < o2.getDistance())
return -1;
if (o1.getDistance() > o2.getDistance())
return 1;
else
return 0;
}
};
slabs = new PriorityQueue<>(comparator);
slabs.add(new Slab(-100000, 100000)); slabs.add(new Slab(-100000, 100000));
heightsigmaMin = Double.MAX_VALUE; heightsigmaMin = Double.MAX_VALUE;
//(3.) Apply the following steps as long as the exists active slabs //(3.) Apply the following steps as long as the exists active slabs
while (!slabs.isEmpty()) { boolean active = true;
Slab slab = slabs.getFirst(); Slab slab;
//(a.) Select any active Slab and calc. the inversions while (!this.slabs.isEmpty()) {
int numberOfIntersections = countInversions(slab); slab = this.slabs.peek();
if (slab.getActivity()){
//(a.) Select any active Slab and calc. the inversions
int numberOfIntersections = countInversions(slab);
//(b.) apply plane sweep //(b.) apply plane sweep
int constant = 1; int constant = 1;
if (numberOfIntersections < (constant * n)) { if ((constant * n) >= numberOfIntersections) {
sigmaMin = planeSweep(slab); sigmaMin = planeSweep(slab);
} else {//(c.) otherwise.... } else {//(c.) otherwise....
//get random intersections point... //get random intersections point...
splitActiveSlab(intersectionsPoint, slab); splitActiveSlab(intersectionsPoint, slab);
//(d.) this may update sigma min
upperBound(intersectionsPoint);
//(e.) for i={1,2}, call lower bound(Ui)
lowerBound(subSlabU1);
lowerBound(subSlabU2);
if (subSlabU1.getActivity()){
this.slabs.add(subSlabU1);
}
if (subSlabU2.getActivity()){
this.slabs.add(subSlabU2);
}
}
} else {
this.slabs.remove(slab);
} }
//(d.) this may update sigma min
upperBound(intersectionsPoint);
//(e.) for i={1,2}, call lower bound(Ui)
lowerBound(subSlabU1);
lowerBound(subSlabU2);
}
// printResult();
}
} }
/** /**
@ -98,13 +125,16 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
numberOfInversions = mergeSort(umin, 0, umin.size() - 1, umax); numberOfInversions = mergeSort(umin, 0, umin.size() - 1, umax);
for (Point point : intersections) { for (Point point : intersections) {
if (point.getX() >= slab.getLower() && point.getX() < slab.getUpper()) { if (point.getX() > slab.getLower() && point.getX() < slab.getUpper()) {
randomIntersection.add(point.getX()); randomIntersection.add(point.getX());
} }
} }
Collections.shuffle(randomIntersection); Collections.shuffle(randomIntersection);
intersectionsPoint = randomIntersection.get(0); int access = (int) ( randomIntersection.size() * 0.5);
if (!randomIntersection.isEmpty()){
intersectionsPoint = randomIntersection.get(access);
}
return numberOfInversions; return numberOfInversions;
} }
@ -112,12 +142,14 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
//Parameter anpassen //Parameter anpassen
/** /**
* * Angepasster Merge-Sort Algorithmus.
* @param a * Die Funktion bekommt neben den standard Parametern zusätzlich eine Liste mit Elementen
* @param start * die als Groundtruth dienen.
* @param end * @param a Eingabefeld mit den Elementen die überprüft werden sollen.
* @param aux * @param start Startpunkt des Eingabefeldes.
* @return * @param end Endpunkt des Eingabefeldes.
* @param aux Groundtruth Ordnung um die Anzahl der Inversionen zu bestimmen.
* @return Anzahl der inversionen zwischen a und aux.
*/ */
public int mergeSort(List<Double> a, int start, int end, List<Double> aux) { public int mergeSort(List<Double> a, int start, int end, List<Double> aux) {
if (start >= end) { if (start >= end) {
@ -169,61 +201,70 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
double heightOfBracelet = heightsigmaMin; double heightOfBracelet = heightsigmaMin;
for (Point current : xQueue){ for (Point current : xQueue){
double[] currentBracelet = calcKMinusBracelet(current); Double[] currentBracelet = calcKMinusBracelet(current, kMinus);
if (currentBracelet == null){ if (currentBracelet == null){
continue; continue;
} else if (currentBracelet[0] < heightOfBracelet){ } else if (currentBracelet[0] < heightOfBracelet){
heightOfBracelet = currentBracelet[0]; heightOfBracelet = currentBracelet[0];
bracelet = new Line(current.getX(), current.getX(), currentBracelet[1], currentBracelet[2]); bracelet = new Line(current.getX(), current.getX(), currentBracelet[1], currentBracelet[2]);
System.out.println("R: "+bracelet.getM()+"x +"+bracelet.getB());
} }
} }
slab.setActivity(false);
return bracelet; return bracelet;
} }
/** /**
* @param point * Diese Methode spaltet den aktiven Slab an der x Koordinate point. Es werden zwei neue Slabs erzeugt.
* @param point x Koordinate an der, der Split geschieht.
*/ */
public void splitActiveSlab(double point, Slab active) { public void splitActiveSlab(double point, Slab active) {
subSlabU1 = new Slab(active.getLower(), point); subSlabU1 = new Slab(active.getLower(), point);
subSlabU2 = new Slab(point, active.getUpper()); subSlabU2 = new Slab(point, active.getUpper());
this.slabs.removeFirst();
this.slabs.remove(active);
} }
/** /**
*
* @param point * @param point
*/ */
public void upperBound(double point) { public void upperBound(double point) {
double height; double height = heightsigmaMin;
double tmpHeight;
ArrayList<Double> sortedLineSequence = getEjValues(point); ArrayList<Double> sortedLineSequence = getEjValues(point);
for (int i = 1; i < (n - (kMinus + 1)); i++) {
height = sortedLineSequence.get(i + (((int) kMinus) - 1)) - sortedLineSequence.get(i); int itnbr = ((n - kMinus) + 1);
for (int i = 0; i < itnbr; i++) {
tmpHeight = sortedLineSequence.get((i + kMinus) - 1) - sortedLineSequence.get(i);
if (tmpHeight < height){
height = tmpHeight;
}
if (height < heightsigmaMin) { if (height < heightsigmaMin) {
sigmaMin.setEndPoints(point, sortedLineSequence.get(i + (((int) kMinus) - 1)) heightsigmaMin = height;
,point, sortedLineSequence.get(i)); if (sigmaMin != null){
sigmaMin.setEndPoints(point, sortedLineSequence.get(i)
,point, sortedLineSequence.get((i + kMinus) - 1));
} else {
sigmaMin = new Line(point, point, sortedLineSequence.get(i), sortedLineSequence.get((i + kMinus) - 1));
}
} }
} }
} }
/** /**
* @param slab * @param pslab
* @return * @return
*/ */
public void lowerBound(Slab slab) { public void lowerBound(Slab pslab) {
int[] alpha = new int[n]; int[] alpha = new int[n];
int[] beta = new int[n]; int[] beta = new int[n];
alpha[0] = 0;
beta[0] = 0;
int strictlyGreater = 0; int strictlyGreater = 0;
//Teil I. //Teil I.
@ -231,50 +272,62 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
ArrayList<Double> uminList; ArrayList<Double> uminList;
//y koordinaten der Schnittpunkte //y koordinaten der Schnittpunkte
ArrayList<Point> lines = new ArrayList<>(); ArrayList<Line> lines = new ArrayList<>();
System.out.println("Anzahl der Slabs: "+this.slabs.size());
for (Line p : set) { for (Line p : set) {
lines.add(new Point(((slab.getLower() * p.getM()) + p.getB()), ((slab.getUpper() * p.getM()) + p.getB()))); lines.add(new Line(pslab.getLower(), pslab.getUpper(),((pslab.getLower() * p.getM()) + p.getB()), ((pslab.getUpper() * p.getM()) + p.getB())));
} }
umaxList = getEjValues(pslab.getUpper());
uminList = getEjValues(pslab.getLower());
umaxList = getEjValues(slab.getUpper()); for (int i = 0; i < n; i++) {
uminList = getEjValues(slab.getLower()); Line level = new Line(pslab.getLower(),pslab.getUpper(),uminList.get(i), umaxList.get(i));
for (Line line : lines) {
for (int i = 1; i < n; i++) { if ((line.getY1() < level.getY1()) && (line.getY2() < level.getY2())) {
Point level = new Point(uminList.get(i), umaxList.get(i));
for (Point point : lines) {
if ((point.getX() < level.getX()) && (point.getY() < level.getY())) {
alpha[i]++; alpha[i]++;
} }
if ((point.getX() > level.getX()) && (point.getY() > level.getY())) { if ((line.getY1() > level.getY1()) && (line.getY2() > level.getY2())) {
strictlyGreater++; strictlyGreater++;
} }
} }
beta[i] = n - (alpha[i] + strictlyGreater); beta[i] = n - (alpha[i] + strictlyGreater);
strictlyGreater = 0;
} }
//TEST der Alpha und Beta werte, siehe JUnit Test
//for (int i=0;i<alpha.length;i++){
// System.out.println("Alpha["+i+"]: "+alpha[i]+"\t Beta["+i+"]: "+beta[i]);
//}
//Test
//Teil II. //Teil II.
int i = 1; int i = 0;
double h = Double.MAX_VALUE; double h;
for (int j = 1; j < n; j++) { pslab.setActivity(false);
while (((i < n) && (Math.abs(beta[i] - alpha[j]) < kPlus))){ for (int j = 0; j < n; j++) {
System.out.println("i: "+i+"\t "+Math.abs(beta[i] - alpha[j])+"\t kPlus: "+kPlus); while ((i < n && (Math.abs(beta[i] - alpha[j]) < kPlus))){
i++; i++;
} }
//test
//if (i < n)
// System.out.println("i: "+i+", j:"+j+"\t "+Math.abs(beta[i] - alpha[j])+"\t kPlus: "+kPlus);
if (i >= n) { if (i >= n) {
//System.out.println("i: "+i+", j:"+j+". ungültig");
pslab.setActivity(false);
break; break;
} else { } else {
h = Math.min((uminList.get(j) - uminList.get(i)), (umaxList.get(j) - umaxList.get(i))); h = Math.min(Math.abs(uminList.get(j) - uminList.get(i)), Math.abs(umaxList.get(j) - umaxList.get(i)));
double error = 0.01;
if (((1 + error) * h) < heightsigmaMin) {
//System.out.println("h: "+ h +" ist kleiner als height(sigmaMin): "+heightsigmaMin);
pslab.setActivity(true);
return;
}
} }
i = 0;
} }
double error = 0.01;
System.out.println("h: "+h);
if (((1 + error) * h) < heightsigmaMin) {
this.slabs.addLast(slab);
}
} }
/** /**
@ -298,24 +351,26 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
} }
/** /**
* * Die Funktion berechnet anhand einer vertikalen Gerade x = px das sogenannte kleinste kMinus Bracelet.
* @param x * Mit anderen Worten es wird eine vertikale Teilgerade berechnet die mindestens kMinus Geraden schneidet
* @return * und dabei minimal ist.
* @param px Koordinate um die "vertikale Gerade" zu simulieren.
* @return Das Array enthält höhe des Bracelet, e_j und e_(j + kMinus - 1)
*/ */
public double[] calcKMinusBracelet(Point x) { public Double[] calcKMinusBracelet(Point px, int kMinusValue) {
//y Koordinaten für das kMinus brecalet //y Koordinaten für das kMinus brecalet
LinkedList<Double> intersections = new LinkedList<>(); LinkedList<Double> intersections = new LinkedList<>();
for (Line line : set) { for (Line line : set) {
intersections.add((x.getX() * line.getM())+line.getB()); intersections.add((px.getX() * line.getM())+line.getB());
} }
if (intersections.size() < kMinus){ if (intersections.size() >= kMinusValue){
return null;
} else {
Collections.sort(intersections); Collections.sort(intersections);
double height = Math.abs(intersections.getFirst() - intersections.getLast()); double height = Math.abs(intersections.get(0) - intersections.get(0 + kMinusValue - 1));
double[] ret = {height, intersections.getFirst(), intersections.getLast()}; Double[] ret = {height, intersections.get(0), intersections.get(0 + kMinusValue - 1)};
return ret; return ret;
} else {
return null;
} }
} }
@ -324,7 +379,7 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
* Hilfsklasse um die Slabs zu verteilen, private Klasse da sonst nicht verwendett wird und somit eine * Hilfsklasse um die Slabs zu verteilen, private Klasse da sonst nicht verwendett wird und somit eine
* äußere Klasse überflüssig ist... * äußere Klasse überflüssig ist...
*/ */
private static class Slab { protected static class Slab {
private double upper; private double upper;
private double lower; private double lower;
private Boolean activity; private Boolean activity;
@ -332,6 +387,7 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
public Slab(double lower, double upper) { public Slab(double lower, double upper) {
this.upper = upper; this.upper = upper;
this.lower = lower; this.lower = lower;
this.activity = true;
} }
public Boolean getActivity() { public Boolean getActivity() {
@ -358,5 +414,103 @@ public class LeastMedianOfSquaresEstimator extends Algorithm {
this.lower = lower; this.lower = lower;
} }
public Double getDistance(){
return Math.abs(this.upper - this.lower);
}
}
/**
* Im Allgemeinen werden keine Getter und Setter Methoden benötigt aber sie sind nützlich bei den JUnit Testfällen.
*/
public LinkedList<Line> getSet() {
return set;
}
public void setSet(LinkedList<Line> set) {
this.set = set;
}
public LinkedList<Point> getIntersections() {
return intersections;
}
public void setIntersections(LinkedList<Point> intersections) {
this.intersections = intersections;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public double getQuantileError() {
return quantileError;
}
public void setQuantileError(double quantileError) {
this.quantileError = quantileError;
}
public int getkPlus() {
return kPlus;
}
public void setkPlus(int kPlus) {
this.kPlus = kPlus;
}
public int getkMinus() {
return kMinus;
}
public void setkMinus(int kMinus) {
this.kMinus = kMinus;
}
public Slab getSubSlabU1() {
return subSlabU1;
}
public void setSubSlabU1(Slab subSlabU1) {
this.subSlabU1 = subSlabU1;
}
public Slab getSubSlabU2() {
return subSlabU2;
}
public void setSubSlabU2(Slab subSlabU2) {
this.subSlabU2 = subSlabU2;
}
public Line getSigmaMin() {
return sigmaMin;
}
public void setSigmaMin(Line sigmaMin) {
this.sigmaMin = sigmaMin;
}
public double getHeightsigmaMin() {
return heightsigmaMin;
}
public void setHeightsigmaMin(double heightsigmaMin) {
this.heightsigmaMin = heightsigmaMin;
}
public double getIntersectionsPoint() {
return intersectionsPoint;
}
public void setIntersectionsPoint(double intersectionsPoint) {
this.intersectionsPoint = intersectionsPoint;
} }
} }

View File

@ -8,6 +8,7 @@ import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -26,12 +27,19 @@ public class LeastMedianOfSquaresEstimatorTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
LinkedList<Line> line = new LinkedList<>(); Double[] x = {18d,24d,30d,34d,38d};
Double[] y = {18d,26d,30d,40d,70d};
LinkedList<Line> lines = new LinkedList<>();
LinkedList<Point> intersections = new LinkedList<>(); LinkedList<Point> intersections = new LinkedList<>();
for (int i=0; i<5; i++)
lines.add(new Line(x[i], y[i]));
lms = new LeastMedianOfSquaresEstimator(line, intersections);
lms = new LeastMedianOfSquaresEstimator(lines, intersections);
} }
@Test @Test
@ -62,4 +70,55 @@ public class LeastMedianOfSquaresEstimatorTest {
} }
@Test
public void geEjValues() throws Exception {
Double[] expected = {36d,50d,60d,74d,108d};
ArrayList<Double> actual = lms.getEjValues(1d);
assertArrayEquals(expected, actual.toArray());
}
@Test
public void calcKMinusBracelet() throws Exception {
Point point = new Point(1d, 1d);
Double[] expected = {24d, 36d, 60d};
Double[] actual = lms.calcKMinusBracelet(point, 3);
assertArrayEquals(expected, actual);
}
@Test
public void upperBound() throws Exception {
lms.setkMinus(3);
lms.setHeightsigmaMin(500);
lms.setSigmaMin(new Line(0,0,0,0));
Line expected = new Line(5,5,146,210);
lms.upperBound(5d);
assertEquals(expected.getX1(), lms.getSigmaMin().getX1(),0.01);
assertEquals(expected.getX2(), lms.getSigmaMin().getX2(),0.01);
assertEquals(expected.getY1(), lms.getSigmaMin().getY1(),0.01);
assertEquals(expected.getY2(), lms.getSigmaMin().getY2(),0.01);
}
@Test
public void lowerBound() throws Exception {
//kann nur über sout geprüft werden test passt aber
Double[] expectedAlpha = {0d,0d,0d,2d,4d};
Double[] expectedBeta = {2d,4d,4d,2d,1d};
lms.setHeightsigmaMin(500);
LeastMedianOfSquaresEstimator.Slab slab = new LeastMedianOfSquaresEstimator.Slab(-2,0);
lms.lowerBound(slab);
assertTrue(slab.getActivity());
}
@Test
public void planeSweep() throws Exception {
}
} }