145 lines
4.0 KiB
Java
145 lines
4.0 KiB
Java
package Presenter.Algorithms;
|
|
|
|
import Model.Line;
|
|
import Model.Pair;
|
|
import Model.Slab;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.LinkedList;
|
|
import java.util.Random;
|
|
|
|
/**
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
*
|
|
* @Author: Armin Wolf
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
* @Date: 28.05.2017.
|
|
*/
|
|
public class RepeatedMedianEstimator implements Algorithm {
|
|
|
|
private LinkedList<Line> set;
|
|
private Slab interval;
|
|
private InversionCounter invCounter = new InversionCounter();
|
|
|
|
//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<Line> linesInLeftSlab;
|
|
private ArrayList<Line> linesInCenterSlab;
|
|
private ArrayList<Line> linesInRightSlab;
|
|
|
|
private Double r;
|
|
private Integer n;
|
|
private Double k;
|
|
private Double kLow;
|
|
private Double kHigh;
|
|
private Double beta;
|
|
|
|
private Line thetaLow;
|
|
private Line thetaHigh;
|
|
|
|
|
|
|
|
public RepeatedMedianEstimator(LinkedList<Line> 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<Line> lines = sampleLines(linesInCenterSlab, r);
|
|
|
|
//TODO: hier kommt der neue Ansatz vom zweiten Algorithmus hin
|
|
estimateMedianIntersectionAbscissas(lines);
|
|
|
|
k = (Math.floor(n * 0.5) - linesInLeftSlab.size());
|
|
computeSlabBorders();
|
|
thetaLow = randomizedSelect(linesInCenterSlab,0,linesInCenterSlab.size()-1,kLow);
|
|
thetaHigh = randomizedSelect(linesInCenterSlab,0,linesInCenterSlab.size()-1,kHigh);
|
|
countNumberOfIntersectionsAbscissas();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public void computeSlabBorders(){
|
|
kLow = Math.max(1, Math.ceil(((r * k)/(linesInCenterSlab.size()))-((3 * Math.sqrt(r))/(2))));
|
|
kHigh = Math.min(1, Math.ceil(((r * k)/(linesInCenterSlab.size()))+((3 * Math.sqrt(r))/(2))));
|
|
}
|
|
|
|
public ArrayList<Line> sampleLines(ArrayList<Line> set, Double r){
|
|
|
|
ArrayList<Line> sampledLines = new ArrayList<>();
|
|
|
|
Random random = new Random(n);
|
|
for (int i=0; i<n; i++){
|
|
sampledLines.add(set.get(random.nextInt()));
|
|
}
|
|
|
|
return sampledLines;
|
|
}
|
|
|
|
public Line randomizedSelect(ArrayList<Line> a, int start, int end, double i){
|
|
if (start == end)
|
|
return a.get(start);
|
|
int q = randomizedPartition(a, start, end);
|
|
int tmpPivot = q - start + 1;
|
|
|
|
if ( i == tmpPivot ){
|
|
return a.get(q);
|
|
} else if ( i < tmpPivot ) {
|
|
return randomizedSelect(a, start, q-1, i);
|
|
} else {
|
|
return randomizedSelect(a, q+1, end, i-tmpPivot);
|
|
}
|
|
}
|
|
|
|
public int randomizedPartition(ArrayList<Line> a, int start, int end){
|
|
int delta = Math.abs(end - start);
|
|
Random random = new Random(delta);
|
|
int i = start + random.nextInt();
|
|
Collections.swap(a, end, i);
|
|
return partition(a, start, end);
|
|
}
|
|
|
|
public int partition(ArrayList<Line> a, int start, int end){
|
|
Line x = a.get(end);
|
|
int i = start - 1;
|
|
for (int j=start; j<end; j++){
|
|
if (a.get(j).getM() <= x.getM()){
|
|
i++;
|
|
Collections.swap(a, i, j);
|
|
}
|
|
}
|
|
Collections.swap(a, i+1, end);
|
|
return i+1;
|
|
}
|
|
|
|
public void countNumberOfIntersectionsAbscissas(){
|
|
|
|
}
|
|
|
|
public void estimateMedianIntersectionAbscissas(ArrayList<Line> sampledLines){
|
|
int inversions = invCounter.run(sampledLines, interval);
|
|
}
|
|
|
|
}
|
|
|
|
|