package de.wwwu.awolf.presenter.evaluation; import de.wwwu.awolf.model.Line; import de.wwwu.awolf.model.Point; import de.wwwu.awolf.model.evaluation.ComparisonResult; import de.wwwu.awolf.presenter.algorithms.Algorithm; import de.wwwu.awolf.presenter.algorithms.advanced.LeastMedianOfSquaresEstimator; import de.wwwu.awolf.presenter.algorithms.advanced.RepeatedMedianEstimator; import de.wwwu.awolf.presenter.algorithms.advanced.TheilSenEstimator; import de.wwwu.awolf.presenter.algorithms.naiv.NaivLeastMedianOfSquaresEstimator; import de.wwwu.awolf.presenter.algorithms.naiv.NaivRepeatedMedianEstimator; import de.wwwu.awolf.presenter.algorithms.naiv.NaivTheilSenEstimator; import de.wwwu.awolf.presenter.util.Logging; import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.concurrent.*; public class AlgorithmComparison { private final List types; private final ExecutorService executorService; private final Map algorithmMap; private final CompletionService completionService; public AlgorithmComparison(List types, List lines, List nodes) { this.types = types; this.executorService = Executors.newFixedThreadPool(3); completionService = new ExecutorCompletionService<>(this.executorService); algorithmMap = new EnumMap<>(Algorithm.Type.class); algorithmMap.put(Algorithm.Type.LMS, new LeastMedianOfSquaresEstimator(lines, nodes)); algorithmMap.put(Algorithm.Type.RM, new RepeatedMedianEstimator(lines)); algorithmMap.put(Algorithm.Type.TS, new TheilSenEstimator(lines, nodes)); algorithmMap.put(Algorithm.Type.NAIV_LMS, new NaivLeastMedianOfSquaresEstimator(lines)); algorithmMap.put(Algorithm.Type.NAIV_RM, new NaivRepeatedMedianEstimator(lines)); algorithmMap.put(Algorithm.Type.NAIV_TS, new NaivTheilSenEstimator(lines)); } public ComparisonResult compare() { ComparisonResult comparisonResult = new ComparisonResult(); getTypes().forEach(tye -> { completionService.submit(algorithmMap.get(tye)); }); for (Algorithm.Type value : getTypes()) { Algorithm.Type type; synchronized (types) { type = value; } try { Line line = completionService.take().get(); comparisonResult.put(type, line); } catch (InterruptedException interupted) { Logging.logError("Error while waiting for the Line result. Type: " + type, interupted); Thread.currentThread().interrupt(); } catch (ExecutionException e) { Logging.logError("Execution failed. Type: " + type, e); } } return comparisonResult; } private List getTypes() { return types; } }