<commit_before>package com.zuehlke.carrera.javapilot.akka.experimental;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.zuehlke.carrera.javapilot.akka.PowerAction;
import com.zuehlke.carrera.javapilot.services.ExtendedFloatingHistory;
import com.zuehlke.carrera.relayapi.messages.RaceStartMessage;
import com.zuehlke.carrera.relayapi.messages.SensorEvent;
import com.zuehlke.carrera.timeseries.FloatingHistory;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.stream.Collectors;


public class PlayingWithSmoothing extends UntypedActor {
    private final ActorRef marco;
    private int currentPower = 20;

    private int measuringSpeed = 110;
    private double smoothed = 0.0;

    private int smoothing = 160;
    private double lastTimestamp = 0.0;

    private static final int TRACK_PART_LENGTH = 50;
    private static final int TRACK_PART_MIN_LENGTH = 10;
    private static final int LINE_TOP_THRESHOLD = 200;
    private static final int DIRECTION_CHANGED_COUNT = 5;


    private FloatingHistory gzDiffHistory = new FloatingHistory(4);

    private FloatingHistory actualTrackPart = new FloatingHistory(TRACK_PART_LENGTH);
    private FloatingHistory directionChangedHistory = new ExtendedFloatingHistory(DIRECTION_CHANGED_COUNT, 0);

    private int countActualPart;

    public PlayingWithSmoothing(ActorRef pilotActor) {
        this.marco = pilotActor;
    }

    @Override
    public void onReceive(Object message) throws Exception {
        if (message instanceof SensorEvent) {
            handleSensorEvent((SensorEvent) message);
        } else if (message instanceof RaceStartMessage) {
            handleRaceStart();
        }
    }

    private void handleRaceStart() {

    }

    private void handleSensorEvent(SensorEvent message) {
        if (message.getTimeStamp() < lastTimestamp || System.currentTimeMillis() - message.getTimeStamp() > 10) {
            return;
        }
        double gz = message.getG()[2];
        double previousSmoothed = smoothed;

        double smoothValue = lowPassFilter(gz, message.getTimeStamp());
        double smoothDiff = previousSmoothed - smoothValue;

        boolean directionChanged = Math.abs(smoothDiff - gzDiffHistory.currentMean()) > 100;

        if (directionChanged) {
            show((int) smoothValue, "-");
            buildTrackPart(smoothValue, 1);
        } else {
            show((int) smoothValue);
            buildTrackPart(smoothValue, 0);
        }


        gzDiffHistory.shift(smoothDiff);

        if (iAmStillStanding()) {
            increase(5);
            marco.tell(new PowerAction(currentPower), getSelf());
        } else {
            currentPower = measuringSpeed;
            marco.tell(new PowerAction(currentPower), getSelf());
        }
    }

    private void buildTrackPart(double smoothValue, int directionChanged) {
        directionChangedHistory.shift(directionChanged);
        boolean forceEvaluation = false;

        boolean MeanDirectionChanged = directionChangedHistory.currentMean() > 0.5;
        if (MeanDirectionChanged) {
            directionChangedHistory = new ExtendedFloatingHistory(DIRECTION_CHANGED_COUNT, 0);
            forceEvaluation = true;
        }
        evaluateTrackType(smoothValue, forceEvaluation);
    }

    private void evaluateTrackType(double smoothValue, boolean forceEvaluation) {
        actualTrackPart.shift(smoothValue);
        countActualPart++;

        if (countActualPart == TRACK_PART_LENGTH || (forceEvaluation && countActualPart > TRACK_PART_MIN_LENGTH) ) {
            double currentMean = actualTrackPart.currentMean();<commit_msg>evaluation of track parts is nearly working<commit_after>package com.zuehlke.carrera.javapilot.akka.experimental;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.zuehlke.carrera.javapilot.akka.PowerAction;
import com.zuehlke.carrera.javapilot.services.ExtendedFloatingHistory;
import com.zuehlke.carrera.relayapi.messages.RaceStartMessage;
import com.zuehlke.carrera.relayapi.messages.SensorEvent;
import com.zuehlke.carrera.timeseries.FloatingHistory;
import org.apache.commons.lang.StringUtils;


public class PlayingWithSmoothing extends UntypedActor {
    private final ActorRef marco;
    private int currentPower = 20;

    private int measuringSpeed = 110;
    private double smoothed = 0.0;

    private int smoothing = 160;
    private double lastTimestamp = 0.0;

    private static final int TRACK_PART_LENGTH = 50;
    private static final int TRACK_PART_MIN_LENGTH = 5;
    private static final int LINE_TOP_THRESHOLD = 200;
    private static final int DIRECTION_CHANGED_COUNT = 5;


    private FloatingHistory gzDiffHistory = new FloatingHistory(4);

    private FloatingHistory actualTrackPart = new FloatingHistory(TRACK_PART_LENGTH);

    private int countActualPart;
    private  int lastValue = 0;

    public PlayingWithSmoothing(ActorRef pilotActor) {
        this.marco = pilotActor;
    }

    @Override
    public void onReceive(Object message) throws Exception {
        if (message instanceof SensorEvent) {
            handleSensorEvent((SensorEvent) message);
        } else if (message instanceof RaceStartMessage) {
            handleRaceStart();
        }
    }

    private void handleRaceStart() {

    }

    private void handleSensorEvent(SensorEvent message) {
        if (message.getTimeStamp() < lastTimestamp || System.currentTimeMillis() - message.getTimeStamp() > 10) {
            return;
        }
        double gz = message.getG()[2];
        double previousSmoothed = smoothed;

        double smoothValue = lowPassFilter(gz, message.getTimeStamp());
        double smoothDiff = previousSmoothed - smoothValue;

        if (directionChanged(smoothValue)) {
            show((int) smoothValue, "-");
        } else {
            show((int) smoothValue);
        }

        evaluateTrackType(smoothValue, directionChanged(smoothValue));

        gzDiffHistory.shift(smoothDiff);

        if (iAmStillStanding()) {
            increase(5);
            marco.tell(new PowerAction(currentPower), getSelf());
        } else {
            currentPower = measuringSpeed;
            marco.tell(new PowerAction(currentPower), getSelf());
        }
    }

    private boolean directionChanged(double smoothValue) {
        boolean directionChanged = false;
        if (smoothValue > -LINE_TOP_THRESHOLD && smoothValue < LINE_TOP_THRESHOLD) {
            directionChanged = lastValue != 0;
            lastValue = 0;
        } else if (smoothValue > LINE_TOP_THRESHOLD) {
            directionChanged = lastValue != 1;
            lastValue = 1;
        } else if (smoothValue < -LINE_TOP_THRESHOLD) {
            directionChanged = lastValue != -1;
            lastValue = -1;
        }

        return directionChanged;
    }

    private void evaluateTrackType(double smoothValue, boolean forceEvaluation) {
        actualTrackPart.shift(smoothValue);
        countActualPart++;

        if (countActualPart == TRACK_PART_LENGTH || (forceEvaluation && countActualPart > TRACK_PART_MIN_LENGTH) ) {
            double currentMean = actualTrackPart.currentMean();
{'fraction_non_alphanumeric': 0.08316065754938527, 'fraction_numerical': 0.008979140765299074, 'mean_word_length': 3.108967082860386, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501928', 'n_tokens_mistral': 2156, 'n_tokens_neox': 1984, 'n_words': 441}