diff --git a/backend/cars/fuzzy_logic.py b/backend/cars/fuzzy_logic.py index 0e5158c..9b859da 100644 --- a/backend/cars/fuzzy_logic.py +++ b/backend/cars/fuzzy_logic.py @@ -3,20 +3,13 @@ import skfuzzy as fuzz from skfuzzy import control as ctrl -def get_fuzzy_response(request_params): +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.Consequent(np.arange(4.0, 15.0, 0.1), 'combustion') - - # Generowanie przykładowych danych dla uniwersum - production_year.automf(3) - mileage.automf(3) - engine_capacity.automf(3) - combustion.automf(3) - + 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]) @@ -34,51 +27,22 @@ def get_fuzzy_response(request_params): 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 i 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, i['production_year'])) + if 'mileage' in request_params: + comparator.append(fuzz.interp_membership(mileage.universe, mileage[str(request_params['mileage'])].mf, i['mileage'])) + if 'engine_capacity' in request_params: + comparator.append(fuzz.interp_membership(engine_capacity.universe, engine_capacity[str(request_params['engine_capacity'])].mf, i['engine_capacity'])) + if 'combustion' in request_params: + comparator.append(fuzz.interp_membership(combustion.universe, combustion[str(request_params['combustion'])].mf, i['combustion'])) + if min(comparator) > 0.75: + end_object_list.append(object) + + return end_object_list - rules = [] - if 'production_year' in request_params: - rules.append(('production_year', request_params['production_year'])) - if 'mileage' in request_params: - rules.append(('mileage', request_params['mileage'])) - if 'engine_capacity' in request_params: - rules.append(('engine_capacity', request_params['engine_capacity'])) - if 'combustion' in request_params: - rules.append(('combustion', request_params['combustion'])) - - - combustion_rule = ctrl.Rule(production_year[request_params['production_year']] & mileage[request_params['mileage']] & engine_capacity[request_params['engine_capacity']], combustion[request_params['combustion']]) - # engine_capacity_rule = ctrl.Rule(production_year[request_params['production_year']] & mileage[request_params['mileage']] & combustion[request_params['combustion']], engine_capacity[request_params['engine_capacity']]) - # production_year_rule = ctrl.Rule(engine_capacity[request_params['engine_capacity']] & mileage[request_params['mileage']] & combustion[request_params['combustion']], production_year[request_params['production_year']]) - # mileage_rule = ctrl.Rule(engine_capacity[request_params['engine_capacity']] & production_year[request_params['production_year']] & combustion[request_params['combustion']], mileage[request_params['mileage']]) - - - combustion_ctrl = ctrl.ControlSystemSimulation(ctrl.ControlSystem([combustion_rule])) - # engine_ctrl = ctrl.ControlSystemSimulation(ctrl.ControlSystem([engine_capacity_rule])) - # production_year_ctrl = ctrl.ControlSystemSimulation(ctrl.ControlSystem([production_year_rule])) - # mileage_ctrl = ctrl.ControlSystemSimulation(ctrl.ControlSystem([mileage_rule])) - - combustion_ctrl.input['production_year'] = 2015 - combustion_ctrl.input['mileage'] = 20000 - combustion_ctrl.input['engine_capacity'] = 1.4 - combustion_ctrl.compute() - print(combustion_ctrl.output['combustion']) - - # engine_ctrl.inputs['production_year'] = 2011 - # engine_ctrl.inputs['mileage'] = 66000 - # engine_ctrl.inputs['combustion'] = 7.3 - # engine_ctrl.compute() - # print(engine_ctrl.output['engine_capacity']) - - # production_year_ctrl.inputs['engine_capacity'] = 8.0 - # production_year_ctrl.inputs['mileage'] = 200000 - # production_year_ctrl.inputs['combustion'] = 12 - # production_year_ctrl.compute() - # print(production_year_ctrl.output['production_year']) - - # mileage_ctrl.inputs['engine_capacity'] = 1.4 - # mileage_ctrl.inputs['production_year'] = 2007 - # mileage_ctrl.inputs['combustion'] = 6.2 - # mileage_ctrl.compute() - # print(mileage_ctrl.output['mileage']) - diff --git a/backend/cars/views.py b/backend/cars/views.py index ae30d1a..2dde8ad 100644 --- a/backend/cars/views.py +++ b/backend/cars/views.py @@ -11,7 +11,7 @@ class CarList(generics.ListAPIView): def list(self, request): values = map_query_params(request.query_params) - response = get_fuzzy_response(values) + response = get_fuzzy_response(values, queryset) print(response) queryset = self.get_queryset() serializer = CarSerializer(queryset, many=True)