fix season

This commit is contained in:
Anton Romanov 2021-05-28 16:16:53 +04:00
parent 65fa782b10
commit 50fb79670d
4 changed files with 36 additions and 47 deletions

View File

@ -1,8 +1,6 @@
/* /*
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
* * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
* *
*/ */
@ -26,10 +24,11 @@ import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@Service @Service
public class MethodParamBruteForce { public class MethodParamBruteForce {
private final int DEFAULT_THREAD_COUNT = 10; private final int DEFAULT_THREAD_COUNT = 50;
private final List<TimeSeriesMethod> methods; private final List<TimeSeriesMethod> methods;
private final ScoreMethod scoreMethod = new Smape(); private final ScoreMethod scoreMethod = new Smape();
private final ExecutorService executors = Executors.newFixedThreadPool(DEFAULT_THREAD_COUNT); private final ExecutorService executors = Executors.newFixedThreadPool(DEFAULT_THREAD_COUNT);
@ -47,29 +46,22 @@ public class MethodParamBruteForce {
} }
public ModelingResult getSmoothedTimeSeries(TimeSeries timeSeries) throws ModelingException { public ModelingResult getSmoothedTimeSeries(TimeSeries timeSeries) throws ModelingException {
//List<Future<ModelingResult>> results = new ArrayList<>(); List<Future<ModelingResult>> results = new ArrayList<>();
List<ModelingResult> results2 = new CopyOnWriteArrayList<>(); List<ModelingResult> results2 = new CopyOnWriteArrayList<>();
try {
for (TimeSeriesMethod method : methods) { for (TimeSeriesMethod method : methods) {
List<List<TimeSeriesMethodParamValue>> availableParametersValues = getAvailableParametersValues(method.getAvailableParameters()); List<List<TimeSeriesMethodParamValue>> availableParametersValues = getAvailableParametersValues(method.getAvailableParameters());
for (List<TimeSeriesMethodParamValue> parametersValues : availableParametersValues) { for (List<TimeSeriesMethodParamValue> parametersValues : availableParametersValues) {
/*results.add(executors.submit(() -> { results.add(executors.submit(() -> {
TimeSeries model = method.createFor(timeSeries) TimeSeriesMethod methodInstance = method.getClass().getDeclaredConstructor().newInstance();
TimeSeries model = methodInstance.createFor(timeSeries)
.setAvailableParameters(parametersValues) .setAvailableParameters(parametersValues)
.getModel(); .getModel();
return new ModelingResult(model, parametersValues, scoreMethod.getScore(timeSeries, model)); return new ModelingResult(model, parametersValues, scoreMethod.getScore(timeSeries, model), methodInstance);
}));*/ }));
TimeSeries model = method.createFor(timeSeries)
.setAvailableParameters(parametersValues)
.getModel();
results2.add(new ModelingResult(model,
parametersValues,
scoreMethod.getScore(timeSeries, model),
method));
} }
} }
try { results.forEach(modelingResultFuture -> {
/*results.forEach(modelingResultFuture -> {
try { try {
modelingResultFuture.get(); modelingResultFuture.get();
} catch (Exception e) { } catch (Exception e) {
@ -78,14 +70,13 @@ public class MethodParamBruteForce {
}); });
for (Future<ModelingResult> futureModelingResult : results) { for (Future<ModelingResult> futureModelingResult : results) {
results2.add(futureModelingResult.get()); results2.add(futureModelingResult.get());
}*/ }
return results2.stream() return results2.stream()
.min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue())) .min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue()))
.orElse(null); .orElse(null);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); throw new ModelingException(e.getMessage());
} }
return null;
} }
private List<List<TimeSeriesMethodParamValue>> getAvailableParametersValues(List<MethodParameter> availableParameters) { private List<List<TimeSeriesMethodParamValue>> getAvailableParametersValues(List<MethodParameter> availableParameters) {
@ -116,7 +107,7 @@ public class MethodParamBruteForce {
if (parameterOffset.get(parameters.get(i)) == parameterValues.get(parameters.get(i)).size() - 1) { if (parameterOffset.get(parameters.get(i)) == parameterValues.get(parameters.get(i)).size() - 1) {
parameterOffset.put(parameters.get(i), 0); parameterOffset.put(parameters.get(i), 0);
i++; i++;
//continue; continue;
} }
if (parameterOffset.get(parameters.get(i)) < parameterValues.get(parameters.get(i)).size()) { if (parameterOffset.get(parameters.get(i)) < parameterValues.get(parameters.get(i)).size()) {
parameterOffset.put(parameters.get(i), parameterOffset.get(parameters.get(i)) + 1); parameterOffset.put(parameters.get(i), parameterOffset.get(parameters.get(i)) + 1);

View File

@ -1,13 +1,12 @@
/* /*
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
* * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
* *
*/ */
package ru.ulstu.tsMethods.exponential; package ru.ulstu.tsMethods.exponential;
import org.springframework.stereotype.Component;
import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.models.exceptions.ModelingException;
import ru.ulstu.models.exceptions.TimeSeriesValidateException; import ru.ulstu.models.exceptions.TimeSeriesValidateException;
@ -24,6 +23,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@Component
public class AddTrendAddSeason extends TimeSeriesMethod { public class AddTrendAddSeason extends TimeSeriesMethod {
private ExponentialMethodParamValue<Alpha> alpha = new ExponentialMethodParamValue<>(Alpha.getInstance(), 0.5); private ExponentialMethodParamValue<Alpha> alpha = new ExponentialMethodParamValue<>(Alpha.getInstance(), 0.5);
private ExponentialMethodParamValue<Beta> beta = new ExponentialMethodParamValue<>(Beta.getInstance(), 0.5); private ExponentialMethodParamValue<Beta> beta = new ExponentialMethodParamValue<>(Beta.getInstance(), 0.5);
@ -107,12 +107,13 @@ public class AddTrendAddSeason extends TimeSeriesMethod {
@Override @Override
protected TimeSeries makeForecast(TimeSeries forecast) throws ModelingException { protected TimeSeries makeForecast(TimeSeries forecast) throws ModelingException {
TimeSeries model = getModel();
for (int t = 1; t < forecast.getLength(); t++) { for (int t = 1; t < forecast.getLength(); t++) {
iComponent.add(gamma.getDoubleValue() * forecast.getNumericValue(t - 1) / sComponent.get(sComponent.size() - 1) iComponent.add(gamma.getDoubleValue() * forecast.getNumericValue(t - 1) / sComponent.get(sComponent.size() - 1)
+ (1 - gamma.getDoubleValue()) * iComponent.get(t + getModel().getLength() - season.getIntValue())); + (1 - gamma.getDoubleValue()) * iComponent.get(t + model.getLength() - season.getIntValue()));
forecast.getValues().get(t).setValue((sComponent.get(sComponent.size() - 1) + tComponent.get(tComponent.size() - 1) * t) forecast.getValues().get(t).setValue((sComponent.get(sComponent.size() - 1) + tComponent.get(tComponent.size() - 1) * t)
* iComponent.get(t + getModel().getLength() - season.getIntValue())); * iComponent.get(t + model.getLength() - season.getIntValue()));
} }
return forecast; return forecast;
} }

View File

@ -1,13 +1,12 @@
/* /*
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
* * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
* *
*/ */
package ru.ulstu.tsMethods.exponential; package ru.ulstu.tsMethods.exponential;
import org.springframework.stereotype.Component;
import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeries;
import ru.ulstu.tsMethods.MethodParameter; import ru.ulstu.tsMethods.MethodParameter;
import ru.ulstu.tsMethods.TimeSeriesMethod; import ru.ulstu.tsMethods.TimeSeriesMethod;
@ -19,7 +18,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@Component
public class NoTrendNoSeason extends TimeSeriesMethod { public class NoTrendNoSeason extends TimeSeriesMethod {
private ExponentialMethodParamValue<Alpha> alpha = new ExponentialMethodParamValue<>(Alpha.getInstance(), 0.5); private ExponentialMethodParamValue<Alpha> alpha = new ExponentialMethodParamValue<>(Alpha.getInstance(), 0.5);
private final List<Double> sComponent = new ArrayList<>(); private final List<Double> sComponent = new ArrayList<>();

View File

@ -1,8 +1,6 @@
/* /*
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved
* * Copyright (C) 2021 Anton Romanov - All Rights Reserved * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
* * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
* *
*/ */
@ -12,7 +10,7 @@ public class Season extends ExponentialMethodParameter {
private final static int DEFAULT_SEASON_OPTIMIZATION_STEP = 1; private final static int DEFAULT_SEASON_OPTIMIZATION_STEP = 1;
public Season() { public Season() {
super("Сезонность", 0, 12, DEFAULT_SEASON_OPTIMIZATION_STEP); super("Сезонность", 1, 12, DEFAULT_SEASON_OPTIMIZATION_STEP);
} }
public static Season getInstance() { public static Season getInstance() {