Перепиши определение нечеткой логики для распознавание ddos атак. Улучши его чтобы оно лучше работало с реальными атаками 4 уровня модели OSI.
# -----------------------------
# 1. Определение нечеткой логики
# -----------------------------

# Диапазоны значений для переменных
PACKET_RATE_RANGE = np.arange(0, 2001, 1)  # Скорость пакетов (пакетов/с)
PACKET_SIZE_RANGE = np.arange(0, 2001, 1)  # Размер пакета (байт)
SRC_IP_VARIATION_RANGE = np.arange(0, 1001, 1)  # Количество уникальных IP-адресов источников
PROTOCOL_TYPE_RANGE = np.arange(0, 3, 1)  # Тип протокола: 0 - TCP, 1 - UDP, 2 - ICMP
DST_PORT_RANGE = np.arange(0, 65536, 1)  # Порт назначения
PACKET_FLAGS_RANGE = np.arange(0, 6, 1)  # Флаги пакета
CONN_RATE_RANGE = np.arange(0, 1001, 1)  # Скорость соединений (conn/sec)
INCOMPLETE_PERCENT_RANGE = np.arange(0, 101, 1)  # Процент незавершенных соединений (%)
TRAFFIC_VOLUME_RANGE = np.arange(0, 1000001, 1)  # Объем трафика (bytes/sec)
DDOS_RISK_RANGE = np.arange(0, 101, 1)  # Риск DDoS-атаки (%)

# Определение входных переменных
packet_rate = ctrl.Antecedent(PACKET_RATE_RANGE, 'packet_rate')
packet_size = ctrl.Antecedent(PACKET_SIZE_RANGE, 'packet_size')
src_ip_variation = ctrl.Antecedent(SRC_IP_VARIATION_RANGE, 'src_ip_variation')
protocol_type = ctrl.Antecedent(PROTOCOL_TYPE_RANGE, 'protocol_type')
dst_port = ctrl.Antecedent(DST_PORT_RANGE, 'dst_port')
packet_flags = ctrl.Antecedent(PACKET_FLAGS_RANGE, 'packet_flags')
conn_rate = ctrl.Antecedent(CONN_RATE_RANGE, 'conn_rate')
incomplete_percent = ctrl.Antecedent(INCOMPLETE_PERCENT_RANGE, 'incomplete_percent')
traffic_volume = ctrl.Antecedent(TRAFFIC_VOLUME_RANGE, 'traffic_volume')

# Определение выходной переменной
ddos_risk = ctrl.Consequent(DDOS_RISK_RANGE, 'ddos_risk')

# Функции принадлежности для переменных

# packet_rate
packet_rate['low'] = fuzz.trimf(packet_rate.universe, [0, 0, 500])
packet_rate['medium'] = fuzz.trimf(packet_rate.universe, [400, 800, 1200])
packet_rate['high'] = fuzz.trimf(packet_rate.universe, [1000, 2000, 2000])

# packet_size
packet_size['small'] = fuzz.trimf(packet_size.universe, [0, 0, 700])
packet_size['medium'] = fuzz.trimf(packet_size.universe, [600, 1000, 1400])
packet_size['large'] = fuzz.trimf(packet_size.universe, [1300, 2000, 2000])

# src_ip_variation
src_ip_variation['low'] = fuzz.trimf(src_ip_variation.universe, [0, 0, 200])
src_ip_variation['medium'] = fuzz.trimf(src_ip_variation.universe, [150, 400, 650])
src_ip_variation['high'] = fuzz.trimf(src_ip_variation.universe, [600, 1000, 1000])

# protocol_type
protocol_type['tcp'] = fuzz.trimf(protocol_type.universe, [0, 0, 0])
protocol_type['udp'] = fuzz.trimf(protocol_type.universe, [1, 1, 1])
protocol_type['icmp'] = fuzz.trimf(protocol_type.universe, [2, 2, 2])

# dst_port
dst_port['common'] = fuzz.trapmf(dst_port.universe, [0, 0, 1024, 1024])
dst_port['uncommon'] = fuzz.trapmf(dst_port.universe, [1023, 65535, 65535, 65535])

# packet_flags
packet_flags['none'] = fuzz.trimf(packet_flags.universe, [0, 0, 0])
packet_flags['syn'] = fuzz.trimf(packet_flags.universe, [1, 1, 1])
packet_flags['ack'] = fuzz.trimf(packet_flags.universe, [2, 2, 2])
packet_flags['syn_ack'] = fuzz.trimf(packet_flags.universe, [3, 3, 3])
packet_flags['rst'] = fuzz.trimf(packet_flags.universe, [4, 4, 4])
packet_flags['fin'] = fuzz.trimf(packet_flags.universe, [5, 5, 5])

# conn_rate
conn_rate['low'] = fuzz.trimf(conn_rate.universe, [0, 0, 300])
conn_rate['medium'] = fuzz.trimf(conn_rate.universe, [200, 500, 800])
conn_rate['high'] = fuzz.trimf(conn_rate.universe, [600, 1000, 1000])

# incomplete_percent
incomplete_percent['low'] = fuzz.trimf(incomplete_percent.universe, [0, 0, 20])
incomplete_percent['medium'] = fuzz.trimf(incomplete_percent.universe, [10, 50, 90])
incomplete_percent['high'] = fuzz.trimf(incomplete_percent.universe, [80, 100, 100])

# traffic_volume
traffic_volume['low'] = fuzz.trimf(traffic_volume.universe, [0, 0, 200000])
traffic_volume['medium'] = fuzz.trimf(traffic_volume.universe, [100000, 500000, 900000])
traffic_volume['high'] = fuzz.trimf(traffic_volume.universe, [800000, 1000000, 1000000])

# ddos_risk
ddos_risk['low'] = fuzz.trimf(ddos_risk.universe, [0, 0, 30])
ddos_risk['medium'] = fuzz.trimf(ddos_risk.universe, [20, 50, 80])
ddos_risk['high'] = fuzz.trimf(ddos_risk.universe, [70, 100, 100])

# Правила нечеткой логики
rules = [
    ctrl.Rule(packet_rate['low'] & src_ip_variation['low'], ddos_risk['low']),
    ctrl.Rule(packet_rate['medium'] & src_ip_variation['medium'], ddos_risk['medium']),
    ctrl.Rule(packet_rate['high'] | src_ip_variation['high'], ddos_risk['high']),
    ctrl.Rule(packet_size['large'] & packet_rate['high'], ddos_risk['high']),
    ctrl.Rule(packet_size['small'] & packet_rate['high'], ddos_risk['medium']),
    ctrl.Rule(packet_size['small'] & src_ip_variation['high'], ddos_risk['high']),
    ctrl.Rule(packet_rate['medium'] & src_ip_variation['high'], ddos_risk['high']),
    ctrl.Rule(packet_rate['high'] & src_ip_variation['medium'], ddos_risk['high']),
    ctrl.Rule(protocol_type['icmp'], ddos_risk['high']),
    ctrl.Rule(protocol_type['udp'] & packet_rate['high'], ddos_risk['high']),
    ctrl.Rule(dst_port['uncommon'] & packet_rate['high'], ddos_risk['medium']),
    ctrl.Rule(packet_flags['syn'] & packet_rate['high'], ddos_risk['high']),
    ctrl.Rule(packet_flags['rst'] & packet_rate['high'], ddos_risk['medium']),
    ctrl.Rule(packet_flags['fin'] & packet_rate['high'], ddos_risk['medium']),
    ctrl.Rule(incomplete_percent['high'], ddos_risk['high']),
    ctrl.Rule(incomplete_percent['medium'] & conn_rate['high'], ddos_risk['high']),
    ctrl.Rule(incomplete_percent['medium'] & conn_rate['medium'], ddos_risk['medium']),
    ctrl.Rule(incomplete_percent['low'] & conn_rate['low'], ddos_risk['low']),
    ctrl.Rule(traffic_volume['high'] & incomplete_percent['medium'], ddos_risk['medium']),
    ctrl.Rule(traffic_volume['high'] & incomplete_percent['high'], ddos_risk['high']),
    ctrl.Rule(conn_rate['high'] & incomplete_percent['low'], ddos_risk['medium']),
    ctrl.Rule(traffic_volume['low'] & incomplete_percent['low'] & conn_rate['low'], ddos_risk['low'])
]