2020-03-21 00:37:09 +00:00
|
|
|
package de.wwwu.awolf.presenter.algorithms.naiv;
|
2017-09-15 18:18:36 +00:00
|
|
|
|
2020-03-21 00:37:09 +00:00
|
|
|
import de.wwwu.awolf.model.Line;
|
2020-03-28 15:24:35 +00:00
|
|
|
import de.wwwu.awolf.model.communication.Data;
|
|
|
|
import de.wwwu.awolf.presenter.AbstractPresenter;
|
2020-03-21 00:37:09 +00:00
|
|
|
import de.wwwu.awolf.presenter.algorithms.Algorithm;
|
|
|
|
import de.wwwu.awolf.presenter.util.FastElementSelector;
|
|
|
|
import de.wwwu.awolf.presenter.util.Logging;
|
2017-09-15 18:18:36 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-03-21 00:37:09 +00:00
|
|
|
import java.util.List;
|
2020-03-28 15:24:35 +00:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.Flow;
|
2017-09-15 18:18:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
|
|
*
|
|
|
|
* @Author: Armin Wolf
|
|
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
|
|
* @Date: 15.09.2017.
|
|
|
|
*/
|
|
|
|
public class NaivTheilSenEstimator implements Algorithm {
|
|
|
|
|
2020-03-28 15:24:35 +00:00
|
|
|
private static final Algorithm.Type type = Type.NAIV_TS;
|
2017-09-15 18:18:36 +00:00
|
|
|
|
2020-03-28 15:24:35 +00:00
|
|
|
private List<Line> setOfLines;
|
2019-08-01 05:19:04 +00:00
|
|
|
private double slope;
|
|
|
|
private double yInterception;
|
2020-03-28 15:24:35 +00:00
|
|
|
private Flow.Subscriber<? super Data> subscriber;
|
|
|
|
private AbstractPresenter presenter;
|
2017-09-15 18:18:36 +00:00
|
|
|
|
|
|
|
@Override
|
2020-03-21 19:54:03 +00:00
|
|
|
public Line call() {
|
2020-03-28 15:24:35 +00:00
|
|
|
this.slope = 0d;
|
|
|
|
this.yInterception = 0d;
|
2020-03-21 00:37:09 +00:00
|
|
|
Logging.logInfo("=== S T A R T - naiv T S ===");
|
2017-10-12 15:21:16 +00:00
|
|
|
long start;
|
|
|
|
long end;
|
|
|
|
start = System.currentTimeMillis();
|
2020-03-21 00:37:09 +00:00
|
|
|
List<Double> slopesList = new ArrayList<>();
|
2017-09-15 18:18:36 +00:00
|
|
|
int cnt = 0;
|
2020-03-28 15:24:35 +00:00
|
|
|
for (int i = 0; i < setOfLines.size(); i++) {
|
|
|
|
double x = setOfLines.get(i).getM();
|
|
|
|
double y = setOfLines.get(i).getB();
|
2017-09-15 18:18:36 +00:00
|
|
|
|
2020-03-28 15:24:35 +00:00
|
|
|
for (int j = i + 1; j < setOfLines.size(); j++) {
|
|
|
|
if (x != setOfLines.get(j).getM()) { // x must be different, otherwise slope becomes infinite
|
|
|
|
Double slope = (setOfLines.get(j).getB() - y) / (setOfLines.get(j).getM() - x);
|
2017-09-15 18:18:36 +00:00
|
|
|
slopesList.add(slope);
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-21 00:37:09 +00:00
|
|
|
List<Double> list1 = new ArrayList<>();
|
|
|
|
List<Double> list2 = new ArrayList<>();
|
2020-03-28 15:24:35 +00:00
|
|
|
for (Line line : setOfLines) {
|
2020-03-21 00:37:09 +00:00
|
|
|
list1.add(line.getM());
|
|
|
|
list2.add(line.getB());
|
2017-09-15 18:18:36 +00:00
|
|
|
}
|
2020-03-21 00:37:09 +00:00
|
|
|
double median1 = FastElementSelector.randomizedSelect(list1, list1.size() * 0.5);
|
|
|
|
double median2 = FastElementSelector.randomizedSelect(list2, list2.size() * 0.5);
|
|
|
|
slope = FastElementSelector.randomizedSelect(slopesList, slopesList.size() * 0.5);
|
2017-09-15 18:18:36 +00:00
|
|
|
yInterception = median2 - slope * median1;
|
2017-10-12 15:21:16 +00:00
|
|
|
end = System.currentTimeMillis();
|
2020-03-28 15:24:35 +00:00
|
|
|
Logging.logInfo("=== E N D - naiv T S ===");
|
|
|
|
Logging.logInfo("Slope: " + getSlope() + ", y-Interception: " + getYInterception());
|
2017-09-15 18:18:36 +00:00
|
|
|
|
2020-03-21 19:54:03 +00:00
|
|
|
return new Line(getSlope(), getYInterception());
|
2017-09-15 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 15:24:35 +00:00
|
|
|
@Override
|
|
|
|
public void setInput(Set<Line> lines) {
|
|
|
|
this.setOfLines = new ArrayList<>(lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPresenter(AbstractPresenter presenter) {
|
|
|
|
this.presenter = presenter;
|
|
|
|
subscribe(presenter);
|
|
|
|
}
|
|
|
|
|
2020-03-21 19:54:03 +00:00
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
/**
|
|
|
|
* @return Steigung
|
|
|
|
*/
|
2020-03-21 19:54:03 +00:00
|
|
|
public double getSlope() {
|
2017-09-16 13:26:33 +00:00
|
|
|
return slope * -1;
|
2017-09-15 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 13:21:53 +00:00
|
|
|
/**
|
|
|
|
* @return y-Achsenabschnitt
|
|
|
|
*/
|
2020-03-21 19:54:03 +00:00
|
|
|
public double getYInterception() {
|
2017-09-16 13:26:33 +00:00
|
|
|
return yInterception * -1;
|
2017-09-15 18:18:36 +00:00
|
|
|
}
|
2020-03-28 15:24:35 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void subscribe(Flow.Subscriber<? super Data> subscriber) {
|
|
|
|
this.subscriber = subscriber;
|
|
|
|
}
|
2017-09-15 18:18:36 +00:00
|
|
|
}
|