{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from skfuzzy import control as ctrl\n",
    "\n",
    "level = ctrl.Antecedent(np.arange(1.5, 9.0, 0.1), \"level\")\n",
    "flow = ctrl.Antecedent(np.arange(0, 0.6, 0.01), \"flow\")\n",
    "influx = ctrl.Consequent(np.arange(0, 0.6, 0.01), \"influx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\user\\Projects\\python\\fuzzy\\.venv\\Lib\\site-packages\\skfuzzy\\control\\fuzzyvariable.py:125: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown\n",
      "  fig.show()\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39568 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36300 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36296 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import skfuzzy as fuzz\n",
    "\n",
    "level[\"low\"] = fuzz.zmf(level.universe, 2, 4)\n",
    "level[\"average\"] = fuzz.trapmf(level.universe, [2, 4, 6, 8])\n",
    "level[\"high\"] = fuzz.smf(level.universe, 6, 8)\n",
    "level.view()\n",
    "\n",
    "flow[\"low\"] = fuzz.zmf(flow.universe, 0.2, 0.3)\n",
    "flow[\"average\"] = fuzz.trapmf(flow.universe, [0.15, 0.25, 0.35, 0.45])\n",
    "flow[\"high\"] = fuzz.smf(flow.universe, 0.3, 0.4)\n",
    "flow.view()\n",
    "\n",
    "influx[\"low\"] = fuzz.zmf(influx.universe, 0.2, 0.3)\n",
    "influx[\"average\"] = fuzz.trapmf(influx.universe, [0.15, 0.25, 0.35, 0.45])\n",
    "influx[\"high\"] = fuzz.smf(influx.universe, 0.3, 0.4)\n",
    "influx.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<Figure size 640x480 with 1 Axes>, <Axes: >)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 25568 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rule1 = ctrl.Rule(level[\"low\"] & flow[\"high\"], influx[\"high\"])\n",
    "rule2 = ctrl.Rule(level[\"low\"] & flow[\"average\"], influx[\"high\"])\n",
    "rule3 = ctrl.Rule(level[\"low\"] & flow[\"low\"], influx[\"average\"])\n",
    "rule4 = ctrl.Rule(level[\"average\"] & flow[\"high\"], influx[\"high\"])\n",
    "rule5 = ctrl.Rule(level[\"average\"] & flow[\"average\"], influx[\"average\"])\n",
    "rule6 = ctrl.Rule(level[\"average\"] & flow[\"low\"], influx[\"average\"])\n",
    "rule7 = ctrl.Rule(level[\"high\"] & flow[\"high\"], influx[\"average\"])\n",
    "rule8 = ctrl.Rule(level[\"high\"] & flow[\"average\"], influx[\"low\"])\n",
    "rule9 = ctrl.Rule(level[\"high\"] & flow[\"low\"], influx[\"low\"])\n",
    "\n",
    "rule1.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "influx_ctrl = ctrl.ControlSystem(\n",
    "    [\n",
    "        rule1,\n",
    "        rule2,\n",
    "        rule3,\n",
    "        rule4,\n",
    "        rule5,\n",
    "        rule6,\n",
    "        rule7,\n",
    "        rule8,\n",
    "        rule9,\n",
    "    ]\n",
    ")\n",
    "\n",
    "influxes = ctrl.ControlSystemSimulation(influx_ctrl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=============\n",
      " Antecedents \n",
      "=============\n",
      "Antecedent: level                   = 2.5\n",
      "  - low                             : 0.875\n",
      "  - average                         : 0.25\n",
      "  - high                            : 0.0\n",
      "Antecedent: flow                    = 0.4\n",
      "  - low                             : 0.0\n",
      "  - average                         : 0.4999999999999997\n",
      "  - high                            : 1.0\n",
      "\n",
      "=======\n",
      " Rules \n",
      "=======\n",
      "RULE #0:\n",
      "  IF level[low] AND flow[high] THEN influx[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[low]                                             : 0.875\n",
      "  - flow[high]                                             : 1.0\n",
      "                                 level[low] AND flow[high] = 0.875\n",
      "  Activation (THEN-clause):\n",
      "                                              influx[high] : 0.875\n",
      "\n",
      "RULE #1:\n",
      "  IF level[low] AND flow[average] THEN influx[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[low]                                             : 0.875\n",
      "  - flow[average]                                          : 0.4999999999999997\n",
      "                              level[low] AND flow[average] = 0.4999999999999997\n",
      "  Activation (THEN-clause):\n",
      "                                              influx[high] : 0.4999999999999997\n",
      "\n",
      "RULE #2:\n",
      "  IF level[low] AND flow[low] THEN influx[average]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[low]                                             : 0.875\n",
      "  - flow[low]                                              : 0.0\n",
      "                                  level[low] AND flow[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                           influx[average] : 0.0\n",
      "\n",
      "RULE #3:\n",
      "  IF level[average] AND flow[high] THEN influx[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[average]                                         : 0.25\n",
      "  - flow[high]                                             : 1.0\n",
      "                             level[average] AND flow[high] = 0.25\n",
      "  Activation (THEN-clause):\n",
      "                                              influx[high] : 0.25\n",
      "\n",
      "RULE #4:\n",
      "  IF level[average] AND flow[average] THEN influx[average]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[average]                                         : 0.25\n",
      "  - flow[average]                                          : 0.4999999999999997\n",
      "                          level[average] AND flow[average] = 0.25\n",
      "  Activation (THEN-clause):\n",
      "                                           influx[average] : 0.25\n",
      "\n",
      "RULE #5:\n",
      "  IF level[average] AND flow[low] THEN influx[average]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[average]                                         : 0.25\n",
      "  - flow[low]                                              : 0.0\n",
      "                              level[average] AND flow[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                           influx[average] : 0.0\n",
      "\n",
      "RULE #6:\n",
      "  IF level[high] AND flow[high] THEN influx[average]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[high]                                            : 0.0\n",
      "  - flow[high]                                             : 1.0\n",
      "                                level[high] AND flow[high] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                           influx[average] : 0.0\n",
      "\n",
      "RULE #7:\n",
      "  IF level[high] AND flow[average] THEN influx[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[high]                                            : 0.0\n",
      "  - flow[average]                                          : 0.4999999999999997\n",
      "                             level[high] AND flow[average] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                               influx[low] : 0.0\n",
      "\n",
      "RULE #8:\n",
      "  IF level[high] AND flow[low] THEN influx[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - level[high]                                            : 0.0\n",
      "  - flow[low]                                              : 0.0\n",
      "                                 level[high] AND flow[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                               influx[low] : 0.0\n",
      "\n",
      "\n",
      "==============================\n",
      " Intermediaries and Conquests \n",
      "==============================\n",
      "Consequent: influx                   = 0.4315251220586045\n",
      "  low:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "  average:\n",
      "    Accumulate using accumulation_max : 0.25\n",
      "  high:\n",
      "    Accumulate using accumulation_max : 0.875\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.float64(0.4315251220586045)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "influxes.input[\"level\"] = 2.5\n",
    "influxes.input[\"flow\"] = 0.4\n",
    "influxes.compute()\n",
    "influxes.print_state()\n",
    "influxes.output[\"influx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36852 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "influx.view(sim=influxes)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
