2020-03-21 00:37:09 +00:00
|
|
|
|
package de.wwwu.awolf.presenter.algorithms.advanced;
|
|
|
|
|
|
|
|
|
|
import de.wwwu.awolf.model.Interval;
|
|
|
|
|
import de.wwwu.awolf.model.Line;
|
|
|
|
|
import de.wwwu.awolf.model.communication.AlgorithmData;
|
|
|
|
|
import de.wwwu.awolf.model.communication.Data;
|
|
|
|
|
import de.wwwu.awolf.model.communication.SubscriberType;
|
|
|
|
|
import de.wwwu.awolf.presenter.Presenter;
|
|
|
|
|
import de.wwwu.awolf.presenter.algorithms.Algorithm;
|
|
|
|
|
import de.wwwu.awolf.presenter.util.FastElementSelector;
|
|
|
|
|
import de.wwwu.awolf.presenter.util.IntersectionCounter;
|
|
|
|
|
import de.wwwu.awolf.presenter.util.Logging;
|
|
|
|
|
import de.wwwu.awolf.presenter.util.RandomSampler;
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
2020-03-21 00:37:09 +00:00
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.Flow;
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
|
|
|
*
|
|
|
|
|
* @Author: Armin Wolf
|
|
|
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
|
|
|
* @Date: 28.05.2017.
|
|
|
|
|
*/
|
2020-03-21 00:37:09 +00:00
|
|
|
|
public class RepeatedMedianEstimator implements Algorithm, Flow.Publisher<Data> {
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
|
|
|
|
private Presenter presenter;
|
2020-03-21 00:37:09 +00:00
|
|
|
|
private List<Line> set;
|
|
|
|
|
private Map<Line, Double> medianIntersections = new HashMap<>();
|
|
|
|
|
private Map<Line, ArrayList<Double>> intersectionAbscissas = new HashMap<>();
|
2017-09-10 15:45:47 +00:00
|
|
|
|
private Interval interval;
|
|
|
|
|
|
|
|
|
|
//in der Literatur als L_i, C_i, und R_i bekannt
|
2020-03-21 00:37:09 +00:00
|
|
|
|
private List<Double> countLeftSlab;
|
|
|
|
|
private List<Double> countCenterSlab;
|
|
|
|
|
private List<Double> countRightSlab;
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
|
|
|
|
//die Mengen L,C und R
|
2020-03-21 00:37:09 +00:00
|
|
|
|
private List<Line> linesInLeftSlab;
|
|
|
|
|
private List<Line> linesInCenterSlab;
|
|
|
|
|
private List<Line> linesInRightSlab;
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
2019-08-01 05:19:04 +00:00
|
|
|
|
private double r;
|
|
|
|
|
private int n;
|
|
|
|
|
private double k;
|
|
|
|
|
private double kLow;
|
|
|
|
|
private double kHigh;
|
|
|
|
|
private double beta;
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
2019-08-01 05:19:04 +00:00
|
|
|
|
private double thetaLow;
|
|
|
|
|
private double thetaHigh;
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
2019-08-01 05:19:04 +00:00
|
|
|
|
private double slope;
|
|
|
|
|
private double yInterception;
|
2020-03-21 00:37:09 +00:00
|
|
|
|
private Flow.Subscriber<? super AlgorithmData> subscriber;
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Konstruktor
|
2020-03-20 17:08:18 +00:00
|
|
|
|
*
|
|
|
|
|
* @param set Liste der Geraden
|
|
|
|
|
* @param presenter Presenter (Beobachter)
|
2017-10-15 13:21:53 +00:00
|
|
|
|
*/
|
2020-03-21 00:37:09 +00:00
|
|
|
|
public RepeatedMedianEstimator(List<Line> set, Presenter presenter) {
|
2017-09-10 15:45:47 +00:00
|
|
|
|
this.set = set;
|
|
|
|
|
this.presenter = presenter;
|
|
|
|
|
interval = new Interval(-10000, 10000);
|
|
|
|
|
n = set.size();
|
|
|
|
|
beta = 0.5;
|
|
|
|
|
countLeftSlab = new ArrayList<>();
|
|
|
|
|
countCenterSlab = new ArrayList<>();
|
|
|
|
|
countRightSlab = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
countLeftSlab.add(0d);
|
|
|
|
|
countRightSlab.add(0d);
|
|
|
|
|
countCenterSlab.add(n - 1.0);
|
|
|
|
|
intersectionAbscissas.put(set.get(i), new ArrayList<>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
linesInLeftSlab = new ArrayList<>();
|
|
|
|
|
linesInCenterSlab = new ArrayList<>(set);
|
|
|
|
|
linesInRightSlab = new ArrayList<>();
|
2020-03-21 00:37:09 +00:00
|
|
|
|
subscribe(presenter);
|
2017-09-10 15:45:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Konstruktor
|
2020-03-20 17:08:18 +00:00
|
|
|
|
*
|
2017-10-15 13:21:53 +00:00
|
|
|
|
* @param set Liste der Geraden
|
|
|
|
|
*/
|
2020-03-21 00:37:09 +00:00
|
|
|
|
public RepeatedMedianEstimator(List<Line> set) {
|
2017-09-10 15:45:47 +00:00
|
|
|
|
this(set, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-15 13:21:53 +00:00
|
|
|
|
* Führt den Algortihmus zur Berechnung des RM-Schätzers durch.
|
2020-03-20 17:08:18 +00:00
|
|
|
|
* <p>
|
2017-10-15 13:21:53 +00:00
|
|
|
|
* Paper:
|
|
|
|
|
* Matousek, Jiri, D. M. Mount und N. S. Netanyahu
|
|
|
|
|
* „Efficient Randomized Algorithms for the Repeated Median Line Estimator“. 1998
|
|
|
|
|
* Algorithmica 20.2, S. 136–150
|
2017-09-10 15:45:47 +00:00
|
|
|
|
*/
|
|
|
|
|
public void run() {
|
|
|
|
|
|
2020-03-21 00:37:09 +00:00
|
|
|
|
Logging.logInfo("=== S T A R T - R M ===");
|
2017-10-12 15:21:16 +00:00
|
|
|
|
long start;
|
|
|
|
|
long end;
|
|
|
|
|
start = System.currentTimeMillis();
|
|
|
|
|
|
2017-09-10 15:45:47 +00:00
|
|
|
|
while (linesInCenterSlab.size() != 1) {
|
|
|
|
|
n = linesInCenterSlab.size();
|
|
|
|
|
r = Math.ceil(Math.pow(n, beta));
|
2020-03-21 00:37:09 +00:00
|
|
|
|
List<Line> lines = RandomSampler.run(linesInCenterSlab, r, linesInCenterSlab.size());
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
//Für jede Gerade aus der Stichprobe wird der Schnittpunkt mit der medianen
|
|
|
|
|
//x-Koordinate bestimmt
|
2020-03-21 00:37:09 +00:00
|
|
|
|
List<Double> medianIntersectionAbscissas = new ArrayList<>();
|
2017-09-10 15:45:47 +00:00
|
|
|
|
for (Line l : lines) {
|
|
|
|
|
Double abscissa = estimateMedianIntersectionAbscissas(l);
|
|
|
|
|
medianIntersections.put(l, abscissa);
|
|
|
|
|
medianIntersectionAbscissas.add(abscissa);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
//Rang vom RM-Wert in C
|
2017-09-10 15:45:47 +00:00
|
|
|
|
k = Math.max(1, Math.min(set.size(), (Math.ceil(n * 0.5) - linesInLeftSlab.size())));
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
//berechne k_lo und k_hi
|
2017-09-10 15:45:47 +00:00
|
|
|
|
computeSlabBorders();
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
//Berechne die Elemente mit dem Rang Theta_lo und Theta_hi
|
2017-09-10 15:45:47 +00:00
|
|
|
|
thetaLow = FastElementSelector.randomizedSelect(medianIntersectionAbscissas, kLow);
|
|
|
|
|
thetaHigh = FastElementSelector.randomizedSelect(medianIntersectionAbscissas, kHigh);
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
//Für jede Gerade in C wird die Anzahl der Schnittpunkte die im Intervall liegen hochgezählt
|
2017-09-10 15:45:47 +00:00
|
|
|
|
countNumberOfIntersectionsAbscissas();
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
//verkleinere das Intervall
|
2017-09-10 15:45:47 +00:00
|
|
|
|
contractIntervals();
|
|
|
|
|
|
|
|
|
|
}
|
2017-10-12 15:21:16 +00:00
|
|
|
|
end = System.currentTimeMillis();
|
2020-03-21 00:37:09 +00:00
|
|
|
|
Logging.logInfo("Zeit: " + ((end - start) / 1000));
|
2019-08-01 05:19:04 +00:00
|
|
|
|
|
|
|
|
|
pepareResult();
|
2017-09-10 15:45:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-15 13:21:53 +00:00
|
|
|
|
* Berechnet die mediane x-Koordinate über den Schnittpunkten.
|
|
|
|
|
*
|
|
|
|
|
* @param sampledLine Stichprobe von Geraden
|
2020-03-20 17:08:18 +00:00
|
|
|
|
* @return mediane x-Koordinate über den Schnittpunkten
|
2017-09-10 15:45:47 +00:00
|
|
|
|
*/
|
|
|
|
|
public Double estimateMedianIntersectionAbscissas(Line sampledLine) {
|
|
|
|
|
|
2020-03-21 00:37:09 +00:00
|
|
|
|
int index = Integer.parseInt(sampledLine.getId());
|
2017-09-10 15:45:47 +00:00
|
|
|
|
IntersectionCounter intersectionCounter = new IntersectionCounter();
|
2020-03-21 00:37:09 +00:00
|
|
|
|
List<Double> intersections = intersectionCounter.calculateIntersectionAbscissas(linesInCenterSlab, sampledLine);
|
2017-09-10 15:45:47 +00:00
|
|
|
|
|
2020-03-21 00:37:09 +00:00
|
|
|
|
double ki = Math.ceil((n - 1) * 0.5) - FastElementSelector.randomizedSelect(countLeftSlab, index);
|
2017-09-10 15:45:47 +00:00
|
|
|
|
double i = (Math.ceil((Math.sqrt(n) * ki) / FastElementSelector.randomizedSelect(countCenterSlab, index)));
|
|
|
|
|
int accessIndex;
|
|
|
|
|
if (i < 0)
|
|
|
|
|
accessIndex = 0;
|
|
|
|
|
else if (i >= intersections.size())
|
|
|
|
|
accessIndex = intersections.size() - 1;
|
|
|
|
|
else
|
|
|
|
|
accessIndex = (int) i;
|
|
|
|
|
|
|
|
|
|
return FastElementSelector.randomizedSelect(intersections, accessIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-15 13:21:53 +00:00
|
|
|
|
* Berechnet die potenziell neuen Intervallgrenzen.
|
2017-09-10 15:45:47 +00:00
|
|
|
|
*/
|
|
|
|
|
public void computeSlabBorders() {
|
2017-10-15 13:21:53 +00:00
|
|
|
|
kLow = Math.max(1, Math.floor(((r * k) / (linesInCenterSlab.size()))
|
2020-03-21 00:37:09 +00:00
|
|
|
|
- ((3 * Math.sqrt(r)) * (0.5))));
|
2017-10-15 13:21:53 +00:00
|
|
|
|
kHigh = Math.min(r, Math.floor(((r * k) / (linesInCenterSlab.size()))
|
2020-03-21 00:37:09 +00:00
|
|
|
|
+ ((3 * Math.sqrt(r)) * (0.5))));
|
2017-09-10 15:45:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-15 13:21:53 +00:00
|
|
|
|
* Berechnet die Anzahl der Schnittpunkte pro Bereich. Insgesammt gibt es drei Bereiche:
|
|
|
|
|
* Im Intervall => (a,b], vor dem Intervall => (a', a], hinter dem Intervall => (b, b'].
|
2017-09-10 15:45:47 +00:00
|
|
|
|
*/
|
|
|
|
|
public void countNumberOfIntersectionsAbscissas() {
|
|
|
|
|
for (Line line : linesInCenterSlab) {
|
2020-03-21 00:37:09 +00:00
|
|
|
|
List<Double> intersections = intersectionAbscissas.get(line);
|
|
|
|
|
int index = Integer.parseInt(line.getId());
|
2017-09-10 15:45:47 +00:00
|
|
|
|
int left = 0;
|
|
|
|
|
int center = 0;
|
|
|
|
|
int right = 0;
|
|
|
|
|
|
|
|
|
|
for (Double intersection : intersections) {
|
|
|
|
|
if (intersection <= thetaLow) {
|
|
|
|
|
left++;
|
|
|
|
|
} else if (intersection > thetaLow && intersection <= thetaHigh) {
|
|
|
|
|
center++;
|
|
|
|
|
} else if (intersection > thetaHigh) {
|
|
|
|
|
right++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 17:08:18 +00:00
|
|
|
|
countLeftSlab.set(index, (double) left);
|
2017-09-10 15:45:47 +00:00
|
|
|
|
countCenterSlab.set(index, (double) center);
|
2020-03-20 17:08:18 +00:00
|
|
|
|
countRightSlab.set(index, (double) right);
|
2017-09-10 15:45:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-21 00:37:09 +00:00
|
|
|
|
* TODO bad idea!!!
|
2017-10-15 13:21:53 +00:00
|
|
|
|
* Verkleinert das aktuelle Intervall. Eines der drei Bereiche wird als neues Intervall gewählt.
|
|
|
|
|
* Auf diesem Intervall werden dann in der nächsten Iteration wieder drei Bereiche bestimmt.
|
2017-09-10 15:45:47 +00:00
|
|
|
|
*/
|
|
|
|
|
public void contractIntervals() {
|
|
|
|
|
for (int i = 0; i < linesInCenterSlab.size(); i++) {
|
|
|
|
|
|
|
|
|
|
double left = countLeftSlab.get(i);
|
|
|
|
|
double center = countCenterSlab.get(i);
|
|
|
|
|
double right = countRightSlab.get(i);
|
|
|
|
|
|
|
|
|
|
double max = Math.max(left, Math.max(center, right));
|
|
|
|
|
|
|
|
|
|
if (left == max) {
|
|
|
|
|
linesInLeftSlab.add(linesInCenterSlab.get(i));
|
|
|
|
|
linesInCenterSlab.remove(i);
|
|
|
|
|
} else if (right == max) {
|
|
|
|
|
linesInRightSlab.add(linesInCenterSlab.get(i));
|
|
|
|
|
linesInCenterSlab.remove(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//wähle C als C
|
2020-03-21 00:37:09 +00:00
|
|
|
|
if (linesInLeftSlab.size() < Math.ceil(n * 0.5) && Math.ceil(n * 0.5) <= linesInLeftSlab.size() + linesInCenterSlab.size()) {
|
2017-09-10 15:45:47 +00:00
|
|
|
|
interval.setLower(thetaLow + 0.1);
|
|
|
|
|
interval.setUpper(thetaHigh);
|
|
|
|
|
}
|
|
|
|
|
// wähle L als C
|
2020-03-21 00:37:09 +00:00
|
|
|
|
else if (Math.ceil(n * 0.5) <= linesInLeftSlab.size()) {
|
2017-09-10 15:45:47 +00:00
|
|
|
|
interval.setUpper(thetaLow);
|
|
|
|
|
}
|
|
|
|
|
//wähle R als C
|
2020-03-21 00:37:09 +00:00
|
|
|
|
else if (linesInLeftSlab.size() + linesInCenterSlab.size() < Math.ceil(n * 0.5) && Math.ceil(n * 0.5) <= (linesInLeftSlab.size() + linesInCenterSlab.size() + linesInRightSlab.size())) {
|
2017-09-10 15:45:47 +00:00
|
|
|
|
interval.setLower(thetaHigh - 0.1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-10-15 10:45:47 +00:00
|
|
|
|
public void pepareResult() {
|
2020-03-21 00:37:09 +00:00
|
|
|
|
if (this.subscriber != null) {
|
2017-09-10 15:45:47 +00:00
|
|
|
|
double m = thetaLow;
|
|
|
|
|
double b = (-1) * (
|
|
|
|
|
(linesInCenterSlab.get(0).getM() * (thetaLow)) + linesInCenterSlab.get(0)
|
|
|
|
|
.getB());
|
|
|
|
|
|
|
|
|
|
slope = m;
|
|
|
|
|
yInterception = b;
|
2020-03-21 00:37:09 +00:00
|
|
|
|
AlgorithmData data = new AlgorithmData();
|
|
|
|
|
data.setType(SubscriberType.RM);
|
|
|
|
|
data.setLineData(new Line(m, b));
|
|
|
|
|
this.subscriber.onNext(data);
|
2017-09-10 15:45:47 +00:00
|
|
|
|
} else {
|
|
|
|
|
double m = thetaLow;
|
|
|
|
|
double b = (-1) * ((linesInCenterSlab.get(0).getM() * (thetaLow)) + linesInCenterSlab.get(0).getB());
|
|
|
|
|
slope = m;
|
|
|
|
|
yInterception = b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return Anzahl der Geraden
|
|
|
|
|
*/
|
2017-09-10 15:45:47 +00:00
|
|
|
|
public Integer getN() {
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param n Anzahl der Geraden
|
|
|
|
|
*/
|
2017-09-10 15:45:47 +00:00
|
|
|
|
public void setN(Integer n) {
|
|
|
|
|
this.n = n;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param beta Parameter Beta
|
|
|
|
|
*/
|
2017-09-10 15:45:47 +00:00
|
|
|
|
public void setBeta(Double beta) {
|
|
|
|
|
this.beta = beta;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return Steigung
|
|
|
|
|
*/
|
2017-09-10 15:45:47 +00:00
|
|
|
|
public Double getSlope() {
|
|
|
|
|
return slope;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return y-Achsenabschnitt
|
|
|
|
|
*/
|
2017-09-10 15:45:47 +00:00
|
|
|
|
public Double getyInterception() {
|
|
|
|
|
return yInterception;
|
|
|
|
|
}
|
2017-10-23 15:48:36 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return temporäres untere Intervallgrenze
|
|
|
|
|
*/
|
|
|
|
|
public Double getkLow() {
|
|
|
|
|
return kLow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param kLow temporäres untere Intervallgrenze
|
|
|
|
|
*/
|
|
|
|
|
public void setkLow(Double kLow) {
|
|
|
|
|
this.kLow = kLow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return temporäres oberes Intervallgrenze
|
|
|
|
|
*/
|
|
|
|
|
public Double getkHigh() {
|
|
|
|
|
return kHigh;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param kHigh temporäres oberes Intervallgrenze
|
|
|
|
|
*/
|
|
|
|
|
public void setkHigh(Double kHigh) {
|
|
|
|
|
this.kHigh = kHigh;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-20 17:08:18 +00:00
|
|
|
|
* @return verteilung der Punkte
|
2017-10-23 15:48:36 +00:00
|
|
|
|
*/
|
2020-03-21 00:37:09 +00:00
|
|
|
|
public List<Double> getCountLeftSlab() {
|
2017-10-23 15:48:36 +00:00
|
|
|
|
return countLeftSlab;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return verteilung der Punkte
|
|
|
|
|
*/
|
2020-03-21 00:37:09 +00:00
|
|
|
|
public List<Double> getCountCenterSlab() {
|
2017-10-23 15:48:36 +00:00
|
|
|
|
return countCenterSlab;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return verteilung der Punkte
|
|
|
|
|
*/
|
2020-03-21 00:37:09 +00:00
|
|
|
|
public List<Double> getCountRightSlab() {
|
2017-10-23 15:48:36 +00:00
|
|
|
|
return countRightSlab;
|
|
|
|
|
}
|
2020-03-21 00:37:09 +00:00
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void subscribe(Flow.Subscriber<? super Data> subscriber) {
|
|
|
|
|
this.subscriber = subscriber;
|
|
|
|
|
}
|
2017-09-10 15:45:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|