From 93331fc4b604b1203eea3a5dbc653b2aa815b233 Mon Sep 17 00:00:00 2001 From: Piotr Kopycki Date: Wed, 5 Jan 2022 11:54:54 +0100 Subject: [PATCH] Saving result as csv, upgraded program structure, visualisation implemented --- main.py | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- res.csv | 54 ---------------- res3.csv | 55 +++++++++++++++++ 3 files changed, 232 insertions(+), 61 deletions(-) delete mode 100644 res.csv create mode 100644 res3.csv diff --git a/main.py b/main.py index 0b1a566..7e56ad7 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,9 @@ import numpy as np import skfuzzy as fuzz +from skfuzzy import control as ctrl import matplotlib.pyplot as plt from read_data import get_leagues_data +import pandas as pd # https://pythonhosted.org/scikit-fuzzy/auto_examples/plot_tipping_problem.html @@ -157,7 +159,7 @@ def defineRules(): global teamsCfL_min_level, teamsCfL_mid_level, teamsCfL_max_level global score_tragic, score_bad, score_moderate, score_good, score_best, unv_score - # OR + # OR zmienić na AND (bo Or wybija wysokie wartości) active_rule1 = np.fmax(clubs_some_level, np.fmax(clubs_few_level, np.fmax(goals_few_level, @@ -239,18 +241,170 @@ def defineRules(): score_activation = fuzz.interp_membership(unv_score, aggregated_rules, score) # for plot return score_activation +class LeagueController: + clubs = None + goals = None + avgValue = None + players = None + teamsChL = None + teamsEuL = None + teamsCfL = None + score = None + + rule1 = None + rule2 = None + rule3 = None + rule4 = None + rule5 = None + + league_ctrl = None + league_system = None + + def __init__(self): + # Universes for variables + self.clubs = ctrl.Antecedent(np.arange(2, 31, 1), 'clubs') + self.goals = ctrl.Antecedent(np.arange(0.99, 5.01, 0.01), 'goals') + self.avgValue = ctrl.Antecedent(np.arange(0.01, 500.01, 0.01), 'avgValue') + self.players = ctrl.Antecedent(np.arange(0, 101, 1), 'players') + self.teamsChL = ctrl.Antecedent(np.arange(0, 6, 1), 'teamsChL') + self.teamsEuL = ctrl.Antecedent(np.arange(0, 6, 1), 'teamsEuL') + self.teamsCfL = ctrl.Antecedent(np.arange(0, 6, 1), 'teamsCfL') + # Result score universum + self.score = ctrl.Consequent(np.arange(0, 101, 1), 'score') + + # Membership Functions + # trimf - triangular function + # trapmf - trapezoidal function + self.clubs['few'] = fuzz.trapmf(self.clubs.universe, [2, 2, 5, 8]) + self.clubs['some'] = fuzz.trapmf(self.clubs.universe, [3, 6, 9, 12]) + self.clubs['several'] = fuzz.trapmf(self.clubs.universe, [8, 11, 15, 18]) + self.clubs['many'] = fuzz.trapmf(self.clubs.universe, [15, 18, 30, 30]) + + self.goals['few'] = fuzz.trapmf(self.goals.universe, [1.0, 1.0, 2.0, 3.0]) + self.goals['moderate'] = fuzz.trimf(self.goals.universe, [2.5, 3.0, 3.5]) + self.goals['many'] = fuzz.trapmf(self.goals.universe, [3.0, 3.5, 5.0, 5.0]) + + self.avgValue['vpoor'] = fuzz.trapmf(self.avgValue.universe, [0.01, 0.01, 1.0, 2.0]) + self.avgValue['poor'] = fuzz.trimf(self.avgValue.universe, [1.0, 5.0, 15.0]) + self.avgValue['mpoor'] = fuzz.trimf(self.avgValue.universe, [7.5, 20.0, 50.0]) + self.avgValue['moderate'] = fuzz.trimf(self.avgValue.universe, [20.0, 50.0, 100.0]) + self.avgValue['mrich'] = fuzz.trimf(self.avgValue.universe, [50.0, 100.0, 175.0]) + self.avgValue['rich'] = fuzz.trimf(self.avgValue.universe, [150.0, 200.0, 275.0]) + self.avgValue['vrich'] = fuzz.trapmf(self.avgValue.universe, [200.0, 300.0, 500.0, 500.0]) + + self.players['none'] = fuzz.trimf(self.players.universe, [0, 0, 1]) + self.players['few'] = fuzz.trapmf(self.players.universe, [1, 5, 10, 15]) + self.players['moderate'] = fuzz.trapmf(self.players.universe, [10, 15, 20, 25]) + self.players['many'] = fuzz.trapmf(self.players.universe, [20, 25, 100, 100]) + + self.teamsChL['min'] = fuzz.trapmf(self.teamsChL.universe, [0, 0, 1, 2]) + self.teamsChL['mid'] = fuzz.trapmf(self.teamsChL.universe, [1, 2, 3, 4]) + self.teamsChL['max'] = fuzz.trapmf(self.teamsChL.universe, [3, 4, 5, 5]) + + self.teamsEuL['min'] = fuzz.trimf(self.teamsEuL.universe, [0, 0, 1]) + self.teamsEuL['mid'] = fuzz.trimf(self.teamsEuL.universe, [0, 1, 2]) + self.teamsEuL['max'] = fuzz.trapmf(self.teamsEuL.universe, [1, 2, 5, 5]) + + self.teamsCfL['min'] = fuzz.trapmf(self.teamsCfL.universe, [0, 0, 1, 2]) + self.teamsCfL['mid'] = fuzz.trimf(self.teamsCfL.universe, [1, 2, 3]) + self.teamsCfL['max'] = fuzz.trapmf(self.teamsCfL.universe, [2, 3, 5, 5]) + + self.score['tragic'] = fuzz.trapmf(self.score.universe, [0, 0, 15, 25]) + self.score['bad'] = fuzz.trapmf(self.score.universe, [15, 25, 35, 45]) + self.score['moderate'] = fuzz.trapmf(self.score.universe, [35, 45, 55, 65]) + self.score['good'] = fuzz.trapmf(self.score.universe, [55, 65, 75, 85]) + self.score['best'] = fuzz.trapmf(self.score.universe, [75, 85, 100, 100]) + + # FuzzyRules + self.rule1 = ctrl.Rule(self.clubs['some'] | self.clubs['few'] | self.goals['few'] | self.avgValue['mpoor'] | self.avgValue['poor'] | self.avgValue['vpoor'] | self.players['none'] | self.teamsChL['min'] | self.teamsEuL['min'] | self.teamsCfL['max'] | self.teamsCfL['mid'], self.score['tragic']) + self.rule2 = ctrl.Rule(self.clubs['several'] | self.clubs['some'] | self.clubs['few'] | self.goals['moderate'] | self.goals['few'] | self.avgValue['moderate'] | self.avgValue['mpoor'] | self.avgValue['poor'] | self.players['few'] | self.players['none'] | self.teamsChL['min'] | self.teamsEuL['min'] | self.teamsCfL['max'] | self.teamsCfL['mid'], self.score['bad']) + self.rule3 = ctrl.Rule(self.clubs['several'] | self.clubs['some'] | self.goals['moderate'] | self.avgValue['mrich'] | self.avgValue['moderate'] | self.avgValue['mpoor'] | self.players['moderate'] | self.players['few'] | self.teamsChL['mid'] | self.teamsChL['min'] | self.teamsEuL['mid'] | self.teamsEuL['min'] | self.teamsCfL['max'] | self.teamsCfL['mid'], self.score['moderate']) + self.rule4 = ctrl.Rule(self.clubs['many'] | self.clubs['several'] | self.goals['many'] | self.goals['moderate'] | self.avgValue['vrich'] | self.avgValue['rich'] | self.avgValue['mrich'] | self.players['many'] | self.players['moderate'] | self.teamsChL['mid'] | self.teamsEuL['mid'] | self.teamsCfL['mid'], self.score['good']) + self.rule5 = ctrl.Rule(self.clubs['many'] | self.goals['many'] | self.avgValue['vrich'] | self.players['many'] | self.teamsChL['max'] | self.teamsEuL['max'] | self.teamsCfL['min'], self.score['best']) + + self.league_ctrl = ctrl.ControlSystem([self.rule1, self.rule2, self.rule3, self.rule4, self.rule5]) + self.league_system = ctrl.ControlSystemSimulation(self.league_ctrl) + + def getScore(self, values): + self.league_system.input['clubs'] = values['clubs'] + self.league_system.input['goals'] = values['goalsPerMatch'] + self.league_system.input['avgValue'] = values['avgMarketValueInMln'] + self.league_system.input['players'] = values['famousPlayers'] + self.league_system.input['teamsChL'] = values['teamsInChampionsLeague'] + self.league_system.input['teamsEuL'] = values['teamsInEuropaLeague'] + self.league_system.input['teamsCfL'] = values['teamsInConferenceLeague'] + + self.league_system.compute() + + return self.league_system.output['score'] + + #score.view(sim=league_system) + + def displayMembershipFunctionPlot(self, function_id): + if function_id == 0: + self.score.view() + elif function_id == 1: + self.clubs.view() + elif function_id == 2: + self.goals.view() + elif function_id == 3: + self.avgValue.view() + elif function_id == 4: + self.players.view() + elif function_id == 5: + self.teamsChL.view() + elif function_id == 6: + self.teamsEuL.view() + elif function_id == 7: + self.teamsCfL.view() + + def displayRulePlot(self, rule_id): + if rule_id == 1: + self.rule1.view() + elif rule_id == 2: + self.rule2.view() + elif rule_id == 3: + self.rule3.view() + elif rule_id == 4: + self.rule4.view() + elif rule_id == 5: + self.rule5.view() + + def displayControllerResultPlot(self): + self.score.view(sim=self.league_system) + + if __name__ == "__main__": - initFuzzyMembershipFunctions() + league_name_col = [] + league_score_col = [] + # initFuzzyMembershipFunctions() + controller = LeagueController() data_source = None + function_rule_result = None + function_id = None + rule_id = None + view_plot = None + data_save = None while not(data_source == '1' or data_source == '2'): data_source = input('Wpisz 1 by wczytać dane z pliku lub 2 by wpisać dane z konsoli:\n') if data_source == '1': leagues_data = get_leagues_data('leagues.csv') for league in leagues_data: - activateFuzzyMembershipFunctions(league) - result = defineRules() + #activateFuzzyMembershipFunctions(league) + #result = defineRules() + result = controller.getScore(league) print(f"{league['leagueName']} score is: {result}") + league_name_col.append(league['leagueName']) + league_score_col.append(result) + data_save = input('Wpisz 1 by zapisać dane do pliku lub dowolny klawisz żeby kontynuować:\n') + if data_save == '1': + file_name = input('Podaj nazwę pliku:\n') + file_name += ".csv" + d = {'League':league_name_col,'Score':league_score_col} + df = pd.DataFrame(d) + df.to_csv(file_name, index=False) + print('Wyniki zostały zapisane w pliku csv w folderze programu') else: league = {'leagueName': input('Podaj nazwę ligi:\n'), 'country': input('Podaj kraj ligi:\n'), 'clubs': input('Podaj liczbę klubów grających w lidze:\n'), @@ -260,8 +414,24 @@ if __name__ == "__main__": 'teamsInEuropaLeague': input('Podaj liczbę klubów grających w Lidze Europy:\n'), 'teamsInConferenceLeague': input('Podaj liczbę klubów grających w Lidze Konferencji:\n'), 'famousPlayers': input('Podaj liczbę sławnych piłkarzy:\n')} - activateFuzzyMembershipFunctions(league) - result = defineRules() + # activateFuzzyMembershipFunctions(league) + # result = defineRules() + result = controller.getScore(league) print(f"{league['leagueName']} score is: {result}") - + view_plot = input('Wpisz 1 jeżeli chcesz obejrzeć wykresy funkcji i reguł dla ostatnio sprawdzanej ligi lub dowolny klawisz żeby kontynuować:\n') + if view_plot == '1': + while (function_rule_result != '0'): + function_rule_result = input('Wybierz rodzaj wykresu:\n1 - wykresy funkcji przynależności\n2 - wykresy reguł\n3 - wykres wyniku kontrolera\n0 - skończ wizualizację\n') + if function_rule_result == "1": + while (function_id != '8'): + function_id = input('Wybierz funkcje przynależności:\n0 - funkcje punktów atrakcyjności ligi\n1 - funkcje klubów w lidze\n2 - funkcje średniej ilości golów\n3 - funkcje średniej wartości rynkowej\n4 - funkcje ilości słynnych zawodników\n5 - funkcje drużyn w lidze mistrzów\n6 - funkcję drużyn w lidze europy\n7 - funkcję drużyn w lidze konfederacji\n8 - powrót do poprzedniego menu\n') + if function_id == "0" or function_id == "1" or function_id == "2" or function_id == "3" or function_id == "4" or function_id == "5" or function_id == "6" or function_id == "7": + controller.displayMembershipFunctionPlot(int(function_id)) + elif function_rule_result == "2": + while (rule_id != '6'): + rule_id = input('Wybierz regułę:\n1 - reguła dla tragicznych lig\n2 - reguła dla słabych lig\n3 - reguła dla przeciętnych lig\n4 - reguła dla dobrych lig\n5 - reguła dla najlepszych lig\n6 - powrót do poprzedniego menu\n') + if rule_id == "1" or rule_id == "2" or rule_id == "3" or rule_id == "4" or rule_id == "5": + controller.displayRulePlot(int(rule_id)) + elif function_rule_result == "3": + controller.displayControllerResultPlot() #plt.show() \ No newline at end of file diff --git a/res.csv b/res.csv deleted file mode 100644 index 7949b7d..0000000 --- a/res.csv +++ /dev/null @@ -1,54 +0,0 @@ -Premier League score is, 1.0 -LaLiga score is, 1.0 -Serie A score is, 1.0 -Bundesliga score is, 1.0 -Ligue 1 score is, 0.6926977372958362 -Liga Bwin score is, 1.0 -Eredivisie score is, 1.0 -Bundesliga score is, 0.5057471264367834 -Premiership score is, 0.5057471264367834 -Premier Liga score is, 0.8734130372900716 -Premier Liga score is, 0.8734130372900716 -Super liga Srbije score is, 0.7140096618357546 -Jupiler Pro League score is, 1.0 -Super League score is, 1.0 -Protathlima Cyta score is, 0.5057471264367834 -1.HNL score is, 0.5403828197945835 -Fortuna Liga score is, 0.8734130372900716 -Super Lig score is, 1.0 -Super League 1 score is, 0.5057471264367834 -Superligaen score is, 0.5057471264367834 -Eliteserien score is, 0.8434674958254214 -Ligat ha'Al score is, 0.5057471264367834 -Allsvenskan score is, 0.7140096618357546 -efbet Liga score is, 0.5057471264367834 -Premyer Liqa score is, 0.97034078807242 -Liga 1 score is, 0.7140096618357546 -Ekstraklasa score is, 1.0 -NB I. score is, 0.9248363528865283 -Fortuna Liga score is, 0.5057471264367834 -Premier Liga score is, 0.5057471264367834 -Prva Liga score is, 0.6908378245957848 -Vysheyshaya Liga score is, 0.7140096618357546 -Divizia Nationala score is, 0.8463231347289373 -A Lyga score is, 0.6908378245957848 -Premijer Liga score is, 0.5057471264367834 -Veikkausliiga score is, 0.5057471264367834 -BGL Ligue score is, 1.0 -Virsliga score is, 0.5321408915795232 -BKT Superliga e Kosovës score is, 0.5057471264367834 -Premier Division score is, 0.6908378245957848 -Premiership score is, 0.5057471264367834 -Kategoria Superiore score is, 0.6908378245957848 -Bardzragujn khumb score is, 0.6908378245957848 -Betri-deildin score is, 1.0 -Premium Liiga score is, 1.0 -Premier League score is, 0.5057471264367834 -Crystalbet Erovnuli Liga score is, 0.6908378245957848 -Prva liga score is, 0.5057471264367834 -Cymru Premier score is, 0.5057471264367834 -Gibraltar National League score is, 1.0 -Pepsi Max deild score is, 0.5057471264367834 -1. CFL score is, 0.6908378245957848 -Primera Divisió score is, 0.5057471264367834 -Camp. Sammarinese score is, 0.5057471264367834 \ No newline at end of file diff --git a/res3.csv b/res3.csv new file mode 100644 index 0000000..3528d48 --- /dev/null +++ b/res3.csv @@ -0,0 +1,55 @@ +League,Score +Premier League,61.025119126510795 +LaLiga,56.15575447570333 +Serie A,60.40382819794585 +Bundesliga,60.30366482401773 +Ligue 1,58.07302262704164 +Liga Bwin,50.0 +Eredivisie,50.0 +Bundesliga,40.057471264367834 +Premiership,40.057471264367834 +Premier Liga,43.734130372900715 +Premier Liga,43.734130372900715 +Super liga Srbije,42.14850588590407 +Jupiler Pro League,50.0 +Super League,45.29147855530481 +Protathlima Cyta,40.057471264367834 +1.HNL,39.596171802054165 +Fortuna Liga,43.734130372900715 +Super Lig,50.0 +Super League 1,40.057471264367834 +Superligaen,40.057471264367834 +Eliteserien,43.44018053492688 +Ligat ha'Al,40.057471264367834 +Allsvenskan,42.14850588590407 +efbet Liga,40.057471264367834 +Premyer Liqa,35.30248317573896 +Liga 1,42.14850588590407 +Ekstraklasa,50.0 +NB I.,44.24836352886529 +Fortuna Liga,40.057471264367834 +Premier Liga,40.057471264367834 +Prva Liga,38.10204081632655 +Vysheyshaya Liga,42.14850588590407 +Divizia Nationala,43.47289117836451 +A Lyga,38.10204081632655 +Premijer Liga,40.057471264367834 +Veikkausliiga,40.057471264367834 +BGL Ligue,47.32523094688215 +Virsliga,40.33120598591547 +BKT Superliga e Kosovës,40.057471264367834 +Premier Division,38.10204081632655 +Premiership,40.057471264367834 +Kategoria Superiore,38.10204081632655 +Bardzragujn khumb,38.10204081632655 +Betri-deildin,50.0 +Premium Liiga,46.40917191914804 +Premier League,40.057471264367834 +Crystalbet Erovnuli Liga,38.10204081632655 +Prva liga,40.057471264367834 +Cymru Premier,40.057471264367834 +Gibraltar National League,50.0 +Pepsi Max deild,40.057471264367834 +1. CFL,38.10204081632655 +Primera Divisió,40.057471264367834 +Camp. Sammarinese,40.057471264367834