fuzzy-logic-movies/engine.py

74 lines
3.6 KiB
Python
Raw Normal View History

2022-12-17 16:47:45 +01:00
from simpful import *
2023-01-09 19:50:09 +01:00
FS = FuzzySystem(show_banner=False)
2022-12-17 16:47:45 +01:00
2023-01-07 15:21:05 +01:00
# Define fuzzy sets for the variable
2022-12-17 16:47:45 +01:00
# RELEASE_YEAR
2023-01-07 22:35:00 +01:00
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")
2023-01-03 13:45:59 +01:00
2023-01-07 15:21:05 +01:00
FS.add_linguistic_variable("RELEASE_YEAR",
LinguisticVariable([release_year_newer, release_year_similar, release_year_older],
universe_of_discourse=[-136, 136]))
2022-12-17 16:47:45 +01:00
# RUNTIME
2023-01-07 22:35:00 +01:00
runtime_shorter = TriangleFuzzySet(-90, -90, 0, term="shorter")
runtime_similar = TriangleFuzzySet(-90, 0, 90, term="similar")
runtime_longer = TriangleFuzzySet(0, 90, 90, term="longer")
2023-01-03 13:45:59 +01:00
2023-01-07 15:21:05 +01:00
FS.add_linguistic_variable("RUNTIME", LinguisticVariable([runtime_shorter, runtime_similar, runtime_longer],
universe_of_discourse=[-476, 476]))
2022-12-17 16:47:45 +01:00
# SEASONS
2023-01-07 22:35:00 +01:00
seasons_less = TriangleFuzzySet(-5, -5, 0, term="less")
seasons_similar = TriangleFuzzySet(-5, 0, 5, term="similar")
seasons_more = TriangleFuzzySet(0, 5, 5, term="more")
2023-01-03 13:45:59 +01:00
2023-01-07 15:21:05 +01:00
FS.add_linguistic_variable("SEASONS", LinguisticVariable([seasons_less, seasons_similar, seasons_more],
universe_of_discourse=[-84, 84]))
2022-12-17 16:47:45 +01:00
# GENRES
2023-01-07 22:35:00 +01:00
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")
2023-01-03 13:45:59 +01:00
2023-01-07 15:21:05 +01:00
FS.add_linguistic_variable("GENRES", LinguisticVariable([genres_different, genres_similar, genres_same],
2023-01-07 22:35:00 +01:00
universe_of_discourse=[0, 1]))
2022-12-17 16:47:45 +01:00
# EMOTIONS
2023-01-07 22:35:00 +01:00
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")
2023-01-03 13:45:59 +01:00
2023-01-07 15:21:05 +01:00
FS.add_linguistic_variable("EMOTIONS", LinguisticVariable([emotions_different, emotions_similar, emotions_same],
2023-01-07 22:35:00 +01:00
universe_of_discourse=[0, 1]))
2022-12-17 16:47:45 +01:00
2023-01-07 15:21:05 +01:00
# RECOMMENDATION
low_recommendation = TriangleFuzzySet(0, 0, 50, term="low_recommendation")
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]))
2022-12-17 16:47:45 +01:00
2023-01-04 17:23:30 +01:00
# RULES
2023-01-07 15:21:05 +01:00
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)"
2023-01-04 14:09:37 +01:00
# Z regułami trzeba eksperymentować, można porównywać ze scorem dla sprawdzania skuteczności
2022-12-17 16:47:45 +01:00
FS.add_rules([RULE1, RULE2, RULE3, RULE4, RULE5])
2023-01-07 15:21:05 +01:00
# FS.set_variable("RELEASE_YEAR", -12.0)
# FS.set_variable("RUNTIME", -10.0)
# FS.set_variable("SEASONS", -2.0)
# FS.set_variable("GENRES", 50.0)
# FS.set_variable("EMOTIONS", 1.0)
#
# print(FS.inference(["RECOMMENDATION"]))
2022-12-17 16:47:45 +01:00
2023-01-07 15:21:05 +01:00
# FS.produce_figure(outputfile='visualize_terms.pdf')