{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "603763b9", "metadata": {}, "outputs": [], "source": [ "from simpful import *\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "5e8a33b1", "metadata": {}, "source": [ "## Sterownik 1 - Decyzja o udziale w grze\n", "##### Dane wejściowe:\n", "- Wartość żetonów\n", "- Liczba przegranych z rzędu\n", "\n", "##### Dane wyjściowe:\n", "- Decyzja dot. udziału w grze" ] }, { "cell_type": "code", "execution_count": 29, "id": "a3a08d50", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * Processing output for variable 'decision'\n", " whose universe of discourse is: [0, 25]\n", " contains the following fuzzy sets: [, , ]\n", " ** Rule composition: f.(c.(numLossInRow IS average) AND c.(chipValue IS average)) -> ('decision', 'play', '1.0') , output variable: 'decision' with term: 'play'\n", " ** Rule composition: f.(c.(numLossInRow IS low) OR c.(chipValue IS low)) -> ('decision', 'leave', '1.0') , output variable: 'decision' with term: 'leave'\n", " ** Rule composition: f.(c.(numLossInRow IS high) AND c.(chipValue IS high)) -> ('decision', 'playDouble', '1.0') , output variable: 'decision' with term: 'playDouble'\n", " * Indices: {'play': 1, 'leave': 0, 'playDouble': 2}\n", " * Weighted values: 6201.36\tValues: 458.14\tCoG: 13.54\n" ] }, { "data": { "text/plain": [ "'play'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FS = FuzzySystem(show_banner=False)\n", "\n", "FS.add_linguistic_variable(\"chipValue\", AutoTriangle(3, terms=['low', 'average', 'high'], universe_of_discourse=[0, 10]))\n", "FS.add_linguistic_variable(\"numLossInRow\", AutoTriangle(3, terms=['low', 'average', 'high'], universe_of_discourse=[0, 10]))\n", "\n", "O1 = TriangleFuzzySet(0,0,13, term=\"leave\")\n", "O2 = TriangleFuzzySet(0,13,25, term=\"play\")\n", "O3 = TriangleFuzzySet(13,25,25, term=\"playDouble\")\n", "FS.add_linguistic_variable(\"decision\", LinguisticVariable([O1, O2, O3], universe_of_discourse=[0, 25]))\n", "\n", "FS.add_rules([\n", "\t\"IF (numLossInRow IS average) AND (chipValue IS average) THEN (decision IS play)\",\n", "\t\"IF (numLossInRow IS low) OR (chipValue IS low) THEN (decision IS leave)\",\n", " \"IF (numLossInRow IS high) AND (chipValue IS high) THEN (decision IS playDouble)\",\n", "\t])\n", "\n", "FS.set_variable(\"chipValue\", 7) \n", "FS.set_variable(\"numLossInRow\", 7) \n", "\n", "result = FS.inference(verbose=True)\n", "decision_terms = [(i.get_term(), FS.get_fuzzy_set('decision', i.get_term()).get_value(result['decision'])) for i in FS.get_fuzzy_sets('decision')]\n", "endDecision = max(decision_terms, key=lambda item:item[1])[0]\n", "endDecision" ] }, { "cell_type": "code", "execution_count": 1, "id": "e2cbe240", "metadata": {}, "outputs": [], "source": [ "# FS.plot_variable('chipValue')\n", "# FS.plot_variable('numLossInRow')\n", "# FS.plot_variable('decision')" ] }, { "cell_type": "markdown", "id": "7ef6a277", "metadata": {}, "source": [ "## Sterownik 2 - Decyzja o strategii w przypadku pary\n", "##### Dane wejściowe:\n", "- Wartość widocznej karty krupiera\n", "- Wartość karty gracza (jednej z pary)\n", "\n", "##### Dane wyjściowe:\n", "- Decyzja dot. akcji w grze" ] }, { "cell_type": "code", "execution_count": 38, "id": "2f13e6ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * Processing output for variable 'decision'\n", " whose universe of discourse is: [0, 25]\n", " contains the following fuzzy sets: [, ]\n", " ** Rule composition: c.(playerCardValue IS high) -> ('decision', 'split', '1.0') , output variable: 'decision' with term: 'split'\n", " ** Rule composition: f.(c.(playerCardValue IS average) AND c.(dealerCardValue IS average)) -> ('decision', 'split', '1.0') , output variable: 'decision' with term: 'split'\n", " ** Rule composition: f.(c.(playerCardValue IS low) AND c.(dealerCardValue IS average)) -> ('decision', 'split', '1.0') , output variable: 'decision' with term: 'split'\n", " ** Rule composition: f.(c.(playerCardValue IS low) AND c.(dealerCardValue IS low)) -> ('decision', 'continue', '1.0') , output variable: 'decision' with term: 'continue'\n", " ** Rule composition: f.(c.(playerCardValue IS low) AND c.(dealerCardValue IS high)) -> ('decision', 'continue', '1.0') , output variable: 'decision' with term: 'continue'\n", " * Indices: {'split': 1, 'continue': 0}\n", " * Weighted values: 4597.07\tValues: 290.67\tCoG: 15.82\n" ] }, { "data": { "text/plain": [ "'split'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FS = FuzzySystem(show_banner=False)\n", "\n", "T1 = TriangleFuzzySet(0,0,6, term=\"low\")\n", "T2 = TriangleFuzzySet(4,6,8, term=\"average\")\n", "T3 = TriangleFuzzySet(6,11,11, term=\"high\")\n", "FS.add_linguistic_variable(\"dealerCardValue\", LinguisticVariable([T1, T2, T3], universe_of_discourse=[0, 11]))\n", "FS.add_linguistic_variable(\"playerCardValue\", LinguisticVariable([T1, T2, T3], universe_of_discourse=[0, 11]))\n", "\n", "\n", "O1 = TriangleFuzzySet(0,0,15, term=\"continue\")\n", "O2 = TriangleFuzzySet(11,25,25, term=\"split\")\n", "FS.add_linguistic_variable(\"decision\", LinguisticVariable([O1, O2], universe_of_discourse=[0, 25]))\n", "\n", "FS.add_rules([\n", "\t\"IF (playerCardValue IS high) THEN (decision IS split)\",\n", "\t\"IF (playerCardValue IS average) AND (dealerCardValue IS average) THEN (decision IS split)\",\n", " \"IF (playerCardValue IS average) AND (dealerCardValue IS low) THEN (decision IS split)\",\n", " \"IF (playerCardValue IS low) AND (dealerCardValue IS high) THEN (decision IS continue)\",\n", " \"IF (playerCardValue IS low) AND (dealerCardValue IS average) THEN (decision IS split)\",\n", " \"IF (playerCardValue IS low) AND (dealerCardValue IS low) THEN (decision IS continue)\",\n", " \"IF (playerCardValue IS low) AND (dealerCardValue IS high) THEN (decision IS continue)\"\n", "\t])\n", "\n", "FS.set_variable(\"dealerCardValue\", 5) \n", "FS.set_variable(\"playerCardValue\", 3) \n", "\n", "result = FS.inference(verbose=True)\n", "decision_terms = [(i.get_term(), FS.get_fuzzy_set('decision', i.get_term()).get_value(result['decision'])) for i in FS.get_fuzzy_sets('decision')]\n", "endDecision = max(decision_terms, key=lambda item:item[1])[0]\n", "endDecision" ] }, { "cell_type": "code", "execution_count": 8, "id": "ba24b10d", "metadata": {}, "outputs": [], "source": [ "# FS.plot_variable('dealerCardValue')\n", "# FS.plot_variable('playerCardValue')\n", "# FS.plot_variable('decision')" ] }, { "cell_type": "markdown", "id": "4f58a14a", "metadata": {}, "source": [ "## Sterownik 3 - Decyzja jaką akcję podjąć (gracz bez asa wśród dwóch kart)\n", "##### Dane wejściowe:\n", "- Wartość widocznej karty krupiera\n", "- Suma kart gracza\n", "- Suma zliczonych kart\n", "\n", "##### Dane wyjściowe:\n", "- Decyzja dot. akcji w grze" ] }, { "cell_type": "code", "execution_count": 6, "id": "d78bc74a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * Processing output for variable 'decision'\n", " whose universe of discourse is: [0, 25]\n", " contains the following fuzzy sets: [, , ]\n", " ** Rule composition: c.(playerCardsValue IS high) -> ('decision', 'stand', '1.0') , output variable: 'decision' with term: 'stand'\n", " ** Rule composition: c.(playerCardsValue IS low) -> ('decision', 'hit', '1.0') , output variable: 'decision' with term: 'hit'\n", " ** Rule composition: f.(c.(playerCardsValue IS average) AND c.(dealerCardValue IS low)) -> ('decision', 'stand', '1.0') , output variable: 'decision' with term: 'stand'\n", " ** Rule composition: f.(c.(playerCardsValue IS average) AND c.(dealerCardValue IS high)) -> ('decision', 'hit', '1.0') , output variable: 'decision' with term: 'hit'\n", " ** Rule composition: f.(c.(playerCardsValue IS average) AND f.(c.(dealerCardValue IS average) AND c.(countedCardsValue IS low))) -> ('decision', 'hit', '1.0') , output variable: 'decision' with term: 'hit'\n", " * Indices: {'stand': 2, 'hit': 1}\n", " * Weighted values: 1913.98\tValues: 152.62\tCoG: 12.54\n", "{'decision': 12.540414715943523}\n" ] }, { "data": { "text/plain": [ "'hit'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# dodatkowy argument określający liczbę kart gracza - w przypadku rozpoczęcia rozgrywki możliwa jest decyzja o podwojeniu\n", "playerCardsNum = 3\n", "\n", "FS = FuzzySystem(show_banner=False)\n", "\n", "FS.add_linguistic_variable(\"dealerCardValue\", LinguisticVariable([\n", " TriangleFuzzySet(0,0,6, term=\"low\"),\n", " TriangleFuzzySet(4,6,8, term=\"average\"),\n", " TriangleFuzzySet(6,11,11, term=\"high\")], \n", " universe_of_discourse=[0, 11]))\n", "\n", "FS.add_linguistic_variable(\"playerCardsValue\", LinguisticVariable([\n", " TriangleFuzzySet(0,0,12, term=\"low\"),\n", " TriangleFuzzySet(11,14,17, term=\"average\"),\n", " TriangleFuzzySet(12,21,21, term=\"high\")], \n", " universe_of_discourse=[0, 21]))\n", "\n", "FS.add_linguistic_variable(\"countedCardsValue\", LinguisticVariable([\n", " TriangleFuzzySet(-20,-20,0, term=\"low\"),\n", " TriangleFuzzySet(-5,0,5, term=\"average\"),\n", " TriangleFuzzySet(0,20,20, term=\"high\")], \n", " universe_of_discourse=[-20, 20]))\n", "\n", "\n", "O1 = TriangleFuzzySet(0,0,13, term=\"doubleDown\")\n", "O2 = TriangleFuzzySet(0,13,25, term=\"hit\")\n", "O3 = TriangleFuzzySet(13,25,25, term=\"stand\")\n", "FS.add_linguistic_variable(\"decision\", LinguisticVariable([O1, O2, O3], universe_of_discourse=[0, 25]))\n", "\n", "\n", "if playerCardsNum == 2:\n", " FS.add_rules([\n", " \"IF (playerCardsValue IS low) THEN (decision IS doubleDown)\",\n", " \"IF (playerCardsValue IS average) AND (dealerCardValue IS high) THEN (decision IS doubleDown)\",\n", " \"IF (playerCardsValue IS high) THEN (decision IS stand)\",\n", " \"IF (playerCardsValue IS average) AND (dealerCardValue IS average) THEN (decision IS stand)\",\n", " \"IF (playerCardsValue IS average) AND (dealerCardValue IS average) THEN (decision IS doubleDown)\"\n", " ])\n", " \n", "else: \n", " FS.add_rules([\n", " \"IF (playerCardsValue IS high) THEN (decision IS stand)\",\n", " \"IF (playerCardsValue IS low) THEN (decision IS hit)\",\n", " \"IF (playerCardsValue IS average) AND (dealerCardValue IS low) THEN (decision IS stand)\",\n", " \"IF (playerCardsValue IS average) AND (dealerCardValue IS high) THEN (decision IS hit)\",\n", " \"IF (playerCardsValue IS average) AND (dealerCardValue IS average) AND (countedCardsValue IS low) THEN (decision IS hit)\",\n", " ])\n", " \n", " \n", "FS.set_variable(\"dealerCardValue\", 4) \n", "FS.set_variable(\"playerCardsValue\", 10)\n", "FS.set_variable(\"countedCardsValue\", 0)\n", "\n", "result = FS.inference(verbose=True)\n", "print(result)\n", "decision_terms = [(i.get_term(), FS.get_fuzzy_set('decision', i.get_term()).get_value(result['decision'])) for i in FS.get_fuzzy_sets('decision')]\n", "endDecision = max(decision_terms, key=lambda item:item[1])[0]\n", "endDecision" ] }, { "cell_type": "code", "execution_count": 7, "id": "c9ea8787", "metadata": {}, "outputs": [], "source": [ "# FS.plot_variable('dealerCardValue')\n", "# FS.plot_variable('playerCardsValue')\n", "# FS.plot_variable('countedCardsValue')\n", "# FS.plot_variable('decision')" ] }, { "cell_type": "markdown", "id": "32fbd42b", "metadata": {}, "source": [ "## Sterownik 4 - Decyzja jaką akcję podjąć (gracz z asem wśród dwóch kart)\n", "##### Dane wejściowe:\n", "- Wartość widocznej karty krupiera\n", "- Wartość drugiej karty gracza\n", "- Suma zliczonych kart\n", "\n", "##### Dane wyjściowe:\n", "- Decyzja dot. akcji w grze" ] }, { "cell_type": "code", "execution_count": 15, "id": "878e6c15", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * Processing output for variable 'decision'\n", " whose universe of discourse is: [0, 25]\n", " contains the following fuzzy sets: [, , ]\n", " ** Rule composition: c.(playerCardValue IS high) -> ('decision', 'Stand', '1.0') , output variable: 'decision' with term: 'Stand'\n", " ** Rule composition: c.(playerCardValue IS low) -> ('decision', 'hit', '1.0') , output variable: 'decision' with term: 'hit'\n", " ** Rule composition: f.(c.(playerCardValue IS average) AND c.(dealerCardValue IS high)) -> ('decision', 'Hit', '1.0') , output variable: 'decision' with term: 'Hit'\n", " ** Rule composition: f.(c.(playerCardValue IS average) AND f.(c.(dealerCardValue IS average) AND c.(countedCardsValue IS high))) -> ('decision', 'Stand', '1.0') , output variable: 'decision' with term: 'Stand'\n", " ** Rule composition: f.(c.(playerCardValue IS average) AND f.(c.(dealerCardValue IS average) AND c.(countedCardsValue IS low))) -> ('decision', 'Hit', '1.0') , output variable: 'decision' with term: 'Hit'\n", " * Indices: {'Stand': -1, 'hit': 1, 'Hit': -1}\n", " * Weighted values: 3662.59\tValues: 180.07\tCoG: 20.34\n", "{'decision': 20.339814678947857}\n" ] }, { "data": { "text/plain": [ "'stand'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# dodatkowy argument określający liczbę kart gracza - w przypadku rozpoczęcia rozgrywki możliwa jest decyzja o podwojeniu\n", "playerCardsNum = 3\n", "\n", "FS = FuzzySystem(show_banner=False)\n", "\n", "FS.add_linguistic_variable(\"dealerCardValue\", LinguisticVariable([\n", " TriangleFuzzySet(0,0,6, term=\"low\"),\n", " TriangleFuzzySet(4,6,8, term=\"average\"),\n", " TriangleFuzzySet(6,11,11, term=\"high\")], \n", " universe_of_discourse=[0, 11]))\n", "\n", "FS.add_linguistic_variable(\"playerCardValue\", LinguisticVariable([\n", " TriangleFuzzySet(0,0,5, term=\"low\"),\n", " TriangleFuzzySet(4,6,8, term=\"average\"),\n", " TriangleFuzzySet(7,9,9, term=\"high\")], \n", " universe_of_discourse=[0, 9]))\n", "\n", "FS.add_linguistic_variable(\"countedCardsValue\", LinguisticVariable([\n", " TriangleFuzzySet(-20,-20,0, term=\"low\"),\n", " TriangleFuzzySet(-5,0,5, term=\"average\"),\n", " TriangleFuzzySet(0,20,20, term=\"high\")], \n", " universe_of_discourse=[-20, 20]))\n", "\n", "\n", "O1 = TriangleFuzzySet(0,0,13, term=\"doubleDown\")\n", "O2 = TriangleFuzzySet(0,13,25, term=\"hit\")\n", "O3 = TriangleFuzzySet(13,25,25, term=\"stand\")\n", "FS.add_linguistic_variable(\"decision\", LinguisticVariable([O1, O2, O3], universe_of_discourse=[0, 25]))\n", "\n", "if playerCardsNum == 2:\n", " FS.add_rules([\n", " \"IF (playerCardValue IS average) AND (dealerCardValue IS average) THEN (decision IS doubleDown)\",\n", " \"IF (playerCardValue IS high) THEN (decision IS Stand)\",\n", " \"IF (playerCardValue IS average) AND (dealerCardValue IS high) THEN (decision IS hit)\",\n", " \"IF (playerCardValue IS low) AND (dealerCardValue IS low) THEN (decision IS hit)\",\n", " ])\n", "else:\n", " FS.add_rules([\n", " \"IF (playerCardValue IS high) THEN (decision IS Stand)\",\n", " \"IF (playerCardValue IS low) THEN (decision IS hit)\",\n", " \"IF (playerCardValue IS average) AND (dealerCardValue IS high) THEN (decision IS Hit)\",\n", " \"IF (playerCardValue IS average) AND (dealerCardValue IS average) AND (countedCardsValue IS high) THEN (decision IS Stand)\",\n", " \"IF (playerCardValue IS average) AND (dealerCardValue IS average) AND (countedCardsValue IS low) THEN (decision IS Hit)\"\n", " ])\n", "\n", "FS.set_variable(\"dealerCardValue\", 2) \n", "FS.set_variable(\"playerCardValue\", 8)\n", "FS.set_variable(\"countedCardsValue\", 0)\n", "\n", "result = FS.inference(verbose=True)\n", "print(result)\n", "decision_terms = [(i.get_term(), FS.get_fuzzy_set('decision', i.get_term()).get_value(result['decision'])) for i in FS.get_fuzzy_sets('decision')]\n", "endDecision = max(decision_terms, key=lambda item:item[1])[0]\n", "endDecision" ] }, { "cell_type": "code", "execution_count": 2, "id": "82f441d3", "metadata": {}, "outputs": [], "source": [ "# FS.plot_variable('dealerCardValue')\n", "# FS.plot_variable('playerCardValue')\n", "# FS.plot_variable('countedCardsValue')\n", "# FS.plot_variable('decision')" ] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }