Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Soccer.API;
- using Soccer.Commons;
- namespace DefensiveSucces
- {
- public class CentroidHandler
- {
- // class variables
- private MatchData md;
- // Centroide
- List<Tuple<double, double>> centroidHome = new List<Tuple<double, double>>();
- List<Tuple<double, double>> centroidAway = new List<Tuple<double, double>>();
- List<double> centroidPosDiff = new List<double>();
- List<double> centroidVelo = new List<double>();
- List<double> centroidAcc = new List<double>();
- List<double> centroidHomeVelo = new List<double>();
- List<double> centroidAwayVelo = new List<double>();
- List<double> centroidVeloDiff;
- List<double> centroidHomeAcc;
- List<double> centroidAwayAcc;
- List<double> centroidAccDiff;
- public CentroidHandler(MatchData md)
- {
- this.md = md;
- calculateCentroid();
- calculateCentroidSpeed();
- calculateCentroidAcceleration();
- centroidPositionalDifference();
- centroidVeloDifference();
- centroidAccDifference();
- Exporter.writeSimpleListWithTime(centroidPosDiff, "diff.csv", "Frame;m");
- Exporter.writeSimpleListWithTime(MovingAverage(150, centroidVeloDiff.ToArray()).ToList(), "velo.csv", "Frame;v");
- Exporter.writeSimpleListWithTime(centroidAccDiff, "acc.csv", "Frame;a");
- }
- public void centroidVeloDifference()
- {
- List<double> temp = new List<double>();
- for (int i = 0; i < centroidAwayVelo.Count; i++)
- {
- double dif = centroidHomeVelo[i] - centroidAwayVelo[i];
- temp.Add(md.Moments[i].gamesection == GameSection.FirstHalf
- ? dif
- : -1 * dif); // Werte für 2. Spielhälfte umdrehen wegen Richtungswechsel
- }
- //centroidVeloDiff = MovingAverage(150, temp.ToArray()).ToList();
- centroidVeloDiff = temp;
- }
- public void centroidAccDifference()
- {
- List<double> temp = new List<double>();
- for (int i = 0; i < centroidAwayAcc.Count; i++)
- {
- double dif = centroidHomeAcc[i] - centroidAwayAcc[i];
- temp.Add(dif);
- }
- //centroidAccDiff = MovingAverage(50, temp.ToArray()).ToList();
- centroidAccDiff = temp;
- }
- public void calculateCentroid()
- // erstellt die Centroide beider Mannschaften
- {
- for (int i = 0; i < md.Moments.Length; i++)
- // Alle Frames durchgehen
- {
- // Berechnungen Heimmannschaft
- int counter = 0;
- double sum_x = 0;
- double sum_y = 0;
- foreach (Player p in md.Players)
- // Koords für Heimspieler rechnen
- {
- if (p.isOnField(i) && p.ishome && p.playingPosition != "TW")
- {
- counter++;
- sum_x += p.Positions[i].X;
- sum_y += p.Positions[i].Y;
- }
- }
- double cent_x = Math.Round(sum_x / counter, 2);
- double cent_y = Math.Round(sum_y / counter, 2);
- centroidHome.Add(new Tuple<double, double>(cent_x, cent_y));
- // Berechnungen Auswärtsmannschaft
- counter = 0;
- sum_x = 0;
- sum_y = 0;
- foreach (Player p in md.Players)
- // Koords für Heimspieler rechnen
- {
- if (p.isOnField(i) && !p.ishome && p.playingPosition != "TW")
- {
- counter++;
- sum_x += p.Positions[i].X;
- sum_y += p.Positions[i].Y;
- }
- }
- cent_x = Math.Round(sum_x / counter, 2);
- cent_y = Math.Round(sum_y / counter, 2);
- centroidAway.Add(new Tuple<double, double>(cent_x, cent_y));
- }
- }
- public void centroidPositionalDifference()
- // erstellt die X-Differenz zwischen beiden Centroiden
- // immer Home - Away rechnen
- {
- List<double> temp = new List<double>();
- for (int i = 0; i < md.Moments.Length; i++)
- {
- // 1. Half
- if (md.Moments[i].gamesection == GameSection.FirstHalf)
- {
- if (md.Match.playingdirection_home_1half < 0)
- {
- temp.Add(centroidHome[i].Item1 - centroidAway[i].Item1);
- }
- else
- {
- temp.Add(-1 * (centroidHome[i].Item1 - centroidAway[i].Item1));
- }
- }
- else
- {
- if (md.Match.playingdirection_home_1half < 0)
- {
- temp.Add(-1 * (centroidHome[i].Item1 - centroidAway[i].Item1));
- }
- else
- {
- temp.Add(centroidHome[i].Item1 - centroidAway[i].Item1);
- }
- }
- }
- //centroidPosDiff = MovingAverage(150, temp.ToArray()).ToList();
- centroidPosDiff = temp;
- }
- public void calculateCentroidSpeed()
- {
- // LINQ Shortcut um nur das erste Element des Tuples pro Listeneintrag als neue Liste zu erhalten
- List<double> temp_home =
- derivative(centroidHome.Select(t => t.Item1).ToList());
- List<double> temp_away =
- derivative(centroidAway.Select(t => t.Item1).ToList());
- for (int i = 0; i < temp_home.Count; i++)
- {
- if (md.Moments[i].gamesection == GameSection.FirstHalf)
- {
- if (temp_home[i] < 0)
- {
- centroidHomeVelo.Add(temp_home[i]);
- centroidAwayVelo.Add(temp_away[i]);
- }
- }
- else
- {
- if (temp_home[i] > 0)
- {
- centroidHomeVelo.Add(temp_home[i]);
- centroidAwayVelo.Add(temp_away[i]);
- }
- }
- }
- }
- public void calculateCentroidAcceleration()
- {
- // LINQ Shortcut um nur das erste Element des Tuples pro Listeneintrag als neue Liste zu erhalten
- this.centroidHomeAcc =
- derivative(centroidHomeVelo);
- this.centroidAwayAcc =
- derivative(centroidAwayVelo);
- }
- public List<double> derivative(List<double> sourceList)
- // gibt die Ableitung der Liste zurück
- {
- List<double> temp = new List<double>();
- for (int i = 0; i < sourceList.Count - 1; i++)
- {
- temp.Add((sourceList[i] - sourceList[i + 1]) / 0.04);
- }
- return temp;
- }
- private static double[] MovingAverage(int frameSize, double[] data)
- {
- double sum = 0;
- double[] avgPoints = new double[data.Length - frameSize + 1];
- for (int counter = 0; counter <= data.Length - frameSize; counter++)
- {
- int innerLoopCounter = 0;
- int index = counter;
- while (innerLoopCounter < frameSize)
- {
- sum = sum + data[index];
- innerLoopCounter += 1;
- index += 1;
- }
- avgPoints[counter] = sum / frameSize;
- sum = 0;
- }
- return avgPoints;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement