{ "cells": [ { "source": [ "# Tutorial 4 - Certainty factors" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "This tutorial uses the fuzzy inference system developed in Tutorial 1." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import os\n", "import warnings\n", "\n", "os.chdir('/workspaces/fuzzy-expert')\n", "warnings.filterwarnings(\"ignore\")" ] }, { "source": [ "## Fuzzy Variables" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from fuzzy_expert.variable import FuzzyVariable\n", "from fuzzy_expert.rule import FuzzyRule\n", "from fuzzy_expert.inference import DecompositionalInference\n", "\n", "variables = {\n", " \"score\": FuzzyVariable(\n", " universe_range=(150, 200),\n", " terms={\n", " \"High\": [(175, 0), (180, 0.2), (185, 0.7), (190, 1)],\n", " \"Low\": [(155, 1), (160, 0.8), (165, 0.5), (170, 0.2), (175, 0)],\n", " },\n", " ),\n", " \"ratio\": FuzzyVariable(\n", " universe_range=(0.1, 1),\n", " terms={\n", " \"Goodr\": [(0.3, 1), (0.4, 0.7), (0.41, 0.3), (0.42, 0)],\n", " \"Badr\": [(0.44, 0), (0.45, 0.3), (0.5, 0.7), (0.7, 1)],\n", " },\n", " ),\n", " #\n", " \"credit\": FuzzyVariable(\n", " universe_range=(0, 10),\n", " terms={\n", " \"Goodc\": [(2, 1), (3, 0.7), (4, 0.3), (5, 0)],\n", " \"Badc\": [(5, 0), (6, 0.3), (7, 0.7), (8, 1)],\n", " },\n", " ),\n", " #\n", " \"decision\": FuzzyVariable(\n", " universe_range=(0, 10),\n", " terms={\n", " \"Approve\": [(5, 0), (6, 0.3), (7, 0.7), (8, 1)],\n", " \"Reject\": [(2, 1), (3, 0.7), (4, 0.3), (5, 0)],\n", " },\n", " ),\n", "}" ] }, { "source": [ "## Fuzzy rules with certainty factors" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "It is possible to assign a certainty factor (`cf`) to each rule. If this value is not specified, it has assumed to be equal to 1.0. In addition, the `threshold_cf` is the minimum certainty factor required to consider the rule fired; this is, rules with a computed certainty factor below the threshold are not considering for computing the output of the system. The first rule has a certainty factor of `0.9`, while the second rule has a certainty factor of `1.0` (by default)." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "rules = [\n", " FuzzyRule(\n", " cf=0.9,\n", " premise=[\n", " (\"score\", \"High\"),\n", " (\"AND\", \"ratio\", \"Goodr\"),\n", " (\"AND\", \"credit\", \"Goodc\"),\n", " ],\n", " consequence=[(\"decision\", \"Approve\")],\n", " ),\n", " FuzzyRule(\n", " premise=[\n", " (\"score\", \"Low\"),\n", " (\"AND\", \"ratio\", \"Badr\"),\n", " (\"OR\", \"credit\", \"Badc\"),\n", " ],\n", " consequence=[(\"decision\", \"Reject\")],\n", " )\n", "]" ] }, { "source": [ "## Facts with certainty factors" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "In addition, also it is possible to assign certainty factors to the facts. When a certainty factor not is specified by the user, it has a default value or 1.0. In the following code, the variables `score`, `ratio`, and `credit` have certainty factors of `0.9`, `1.0`, and `0.95` respectively. The conclusion is `decision=8.01` with a certainty factor of `0.95`." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "({'decision': 8.010492631084489}, 0.95)" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "from fuzzy_expert.inference import DecompositionalInference\n", "\n", "model = DecompositionalInference(\n", " and_operator=\"min\",\n", " or_operator=\"max\",\n", " implication_operator=\"Rc\",\n", " composition_operator=\"max-min\",\n", " production_link=\"max\",\n", " defuzzification_operator=\"cog\",\n", ")\n", "\n", "model(\n", " variables=variables,\n", " rules=rules,\n", " score=(190, 0.9),\n", " ratio=(0.39, 1.0),\n", " credit=(1.5, 0.95),\n", ")" ] } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3.9.5 64-bit" }, "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.9.5" }, "interpreter": { "hash": "4cd7ab41f5fca4b9b44701077e38c5ffd31fe66a6cab21e0214b68d958d0e462" } }, "nbformat": 4, "nbformat_minor": 5 }