From ce8fd326427edc98eb5b3be8abc683c76f138a2c Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 27 May 2021 14:41:53 +0400 Subject: [PATCH] partial bad code --- .../java/ru/ulstu/models/ModelingResult.java | 29 +++++++++ src/main/java/ru/ulstu/models/Score.java | 21 +++++++ src/main/java/ru/ulstu/score/ScoreMethod.java | 7 +++ .../ulstu/services/MethodParamBruteForce.java | 60 +++++++++++++++++++ .../ru/ulstu/tsMethods/TimeSeriesMethod.java | 11 +++- .../exponential/NoTrendNoSeason.java | 30 +++++++++- .../param/ExponentialMethodParamValue.java | 6 +- 7 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 src/main/java/ru/ulstu/models/ModelingResult.java create mode 100644 src/main/java/ru/ulstu/models/Score.java create mode 100644 src/main/java/ru/ulstu/score/ScoreMethod.java create mode 100644 src/main/java/ru/ulstu/services/MethodParamBruteForce.java diff --git a/src/main/java/ru/ulstu/models/ModelingResult.java b/src/main/java/ru/ulstu/models/ModelingResult.java new file mode 100644 index 0000000..7bd33a8 --- /dev/null +++ b/src/main/java/ru/ulstu/models/ModelingResult.java @@ -0,0 +1,29 @@ +package ru.ulstu.models; + +import ru.ulstu.tsMethods.exponential.param.TimeSeriesMethodParamValue; + +import java.util.List; + +public class ModelingResult { + private final TimeSeries timeSeries; + private final List paramValues; + private final Score score; + + public ModelingResult(TimeSeries timeSeries, List paramValues, Score score) { + this.timeSeries = timeSeries; + this.paramValues = paramValues; + this.score = score; + } + + public TimeSeries getTimeSeries() { + return timeSeries; + } + + public List getParamValues() { + return paramValues; + } + + public Score getScore() { + return score; + } +} diff --git a/src/main/java/ru/ulstu/models/Score.java b/src/main/java/ru/ulstu/models/Score.java new file mode 100644 index 0000000..c48645b --- /dev/null +++ b/src/main/java/ru/ulstu/models/Score.java @@ -0,0 +1,21 @@ +package ru.ulstu.models; + +import ru.ulstu.score.ScoreMethod; + +public class Score { + private final ScoreMethod scoreMethod; + private final Number value; + + public Score(ScoreMethod scoreMethod, Number value) { + this.scoreMethod = scoreMethod; + this.value = value; + } + + public ScoreMethod getScoreMethod() { + return scoreMethod; + } + + public Number getValue() { + return value; + } +} diff --git a/src/main/java/ru/ulstu/score/ScoreMethod.java b/src/main/java/ru/ulstu/score/ScoreMethod.java new file mode 100644 index 0000000..b3d1aad --- /dev/null +++ b/src/main/java/ru/ulstu/score/ScoreMethod.java @@ -0,0 +1,7 @@ +package ru.ulstu.score; + +import ru.ulstu.models.TimeSeries; + +public abstract class ScoreMethod { + public abstract Number evaluate(TimeSeries originalTimeSeries, TimeSeries modelTimeSeries); +} diff --git a/src/main/java/ru/ulstu/services/MethodParamBruteForce.java b/src/main/java/ru/ulstu/services/MethodParamBruteForce.java new file mode 100644 index 0000000..30692dd --- /dev/null +++ b/src/main/java/ru/ulstu/services/MethodParamBruteForce.java @@ -0,0 +1,60 @@ +package ru.ulstu.services; + +import org.springframework.stereotype.Service; +import ru.ulstu.models.ModelingResult; +import ru.ulstu.models.TimeSeries; +import ru.ulstu.tsMethods.TimeSeriesMethod; +import ru.ulstu.tsMethods.exponential.param.TimeSeriesMethodParam; +import ru.ulstu.tsMethods.exponential.param.TimeSeriesMethodParamValue; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +@Service +public class MethodParamBruteForce { + private final List methods; + private ExecutorService executors = Executors.newFixedThreadPool(10); + + public MethodParamBruteForce(List methods) { + this.methods = methods; + } + + public TimeSeries getForecast(TimeSeries timeSeries) { + + } + + public TimeSeries getForecastWithOptimalLength(TimeSeries timeSeries) { + + } + + public TimeSeries getSmoothedTimeSeries(TimeSeries timeSeries) { + List> results = new ArrayList<>(); + for (TimeSeriesMethod method : methods) { + List availableParams = method.getAvailableParams(); + Map> paramsAvailableValues = new HashMap<>(); + for (TimeSeriesMethodParam param : availableParams) { + paramsAvailableValues.put(param, param.getAvailableValues()); + } + List paramValues = new ArrayList<>(); + results.add(executors.submit(() -> { + return new ModelingResult(method.getModel(), + method.createFor(timeSeries) + .setAvailableParams(paramValues) + .getModel()); + })); + } + TimeSeries result = null; + try { + result = results.get(0).get(); + } catch (Exception e) { + e.printStackTrace(); + } + + return result; + } +} diff --git a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java index 1098c9f..2b69512 100644 --- a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java +++ b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java @@ -6,8 +6,11 @@ import ru.ulstu.models.TimeSeriesValue; import ru.ulstu.models.exceptions.ForecastValidateException; import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.models.exceptions.TimeSeriesValidateException; +import ru.ulstu.tsMethods.exponential.param.TimeSeriesMethodParam; +import ru.ulstu.tsMethods.exponential.param.TimeSeriesMethodParamValue; import java.time.temporal.ChronoUnit; +import java.util.List; /** * Наиболее общая логика моделировани и прогнозирования временных рядов @@ -16,9 +19,7 @@ public abstract class TimeSeriesMethod { protected TimeSeries originalTimeSeries; private TimeSeries model; - public TimeSeriesMethod(TimeSeries originalTimeSeries) throws ModelingException { - this.originalTimeSeries = originalTimeSeries; - } + public abstract TimeSeriesMethod createFor(TimeSeries originalTimeSeries); /** * Возвращает модельное представление временного ряда: для тех же точек времени что и в параметре timeSeries @@ -117,4 +118,8 @@ public abstract class TimeSeriesMethod { } return model; } + + public abstract List getAvailableParams(); + + public abstract TimeSeriesMethod setAvailableParams(List params); } diff --git a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java index ef53e91..01130d8 100644 --- a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java +++ b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java @@ -8,22 +8,31 @@ package ru.ulstu.tsMethods.exponential; +import org.springframework.stereotype.Component; import ru.ulstu.models.TimeSeries; import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.tsMethods.TimeSeriesMethod; import ru.ulstu.tsMethods.exponential.param.Alpha; +import ru.ulstu.tsMethods.exponential.param.ExponentialMethodParam; import ru.ulstu.tsMethods.exponential.param.ExponentialMethodParamValue; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +@Component public class NoTrendNoSeason extends TimeSeriesMethod { - private final ExponentialMethodParamValue alpha; + private ExponentialMethodParamValue alpha; private final List sComponent = new ArrayList<>(); - public NoTrendNoSeason(TimeSeries timeSeries, ExponentialMethodParamValue alpha) throws ModelingException { - super(timeSeries); + public NoTrendNoSeason createFor(TimeSeries timeSeries) { + this.originalTimeSeries = timeSeries; + return this; + } + + public NoTrendNoSeason setAlpha(ExponentialMethodParamValue alpha) { this.alpha = alpha; + return this; } @Override @@ -49,4 +58,19 @@ public class NoTrendNoSeason extends TimeSeriesMethod { } return forecast; } + + @Override + public List getAvailableParams() { + return Collections.singletonList(alpha.getParam()); + } + + @Override + public TimeSeriesMethod setAvailableParams(List params) { + for (ExponentialMethodParamValue param : params) { + if (param.getParam() instanceof Alpha) { + alpha.setValue(param.getValue()); + } + } + return this; + } } diff --git a/src/main/java/ru/ulstu/tsMethods/exponential/param/ExponentialMethodParamValue.java b/src/main/java/ru/ulstu/tsMethods/exponential/param/ExponentialMethodParamValue.java index 608b959..fe83a21 100644 --- a/src/main/java/ru/ulstu/tsMethods/exponential/param/ExponentialMethodParamValue.java +++ b/src/main/java/ru/ulstu/tsMethods/exponential/param/ExponentialMethodParamValue.java @@ -10,7 +10,7 @@ package ru.ulstu.tsMethods.exponential.param; public class ExponentialMethodParamValue { private final T param; - private final Number value; + private Number value; public ExponentialMethodParamValue(T param, Number value) { this.param = param; @@ -32,4 +32,8 @@ public class ExponentialMethodParamValue { public int getIntValue() { return value.intValue(); } + + public void setValue(Number value) { + this.value = value; + } }