add getCompressionTimeSeriesWithFTransform()

This commit is contained in:
Антон Скалкин 2023-04-07 00:12:38 +04:00
parent 1c24fb8055
commit 0cd1e535ad
4 changed files with 62 additions and 6 deletions

View File

@ -2,21 +2,38 @@ package ru.ulstu.estimate;
import org.springframework.stereotype.Service;
import ru.ulstu.datamodel.Model;
import ru.ulstu.datamodel.ModelingResult;
import ru.ulstu.datamodel.exception.ModelingException;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.datamodel.ts.TimeSeriesValue;
import ru.ulstu.method.MethodParamValue;
import ru.ulstu.method.ftransform.FTransform;
import ru.ulstu.score.ScoreMethod;
import ru.ulstu.service.MethodParamBruteForce;
import ru.ulstu.service.TimeSeriesService;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
@Service
public class CompressionMetricService {
private final int DEFAULT_THREAD_COUNT = 50;
private final TimeSeriesService timeSeriesService;
private final FTransform fTransform;
private final MethodParamBruteForce methodParamBruteForce;
private final ScoreMethod scoreMethod;
private final ExecutorService executors = Executors.newFixedThreadPool(DEFAULT_THREAD_COUNT);
public CompressionMetricService(TimeSeriesService timeSeriesService) {
public CompressionMetricService(TimeSeriesService timeSeriesService, FTransform fTransform, MethodParamBruteForce methodParamBruteForce, ScoreMethod scoreMethod) {
this.timeSeriesService = timeSeriesService;
this.fTransform = fTransform;
this.methodParamBruteForce = methodParamBruteForce;
this.scoreMethod = scoreMethod;
}
public void getCompressionMetric() {
@ -33,6 +50,33 @@ public class CompressionMetricService {
return modelingResult.getTimeSeries();
}
protected ModelingResult getCompressionTimeSeriesWithFTransform(TimeSeries timeSeries) throws ExecutionException, InterruptedException {
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));
List<List<MethodParamValue>> availableParametersValues = methodParamBruteForce.getAvailableParametersValues(timeSeries, fTransform.getAvailableParameters());
for (List<MethodParamValue> parametersValues : availableParametersValues) {
results.add(executors.submit(() -> {
Model model = fTransform.getModel(timeSeries, parametersValues);
return new ModelingResult(model.getTimeSeriesModel(),
null,
parametersValues,
scoreMethod.getScore(tsValues, model.getTimeSeriesModel()), fTransform);
}));
}
for (Future<ModelingResult> futureModelingResult : results) {
results2.add(futureModelingResult.get());
}
return results2.stream()
.min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue()))
.orElse(null);
}
}

View File

@ -5,9 +5,11 @@ import ru.ulstu.datamodel.exception.ModelingException;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.datamodel.ts.TimeSeriesValue;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public abstract class ScoreMethod {
private final String name;
@ -16,11 +18,11 @@ public abstract class ScoreMethod {
this.name = name;
}
public Score getScore(Map<LocalDateTime, Double> tsValues, TimeSeries model) throws ModelingException {
public Score getScore(Map<LocalDateTime, Double> tsValues, TimeSeries model) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return new Score(this, evaluate(tsValues, model));
}
public abstract Number evaluate(Map<LocalDateTime, Double> tsValues, TimeSeries model) throws ModelingException;
public abstract Number evaluate(Map<LocalDateTime, Double> tsValues, TimeSeries model) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException;
public String getName() {
return name;

View File

@ -28,7 +28,7 @@ import java.util.concurrent.Future;
import java.util.stream.Collectors;
@Service
class MethodParamBruteForce {
public class MethodParamBruteForce {
private final int DEFAULT_THREAD_COUNT = 50;
private final List<Method> methods;
private final ScoreMethod scoreMethod = new Smape();
@ -175,7 +175,7 @@ class MethodParamBruteForce {
return getSmoothedTimeSeries(timeSeries, methods);
}
private List<List<MethodParamValue>> getAvailableParametersValues(TimeSeries timeSeries, List<MethodParameter> availableParameters) {
public List<List<MethodParamValue>> getAvailableParametersValues(TimeSeries timeSeries, List<MethodParameter> availableParameters) {
List<List<MethodParamValue>> result = new ArrayList<>();
Map<MethodParameter, Integer> parameterOffset = new TreeMap<>();
Map<MethodParameter, List<Number>> parameterValues = new TreeMap<>();

View File

@ -2,8 +2,11 @@ package ru.ulstu.target;
import ru.ulstu.datamodel.exception.ModelingException;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.datamodel.ts.TimeSeriesValue;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ExecutionException;
public abstract class Target {
@ -19,4 +22,11 @@ public abstract class Target {
public abstract TimeSeries calculate(TimeSeries model) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException;
public TimeSeries calculate(Map<LocalDateTime, Double> values) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
TimeSeries model = new TimeSeries();
for (var entry : values.entrySet()) {
model.addValue(new TimeSeriesValue(entry.getKey(), entry.getValue()));
}
return calculate(model);
}
}