3-ftransform-forecasting #5
@ -2,21 +2,38 @@ package ru.ulstu.estimate;
|
|||||||
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.datamodel.Model;
|
||||||
import ru.ulstu.datamodel.ModelingResult;
|
import ru.ulstu.datamodel.ModelingResult;
|
||||||
import ru.ulstu.datamodel.exception.ModelingException;
|
import ru.ulstu.datamodel.exception.ModelingException;
|
||||||
import ru.ulstu.datamodel.ts.TimeSeries;
|
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 ru.ulstu.service.TimeSeriesService;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
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
|
@Service
|
||||||
public class CompressionMetricService {
|
public class CompressionMetricService {
|
||||||
|
|
||||||
|
private final int DEFAULT_THREAD_COUNT = 50;
|
||||||
private final TimeSeriesService timeSeriesService;
|
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.timeSeriesService = timeSeriesService;
|
||||||
|
this.fTransform = fTransform;
|
||||||
|
this.methodParamBruteForce = methodParamBruteForce;
|
||||||
|
this.scoreMethod = scoreMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getCompressionMetric() {
|
public void getCompressionMetric() {
|
||||||
@ -33,6 +50,33 @@ public class CompressionMetricService {
|
|||||||
return modelingResult.getTimeSeries();
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,11 @@ import ru.ulstu.datamodel.exception.ModelingException;
|
|||||||
import ru.ulstu.datamodel.ts.TimeSeries;
|
import ru.ulstu.datamodel.ts.TimeSeries;
|
||||||
import ru.ulstu.datamodel.ts.TimeSeriesValue;
|
import ru.ulstu.datamodel.ts.TimeSeriesValue;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public abstract class ScoreMethod {
|
public abstract class ScoreMethod {
|
||||||
private final String name;
|
private final String name;
|
||||||
@ -16,11 +18,11 @@ public abstract class ScoreMethod {
|
|||||||
this.name = name;
|
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));
|
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() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
|
@ -28,7 +28,7 @@ import java.util.concurrent.Future;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class MethodParamBruteForce {
|
public class MethodParamBruteForce {
|
||||||
private final int DEFAULT_THREAD_COUNT = 50;
|
private final int DEFAULT_THREAD_COUNT = 50;
|
||||||
private final List<Method> methods;
|
private final List<Method> methods;
|
||||||
private final ScoreMethod scoreMethod = new Smape();
|
private final ScoreMethod scoreMethod = new Smape();
|
||||||
@ -175,7 +175,7 @@ class MethodParamBruteForce {
|
|||||||
return getSmoothedTimeSeries(timeSeries, methods);
|
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<>();
|
List<List<MethodParamValue>> result = new ArrayList<>();
|
||||||
Map<MethodParameter, Integer> parameterOffset = new TreeMap<>();
|
Map<MethodParameter, Integer> parameterOffset = new TreeMap<>();
|
||||||
Map<MethodParameter, List<Number>> parameterValues = new TreeMap<>();
|
Map<MethodParameter, List<Number>> parameterValues = new TreeMap<>();
|
||||||
|
@ -2,8 +2,11 @@ package ru.ulstu.target;
|
|||||||
|
|
||||||
import ru.ulstu.datamodel.exception.ModelingException;
|
import ru.ulstu.datamodel.exception.ModelingException;
|
||||||
import ru.ulstu.datamodel.ts.TimeSeries;
|
import ru.ulstu.datamodel.ts.TimeSeries;
|
||||||
|
import ru.ulstu.datamodel.ts.TimeSeriesValue;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public abstract class Target {
|
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 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user