2022-01-16 16:06:58 +01:00
|
|
|
import numpy as np
|
|
|
|
import skfuzzy as fuzz
|
|
|
|
from skfuzzy import control as ctrl
|
2022-01-23 13:45:56 +01:00
|
|
|
from django.conf import settings
|
2022-01-16 16:06:58 +01:00
|
|
|
|
|
|
|
|
2022-01-23 08:42:40 +01:00
|
|
|
def get_fuzzy_response(request_params, objects_list):
|
2022-01-16 16:06:58 +01:00
|
|
|
|
|
|
|
# Stworzenie uniwersum
|
2022-01-23 13:45:56 +01:00
|
|
|
production_year = ctrl.Antecedent(np.arange(1960, 2022, 1), "production_year")
|
|
|
|
mileage = ctrl.Antecedent(np.arange(0, 500000, 1), "mileage")
|
|
|
|
engine_capacity = ctrl.Antecedent(np.arange(0.7, 8.2, 0.1), "engine_capacity")
|
|
|
|
combustion = ctrl.Antecedent(np.arange(4.0, 15.0, 0.1), "combustion")
|
2022-01-16 16:06:58 +01:00
|
|
|
|
|
|
|
# Tworzenie funkcji rozmytych
|
2022-01-23 13:45:56 +01:00
|
|
|
production_year["low"] = fuzz.trimf(
|
|
|
|
production_year.universe, settings.PRODUCTION_YEAR["low"]
|
|
|
|
)
|
|
|
|
production_year["mid"] = fuzz.trimf(
|
|
|
|
production_year.universe, settings.PRODUCTION_YEAR["mid"]
|
|
|
|
)
|
|
|
|
production_year["high"] = fuzz.trimf(
|
|
|
|
production_year.universe, settings.PRODUCTION_YEAR["high"]
|
|
|
|
)
|
|
|
|
|
|
|
|
mileage["low"] = fuzz.trimf(mileage.universe, settings.MILEAGE["low"])
|
|
|
|
mileage["mid"] = fuzz.trimf(mileage.universe, settings.MILEAGE["mid"])
|
|
|
|
mileage["high"] = fuzz.trimf(mileage.universe, settings.MILEAGE["high"])
|
|
|
|
|
|
|
|
engine_capacity["low"] = fuzz.trimf(
|
|
|
|
engine_capacity.universe, settings.ENGINE_CAPACITY["low"]
|
|
|
|
)
|
|
|
|
engine_capacity["mid"] = fuzz.trimf(
|
|
|
|
engine_capacity.universe, settings.ENGINE_CAPACITY["mid"]
|
|
|
|
)
|
|
|
|
engine_capacity["high"] = fuzz.trimf(
|
|
|
|
engine_capacity.universe, settings.ENGINE_CAPACITY["high"]
|
|
|
|
)
|
|
|
|
|
|
|
|
combustion["low"] = fuzz.trimf(combustion.universe, settings.COMBUSTION["low"])
|
|
|
|
combustion["mid"] = fuzz.trimf(combustion.universe, settings.COMBUSTION["mid"])
|
|
|
|
combustion["high"] = fuzz.trimf(combustion.universe, settings.COMBUSTION["high"])
|
|
|
|
|
2022-01-23 08:42:40 +01:00
|
|
|
# Obliczanie przynaleznosci danego obiektu do podanych danych kwerendy i tworzenie przefiltrowanej listy
|
|
|
|
end_object_list = []
|
2022-01-23 12:15:33 +01:00
|
|
|
for car in objects_list:
|
2022-01-23 08:42:40 +01:00
|
|
|
comparator = []
|
2022-01-23 13:45:56 +01:00
|
|
|
if "production_year" in request_params:
|
|
|
|
comparator.append(
|
|
|
|
fuzz.interp_membership(
|
|
|
|
production_year.universe,
|
|
|
|
production_year[str(request_params["production_year"])].mf,
|
|
|
|
car.production_year,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if "mileage" in request_params:
|
|
|
|
comparator.append(
|
|
|
|
fuzz.interp_membership(
|
|
|
|
mileage.universe,
|
|
|
|
mileage[str(request_params["mileage"])].mf,
|
|
|
|
car.mileage,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if "engine_capacity" in request_params:
|
|
|
|
comparator.append(
|
|
|
|
fuzz.interp_membership(
|
|
|
|
engine_capacity.universe,
|
|
|
|
engine_capacity[str(request_params["engine_capacity"])].mf,
|
|
|
|
car.engine_capacity,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if "combustion" in request_params:
|
|
|
|
comparator.append(
|
|
|
|
fuzz.interp_membership(
|
|
|
|
combustion.universe,
|
|
|
|
combustion[str(request_params["combustion"])].mf,
|
|
|
|
car.combustion,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if min(comparator) > settings.COMPARATOR:
|
2022-01-23 12:15:33 +01:00
|
|
|
end_object_list.append(car)
|
2022-01-23 08:42:40 +01:00
|
|
|
|
2022-01-23 13:45:56 +01:00
|
|
|
return end_object_list
|