{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "76a823a2-8bbc-4d61-a86c-d07a8fc3631c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0mRequirement already satisfied: numpy in /usr/local/lib/python3.10/site-packages (1.22.3)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.10/site-packages (1.4.2)\n", "Requirement already satisfied: seaborn in /usr/local/lib/python3.10/site-packages (0.12.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/site-packages (from pandas) (2022.1)\n", "Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/site-packages (from pandas) (2.8.2)\n", "Requirement already satisfied: matplotlib!=3.6.1,>=3.1 in /usr/local/lib/python3.10/site-packages (from seaborn) (3.5.1)\n", "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (9.1.1)\n", "Requirement already satisfied: pyparsing>=2.2.1 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (3.0.9)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (0.11.0)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (4.33.3)\n", "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (21.3)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (1.4.3)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n", "\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0mCollecting pyswip\n", " Cloning https://github.com/yuce/pyswip (to revision master) to /tmp/pip-install-1cq570y5/pyswip_b5c8a7a04c0b40bdb41ecb2fc705ed39\n", " Running command git clone --filter=blob:none --quiet https://github.com/yuce/pyswip /tmp/pip-install-1cq570y5/pyswip_b5c8a7a04c0b40bdb41ecb2fc705ed39\n", " Resolved https://github.com/yuce/pyswip to commit 841d08fe4cbea95da6420f4e27b63461ab816cad\n", " Preparing metadata (setup.py) ... \u001b[?25ldone\n", "\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -tterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution -utterfly (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution - (/usr/local/lib/python3.10/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[?25h" ] } ], "source": [ "import sys\n", "!{sys.executable} -m pip install numpy pandas seaborn\n", "!{sys.executable} -m pip install git+https://github.com/yuce/pyswip@master#egg=pyswip\n", "\n", "import os\n", "import json\n", "from pathlib import Path\n", "\n", "import matplotlib.pyplot as plt\n", "from matplotlib import colors\n", "import numpy as np\n", "\n", "%matplotlib inline\n", "import seaborn as sns\n", "\n", "import matplotlib.pyplot as plt\n", "from matplotlib.colors import ListedColormap, Normalize\n", "import numpy as np\n", "import pandas as pd\n", "\n", "\n", "def get_data(is_train):\n", " path = './data/' + ('training' if is_train else 'evaluation')\n", " data = {}\n", " for filename in os.listdir(path):\n", " with open(os.path.join(path, filename)) as f:\n", " data[filename.rstrip('.json')] = json.load(f)\n", " return {\n", " 'train': {k: [{\n", " 'input': tuple(tuple(r) for r in e['input']),\n", " 'output': tuple(tuple(r) for r in e['output']),\n", " } for e in v['train']] for k, v in data.items()},\n", " 'test': {k: [{\n", " 'input': tuple(tuple(r) for r in e['input']),\n", " 'output': tuple(tuple(r) for r in e['output']),\n", " } for e in v['test']] for k, v in data.items()}\n", " }\n", "\n", "\n", "def plot_task(task, solver=None):\n", " cols = [\n", " '#000', '#0074D9', '#FF4136', '#2ECC40', '#FFDC00',\n", " '#AAAAAA', '#F012BE', '#FF851B', '#7FDBFF', '#870C25'\n", " ]\n", " args = {'cmap': ListedColormap(cols), 'norm': Normalize(vmin=0, vmax=9)}\n", " n = len(task)\n", " m = 2 if solver is None else 3\n", " fig, ax = plt.subplots(m, n, figsize=(n * 4, m * 4))\n", " ofs = 0 if solver is None else 1\n", " if n == 1:\n", " ax[0].imshow(task[0]['input'], **args)\n", " ax[1 + ofs].imshow(task[0]['output'], **args)\n", " ax[0].axis('off')\n", " ax[1 + ofs].axis('off')\n", " if solver is not None:\n", " ax[1].imshow(solver(task[0]['input']), **args)\n", " ax[1].axis('off')\n", " else:\n", " for i, example in enumerate(task):\n", " ax[0, i].imshow(example['input'], **args)\n", " ax[1 + ofs, i].imshow(example['output'], **args)\n", " ax[0, i].axis('off')\n", " ax[1 + ofs, i].axis('off')\n", " if solver is not None:\n", " ax[1, i].imshow(solver(example['input']), **args)\n", " ax[1, i].axis('off')\n", " fig.set_facecolor('#1E1E1E')\n", " plt.subplots_adjust(wspace=0.1, hspace=0.1)\n", " plt.show()\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "264d3bfd-bda7-4b07-8dc1-82ce91119239", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "michael is the father of john\n", "michael is the father of gina\n" ] } ], "source": [ "from pyswip import Prolog\n", "prolog = Prolog()\n", "prolog.assertz(\"father(michael,john)\")\n", "prolog.assertz(\"father(michael,gina)\")\n", "list(prolog.query(\"father(michael,X)\")) == [{'X': 'john'}, {'X': 'gina'}]\n", "for soln in prolog.query(\"father(X,Y)\"):\n", " print(soln[\"X\"], \"is the father of\", soln[\"Y\"])\n", "# michael is the father of john\n", "# michael is the father of gina" ] }, { "cell_type": "code", "execution_count": 6, "id": "6a6d410a-6afe-46fd-b201-c2e7163c6c9e", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "% 1D_testset/ solution/ test_nar_10x10/ Untitled3.ipynb\n", "% chat/ stats.db training/ Untitled.ipynb\n", "% devaluation/ storage/ Untitled1.ipynb \n", "% results.txt test_100/ Untitled2.ipynb \n" ] } ], "source": [ "for soln in prolog.query(\"ls\"): 0\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "e25be42e-b443-4186-850d-b68dbbfcbd18", "metadata": {}, "outputs": [], "source": [ "# import tutorial_code" ] }, { "cell_type": "code", "execution_count": null, "id": "7a9277a1-8102-4ed0-931d-db253bb4b0cb", "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }