{ "cells": [ { "cell_type": "markdown", "id": "7a7ff013", "metadata": {}, "source": [ "# Zadeh operators by using operator overloading" ] }, { "cell_type": "code", "execution_count": 1, "id": "cada993c", "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "id": "aed07c67", "metadata": {}, "source": [ "## Fuzzy value" ] }, { "cell_type": "code", "execution_count": 2, "id": "59aaa0e9", "metadata": {}, "outputs": [], "source": [ "class Zadeh:\n", " \"\"\"\n", " Represents a fuzzy value\n", " \"\"\"\n", "\n", " def __init__(self, value):\n", " self.value = value\n", "\n", " @property\n", " def value(self):\n", " return self._value\n", "\n", " @value.setter\n", " def value(self, value):\n", " if 0 <= value <= 1:\n", " self._value = value\n", " else:\n", " raise ValueError(f'The fuzzy value {value} is invalid!')\n", " \n", " def __and__(self, other):\n", " \"\"\"\n", " T-norm\n", " \"\"\"\n", " if self.value < other.value:\n", " return Zadeh(self.value)\n", " else:\n", " return Zadeh(other.value)\n", "\n", " def __or__(self, other):\n", " \"\"\"\n", " S-norm\n", " \"\"\"\n", " if self.value > other.value:\n", " return Zadeh(self.value)\n", " else:\n", " return Zadeh(other.value)\n", "\n", " def __str__(self):\n", " return f'{self.value}'\n", "\n", " def __repr__(self):\n", " return f'Zadeh({self.value})'\n", "\n", "def not_(z):\n", " \"\"\"\n", " Negation\n", " \"\"\"\n", " return Zadeh(1 - z.value)" ] }, { "cell_type": "markdown", "id": "b94b9098", "metadata": {}, "source": [ "Example" ] }, { "cell_type": "code", "execution_count": 3, "id": "a0b864e6", "metadata": {}, "outputs": [], "source": [ "a = Zadeh(0.37)\n", "b = Zadeh(0.23)\n", "c = Zadeh(0.81)" ] }, { "cell_type": "code", "execution_count": 4, "id": "ea34fdfc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Zadeh(0.18999999999999995)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(a or b) and not_(c)" ] }, { "cell_type": "markdown", "id": "fb89a26a", "metadata": {}, "source": [ "## Membership function" ] }, { "cell_type": "code", "execution_count": 5, "id": "b3865c87", "metadata": {}, "outputs": [], "source": [ "class MembershipFunction:\n", "\n", " def __init__(self, table, value=None):\n", " self._table = table\n", " self.value = value\n", "\n", " def __eq__(self, other):\n", " if other not in self._table:\n", " raise ValueError(f'Undefined language variable! \"{other}\"')\n", " full_table = self._table | {'-inf': -math.inf, '+inf': math.inf}\n", " intervals = sorted(list(full_table.items()), key=lambda item: item[1])\n", " index = None\n", " i = 1\n", " while index is None:\n", " if intervals[i][0] == other:\n", " index = i\n", " i += 1\n", " m = 0\n", " if index == 1 and self.value <= intervals[1][1]:\n", " m = 1\n", " elif index == len(intervals) - 2 and self.value >= intervals[index][1]:\n", " m = 1\n", " elif intervals[index - 1][1] <= self.value < intervals[index][1]:\n", " m = (intervals[index][1] - self.value) / (intervals[index][1] - intervals[index - 1][1])\n", " elif intervals[index][1] <= self.value < intervals[index + 1][1]:\n", " m = (self.value - intervals[index][1]) / (intervals[index + 1][1] - intervals[index][1])\n", " return Zadeh(m)\n", "\n", " @property\n", " def value(self):\n", " return self._value\n", "\n", " @value.setter\n", " def value(self, value):\n", " self._value = value" ] }, { "cell_type": "markdown", "id": "77f5f599", "metadata": {}, "source": [ "Example" ] }, { "cell_type": "code", "execution_count": 6, "id": "576260bc", "metadata": {}, "outputs": [], "source": [ "battery = MembershipFunction({\n", " 'empty': 0,\n", " 'full': 100\n", "})" ] }, { "cell_type": "code", "execution_count": 7, "id": "bd39e9dc", "metadata": {}, "outputs": [], "source": [ "distance = MembershipFunction({\n", " 'close': 0,\n", " 'near': 5,\n", " 'far': 20\n", "})" ] }, { "cell_type": "code", "execution_count": 8, "id": "f53baf2d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.45\n" ] } ], "source": [ "battery.value = 45\n", "distance.value = 12\n", "speed = (battery == 'empty') or (distance == 'close')\n", "print(speed)" ] }, { "cell_type": "code", "execution_count": 9, "id": "22e27f0b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4\n" ] } ], "source": [ "battery.value = 70\n", "distance.value = 3\n", "speed = (battery == 'full') and not_(distance == 'close')\n", "print(speed)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }