Merge pull request 'fix_fuzzy_logic' (#1) from fix_fuzzy_logic into master

Reviewed-on: #1
This commit is contained in:
Bartosz Karwacki 2022-01-23 12:16:50 +01:00
commit f75f3fded1
5 changed files with 19232 additions and 69 deletions

View File

@ -1,3 +1,5 @@
from django.contrib import admin from django.contrib import admin
from .models import Car
admin.site.register(Car)
# Register your models here. # Register your models here.

View File

@ -3,20 +3,13 @@ import skfuzzy as fuzz
from skfuzzy import control as ctrl from skfuzzy import control as ctrl
def get_fuzzy_response(request_params): def get_fuzzy_response(request_params, objects_list):
# Stworzenie uniwersum # Stworzenie uniwersum
production_year = ctrl.Antecedent(np.arange(1960, 2022, 1), 'production_year') production_year = ctrl.Antecedent(np.arange(1960, 2022, 1), 'production_year')
mileage = ctrl.Antecedent(np.arange(0, 500000, 1), 'mileage') mileage = ctrl.Antecedent(np.arange(0, 500000, 1), 'mileage')
engine_capacity = ctrl.Antecedent(np.arange(0.7, 8.2, 0.1), 'engine_capacity') 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') combustion = ctrl.Antecedent(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)
# Tworzenie funkcji rozmytych # Tworzenie funkcji rozmytych
production_year['low'] = fuzz.trimf(production_year.universe, [1960, 1960, 2001]) production_year['low'] = fuzz.trimf(production_year.universe, [1960, 1960, 2001])
@ -35,50 +28,21 @@ def get_fuzzy_response(request_params):
combustion['mid'] = fuzz.trimf(combustion.universe, [4.0, 8.0, 15.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]) combustion['high'] = fuzz.trimf(combustion.universe, [8.5, 15.0, 15.0])
# Obliczanie przynaleznosci danego obiektu do podanych danych kwerendy i tworzenie przefiltrowanej listy
rules = [] end_object_list = []
for car in objects_list:
comparator = []
if 'production_year' in request_params: if 'production_year' in request_params:
rules.append(('production_year', request_params['production_year'])) comparator.append(fuzz.interp_membership(production_year.universe, production_year[str(request_params['production_year'])].mf, car.production_year))
if 'mileage' in request_params: if 'mileage' in request_params:
rules.append(('mileage', request_params['mileage'])) comparator.append(fuzz.interp_membership(mileage.universe, mileage[str(request_params['mileage'])].mf, car.mileage))
if 'engine_capacity' in request_params: if 'engine_capacity' in request_params:
rules.append(('engine_capacity', request_params['engine_capacity'])) comparator.append(fuzz.interp_membership(engine_capacity.universe, engine_capacity[str(request_params['engine_capacity'])].mf, car.engine_capacity))
if 'combustion' in request_params: if 'combustion' in request_params:
rules.append(('combustion', request_params['combustion'])) 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
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'])

View File

@ -11,8 +11,6 @@ class CarList(generics.ListAPIView):
def list(self, request): def list(self, request):
values = map_query_params(request.query_params) values = map_query_params(request.query_params)
response = get_fuzzy_response(values) response = get_fuzzy_response(values, self.get_queryset())
print(response) serializer = CarSerializer(response, many=True)
queryset = self.get_queryset()
serializer = CarSerializer(queryset, many=True)
return Response(serializer.data) return Response(serializer.data)

19216
frontend/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,3 +5,6 @@ django-environ==0.4.5
django-filter==21.1 django-filter==21.1
djangorestframework==3.12.4 djangorestframework==3.12.4
psycopg2-binary==2.9.1 psycopg2-binary==2.9.1
numpy==1.22.1
scikit-fuzzy==0.4.2
matplotlib==3.5.1