178 lines
4.8 KiB
Java
178 lines
4.8 KiB
Java
package Presenter.Algorithms;
|
|
|
|
import Model.Line;
|
|
import Model.Point;
|
|
import Model.Slab;
|
|
import Presenter.*;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedList;
|
|
import java.util.Observable;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
/**
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
*
|
|
* @Author: Armin Wolf
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
* @Date: 28.05.2017.
|
|
*/
|
|
public class TheilSenEstimator extends Observable implements Algorithm {
|
|
|
|
private Presenter presenter;
|
|
private ArrayList<Line> set;
|
|
private ArrayList<Point> intersectionSet;
|
|
private ArrayList<Double> yCoordinates;
|
|
private ArrayList<Double> xCoordinates;
|
|
private Integer numberOfLinesOnLeft;
|
|
private Slab interval;
|
|
|
|
private Double j;
|
|
private Integer jA;
|
|
private Integer jB;
|
|
private Double r;
|
|
private Integer n;
|
|
private Double N;
|
|
private Integer k;
|
|
|
|
private Double a;
|
|
private Double b;
|
|
private Double aVariant;
|
|
private Double bVariant;
|
|
private ArrayList<Double> sampledIntersections;
|
|
|
|
private Double ymin = Double.MAX_VALUE;
|
|
private Double ymax = Double.MIN_VALUE;
|
|
|
|
public TheilSenEstimator(LinkedList<Line> set, LinkedList<Point> intersectionSet, Presenter presenter) {
|
|
this.presenter = presenter;
|
|
this.intersectionSet = new ArrayList<>(intersectionSet);
|
|
this.set = new ArrayList<>(set);
|
|
this.n = set.size();
|
|
this.sampledIntersections = new ArrayList<>();
|
|
Double bin = BinomialCoeffizient.run(n, 2);
|
|
this.numberOfLinesOnLeft = 0;
|
|
this.yCoordinates = new ArrayList<>();
|
|
this.xCoordinates = new ArrayList<>();
|
|
|
|
for (Point l : intersectionSet){
|
|
yCoordinates.add(l.getX());
|
|
xCoordinates.add(l.getY());
|
|
}
|
|
|
|
this.k = Integer.valueOf((int) (bin / 2));
|
|
this.N = BinomialCoeffizient.run(n, 2);
|
|
}
|
|
|
|
public void run(){
|
|
|
|
a = -10000d;
|
|
b = 10000d;
|
|
|
|
interval = new Slab(a,b);
|
|
while (true){
|
|
|
|
if (this.N <= n){
|
|
break;
|
|
} else {
|
|
r = Double.valueOf(n);
|
|
IntersectionCounter counter = new IntersectionCounter();
|
|
int numberOfIntersections = counter.run(set, new Slab(-10000,a));
|
|
k = (int) (BinomialCoeffizient.run(n, 2)/2);
|
|
j = (r /N) * (k - numberOfIntersections);
|
|
jA = (int) Math.max(1,Math.floor(j - (3 * Math.sqrt(r))));
|
|
jB = (int) Math.min(r-1,Math.floor(j + (3 * Math.sqrt(r))));
|
|
|
|
do {
|
|
sampledIntersections = randomSampleOfIntersections(intersectionSet, r);
|
|
//Collections.sort(sampledIntersections);
|
|
aVariant = sampledIntersections.get(jA);
|
|
bVariant = sampledIntersections.get(jB);
|
|
}
|
|
while (!checkCondition());
|
|
|
|
a = aVariant;
|
|
b = bVariant;
|
|
interval.setLower(a);
|
|
interval.setUpper(b);
|
|
N = Double.valueOf(checkNumberOfIntersectionInInterval(a,b));
|
|
}
|
|
}
|
|
|
|
if (presenter != null) {
|
|
setChanged();
|
|
|
|
double m,x;
|
|
double b,y;
|
|
|
|
Collections.sort(xCoordinates);
|
|
Collections.sort(yCoordinates);
|
|
int n = xCoordinates.size();
|
|
if (n % 2 == 0){
|
|
x = xCoordinates.get((n/2)-1) + xCoordinates.get((n/2)-2);
|
|
y = yCoordinates.get((n/2)-1) + yCoordinates.get((n/2)-2);
|
|
} else {
|
|
x = xCoordinates.get(((n+1)/2)-1);
|
|
y = yCoordinates.get(((n+1)/2)-1);
|
|
}
|
|
|
|
ArrayList<Point> resultSt = getKleftMostIntersection(a, this.b);
|
|
int size = resultSt.size();
|
|
if (n % 2 == 0){
|
|
m = resultSt.get((size/2)-1).getX() + resultSt.get((size/2)-2).getX();
|
|
} else {
|
|
m = resultSt.get(((size+1)/2)-1).getX()* (-1);
|
|
}
|
|
|
|
b = (x * m) + y;
|
|
m *= -1;
|
|
|
|
String[] result = {"ts", m+"", b+""};
|
|
notifyObservers(result);
|
|
}
|
|
}
|
|
|
|
private Boolean checkCondition(){
|
|
Boolean cond1 = (intersectionSet.get(k).getX() >= aVariant) && (intersectionSet.get(k).getX() < bVariant);
|
|
Boolean cond2 = (checkNumberOfIntersectionInInterval(aVariant,bVariant) <= ((11 * N) / Math.sqrt(r)));
|
|
return cond1 && cond2;
|
|
}
|
|
|
|
|
|
public ArrayList<Double> randomSampleOfIntersections(ArrayList<Point> set, Double r){
|
|
ArrayList<Double> sampledLines = new ArrayList<>();
|
|
|
|
for (int i = 0; i < r; i++) {
|
|
sampledLines.add(set.get(ThreadLocalRandom.current().nextInt(0, set.size()-1)).getX());
|
|
}
|
|
|
|
return sampledLines;
|
|
|
|
}
|
|
|
|
|
|
public int checkNumberOfIntersectionInInterval(double a, double b){
|
|
int counter = 0;
|
|
for (Point x : intersectionSet){
|
|
if (x.getX() >= a && x.getX() < b){
|
|
counter++;
|
|
}
|
|
}
|
|
return counter;
|
|
}
|
|
|
|
|
|
public ArrayList<Point> getKleftMostIntersection(double a, double b){
|
|
ArrayList<Point> list = new ArrayList<>();
|
|
for (Point x : intersectionSet){
|
|
if (x.getX() >= a && x.getX() < b){
|
|
list.add(x);
|
|
}
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
}
|