Systemy-rozmyte-sql/backend/cars/fuzzy_logic.py
Bartosz Karwacki 0133f5dbae fix response
2022-01-23 12:15:33 +01:00

49 lines
2.5 KiB
Python

import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
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, [1960, 1960, 2001])
production_year['mid'] = fuzz.trimf(production_year.universe, [1960, 2001, 2022])
production_year['high'] = fuzz.trimf(production_year.universe, [2017, 2022, 2022])
mileage['low'] = fuzz.trimf(mileage.universe, [0, 0, 50000])
mileage['mid'] = fuzz.trimf(mileage.universe, [0, 300000, 500000])
mileage['high'] = fuzz.trimf(mileage.universe, [300000, 500000, 500000])
engine_capacity['low'] = fuzz.trimf(engine_capacity.universe, [0.7, 0.7, 1.2])
engine_capacity['mid'] = fuzz.trimf(engine_capacity.universe, [1.2, 2.0, 8.2])
engine_capacity['high'] = fuzz.trimf(engine_capacity.universe, [5.0, 8.2, 8.2])
combustion['low'] = fuzz.trimf(combustion.universe, [4.0, 4.0, 6.0])
combustion['mid'] = fuzz.trimf(combustion.universe, [4.0, 8.0, 15.0])
combustion['high'] = fuzz.trimf(combustion.universe, [8.5, 15.0, 15.0])
# 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 min(comparator) > 0.75:
end_object_list.append(car)
return end_object_list