Systemy-rozmyte-sql/backend/cars/fuzzy_logic.py

49 lines
2.5 KiB
Python
Raw Normal View History

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 08:42:40 +01:00
def get_fuzzy_response(request_params, objects_list):
2022-01-16 16:06:58 +01:00
# 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')
2022-01-23 08:42:40 +01:00
combustion = ctrl.Antecedent(np.arange(4.0, 15.0, 0.1), 'combustion')
2022-01-16 16:06:58 +01:00
# 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])
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 = []
if 'production_year' in request_params:
2022-01-23 12:15:33 +01:00
comparator.append(fuzz.interp_membership(production_year.universe, production_year[str(request_params['production_year'])].mf, car.production_year))
2022-01-23 08:42:40 +01:00
if 'mileage' in request_params:
2022-01-23 12:15:33 +01:00
comparator.append(fuzz.interp_membership(mileage.universe, mileage[str(request_params['mileage'])].mf, car.mileage))
2022-01-23 08:42:40 +01:00
if 'engine_capacity' in request_params:
2022-01-23 12:15:33 +01:00
comparator.append(fuzz.interp_membership(engine_capacity.universe, engine_capacity[str(request_params['engine_capacity'])].mf, car.engine_capacity))
2022-01-23 08:42:40 +01:00
if 'combustion' in request_params:
2022-01-23 12:15:33 +01:00
comparator.append(fuzz.interp_membership(combustion.universe, combustion[str(request_params['combustion'])].mf, car.combustion))
2022-01-23 08:42:40 +01:00
if min(comparator) > 0.75:
2022-01-23 12:15:33 +01:00
end_object_list.append(car)
2022-01-23 08:42:40 +01:00
return end_object_list
2022-01-16 16:06:58 +01:00