From 8bcdff3ba15e6a810e2c9b67bc64368764bebd22 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 13 May 2024 00:07:41 +0400 Subject: [PATCH] #96 -- Fix time series generation --- .../generation/service/GenerationService.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/extractor/generation/service/GenerationService.java b/src/main/java/ru/ulstu/extractor/generation/service/GenerationService.java index 43eb10d..0b168cd 100644 --- a/src/main/java/ru/ulstu/extractor/generation/service/GenerationService.java +++ b/src/main/java/ru/ulstu/extractor/generation/service/GenerationService.java @@ -18,6 +18,7 @@ import java.util.List; public class GenerationService { private final TimeSeriesService timeSeriesService; private final BranchService branchService; + private final static double RANDOM_WEIGHT = 0.1; public GenerationService(TimeSeriesService timeSeriesService, BranchService branchService) { @@ -31,10 +32,35 @@ public class GenerationService { Arrays.stream(TimeSeriesType.values()).forEach(tsType -> { List tsValues = new ArrayList<>(); for (int i = 0; i < generationForm.getTsLength(); i++) { - tsValues.add(new TimeSeriesValue(DateUtils.addDays(startDate, i), (double) i)); + tsValues.add(new TimeSeriesValue(DateUtils.addDays(startDate, i), + getNextAdditiveValue(tsValues, generationForm))); } final String tsName = "Генерированный " + tsType.getDescription(); timeSeriesService.save(tsName, branch, tsType, tsValues); }); } + + private double getNextAdditiveValue(List timeSeriesValues, GenerationForm generationForm) { + double newValue; + int maxTryCount = 10; + do { + if (timeSeriesValues.isEmpty()) { + if (generationForm.getBaseTendency() > 0) { + newValue = 0.1 * Math.random() * (generationForm.getMax() - generationForm.getMin()); + } else { + newValue = generationForm.getMax() - 0.1 * Math.random() * (generationForm.getMax() - generationForm.getMin()); + } + } else { + newValue = timeSeriesValues.get(timeSeriesValues.size() - 1).getValue() + + Math.random() * generationForm.getBaseTendency() + * ((generationForm.getMax() - generationForm.getMin()) / generationForm.getTsLength()) + * generationForm.getTendencyDynamic() + + RANDOM_WEIGHT * (Math.random() - 0.5) * (generationForm.getMax() - generationForm.getMin()); + } + maxTryCount--; + } while (((newValue <= generationForm.getMin()) + || (newValue >= generationForm.getMax())) + && (maxTryCount > 0)); + return newValue; + } }