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
* * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* 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.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@Service
public class MethodParamBruteForce {
private final int DEFAULT_THREAD_COUNT = 10;
private final int DEFAULT_THREAD_COUNT = 50;
private final List<TimeSeriesMethod> methods;
private final ScoreMethod scoreMethod = new Smape();
private final ExecutorService executors = Executors.newFixedThreadPool(DEFAULT_THREAD_COUNT);
@ -47,45 +46,37 @@ public class MethodParamBruteForce {
}
public ModelingResult getSmoothedTimeSeries(TimeSeries timeSeries) throws ModelingException {
//List<Future<ModelingResult>> results = new ArrayList<>();
List<Future<ModelingResult>> results = new ArrayList<>();
List<ModelingResult> results2 = new CopyOnWriteArrayList<>();
try {
for (TimeSeriesMethod method : methods) {
List<List<TimeSeriesMethodParamValue>> availableParametersValues = getAvailableParametersValues(method.getAvailableParameters());
for (List<TimeSeriesMethodParamValue> parametersValues : availableParametersValues) {
/*results.add(executors.submit(() -> {
TimeSeries model = method.createFor(timeSeries)
results.add(executors.submit(() -> {
TimeSeriesMethod methodInstance = method.getClass().getDeclaredConstructor().newInstance();
TimeSeries model = methodInstance.createFor(timeSeries)
.setAvailableParameters(parametersValues)
.getModel();
return new ModelingResult(model, parametersValues, scoreMethod.getScore(timeSeries, model));
}));*/
TimeSeries model = method.createFor(timeSeries)
.setAvailableParameters(parametersValues)
.getModel();
results2.add(new ModelingResult(model,
parametersValues,
scoreMethod.getScore(timeSeries, model),
method));
return new ModelingResult(model, parametersValues, scoreMethod.getScore(timeSeries, model), methodInstance);
}));
}
}
try {
/*results.forEach(modelingResultFuture -> {
results.forEach(modelingResultFuture -> {
try {
modelingResultFuture.get();
} catch (Exception e) {
e.printStackTrace();
}
});
for (Future<ModelingResult> futureModelingResult: results) {
for (Future<ModelingResult> futureModelingResult : results) {
results2.add(futureModelingResult.get());
}*/
}
return results2.stream()
.min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue()))
.orElse(null);
} catch (Exception e) {
e.printStackTrace();
throw new ModelingException(e.getMessage());
}
return null;
}
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) {
parameterOffset.put(parameters.get(i), 0);
i++;
//continue;
continue;
}
if (parameterOffset.get(parameters.get(i)) < parameterValues.get(parameters.get(i)).size()) {
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
* * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
*/
package ru.ulstu.tsMethods.exponential;
import org.springframework.stereotype.Component;
import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.exceptions.ModelingException;
import ru.ulstu.models.exceptions.TimeSeriesValidateException;
@ -24,6 +23,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
public class AddTrendAddSeason extends TimeSeriesMethod {
private ExponentialMethodParamValue<Alpha> alpha = new ExponentialMethodParamValue<>(Alpha.getInstance(), 0.5);
private ExponentialMethodParamValue<Beta> beta = new ExponentialMethodParamValue<>(Beta.getInstance(), 0.5);
@ -107,12 +107,13 @@ public class AddTrendAddSeason extends TimeSeriesMethod {
@Override
protected TimeSeries makeForecast(TimeSeries forecast) throws ModelingException {
TimeSeries model = getModel();
for (int t = 1; t < forecast.getLength(); t++) {
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)
* iComponent.get(t + getModel().getLength() - season.getIntValue()));
* iComponent.get(t + model.getLength() - season.getIntValue()));
}
return forecast;
}

View File

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

View File

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