diff --git a/src/main/java/ru/ulstu/estimate/CompressionMetricService.java b/src/main/java/ru/ulstu/estimate/CompressionMetricService.java index 0dfaa24..3d26eaa 100644 --- a/src/main/java/ru/ulstu/estimate/CompressionMetricService.java +++ b/src/main/java/ru/ulstu/estimate/CompressionMetricService.java @@ -2,21 +2,38 @@ package ru.ulstu.estimate; import org.springframework.stereotype.Service; +import ru.ulstu.datamodel.Model; import ru.ulstu.datamodel.ModelingResult; import ru.ulstu.datamodel.exception.ModelingException; import ru.ulstu.datamodel.ts.TimeSeries; +import ru.ulstu.datamodel.ts.TimeSeriesValue; +import ru.ulstu.method.MethodParamValue; +import ru.ulstu.method.ftransform.FTransform; +import ru.ulstu.score.ScoreMethod; +import ru.ulstu.service.MethodParamBruteForce; import ru.ulstu.service.TimeSeriesService; import java.lang.reflect.InvocationTargetException; -import java.util.concurrent.ExecutionException; +import java.time.LocalDateTime; +import java.util.*; +import java.util.concurrent.*; +import java.util.stream.Collectors; @Service public class CompressionMetricService { + private final int DEFAULT_THREAD_COUNT = 50; private final TimeSeriesService timeSeriesService; + private final FTransform fTransform; + private final MethodParamBruteForce methodParamBruteForce; + private final ScoreMethod scoreMethod; + private final ExecutorService executors = Executors.newFixedThreadPool(DEFAULT_THREAD_COUNT); - public CompressionMetricService(TimeSeriesService timeSeriesService) { + public CompressionMetricService(TimeSeriesService timeSeriesService, FTransform fTransform, MethodParamBruteForce methodParamBruteForce, ScoreMethod scoreMethod) { this.timeSeriesService = timeSeriesService; + this.fTransform = fTransform; + this.methodParamBruteForce = methodParamBruteForce; + this.scoreMethod = scoreMethod; } public void getCompressionMetric() { @@ -33,6 +50,33 @@ public class CompressionMetricService { return modelingResult.getTimeSeries(); } + protected ModelingResult getCompressionTimeSeriesWithFTransform(TimeSeries timeSeries) throws ExecutionException, InterruptedException { + List> results = new ArrayList<>(); + List results2 = new CopyOnWriteArrayList<>(); + Map tsValues = timeSeries.getValues().stream() + .collect(Collectors.toMap(TimeSeriesValue::getDate, TimeSeriesValue::getValue, + (oldValue, newValue) -> oldValue, LinkedHashMap::new)); + + + List> availableParametersValues = methodParamBruteForce.getAvailableParametersValues(timeSeries, fTransform.getAvailableParameters()); + for (List parametersValues : availableParametersValues) { + results.add(executors.submit(() -> { + Model model = fTransform.getModel(timeSeries, parametersValues); + return new ModelingResult(model.getTimeSeriesModel(), + null, + parametersValues, + scoreMethod.getScore(tsValues, model.getTimeSeriesModel()), fTransform); + })); + } + + + for (Future futureModelingResult : results) { + results2.add(futureModelingResult.get()); + } + return results2.stream() + .min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue())) + .orElse(null); + } } diff --git a/src/main/java/ru/ulstu/score/ScoreMethod.java b/src/main/java/ru/ulstu/score/ScoreMethod.java index 7459f29..7e8128f 100644 --- a/src/main/java/ru/ulstu/score/ScoreMethod.java +++ b/src/main/java/ru/ulstu/score/ScoreMethod.java @@ -5,9 +5,11 @@ import ru.ulstu.datamodel.exception.ModelingException; import ru.ulstu.datamodel.ts.TimeSeries; import ru.ulstu.datamodel.ts.TimeSeriesValue; +import java.lang.reflect.InvocationTargetException; import java.time.LocalDateTime; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ExecutionException; public abstract class ScoreMethod { private final String name; @@ -16,11 +18,11 @@ public abstract class ScoreMethod { this.name = name; } - public Score getScore(Map tsValues, TimeSeries model) throws ModelingException { + public Score getScore(Map tsValues, TimeSeries model) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { return new Score(this, evaluate(tsValues, model)); } - public abstract Number evaluate(Map tsValues, TimeSeries model) throws ModelingException; + public abstract Number evaluate(Map tsValues, TimeSeries model) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException; public String getName() { return name; diff --git a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java index c8e76a9..5c8573e 100644 --- a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java +++ b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java @@ -28,7 +28,7 @@ import java.util.concurrent.Future; import java.util.stream.Collectors; @Service -class MethodParamBruteForce { +public class MethodParamBruteForce { private final int DEFAULT_THREAD_COUNT = 50; private final List methods; private final ScoreMethod scoreMethod = new Smape(); @@ -175,7 +175,7 @@ class MethodParamBruteForce { return getSmoothedTimeSeries(timeSeries, methods); } - private List> getAvailableParametersValues(TimeSeries timeSeries, List availableParameters) { + public List> getAvailableParametersValues(TimeSeries timeSeries, List availableParameters) { List> result = new ArrayList<>(); Map parameterOffset = new TreeMap<>(); Map> parameterValues = new TreeMap<>(); diff --git a/src/main/java/ru/ulstu/target/Target.java b/src/main/java/ru/ulstu/target/Target.java index bd11e58..d834ae6 100644 --- a/src/main/java/ru/ulstu/target/Target.java +++ b/src/main/java/ru/ulstu/target/Target.java @@ -2,8 +2,11 @@ package ru.ulstu.target; import ru.ulstu.datamodel.exception.ModelingException; import ru.ulstu.datamodel.ts.TimeSeries; +import ru.ulstu.datamodel.ts.TimeSeriesValue; import java.lang.reflect.InvocationTargetException; +import java.time.LocalDateTime; +import java.util.Map; import java.util.concurrent.ExecutionException; public abstract class Target { @@ -19,4 +22,11 @@ public abstract class Target { public abstract TimeSeries calculate(TimeSeries model) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException; + public TimeSeries calculate(Map values) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + TimeSeries model = new TimeSeries(); + for (var entry : values.entrySet()) { + model.addValue(new TimeSeriesValue(entry.getKey(), entry.getValue())); + } + return calculate(model); + } }