From b6ead2f8e5752d1fd147fbca34120775440ccb3e Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 22 Apr 2023 12:09:55 +0400 Subject: [PATCH] #10 -- add max smoothed time series --- .../controller/TimeSeriesController.java | 6 ++++ .../ulstu/service/MethodParamBruteForce.java | 31 +++++++++++++++++++ .../ru/ulstu/service/TimeSeriesService.java | 6 +++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/controller/TimeSeriesController.java b/src/main/java/ru/ulstu/controller/TimeSeriesController.java index 1248005..b97a2cb 100644 --- a/src/main/java/ru/ulstu/controller/TimeSeriesController.java +++ b/src/main/java/ru/ulstu/controller/TimeSeriesController.java @@ -91,4 +91,10 @@ public class TimeSeriesController { public ResponseEntity> getGroupedTendencies(@RequestBody List timeSeriesList) { return new ResponseEntity<>(timeSeriesService.getGroupedTendencies(timeSeriesList), HttpStatus.OK); } + + @PostMapping("getMaxSmoothing") + @Operation(description = "Получить максимальное сглаживание временного ряда") + public ResponseEntity getMaxSmoothing(@RequestBody TimeSeries timeSeries) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + return new ResponseEntity<>(timeSeriesService.getMaxSmoothedTimeSeries(timeSeries), HttpStatus.OK); + } } diff --git a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java index c8e76a9..57a31b5 100644 --- a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java +++ b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java @@ -9,6 +9,7 @@ import ru.ulstu.datamodel.ts.TimeSeriesValue; import ru.ulstu.method.Method; import ru.ulstu.method.MethodParamValue; import ru.ulstu.method.MethodParameter; +import ru.ulstu.method.ftransform.FTransform; import ru.ulstu.score.ScoreMethod; import ru.ulstu.score.Smape; @@ -163,6 +164,36 @@ class MethodParamBruteForce { .orElse(null); } + public ModelingResult getMaxSmoothedTimeSeries(TimeSeries timeSeries) throws ExecutionException, InterruptedException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + 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)); + Method method = new FTransform(); + List> availableParameterValues = getAvailableParametersValues(timeSeries, method.getAvailableParameters()); + List parametersValue = availableParameterValues.get(availableParameterValues.size() - 1); + Method methodInstance = method.getClass().getDeclaredConstructor().newInstance(); + if (methodInstance.canMakeModel(timeSeries, parametersValue)) { + results.add(executors.submit(() -> { + Model model = methodInstance.getModel(timeSeries, parametersValue); + return new ModelingResult(model.getTimeSeriesModel(), + null, + parametersValue, + scoreMethod.getScore(tsValues, model.getTimeSeriesModel()), + methodInstance); + })); + + } + for (Future futureModelingResult : results) { + results2.add(futureModelingResult.get()); + } + return results2.stream() + .min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue())) + .orElse(null); + } + public ModelingResult getSmoothedTimeSeries(TimeSeries timeSeries, String methodClassName) throws ExecutionException, InterruptedException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ModelingException { Method method = methods.stream() .filter(m -> m.getClass().getSimpleName().equals(methodClassName)) diff --git a/src/main/java/ru/ulstu/service/TimeSeriesService.java b/src/main/java/ru/ulstu/service/TimeSeriesService.java index d7b4920..dcc1c5b 100644 --- a/src/main/java/ru/ulstu/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/service/TimeSeriesService.java @@ -40,6 +40,10 @@ public class TimeSeriesService { return methodParamBruteForce.getSmoothedTimeSeries(timeSeries, methodClassName); } + public ModelingResult getMaxSmoothedTimeSeries(TimeSeries timeSeries) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { + return methodParamBruteForce.getMaxSmoothedTimeSeries(timeSeries); + } + public List getAvailableMethods() { return methodParamBruteForce.getAvailableMethods(); } @@ -54,7 +58,7 @@ public class TimeSeriesService { public TimeSeries getGroupedTendencies(TimeSeries timeSeries) { try { - timeSeries = smoothTimeSeries(timeSeries, "FTransform").getTimeSeries(); + timeSeries = getMaxSmoothedTimeSeries(timeSeries).getTimeSeries(); } catch (Exception e) { e.printStackTrace(); }