From c352ed6a64bf069aae55bdcd5cad5fe3fe546846 Mon Sep 17 00:00:00 2001 From: Armin Wolf Date: Fri, 16 Jun 2017 18:06:50 +0200 Subject: [PATCH] Start: Repeated Median Estimator --- src/main/java/Model/Slab.java | 50 +++++++++++++ .../LeastMedianOfSquaresEstimator.java | 75 ++++--------------- .../Algorithms/RepeatedMedianEstimator.java | 71 ++++++++++++++++++ src/main/java/Presenter/Presenter.java | 9 ++- 4 files changed, 141 insertions(+), 64 deletions(-) create mode 100644 src/main/java/Model/Slab.java diff --git a/src/main/java/Model/Slab.java b/src/main/java/Model/Slab.java new file mode 100644 index 0000000..079aa98 --- /dev/null +++ b/src/main/java/Model/Slab.java @@ -0,0 +1,50 @@ +package Model; + +/** + * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden. + * + * @Author: Armin Wolf + * @Email: a_wolf28@uni-muenster.de + * @Date: 16.06.2017. + */ +public class Slab { + private double upper; + private double lower; + private Boolean activity; + + public Slab(double lower, double upper) { + this.upper = upper; + this.lower = lower; + this.activity = true; + } + + public Boolean getActivity() { + return activity; + } + + public void setActivity(Boolean isActive) { + this.activity = isActive; + } + + public double getUpper() { + return upper; + } + + public void setUpper(double upper) { + this.upper = upper; + } + + public double getLower() { + return lower; + } + + public void setLower(double lower) { + this.lower = lower; + } + + public Double getDistance(){ + + return Math.abs(this.upper - this.lower); + } + +} \ No newline at end of file diff --git a/src/main/java/Presenter/Algorithms/LeastMedianOfSquaresEstimator.java b/src/main/java/Presenter/Algorithms/LeastMedianOfSquaresEstimator.java index 6203177..48b5546 100644 --- a/src/main/java/Presenter/Algorithms/LeastMedianOfSquaresEstimator.java +++ b/src/main/java/Presenter/Algorithms/LeastMedianOfSquaresEstimator.java @@ -2,6 +2,7 @@ package Presenter.Algorithms; import Model.Line; import Model.Point; +import Model.Slab; import Presenter.Presenter; import View.MainFrame; import javafx.beans.*; @@ -34,6 +35,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit private Line sigmaMin; private double heightsigmaMin; private Double intersectionsPoint; + private Double constant = 1d; public LeastMedianOfSquaresEstimator(LinkedList set, LinkedList intersections, Presenter presenter) { this.set = set; @@ -43,6 +45,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit n = set.size(); double quantile = 0.5; double qPlus = quantile; + quantileError = 0.1; double qMinus = qPlus * (1 - quantileError); kMinus = (int) Math.ceil(n * qMinus); kPlus = (int) Math.ceil(n * qPlus); @@ -50,17 +53,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit } public LeastMedianOfSquaresEstimator(LinkedList set, LinkedList intersections) { - this.set = set; - 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); - + new LeastMedianOfSquaresEstimator(set, intersections, null); } public void printResult(){ @@ -70,7 +63,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit /** * */ - public void approximateLMS() { + public void run() { //(2.) Let U <- (-inf, inf) be the initial active slabs... @@ -100,7 +93,6 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit int numberOfIntersections = countInversions(slab); //(b.) apply plane sweep - int constant = 1; if ((constant * n) >= numberOfIntersections) { sigmaMin = planeSweep(slab); } else { @@ -143,7 +135,7 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit } if (presenter != null){ setChanged(); - double m = (getSigmaMin().getX2() + getSigmaMin().getX1()) * 0.5; + double m = (getSigmaMin().getX2() + getSigmaMin().getX1()) * -0.5; double b = (getSigmaMin().getY2() + getSigmaMin().getY1()) * 0.5; notifyObservers(new Line(m,b)); } @@ -405,53 +397,6 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit } - /** - * Hilfsklasse um die Slabs zu verteilen, private Klasse da sonst nicht verwendett wird und somit eine - * äußere Klasse überflüssig ist... - */ - protected static class Slab { - private double upper; - private double lower; - private Boolean activity; - - public Slab(double lower, double upper) { - this.upper = upper; - this.lower = lower; - this.activity = true; - } - - public Boolean getActivity() { - return activity; - } - - public void setActivity(Boolean isActive) { - this.activity = isActive; - } - - public double getUpper() { - return upper; - } - - public void setUpper(double upper) { - this.upper = upper; - } - - public double getLower() { - return lower; - } - - public void setLower(double 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. */ @@ -543,4 +488,12 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit public void setIntersectionsPoint(double intersectionsPoint) { this.intersectionsPoint = intersectionsPoint; } + + public Double getConstant() { + return constant; + } + + public void setConstant(Double constant) { + this.constant = constant; + } } diff --git a/src/main/java/Presenter/Algorithms/RepeatedMedianEstimator.java b/src/main/java/Presenter/Algorithms/RepeatedMedianEstimator.java index 9e56987..c533554 100644 --- a/src/main/java/Presenter/Algorithms/RepeatedMedianEstimator.java +++ b/src/main/java/Presenter/Algorithms/RepeatedMedianEstimator.java @@ -1,5 +1,11 @@ package Presenter.Algorithms; +import Model.Line; +import Model.Slab; + +import java.util.ArrayList; +import java.util.LinkedList; + /** * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden. * @@ -8,4 +14,69 @@ package Presenter.Algorithms; * @Date: 28.05.2017. */ public class RepeatedMedianEstimator implements Algorithm { + + private LinkedList set; + private Slab interval; + + //in der Literatur als L_i, C_i, und R_i bekannt + private Integer countLeftSlab; + private Integer countCenterSlab; + private Integer countRightSlab; + + //die Mengen L,C und R + private ArrayList linesInLeftSlab; + private ArrayList linesInCenterSlab; + private ArrayList linesInRightSlab; + + private Double r; + private Integer n; + private Double k; + private Double kLow; + private Double kHigh; + private Double beta; + + + + public RepeatedMedianEstimator(LinkedList set) { + this.set = set; + interval = new Slab(-10000,10000); + n = set.size(); + beta = 1.0; + countLeftSlab = 0; + countCenterSlab = n - 1; + countRightSlab = 0; + + linesInLeftSlab = new ArrayList<>(); + linesInCenterSlab = new ArrayList<>(set); + linesInRightSlab = new ArrayList<>(); + } + + + public void run(){ + while (linesInCenterSlab.size() != 1){ + r = Math.floor(Math.pow(n, beta)); + ArrayList lines = sampleLines(linesInCenterSlab, r); + + //TODO: hier kommt der neue Ansatz vom zweiten Algorithmus hin + + k = (Math.floor(n * 0.5) - linesInLeftSlab.size()); + computeSlabBorders(); + fastSelectionAlg(); + countNumberOfIntersectionsAbscissas(); + } + } + + + public ArrayList sampleLines(ArrayList set, Double r){return null;} + + public void computeSlabBorders(){ + kLow = Math.max(1, Math.ceil(((r * k)/(linesInCenterSlab.size()))-((3 * Math.sqrt(r))/(2)))); + kHigh = Math.max(1, Math.ceil(((r * k)/(linesInCenterSlab.size()))+((3 * Math.sqrt(r))/(2)))); + } + + public void fastSelectionAlg(){} + public void countNumberOfIntersectionsAbscissas(){} + } + + diff --git a/src/main/java/Presenter/Presenter.java b/src/main/java/Presenter/Presenter.java index 2db8141..0ce499a 100644 --- a/src/main/java/Presenter/Presenter.java +++ b/src/main/java/Presenter/Presenter.java @@ -72,7 +72,6 @@ public class Presenter implements Observer { SwingUtilities.invokeLater(()->{ getView().createPlot(result.getM(), result.getB()); getView().setLmsIsComplete(true); - getView().getPlotButton().setEnabled(true); getView().logSuccess("Berechnung wurde Erfolgreich durchgeführt"); getView().log("m: "+result.getM()+" b: "+result.getB()); }); @@ -84,10 +83,14 @@ public class Presenter implements Observer { view.createArrangement(); } - public void startScatterPlotVisualization() { + public void startScatterPlotVisualization(String[] input) { + Double constant =Double.parseDouble(input[0]); + Double error = Double.parseDouble(input[1]); lms = new LeastMedianOfSquaresEstimator(model.getLines(), model.getNodes(), this); + lms.setConstant(constant); + lms.setQuantileError(error); lms.addObserver(this); - lms.approximateLMS(); + lms.run(); }