From 44fde5c80d8e851892fc4e42582bac39fe8a0391 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 2 Nov 2020 17:04:49 +0400 Subject: [PATCH] #2 -- add params for method --- .../ru/ulstu/services/TimeSeriesService.java | 4 ++- src/main/java/ru/ulstu/tsMethods/Param.java | 5 ---- .../ru/ulstu/tsMethods/TimeSeriesMethod.java | 4 +-- .../exponential/ExponentialMethodParams.java | 26 +++++++++++++++++++ .../exponential/ExponentialParamName.java | 5 ++++ .../exponential/NoTrendNoSeason.java | 17 +++++++----- 6 files changed, 46 insertions(+), 15 deletions(-) delete mode 100644 src/main/java/ru/ulstu/tsMethods/Param.java create mode 100644 src/main/java/ru/ulstu/tsMethods/exponential/ExponentialMethodParams.java create mode 100644 src/main/java/ru/ulstu/tsMethods/exponential/ExponentialParamName.java diff --git a/src/main/java/ru/ulstu/services/TimeSeriesService.java b/src/main/java/ru/ulstu/services/TimeSeriesService.java index 78a8e03..f45b343 100644 --- a/src/main/java/ru/ulstu/services/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/services/TimeSeriesService.java @@ -8,6 +8,8 @@ import ru.ulstu.models.Forecast; import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeriesValue; import ru.ulstu.models.exceptions.ModelingException; +import ru.ulstu.tsMethods.exponential.ExponentialMethodParams; +import ru.ulstu.tsMethods.exponential.ExponentialParamName; import ru.ulstu.tsMethods.exponential.NoTrendNoSeason; import java.time.LocalDateTime; @@ -31,7 +33,7 @@ public class TimeSeriesService { } public Forecast getForecast(TimeSeries timeSeries, int countPoints) throws ModelingException { - NoTrendNoSeason nn = new NoTrendNoSeason(0.8); + NoTrendNoSeason nn = new NoTrendNoSeason(ExponentialMethodParams.of(ExponentialParamName.ALPHA, 0.8)); return nn.getForecast(timeSeries, countPoints); } diff --git a/src/main/java/ru/ulstu/tsMethods/Param.java b/src/main/java/ru/ulstu/tsMethods/Param.java deleted file mode 100644 index f413e76..0000000 --- a/src/main/java/ru/ulstu/tsMethods/Param.java +++ /dev/null @@ -1,5 +0,0 @@ -package ru.ulstu.tsMethods; - -public enum Param { - ALPHA -} diff --git a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java index a6a055c..23adffb 100644 --- a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java +++ b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java @@ -24,7 +24,7 @@ public abstract class TimeSeriesMethod { * @return модель временного ряда * @throws TimeSeriesValidateException */ - public Model getModel(TimeSeries timeSeries) throws TimeSeriesValidateException { + public Model getModel(TimeSeries timeSeries) throws ModelingException { validateTimeSeries(timeSeries); return getModelOfValidTimeSeries(timeSeries); } @@ -36,7 +36,7 @@ public abstract class TimeSeriesMethod { * @param timeSeries исходный временной ряд подлежащий моделированию * @return */ - protected abstract Model getModelOfValidTimeSeries(TimeSeries timeSeries); + protected abstract Model getModelOfValidTimeSeries(TimeSeries timeSeries) throws ModelingException; /** * Выполняет построение прогноза временного ряда. Даты спрогнозированных точек будут сгенерированы по модельным точкам. diff --git a/src/main/java/ru/ulstu/tsMethods/exponential/ExponentialMethodParams.java b/src/main/java/ru/ulstu/tsMethods/exponential/ExponentialMethodParams.java new file mode 100644 index 0000000..e3364e6 --- /dev/null +++ b/src/main/java/ru/ulstu/tsMethods/exponential/ExponentialMethodParams.java @@ -0,0 +1,26 @@ +package ru.ulstu.tsMethods.exponential; + +import com.google.common.collect.ImmutableMap; +import ru.ulstu.models.exceptions.ModelingException; + +import java.util.Map; + +public class ExponentialMethodParams { + Map paramsValues; + + public ExponentialMethodParams(Map paramsValues) { + this.paramsValues = paramsValues; + } + + public Double getValue(ExponentialParamName paramName) throws ModelingException { + if (paramsValues.containsKey(paramName)) { + return paramsValues.get(paramName); + } else { + throw new ModelingException("Неизвестное название параметра для метода экспоненциального сглаживания"); + } + } + + public static ExponentialMethodParams of(ExponentialParamName param1, Double value1) { + return new ExponentialMethodParams(ImmutableMap.of(param1, value1)); + } +} diff --git a/src/main/java/ru/ulstu/tsMethods/exponential/ExponentialParamName.java b/src/main/java/ru/ulstu/tsMethods/exponential/ExponentialParamName.java new file mode 100644 index 0000000..a89272a --- /dev/null +++ b/src/main/java/ru/ulstu/tsMethods/exponential/ExponentialParamName.java @@ -0,0 +1,5 @@ +package ru.ulstu.tsMethods.exponential; + +public enum ExponentialParamName { + ALPHA, BETA, GAMMA +} diff --git a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java index deca8ef..176c65b 100644 --- a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java +++ b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java @@ -3,24 +3,27 @@ package ru.ulstu.tsMethods.exponential; import ru.ulstu.models.Forecast; import ru.ulstu.models.Model; import ru.ulstu.models.TimeSeries; +import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.tsMethods.TimeSeriesMethod; -public class NoTrendNoSeason extends TimeSeriesMethod { - private double alpha; +import static ru.ulstu.tsMethods.exponential.ExponentialParamName.ALPHA; - public NoTrendNoSeason(double alpha) { - this.alpha = alpha; +public class NoTrendNoSeason extends TimeSeriesMethod { + private ExponentialMethodParams exponentialMethodParams; + + public NoTrendNoSeason(ExponentialMethodParams exponentialMethodParams) { + this.exponentialMethodParams = exponentialMethodParams; } @Override - protected Model getModelOfValidTimeSeries(TimeSeries timeSeries) { + protected Model getModelOfValidTimeSeries(TimeSeries timeSeries) throws ModelingException { Model model = new Model(timeSeries); model.addValue(timeSeries.getFirstValue()); //выполняется проход модели по сглаживанию for (int t = 1; t < timeSeries.getValues().size(); t++) { model.addValue(timeSeries.getValues().get(t), - (1 - alpha) * timeSeries.getNumericValue(t) - + alpha * model.getModelTimeSeries().getValues().get(t - 1).getValue()); + (1 - exponentialMethodParams.getValue(ALPHA)) * timeSeries.getNumericValue(t) + + exponentialMethodParams.getValue(ALPHA) * model.getModelTimeSeries().getValues().get(t - 1).getValue()); } return model; }