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")
print(df.columns)
df = add_column(df, get_points_home(df), "c_home_form")
df = add_column(df, get_points_away(df), "c_away_form")
df = add_column(df, get_method(df, True, categorize_points), "c_home_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_diff_away(df), "c_away_diff")
df = add_column(df, get_method(df, True, categorize_diff), "c_home_diff")#categorize_diff
df = add_column(df, get_method(df, False, categorize_diff), "c_away_diff")
df = generateFuzzyLogicData(df)
@ -80,3 +80,4 @@ if __name__ == "__main__":
print(calculatePoints(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
from data_filters import *
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')
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",
"IF strzaly IS średnio THEN bilans IS średnio",
"IF strzaly IS dużo THEN bilans IS dużo"])
FS.set_variable("strzaly", ilosc_strzalow)
bilans = FS.inference()
if bilans['bilans'] >= 12:
return 2
elif bilans['bilans'] <= 6:
return 0
else:
return 1
# Dominacja OK
# Jakość strzałów - Witek
# Agresesywnosc (zolte + czerwone kartki) - Wojtek
# odbiory i wslizgi (xDef) - Michał, ekspert od xDef
# statystyki z calego sezonu - Wojtek
# 5 ostatnich spotkan miedzy druzynami - Witek
def categorize_fuzzy_passes(passes,possession):
def kategorie_strzalow(druzyna, sezon, data, df):
ostatnie_spotkania = last5Matches(sezon, druzyna, data, df)
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
shots = []
for index, spotkanie in ostatnie_spotkania.iterrows():
if spotkanie['home_team'] == druzyna:
ilosc_strzalow = spotkanie['home_shots']
else:
ilosc_strzalow = spotkanie['away_shots']
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)"
])
kategoria = kategoryzuj_strzaly(ilosc_strzalow)
shots.append(kategoria)
ostatnie_spotkania['cat_shots'] = shots
return ostatnie_spotkania
'''
def categorize_shots(shots):
if shots >= 12:
@ -82,6 +155,11 @@ def categorize_points(data, row, teamHome):
else:
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):
points = []