engine update

This commit is contained in:
Mateusz Tylka 2023-01-25 14:39:25 +01:00
parent 62e91b264e
commit 08ee963d86
3 changed files with 60 additions and 57 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

112
engine.py
View File

@ -1,73 +1,73 @@
from simpful import * from simpful import *
FS = FuzzySystem(show_banner=False) def fuzzy_system(release_year_param, runtime_param, seasons_param, genres_param, emotions_param):
# Define fuzzy sets for the variable FS = FuzzySystem(show_banner=False)
# RELEASE_YEAR
release_year_newer = TriangleFuzzySet(-20, -20, 0, term="newer")
release_year_similar = TriangleFuzzySet(-20, 0, 20, term="similar")
release_year_older = TriangleFuzzySet(0, 20, 20, term="older")
FS.add_linguistic_variable("RELEASE_YEAR", # Define fuzzy sets for the variable
LinguisticVariable([release_year_newer, release_year_similar, release_year_older], # RELEASE_YEAR
universe_of_discourse=[-136, 136])) release_year_newer = TriangleFuzzySet(-20, -20, 0, term="newer")
release_year_similar = TriangleFuzzySet(-20, 0, 20, term="similar")
release_year_older = TriangleFuzzySet(0, 20, 20, term="older")
# RUNTIME FS.add_linguistic_variable("RELEASE_YEAR",
runtime_shorter = TriangleFuzzySet(-90, -90, 0, term="shorter") LinguisticVariable([release_year_newer, release_year_similar, release_year_older],
runtime_similar = TriangleFuzzySet(-90, 0, 90, term="similar") universe_of_discourse=[-136, 136]))
runtime_longer = TriangleFuzzySet(0, 90, 90, term="longer")
FS.add_linguistic_variable("RUNTIME", LinguisticVariable([runtime_shorter, runtime_similar, runtime_longer], # RUNTIME
universe_of_discourse=[-476, 476])) runtime_shorter = TriangleFuzzySet(-90, -90, 0, term="shorter")
runtime_similar = TriangleFuzzySet(-90, 0, 90, term="similar")
runtime_longer = TriangleFuzzySet(0, 90, 90, term="longer")
# SEASONS FS.add_linguistic_variable("RUNTIME", LinguisticVariable([runtime_shorter, runtime_similar, runtime_longer],
seasons_less = TriangleFuzzySet(-5, -5, 0, term="less") universe_of_discourse=[-476, 476]))
seasons_similar = TriangleFuzzySet(-5, 0, 5, term="similar")
seasons_more = TriangleFuzzySet(0, 5, 5, term="more")
FS.add_linguistic_variable("SEASONS", LinguisticVariable([seasons_less, seasons_similar, seasons_more], # SEASONS
universe_of_discourse=[-84, 84])) seasons_less = TriangleFuzzySet(-5, -5, 0, term="less")
seasons_similar = TriangleFuzzySet(-5, 0, 5, term="similar")
seasons_more = TriangleFuzzySet(0, 5, 5, term="more")
# GENRES FS.add_linguistic_variable("SEASONS", LinguisticVariable([seasons_less, seasons_similar, seasons_more],
genres_different = TriangleFuzzySet(0, 0, 0.5, term="different") universe_of_discourse=[-84, 84]))
genres_similar = TriangleFuzzySet(0, 0.5, 1, term="similar")
genres_same = TriangleFuzzySet(0.5, 1, 1, term="same")
FS.add_linguistic_variable("GENRES", LinguisticVariable([genres_different, genres_similar, genres_same], # GENRES
universe_of_discourse=[0, 1])) genres_different = TriangleFuzzySet(0, 0, 0.5, term="different")
genres_similar = TriangleFuzzySet(0, 0.5, 1, term="similar")
genres_same = TriangleFuzzySet(0.5, 1, 1, term="same")
# EMOTIONS FS.add_linguistic_variable("GENRES", LinguisticVariable([genres_different, genres_similar, genres_same],
emotions_different = TriangleFuzzySet(0, 0, 0.5, term="different") universe_of_discourse=[0, 1]))
emotions_similar = TriangleFuzzySet(0, 0.5, 1, term="similar")
emotions_same = TriangleFuzzySet(0.5, 1, 1, term="same")
FS.add_linguistic_variable("EMOTIONS", LinguisticVariable([emotions_different, emotions_similar, emotions_same], # EMOTIONS
universe_of_discourse=[0, 1])) emotions_different = TriangleFuzzySet(0, 0, 0.5, term="different")
emotions_similar = TriangleFuzzySet(0, 0.5, 1, term="similar")
emotions_same = TriangleFuzzySet(0.5, 1, 1, term="same")
# RECOMMENDATION FS.add_linguistic_variable("EMOTIONS", LinguisticVariable([emotions_different, emotions_similar, emotions_same],
low_recommendation = TriangleFuzzySet(0, 0, 50, term="low_recommendation") universe_of_discourse=[0, 1]))
medium_recommendation = TriangleFuzzySet(0, 50, 100, term="medium_recommendation")
high_recommendation = TriangleFuzzySet(0, 100, 100, term="high_recommendation")
FS.add_linguistic_variable("RECOMMENDATION",
LinguisticVariable([low_recommendation, medium_recommendation, high_recommendation],
universe_of_discourse=[0, 200]))
# RULES # RECOMMENDATION
RULE1 = "IF (RELEASE_YEAR IS older) AND (RUNTIME IS longer) AND (SEASONS IS more) THEN (RECOMMENDATION IS low_recommendation)" low_recommendation = TriangleFuzzySet(0, 0, 50, term="low_recommendation")
RULE2 = "IF (EMOTIONS IS different) AND (GENRES IS different) THEN (RECOMMENDATION IS low_recommendation)" medium_recommendation = TriangleFuzzySet(0, 50, 100, term="medium_recommendation")
RULE3 = "IF (RELEASE_YEAR IS newer) AND (RUNTIME IS similar) AND (SEASONS IS less) THEN (RECOMMENDATION IS medium_recommendation)" high_recommendation = TriangleFuzzySet(0, 100, 100, term="high_recommendation")
RULE4 = "IF (EMOTIONS IS similar) AND (GENRES IS similar) THEN (RECOMMENDATION IS medium_recommendation)" FS.add_linguistic_variable("RECOMMENDATION",
RULE5 = "IF (RELEASE_YEAR IS similar) AND (RUNTIME IS similar) AND (SEASONS IS similar) AND (EMOTIONS IS same) AND (GENRES IS same) THEN (RECOMMENDATION IS high_recommendation)" LinguisticVariable([low_recommendation, medium_recommendation, high_recommendation],
# Z regułami trzeba eksperymentować, można porównywać ze scorem dla sprawdzania skuteczności universe_of_discourse=[0, 200]))
FS.add_rules([RULE1, RULE2, RULE3, RULE4, RULE5]) # # RULES
# RULE1 = "IF (RELEASE_YEAR IS older) AND (RUNTIME IS longer) AND (SEASONS IS more) THEN (RECOMMENDATION IS low_recommendation)"
# RULE2 = "IF (EMOTIONS IS different) AND (GENRES IS different) THEN (RECOMMENDATION IS low_recommendation)"
# RULE3 = "IF (RELEASE_YEAR IS newer) AND (RUNTIME IS similar) AND (SEASONS IS less) THEN (RECOMMENDATION IS medium_recommendation)"
# RULE4 = "IF (EMOTIONS IS similar) AND (GENRES IS similar) THEN (RECOMMENDATION IS medium_recommendation)"
# RULE5 = "IF (RELEASE_YEAR IS similar) AND (RUNTIME IS similar) AND (SEASONS IS similar) AND (EMOTIONS IS same) AND (GENRES IS same) THEN (RECOMMENDATION IS high_recommendation)"
# # Z regułami trzeba eksperymentować, można porównywać ze scorem dla sprawdzania skuteczności
# FS.set_variable("RELEASE_YEAR", -12.0) # RULES
# FS.set_variable("RUNTIME", -10.0) RULE1 = f"IF (NOT (RELEASE_YEAR IS {release_year_param})) AND (NOT (RUNTIME IS {runtime_param})) AND (NOT (SEASONS IS {seasons_param})) THEN (RECOMMENDATION IS low_recommendation)"
# FS.set_variable("SEASONS", -2.0) RULE2 = f"IF (NOT (EMOTIONS IS {emotions_param})) AND (NOT (GENRES IS {genres_param})) THEN (RECOMMENDATION IS low_recommendation)"
# FS.set_variable("GENRES", 50.0) RULE3 = f"IF (NOT (RELEASE_YEAR IS {release_year_param})) AND (RUNTIME IS {runtime_param}) AND (NOT (SEASONS IS {seasons_param})) THEN (RECOMMENDATION IS medium_recommendation)"
# FS.set_variable("EMOTIONS", 1.0) RULE4 = f"IF (EMOTIONS IS {emotions_param}) AND (GENRES IS {genres_param}) THEN (RECOMMENDATION IS medium_recommendation)"
# RULE5 = f"IF (RELEASE_YEAR IS {release_year_param}) AND (RUNTIME IS {runtime_param}) AND (SEASONS IS {seasons_param}) AND (EMOTIONS IS {emotions_param}) AND (GENRES IS {genres_param}) THEN (RECOMMENDATION IS high_recommendation)"
# print(FS.inference(["RECOMMENDATION"])) FS.add_rules([RULE1, RULE2, RULE3, RULE4, RULE5])
# FS.produce_figure(outputfile='visualize_terms.pdf') return FS

View File

@ -16,7 +16,7 @@ from fastapi import FastAPI
from scipy.spatial.distance import cosine from scipy.spatial.distance import cosine
from sklearn.preprocessing import MultiLabelBinarizer from sklearn.preprocessing import MultiLabelBinarizer
from engine import FS from engine import fuzzy_system
app = FastAPI() app = FastAPI()
data = pd.DataFrame() data = pd.DataFrame()
@ -28,6 +28,8 @@ def inference(first: pandas.core.series.Series, second_id: str, df=None):
else: else:
second = data.loc[second_id] second = data.loc[second_id]
FS = fuzzy_system(release_year_param='similar', runtime_param='similar', seasons_param='similar', genres_param='same', emotions_param='same')
year_diff = int(first['release_year'] - second['release_year']) year_diff = int(first['release_year'] - second['release_year'])
FS.set_variable('RELEASE_YEAR', year_diff) FS.set_variable('RELEASE_YEAR', year_diff)