import numpy as np import skfuzzy as fuzz from skfuzzy import control as ctrl from django.conf import settings def get_fuzzy_response(request_params, objects_list): # Stworzenie uniwersum 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") # Tworzenie funkcji rozmytych 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"]) # Obliczanie przynaleznosci danego obiektu do podanych danych kwerendy i tworzenie przefiltrowanej listy end_object_list = [] for car in objects_list: comparator = [] 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 comparator and min(comparator) > settings.COMPARATOR: end_object_list.append(car) print(end_object_list) return end_object_list