SystemyRozmyte/rules.py
2024-01-27 20:16:01 +01:00

237 lines
7.5 KiB
Python

import simpful
from data_filters import *
import pandas as pd
FS = FuzzySystem()
# Dominacja OK
# Jakość strzałów - Witek
# Agresesywnosc (zolte + czerwone kartki) - Wojtek
# odbiory i wslizgi (xDef) - Michał, ekspert od xDef
# statystyki z calego sezonu - Wojtek OK
# 5 ostatnich spotkan miedzy druzynami - Witek
def categorize_fuzzy_passes(passes,possession):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Pass1 = TriangleFuzzySet(300,300,500, term="low")
Pass2 = TriangleFuzzySet(300,450,600, term="average")
Pass3 = TriangleFuzzySet(400,600,600, term="high")
FS.add_linguistic_variable("passes", LinguisticVariable([Pass1, Pass2, Pass3], universe_of_discourse=[0,1000]))
Poss1 = TriangleFuzzySet(30,30,45, term="low")
Poss2 = TriangleFuzzySet(40,50,60, term="average")
Poss3 = TriangleFuzzySet(55,70,70, term="high")
FS.add_linguistic_variable("possession", LinguisticVariable([Poss1, Poss2, Poss3], universe_of_discourse=[0,100]))
#Pass_domination1 = TriangleFuzzySet(2,2,6, term="low")
#Pass_domination2 = TriangleFuzzySet(3,5,7, term="average")
#Pass_domination3 = TriangleFuzzySet(4,8,8, term="high")
#FS.add_linguistic_variable("passes_domination", LinguisticVariable([Pass_domination1, Pass_domination2, Pass_domination3], universe_of_discourse=[0,10]))
FS.add_rules([
"IF (passes IS low) AND (possession IS low) THEN (pass_domination IS low)",
"IF (passes IS high) AND (possession IS high) THEN (pass_domination IS high)",
"IF (passes IS average) AND (possession IS average) THEN (pass_domination IS average)",
"IF (passes IS low) AND (possession IS high) THEN (pass_domination IS average)",
"IF (passes IS high ) AND (possession IS low) THEN (pass_domination IS average)",
"IF (passes IS average) AND (possession IS high) THEN (pass_domination IS high)",
"IF (passes IS high) AND (possession IS average) THEN (pass_domination IS high)",
"IF (passes IS low) AND (possession IS average) THEN (pass_domination IS low)",
"IF (passes IS average) AND (possession IS low) THEN (pass_domination IS average)"
])
FS.set_variable("passes", passes)
FS.set_variable("possession", possession)
pass_domination = FS.inference()
return pass_domination
def categorize_fuzzy_shots(shots_overall, shots_on_target):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Shot_ov1 = TriangleFuzzySet(0,0,5, term="low") #pozmieniać przedziały (nakładają się)
Shot_ov2 = TriangleFuzzySet(5,10,15, term="medium")
Shot_ov3 = TriangleFuzzySet(15,25,25, term="high")
FS.add_linguistic_variable("shots_overall", LinguisticVariable([Shot_ov1, Shot_ov2, Shot_ov3], universe_of_discourse=[0,35]))
Shot_ont1 = TriangleFuzzySet(0,0,2, term="low")
Shot_ont2 = TriangleFuzzySet(2,4,6, term="medium")
Shot_ont3 = TriangleFuzzySet(6,10,10, term="high")
FS.add_linguistic_variable("shots_on_target", LinguisticVariable([Shot_ont1, Shot_ont2, Shot_ont3], universe_of_discourse=[0,15]))
#Qual_of_shots1 = TriangleFuzzySet(0,0,0.3, term="low")
#Qual_of_shots2 = TriangleFuzzySet(0.2,0.5,0.8, term="medium")
#Qual_of_shots3 = TriangleFuzzySet(0.7,1,1, term="high")
#FS.add_linguistic_variable("expected_goals", LinguisticVariable([Qual_of_shots1, Qual_of_shots2, Qual_of_shots3], universe_of_discourse=[0,1]))
FS.add_rules([
"IF (shots_overall IS low) AND (shots_on_target IS low) THEN (quality_of_shots IS low)",
"IF (shots_overall IS high) AND (shots_on_target IS high) THEN (quality_of_shots IS high)",
"IF (shots_overall IS average) AND (shots_on_target IS average) THEN (quality_of_shots IS average)",
"IF (shots_overall IS low) AND (shots_on_target IS high) THEN (quality_of_shots IS high)",
"IF (shots_overall IS high ) AND (shots_on_target IS low) THEN (quality_of_shots IS low)",
"IF (shots_overall IS average) AND (possession IS high) THEN (quality_of_shots IS high)",
"IF (passes IS high) AND (possession IS average) THEN (quality_of_shots IS high)",
"IF (passes IS low) AND (possession IS average) THEN (quality_of_shots IS low)",
"IF (passes IS average) AND (possession IS low) THEN (quality_of_shots IS average)"
])
def categorize_shots(shots):
if shots >= 12:
return 2
elif shots <= 6:
return 0
else:
return 1
def categorize_passes(pass_count):
if pass_count < 400:
return 0 #słabo
elif 400 <= pass_count <= 500:
return 1 #średnio
else:
return 2 #dużo
def categorize_possesion(shots):
if shots >= 56:
return 2
elif shots <= 40:
return 0
else:
return 1
def categorize_points(data, row, teamHome, matches_type):
if teamHome:
data_5 = matches_type(row['season'], row['home_team'], row['date'], data)
points = calculatePoints(data_5,row['home_team'])
else:
data_5 = matches_type(row['season'], row['away_team'], row['date'], data)
points = calculatePoints(data_5,row['away_team'])
if points <=1:
return 0
elif points >=2:
return 2
else:
return 1
def get_method(data, home_away, method, matches_type):
values = []
for index, row in data.iterrows():
values.append(method(data, row, home_away, matches_type))
return values
def get_points_home(data):
points = []
for index, row in data.iterrows():
points.append(categorize_points(data, row, True))
return points
def get_points_away(data):
points = []
for index, row in data.iterrows():
points.append(categorize_points(data, row, False))
return points
def categorize_diff(data, row, teamHome, matches_type):
if teamHome:
data_5 = matches_type(row['season'], row['home_team'], row['date'], data)
diff = calculateGoalDifference(data_5,row['home_team'])
else:
data_5 = matches_type(row['season'], row['away_team'], row['date'], data)
diff = calculateGoalDifference(data_5,row['away_team'])
if diff <=0:
return 0
else:
return 1
def categorize_aggression(data, row, teamHome, matches_type):
if teamHome:
data_5 = matches_type(row['season'], row['home_team'], row['date'], data)
diff = calculateAggression(data_5,row['home_team'])
else:
data_5 = matches_type(row['season'], row['away_team'], row['date'], data)
diff = calculateAggression(data_5,row['away_team'])
return diff
# if diff <=0:
# return 0
# else:
# return 1
def get_diff_home(data):
points = []
for index, row in data.iterrows():
points.append(categorize_diff(data, row, True))
return points
def get_diff_away(data):
points = []
for index, row in data.iterrows():
points.append(categorize_diff(data, row, False))
return points
def add_column(data_frame, transform_function, new_column, existing_column=None):
if existing_column != None:
new_column_values = data_frame[existing_column].apply(transform_function)
data_frame[new_column] = new_column_values
else:
new_column_values = transform_function
data_frame[new_column] = new_column_values
return data_frame
def get_result_list(df, home_team):
results = []
for score in df['result_full']:
results.append(getResult(score,home_team))
return results