Commit 0ef8ced7 authored by Sören Henning's avatar Sören Henning

refactored measurements generation

parent b25f232e
package anomalydetection;
import anomalydetection.measurement.Measurement;
import teetime.framework.AbstractProducerStage;
abstract class AbstractMeassurementsGeneratorStage extends AbstractProducerStage<Measurement> {
protected final MeasurementGenerator generator;
protected final long generations;
public AbstractMeassurementsGeneratorStage(final MeasurementGenerator generator, final long generations) {
super();
this.generator = generator;
this.generations = generations;
}
@Override
protected void execute() {
for (int i = 0; i < this.generations; i++) {
sendMeasurement();
}
this.terminateStage();
}
protected abstract void sendMeasurement();
}
...@@ -12,8 +12,16 @@ public class AnomalyDetectionConfiguration extends Configuration { ...@@ -12,8 +12,16 @@ public class AnomalyDetectionConfiguration extends Configuration {
public AnomalyDetectionConfiguration() { public AnomalyDetectionConfiguration() {
// Create the stages // Create the stages
final MeassurementsGeneratorStage generator = new MeassurementsGeneratorStage(x -> 500 * Math.sin(x / 240) + 2000, 250, 0.01, 1000, Duration.ofSeconds(1),
3600); MeasurementGenerator gen = new MeasurementGenerator();
gen.setFunction(x -> 500 * Math.sin(x / 60) + 2000);
gen.setNoise(250);
gen.setAnomalyProbability(0.01);
gen.setAnomayStrength(1000);
gen.setMinStepDistance(Duration.ofMillis(500));
gen.setMinStepDistance(Duration.ofSeconds(2));
final MeasurementGeneratorStage generator = new MeasurementGeneratorStage(gen, 3600);
// final MeassurementsGeneratorStage generator = new MeassurementsGeneratorStage(x -> 500 * Math.sin(x / 60) + 2000, 250, 0.01, 1000, // final MeassurementsGeneratorStage generator = new MeassurementsGeneratorStage(x -> 500 * Math.sin(x / 60) + 2000, 250, 0.01, 1000,
// Duration.ofSeconds(1),3600); // Duration.ofSeconds(1),3600);
// final MeassurementsGeneratorStage generator = new MeassurementsGeneratorStage(x -> 500 * Math.pow(Math.sin(x / 60), 1 / 101) + 2000, 250, 0.01, 1000, // final MeassurementsGeneratorStage generator = new MeassurementsGeneratorStage(x -> 500 * Math.pow(Math.sin(x / 60), 1 / 101) + 2000, 250, 0.01, 1000,
......
package anomalydetection;
import java.time.Duration;
import java.time.Instant;
import java.util.function.Function;
import anomalydetection.measurement.Measurement;
import teetime.framework.AbstractProducerStage;
public class MeassurementsGeneratorStage extends AbstractProducerStage<Measurement> {
private final Function<Double, Double> function;
private final double noise;
private final double anomalyProbability;
private final double anomayStrength;
private final Duration stepDistance;
private final long generations;
public MeassurementsGeneratorStage(final Function<Double, Double> function, final double noise, final double anomalyProbability, final double anomayStrength,
final Duration stepDistance, final long generations) {
this.function = function;
this.noise = noise;
this.anomalyProbability = anomalyProbability;
this.anomayStrength = anomayStrength;
this.stepDistance = stepDistance;
this.generations = generations;
}
@Override
protected void execute() {
final Instant now = Instant.now();
for (int i = 0; i < this.generations; i++) {
final Instant time = now.plus(stepDistance.multipliedBy(i));
final double value = Math.max(0, transformToAnomaly(getFunctionValue(i) + getNoise()));
final Measurement measurement = new Measurement(time, value);
this.outputPort.send(measurement);
}
this.terminateStage();
}
private double getFunctionValue(final int x) {
return this.function.apply((double) x);
}
private double getNoise() {
return (Math.random() * 2 * this.noise) - this.noise;
}
private double transformToAnomaly(final double value) {
if (Math.random() < this.anomalyProbability) {
return value + anomayStrength * (Math.random() < 0.5 ? 1 : -1);
} else {
return value;
}
}
}
package anomalydetection;
import java.time.Duration;
import java.time.Instant;
import java.util.function.Function;
import anomalydetection.measurement.Measurement;
public class MeasurementGenerator {
private Function<Double, Double> function;
private double noise;
private double anomalyProbability;
private double anomayStrength;
private Instant startTime;
private Duration minStepDistance;
private Duration maxStepDistance;
private Instant time;
private int index = 0;
public MeasurementGenerator() {
this.function = x -> 0.0;
this.noise = 0;
this.anomalyProbability = 0;
this.anomayStrength = 0;
this.startTime = Instant.now();
this.minStepDistance = Duration.ofSeconds(1);
this.maxStepDistance = Duration.ofSeconds(1);
this.time = this.startTime;
}
public MeasurementGenerator(final Function<Double, Double> function, final double noise, final double anomalyProbability, final double anomayStrength,
final Instant startTime,
final Duration minStepDistance, final Duration maxStepDistance) {
this.function = function;
this.noise = noise;
this.anomalyProbability = anomalyProbability;
this.anomayStrength = anomayStrength;
this.startTime = startTime;
this.minStepDistance = minStepDistance;
this.maxStepDistance = maxStepDistance;
this.time = this.startTime;
}
public Function<Double, Double> getFunction() {
return function;
}
public void setFunction(final Function<Double, Double> function) {
this.function = function;
}
public double getNoise() {
return noise;
}
public void setNoise(final double noise) {
this.noise = noise;
}
public double getAnomalyProbability() {
return anomalyProbability;
}
public void setAnomalyProbability(final double anomalyProbability) {
this.anomalyProbability = anomalyProbability;
}
public double getAnomayStrength() {
return anomayStrength;
}
public void setAnomayStrength(final double anomayStrength) {
this.anomayStrength = anomayStrength;
}
public Instant getStartTime() {
return startTime;
}
public void setStartTime(final Instant startTime) {
this.startTime = startTime;
}
public void setStepDistance(final Duration stepDistance) {
this.minStepDistance = stepDistance;
this.maxStepDistance = stepDistance;
}
public Duration getMinStepDistance() {
return minStepDistance;
}
public void setMinStepDistance(final Duration minStepDistance) {
this.minStepDistance = minStepDistance;
}
public Duration getMaxStepDistance() {
return maxStepDistance;
}
public void setMaxStepDistance(final Duration maxStepDistance) {
this.maxStepDistance = maxStepDistance;
}
public void reset() {
this.time = startTime;
this.index = 0;
}
public Measurement getNext() {
final double value = Math.max(0, calcFunctionValue() + calcNoise() + calcAnomaly());
final Measurement measurement = new Measurement(this.time, value);
this.index++;
this.time = this.time.plus(calcStepDistance());
return measurement;
}
private double calcFunctionValue() {
return this.function.apply((double) index);
}
private double calcNoise() {
// BETTER Should be the same
// return ((Math.random() * 2) - 1) * this.noise;
return (Math.random() * 2 * this.noise) - this.noise;
}
private double calcAnomaly() {
if (Math.random() < this.anomalyProbability) {
return this.anomayStrength * (Math.random() < 0.5 ? 1 : -1);
} else {
return 0;
}
}
private Duration calcStepDistance() {
final long minMillis = this.minStepDistance.toMillis();
final long maxMillis = this.maxStepDistance.toMillis();
final long randomMillis = minMillis + (long) (Math.random() * ((maxMillis - minMillis) + 1));
return Duration.ofMillis(randomMillis);
}
}
package anomalydetection;
import anomalydetection.measurement.Measurement;
public class MeasurementGeneratorStage extends AbstractMeassurementsGeneratorStage {
public MeasurementGeneratorStage(final MeasurementGenerator generator, final long generations) {
super(generator, generations);
}
@Override
protected void sendMeasurement() {
final Measurement measurement = this.generator.getNext();
this.outputPort.send(measurement);
}
}
package anomalydetection;
import java.time.Duration;
import java.time.Instant;
import anomalydetection.measurement.Measurement;
public class RealTimeMeasurementGeneratorStage extends AbstractMeassurementsGeneratorStage {
private Instant lastTime = null;
public RealTimeMeasurementGeneratorStage(final MeasurementGenerator generator, final long generations) {
super(generator, generations);
}
@Override
protected void sendMeasurement() {
final Measurement measurement = this.generator.getNext();
if (lastTime != null) {
this.sleep(Duration.between(lastTime, measurement.getTime()));
}
this.outputPort.send(measurement);
this.lastTime = measurement.getTime();
}
private void sleep(final Duration delay) {
try {
Thread.sleep(delay.toMillis());
} catch (InterruptedException e) {
this.terminateStage();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment