Merge pull request 'max smoothing' (#11) from 10-max-smoothing into master

Reviewed-on: #11
This commit is contained in:
romanov73 2023-04-22 12:10:15 +04:00
commit 9a99f2fa8e
3 changed files with 42 additions and 1 deletions

View File

@ -91,4 +91,10 @@ public class TimeSeriesController {
public ResponseEntity<List<TimeSeries>> getGroupedTendencies(@RequestBody List<TimeSeries> timeSeriesList) {
return new ResponseEntity<>(timeSeriesService.getGroupedTendencies(timeSeriesList), HttpStatus.OK);
}
@PostMapping("getMaxSmoothing")
@Operation(description = "Получить максимальное сглаживание временного ряда")
public ResponseEntity<ModelingResult> getMaxSmoothing(@RequestBody TimeSeries timeSeries) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return new ResponseEntity<>(timeSeriesService.getMaxSmoothedTimeSeries(timeSeries), HttpStatus.OK);
}
}

View File

@ -9,6 +9,7 @@ import ru.ulstu.datamodel.ts.TimeSeriesValue;
import ru.ulstu.method.Method;
import ru.ulstu.method.MethodParamValue;
import ru.ulstu.method.MethodParameter;
import ru.ulstu.method.ftransform.FTransform;
import ru.ulstu.score.ScoreMethod;
import ru.ulstu.score.Smape;
@ -163,6 +164,36 @@ class MethodParamBruteForce {
.orElse(null);
}
public ModelingResult getMaxSmoothedTimeSeries(TimeSeries timeSeries) throws ExecutionException, InterruptedException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
List<Future<ModelingResult>> results = new ArrayList<>();
List<ModelingResult> results2 = new CopyOnWriteArrayList<>();
Map<LocalDateTime, Double> tsValues = timeSeries.getValues().stream()
.collect(Collectors.toMap(TimeSeriesValue::getDate, TimeSeriesValue::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
Method method = new FTransform();
List<List<MethodParamValue>> availableParameterValues = getAvailableParametersValues(timeSeries, method.getAvailableParameters());
List<MethodParamValue> parametersValue = availableParameterValues.get(availableParameterValues.size() - 1);
Method methodInstance = method.getClass().getDeclaredConstructor().newInstance();
if (methodInstance.canMakeModel(timeSeries, parametersValue)) {
results.add(executors.submit(() -> {
Model model = methodInstance.getModel(timeSeries, parametersValue);
return new ModelingResult(model.getTimeSeriesModel(),
null,
parametersValue,
scoreMethod.getScore(tsValues, model.getTimeSeriesModel()),
methodInstance);
}));
}
for (Future<ModelingResult> futureModelingResult : results) {
results2.add(futureModelingResult.get());
}
return results2.stream()
.min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue()))
.orElse(null);
}
public ModelingResult getSmoothedTimeSeries(TimeSeries timeSeries, String methodClassName) throws ExecutionException, InterruptedException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ModelingException {
Method method = methods.stream()
.filter(m -> m.getClass().getSimpleName().equals(methodClassName))

View File

@ -40,6 +40,10 @@ public class TimeSeriesService {
return methodParamBruteForce.getSmoothedTimeSeries(timeSeries, methodClassName);
}
public ModelingResult getMaxSmoothedTimeSeries(TimeSeries timeSeries) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException {
return methodParamBruteForce.getMaxSmoothedTimeSeries(timeSeries);
}
public List<Method> getAvailableMethods() {
return methodParamBruteForce.getAvailableMethods();
}
@ -54,7 +58,7 @@ public class TimeSeriesService {
public TimeSeries getGroupedTendencies(TimeSeries timeSeries) {
try {
timeSeries = smoothTimeSeries(timeSeries, "FTransform").getTimeSeries();
timeSeries = getMaxSmoothedTimeSeries(timeSeries).getTimeSeries();
} catch (Exception e) {
e.printStackTrace();
}