added first fuzzy logic method

This commit is contained in:
s452662 2024-01-21 21:50:20 +01:00
parent 2fff850afc
commit 711ecbe1f7
2 changed files with 117 additions and 38 deletions

View File

@ -45,11 +45,11 @@ if __name__ == "__main__":
df = add_column(df, categorize_shots, "c_home_shots", "home_shots") df = add_column(df, categorize_shots, "c_home_shots", "home_shots")
print(df.columns) print(df.columns)
df = add_column(df, get_points_home(df), "c_home_form") df = add_column(df, get_method(df, True, categorize_points), "c_home_form")
df = add_column(df, get_points_away(df), "c_away_form") df = add_column(df, get_method(df, False, categorize_points), "c_away_form")
df = add_column(df, get_diff_home(df), "c_home_diff") df = add_column(df, get_method(df, True, categorize_diff), "c_home_diff")#categorize_diff
df = add_column(df, get_diff_away(df), "c_away_diff") df = add_column(df, get_method(df, False, categorize_diff), "c_away_diff")
df = generateFuzzyLogicData(df) df = generateFuzzyLogicData(df)
@ -80,3 +80,4 @@ if __name__ == "__main__":
print(calculatePoints(result,'Manchester United')) print(calculatePoints(result,'Manchester United'))
print(calculateGoalDifference(result, 'Manchester United')) print(calculateGoalDifference(result, 'Manchester United'))
print(categorize_fuzzy_passes(450,50))

146
rules.py
View File

@ -1,48 +1,121 @@
import simpful import simpful
from data_filters import * from data_filters import *
import pandas as pd import pandas as pd
'''
def kategoryzuj_strzaly(ilosc_strzalow):
FS = FuzzySystem()
TLV = AutoTriangle(3, terms=['mało', 'średnio', 'dużo'], universe_of_discourse=[0, 25])
FS.add_linguistic_variable("strzaly", TLV)
shots1 = TriangleFuzzySet(0, 0, 6, term='mało') FS = FuzzySystem()
shots2 = TriangleFuzzySet(6, 12, 18, term='średnio')
shots3 = TriangleFuzzySet(12, 25, 25, term='dużo')
FS.add_linguistic_variable("bilans", LinguisticVariable([shots1, shots2, shots3], universe_of_discourse=[0, 25]))
FS.add_rules(["IF strzaly IS mało THEN bilans IS mało", # Dominacja OK
"IF strzaly IS średnio THEN bilans IS średnio", # Jakość strzałów - Witek
"IF strzaly IS dużo THEN bilans IS dużo"]) # Agresesywnosc (zolte + czerwone kartki) - Wojtek
# odbiory i wslizgi (xDef) - Michał, ekspert od xDef
FS.set_variable("strzaly", ilosc_strzalow) # statystyki z calego sezonu - Wojtek
bilans = FS.inference() # 5 ostatnich spotkan miedzy druzynami - Witek
if bilans['bilans'] >= 12: def categorize_fuzzy_passes(passes,possession):
return 2
elif bilans['bilans'] <= 6:
return 0
else:
return 1
def kategorie_strzalow(druzyna, sezon, data, df): FS.set_crisp_output_value("low", 0.0)
ostatnie_spotkania = last5Matches(sezon, druzyna, data, df) FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
shots = [] Pass1 = TriangleFuzzySet(300,300,500, term="low")
for index, spotkanie in ostatnie_spotkania.iterrows(): Pass2 = TriangleFuzzySet(300,450,600, term="average")
if spotkanie['home_team'] == druzyna: Pass3 = TriangleFuzzySet(400,600,600, term="high")
ilosc_strzalow = spotkanie['home_shots']
else: FS.add_linguistic_variable("passes", LinguisticVariable([Pass1, Pass2, Pass3], universe_of_discourse=[0,1000]))
ilosc_strzalow = spotkanie['away_shots']
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)"
])
kategoria = kategoryzuj_strzaly(ilosc_strzalow)
shots.append(kategoria)
ostatnie_spotkania['cat_shots'] = shots
return ostatnie_spotkania
'''
def categorize_shots(shots): def categorize_shots(shots):
if shots >= 12: if shots >= 12:
@ -82,6 +155,11 @@ def categorize_points(data, row, teamHome):
else: else:
return 1 return 1
def get_method(data, home_away, method):
values = []
for index, row in data.iterrows():
values.append(method(data, row, home_away))
return values
def get_points_home(data): def get_points_home(data):
points = [] points = []