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

85 lines
4.1 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
def get_fuzzy_response(request_params):
# 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)
# 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])
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'])