{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Copy of FINAL Template_for CIS521_midterm notes.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "brNDGX-kvzds"
      },
      "source": [
        "# Template for Notes for CIS 521 Midterm 1\n",
        "\n",
        "The midterm exam for CIS 521 will be **open notes**. No other matierals are allowed for the exam - you may not use the textbook, the internet, a Python envionement, or communicate with anyone during the exam. \n",
        "\n",
        "This python notebook is where you should save all of your notes.  It provides a structured template that allows you to add summaries for each of the textbook sections that are part of the required reading, plus room for lecture notes and definitions.\n",
        "\n",
        "Your summaries must be written in your own words.  Do not copy and paste from the textbook.  Do not share your notes with classmates, or copy any of your classmates' notes.  "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aPtM8g7R0Pds"
      },
      "source": [
        "# Instructions\n",
        "\n",
        "1. Make a copy of this Google Colab file. From the colab menu, pick **File > Save a copy in Drive**.  That will save a copy of this notebook in your Google drive.\n",
        "2. Go through each section of the notebook, and fill in all the summaries for each chapter and its corresponding sections.  Be sure to save your work frequently!\n",
        "3. Read the notes below about how you can format your summaries.\n",
        "4. In addition to these notes, you are allowed 1 page (both sides) for figures or handwritten notes.\n",
        "5. Submit your version of this notebook to Gradescope before you start the exam.\n",
        "6. If you are taking the exam in person, then you must print a copy of your notes for yourself.   Here's how to print: \n",
        "* Run the last cell of this notebook\n",
        "* Click the \"Mirror cell in tab\" button (this is located in the upper right corner of the cell, immediately to the right of the trash can).  This will open the cell.  \n",
        "* In Chrome, you can then print your notes using the **File > Print** option from the browser's menu.  \n",
        "* In the print options, change the layout from Landscape to Portrait. \n",
        "* Clicking the Save, will save a PDF that you can then print.\n",
        "\n",
        "## Formatting Notes\n",
        "\n",
        "1. The max length for each section's summary is 500 words / 3000 characters.\n",
        "2. You can also write a 2000 word summary of the lectures for each module.\n",
        "3. You may also write definitions of concepts for each module.  The definitions should be written in your own words (not copy and pasted), and they should be no more than 20 words long for each concept. \n",
        "2. You can use Markdown formatting for your summaries. \n",
        "3. You can use Latex formatting for equations. Put two dollar signs before and after your equation like this: \n",
        "$$ \\epsilon = \\frac{1}{mc^2} $$\n",
        "4. When you write python code or psuedocode algorithms, you should surround it with triple backticks like this:\n",
        "```\n",
        "def function():\n",
        "      print('hello world')\n",
        "```\n",
        "5. Write \\ whenever you want to keep writing on the next line without a newline being inserted."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cw5IpDLCZa4W"
      },
      "source": [
        "## Example summary\n",
        "\n",
        "This is a sample summary of [Section 2.1.1](https://web.stanford.edu/~jurafsky/slp3/2.pdf ) of Jurfasky and Martin's Speech and Language Processing's book\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lzaavW96tcxM",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 0
        },
        "outputId": "f6d4e44e-ab39-4816-ad85-bad83a53d4c5"
      },
      "source": [
        "chapter_2_1_example = \"\"\"\n",
        "Regular expressions are an algebraic notation for characterizing \\\n",
        "a set of strings that are useful for searching for a pattern or through a corpus.\\\n",
        "Only the first matching result will be shown. The search string is case sensitive, \\\n",
        "but it can consist of a single character or a sequence of characters. \\\n",
        "While square braces specify a disjunction of characters, the dash can more easily look \\\n",
        "for characters over a range. The caret specifies the non-existence of a character. \\\n",
        "Additionally, the (?) is used to for flexibility on a single character, meaning \\\n",
        "\"the preceding character or nothing\". More importantly, the Kleene star means zero or \\\n",
        "more occurrences of the immediately previous character or regular expression in square braces.\\\n",
        "...\n",
        "\"\"\"\n",
        "\n",
        "length = len(chapter_2_1_example.split(\" \"))\n",
        "print(\"The example summary is {len} words long, so you could write an additional \\\n",
        "{remaining} words.\".format(len=length, remaining=(500-length)))"
      ],
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "The example summary is 116 words long, so you could write an additional 384 words.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "s3Uk9gQTevcj"
      },
      "source": [
        "## Example summary length\n",
        "\n",
        "The maximum summary length for each section is 500 words / 3000 characters.  Hopefully you'll find that to be quite generous. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8DvZ57A-djo1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 0
        },
        "outputId": "dc812ac5-29bf-4bfb-a34a-c68e0c7d7729"
      },
      "source": [
        "# This is an example of a 500 word placeholder\n",
        "placeholder = \"\"\"ultrices vitae auctor eu augue ut lectus arcu bibendum at varius \\\n",
        " vel pharetra vel turpis nunc eget lorem dolor sed viverra ipsum nunc aliquet \\\n",
        " bibendum enim facilisis gravida neque convallis a cras semper auctor neque vitae \\\n",
        " tempus quam pellentesque nec nam aliquam sem et tortor consequat id porta nibh \\\n",
        " venenatis cras sed felis eget velit aliquet sagittis id consectetur purus ut \\\n",
        " faucibus pulvinar elementum integer enim neque volutpat ac tincidunt vitae semper \\ \n",
        " quis lectus nulla at volutpat diam ut venenatis tellus in metus vulputate eu \\\n",
        " scelerisque felis imperdiet proin fermentum leo vel orci porta non pulvinar neque \\ \n",
        " laoreet suspendisse interdum consectetur libero id faucibus nisl tincidunt eget \\\n",
        " nullam non nisi est sit amet facilisis magna etiam tempor orci eu lobortis \\\n",
        " elementum nibh tellus molestie nunc non blandit massa enim nec dui nunc mattis \\\n",
        " enim ut tellus elementum sagittis vitae et leo duis ut diam quam nulla porttitor \\\n",
        " massa id neque aliquam vestibulum morbi blandit cursus risus at ultrices mi tempus \\\n",
        " imperdiet nulla malesuada pellentesque elit eget gravida cum sociis natoque \\\n",
        " penatibus et magnis dis parturient montes nascetur ridiculus mus mauris vitae \\\n",
        " ultricies leo integer malesuada nunc vel risus commodo viverra maecenas accumsan \\\n",
        " lacus vel facilisis volutpat est velit egestas dui id ornare arcu odio ut sem nulla \\\n",
        " pharetra diam sit amet nisl suscipit adipiscing bibendum est ultricies integer \\\n",
        " quis auctor elit sed vulputate mi sit amet mauris commodo quis imperdiet massa \\\n",
        " tincidunt nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc \\\n",
        " consequat interdum varius sit amet mattis vulputate enim nulla aliquet porttitor \\\n",
        " lacus luctus accumsan tortor posuere ac ut consequat semper viverra nam libero \\\n",
        " justo laoreet sit amet cursus sit amet dictum sit amet justo donec enim diam \\\n",
        " vulputate ut pharetra sit amet aliquam id diam maecenas ultricies mi eget mauris \\\n",
        " pharetra et ultrices neque ornare aenean euismod elementum nisi quis eleifend quam \\\n",
        " adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus urna neque viverra justo \\\n",
        " nec ultrices dui sapien eget mi proin sed libero enim sed faucibus turpis in \\\n",
        " eu mi bibendum neque egestas congue quisque egestas diam in arcu cursus euismod \\\n",
        " quis viverra nibh cras pulvinar mattis nunc sed blandit libero volutpat sed cras \\\n",
        " ornare arcu dui vivamus arcu felis bibendum ut tristique et egestas quis ipsum \\\n",
        " suspendisse ultrices gravida dictum fusce ut placerat orci nulla pellentesque \\\n",
        " dignissim enim sit amet venenatis urna cursus eget nunc scelerisque viverra mauris \\\n",
        " in aliquam sem fringilla ut morbi tincidunt augue interdum velit euismod in \\\n",
        " pellentesque massa placerat duis ultricies lacus sed turpis tincidunt id aliquet \\\n",
        " risus feugiat in ante metus dictum at tempor commodo ullamcorper a lacus vestibulum \\\n",
        " sed arcu non odio euismod lacinia at quis risus sed vulputate odio ut enim blandit \\\n",
        " volutpat maecenas volutpat blandit aliquam etiam erat velit scelerisque in \\\n",
        " dictum non consectetur a erat nam at lectus urna\"\"\"\n",
        "\n",
        "print(\"The length in words is\", len(placeholder.split(' ')))\n",
        "print(\"The length in characters is\", len(placeholder))"
      ],
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "The length in words is 500\n",
            "The length in characters is 3030\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5yG3-YncJfTB"
      },
      "source": [
        "# Permissions to use your summaries\n",
        "\n",
        "We'd like to use your summaries to help develop better educational resources like [remember.school](https://remember.school) or a question-answering system about the textbook.  \n",
        "\n",
        "1. Please enter your name.  \n",
        "\n",
        "2. If you grant permission for us to use your summaries under a [Creative Commons License](https://creativecommons.org/licenses/by/4.0/), please set ```permission = True```.  If you do not grant permission, please set it to false. Your answer won't affect your grade.\n",
        "\n",
        "3. If you grant us permission, and you'd like to remain anonymous, set ```anonymous = True```.  We'll remove your name before sharing your summaries with anyone. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2RECnJArJew4"
      },
      "source": [
        "name = \"\"\n",
        "permission = True\n",
        "anonymous = False"
      ],
      "execution_count": 39,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GZZXyrV-xuAp"
      },
      "source": [
        "# Module 1 - Intro to AI"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y2NfE1CKxzS-"
      },
      "source": [
        "# Russell and Norvig, AIMA 4th Edition, Chapter 1 “Introduction”, Section 1.1 “What is AI”\n",
        "\n",
        "chapter1_1 = \"\"\" Rationality: doing the right thing. \n",
        "Abilities computers need to pass Turing test: natural language processing, knowledge representation, \n",
        "automated reasoning, machine learning.\n",
        "Value alignment problem, the values or objectives put into the machine must be aligned with those of human.\n",
        " \"\"\" \n",
        "\n",
        "# Russell and Norvig, AIMA 4th Edition, Chapter 27 “Philosophical Foundations”, Sections 27.1 “The Limits of AI” and 27.2 “Can Machines Really Think?”\n",
        "chapter27_1 = \"\"\" Good Old-Fashioned AI.\n",
        " \"\"\"\n",
        "chapter27_2 = \"\"\" \"\"\"\n",
        "\n",
        "# Russell and Norvig, AIMA 4th Edition, Chapter 2 “Intelligent Agents” (whole chapter)\n",
        "chapter2_1 = \"\"\" The agent function for an artificial agent will be implemented by an agent program. Agent is meant to be a tool for \n",
        "analyzing systems, not absolute characterization. \"\"\"\n",
        "chapter2_2 = \"\"\"Performance meaure evalutes any given sequence of environmental states. \\\n",
        "It's better to design performance measures according to what actually wants to be achieved, not how one thinks agent should behave.\\\n",
        "Being rational depends on four things: (1) performance meatures that defines success (2) agent's prior knowledge of environment (3)\n",
        "actions that the agent can perform (4) Agent's percept sequence \\\n",
        "Rationality is not the same as perfection.\n",
        " \"\"\"\n",
        "chapter2_3 = \"\"\" Task Environments: PEAS(Performance, Environment, Actuators, Sensors)\\\n",
        "Properties of task Environments:\\\n",
        "Fully observable vs. partially observable\\\n",
        "Single agent vs. multiagent\\\n",
        "deterministic vs. nondeterministic\\\n",
        "episodic vs. sequential\\\n",
        "static vs. dynamic\\\n",
        "Discrete vs. continuous\n",
        "Known vs. Unknown \\\n",
        "Experiments are carried out for many environments from environment class\"\"\"\n",
        "chapter2_4 = \"\"\" Simple reflex agents select actions on basis of the current percept.\\\n",
        "Model-based reflex agents, internal state depends on percept history \\\n",
        "Goal-based agents\\\n",
        "Utility-based agents\\\n",
        "Learning agents: learning element, performance element, critic, problem generator\"\"\"\n",
        "\n",
        "# Lecture notes \n",
        "module1_lecture_notes = \"\"\"\"\"\"\n",
        "\n",
        "\n",
        "# Definitions of concepts from Module 1.\n",
        "# You can define any bolded term in the textbook, \n",
        "# but you most use your own words for the definitions.\n",
        "# Each definition should be at most 20 words long.\n",
        "module1_definitions = {\n",
        "\"agent\" : \"anything that can perceive through sensors and act in an environment through actuators\",\n",
        "\"percept\" : \"contents agent's sensors are perceiving\",\n",
        "\"percept sequence\" : \"all history of what the agent ever perceived.\",\n",
        "\"agent function\" : \"maps given percept sequence to action\",\n",
        "\"embodied cognition\" : \"No need to consider the brain separately\",\n",
        "\"biological naturalism\" : \"menetal states are high level emergent features caused by low level physical processed in neurons\",\n",
        "\"rational agent\": \"One that acts to achieve the best expected outcome\",\n",
        "\"consequentialism\" : \"evaluate agent behavior by consequences\",\n",
        "\"agent architecture\" : \"agent program run on computing device that has sensors and actuators\"\n",
        "}\n",
        "\n"
      ],
      "execution_count": 40,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "elCCgY64spuq"
      },
      "source": [
        "## Preview of your notes for Module 1\n",
        "\n",
        "Here's a preview of your how your notes will be displayed.  At the end of this Python notebook, we'll aggregate your notes for all modules together into a single display.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gYYeNcdM_maE",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 873
        },
        "cellView": "form",
        "outputId": "df62c154-51be-4f1f-9217-80d3ea0906a7"
      },
      "source": [
        "#@title Preview of your summary\n",
        "#@markdown You don't need to modify anything in this cell. Just press play.\n",
        "from IPython.display import display, Markdown, Latex\n",
        "\n",
        "module1_sections = [\n",
        "  (\"AIMA Chapter 1.1 What is AI?\", chapter1_1),\n",
        "  (\"AIMA Chapter 27.1 The Limits of AI\", chapter27_1),\n",
        "  (\"AIMA Chapter 27.1 Can Machines Really Think?\", chapter27_2),\n",
        "  (\"AIMA 2.1 Agents and Environments\", chapter2_1),\n",
        "  (\"AIMA 2.2 Good Behavior: The Concept of Rationality\", chapter2_2),\n",
        "  (\"AIMA 2.3 The Nature of Environments\", chapter2_3),\n",
        "  (\"AIMA 2.4 The Structure of Agents\", chapter2_4),\n",
        "  (\"Module 1 Lecture Notes\", module1_lecture_notes),\n",
        "]\n",
        "\n",
        "\n",
        "def length_check(sections):\n",
        "  warnings = \"\"\n",
        "  for section, summary in sections:\n",
        "    max_word_length = 500\n",
        "    max_character_length = 3000\n",
        "    if \"lecture\" in section.lower():\n",
        "      # It's OK to do longer summaries of the lectures\n",
        "      max_word_length = 2000\n",
        "      max_character_length = 12000  \n",
        "    if (len(summary) > max_character_length or len(summary.split(' ')) > max_word_length):\n",
        "      warnings += \"WARNING: Your summary of '{section}' is too long!\\n\".format(section=section)\n",
        "  return warnings\n",
        "\n",
        "def length_check_definitions(definitions):\n",
        "  warnings = \"\"\n",
        "  for concept, definition in definitions.items():\n",
        "    if(len(definition.split(' ')) > 20):\n",
        "      warnings += \"WARNING: Your definition of '{concept}' is too long!\\n\".format(concept=concept)\n",
        "  return warnings\n",
        "\n",
        "\n",
        "def format_module(module_num, title, sections, definitions, show_placeholder=False):\n",
        "  markdown = \"\"\n",
        "\n",
        "  # Check summary lengths\n",
        "  warnings = length_check(sections)\n",
        "  if warnings != \"\":\n",
        "    markdown += \"## \" + warnings\n",
        "\n",
        "  # Check definition lengths\n",
        "  warnings = length_check_definitions(definitions)\n",
        "  if warnings != \"\":\n",
        "    markdown += \"## \" + warnings\n",
        "\n",
        "  markdown += \"# Module {num}: {title}\\n\".format(num=module_num, title=title)\n",
        "\n",
        "\n",
        "  for section, summary in sections:\n",
        "    if show_placeholder and summary == \" Your summary here \":\n",
        "      summary = placeholder\n",
        "    markdown += \"## {section}\\n{summary}\\n\\n\".format(section=section, summary=summary)\n",
        "  \n",
        "  markdown += \"## Module {num} Definitions\\n\".format(num=module_num)\n",
        "  for concept, definition in definitions.items():\n",
        "    markdown += \"* **{concept}** - {definition}\\n\".format(concept=concept.capitalize(), definition=definition)\n",
        "  return markdown\n",
        "\n",
        "\n",
        "module1_markdown = format_module(1, \"Rational Agents and Task Environments\", module1_sections, module1_definitions)\n",
        "display(Markdown(module1_markdown))"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 1: Rational Agents and Task Environments\n## AIMA Chapter 1.1 What is AI?\n Rationality: doing the right thing. \nAbilities computers need to pass Turing test: natural language processing, knowledge representation, \nautomated reasoning, machine learning.\nValue alignment problem, the values or objectives put into the machine must be aligned with those of human.\n \n\n## AIMA Chapter 27.1 The Limits of AI\n Good Old-Fashioned AI.\n \n\n## AIMA Chapter 27.1 Can Machines Really Think?\n \n\n## AIMA 2.1 Agents and Environments\n The agent function for an artificial agent will be implemented by an agent program. Agent is meant to be a tool for \nanalyzing systems, not absolute characterization. \n\n## AIMA 2.2 Good Behavior: The Concept of Rationality\nPerformance meaure evalutes any given sequence of environmental states. It's better to design performance measures according to what actually wants to be achieved, not how one thinks agent should behave.Being rational depends on four things: (1) performance meatures that defines success (2) agent's prior knowledge of environment (3)\nactions that the agent can perform (4) Agent's percept sequence Rationality is not the same as perfection.\n \n\n## AIMA 2.3 The Nature of Environments\n Task Environments: PEAS(Performance, Environment, Actuators, Sensors)Properties of task Environments:Fully observable vs. partially observableSingle agent vs. multiagentdeterministic vs. nondeterministicepisodic vs. sequentialstatic vs. dynamicDiscrete vs. continuous\nKnown vs. Unknown Experiments are carried out for many environments from environment class\n\n## AIMA 2.4 The Structure of Agents\n Simple reflex agents select actions on basis of the current percept.Model-based reflex agents, internal state depends on percept history Goal-based agentsUtility-based agentsLearning agents: learning element, performance element, critic, problem generator\n\n## Module 1 Lecture Notes\n\n\n## Module 1 Definitions\n* **Agent** - anything that can perceive through sensors and act in an environment through actuators\n* **Percept** - contents agent's sensors are perceiving\n* **Percept sequence** - all history of what the agent ever perceived.\n* **Agent function** - maps given percept sequence to action\n* **Embodied cognition** - No need to consider the brain separately\n* **Biological naturalism** - menetal states are high level emergent features caused by low level physical processed in neurons\n* **Rational agent** - One that acts to achieve the best expected outcome\n* **Consequentialism** - evaluate agent behavior by consequences\n* **Agent architecture** - agent program run on computing device that has sensors and actuators\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9xRbdqDox2zD"
      },
      "source": [
        "#Module 2 - Python"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DpBvu7EMx7sy"
      },
      "source": [
        "# No readings for this module.  Instead you can summarize the lecture instead.\n",
        "\n",
        "# Lecture notes \n",
        "module2_lecture_notes = \"\"\"All data are treated as objects.   \n",
        "Strong typing  \n",
        "Dynamic typing   \n",
        "String is immutable, use % to format, use + to concatenate  \n",
        "Lists are mutable, direct assignment creates reference  \n",
        "== checks value equal, 'is' checks object equal  \n",
        "Tuples: immutable  \n",
        "Dictionary key must be immutable  \n",
        "For functions returning multiple values, return type is tuple  \n",
        "No function overloading  \n",
        "Non-default argument should always precede default arguments; otherwise, it\n",
        "reports SyntaxError  \n",
        "*args: a tuple of non-keyword arguments  \n",
        "**kwargs: a dict of the keyword args passed to the fn  \n",
        "Python uses dynamic scope, sees most current value of a variable  \n",
        "Default parameter values are evaluated only when the def statement they belong to is\n",
        "first executed.  \n",
        "Functions are first-class objects  \n",
        "Multiple inheritance is supported.  \n",
        "parentClass.__init__(self, x, y)  \n",
        "To execute the method in the parent class in addition to new code for some\n",
        "method, explicitly call the parent’s version of the method:  \n",
        "parentClass.methodName(self, a, b, c)  \n",
        "__init__: The constructor for the class.  \n",
        "__len__ : Define how len( obj ) works.  \n",
        "__copy__: Define how to copy a class.  \n",
        "__cmp__ : Define how == works for class.  \n",
        "__add__ : Define how + works for class  \n",
        "__neg__ : Define how unary negation works for class  \n",
        "Duck Typing: look at methods  \n",
        "Filtered list comprehension: [elem * 2 for elem in li if elem > 4]  \n",
        "iterators & generators\"\"\"\n",
        "\n",
        "# Each definition should be at most 20 words long.\n",
        "module2_definitions = {\n",
        "\"mutability\" : \"your definition here\",\n",
        "}"
      ],
      "execution_count": 42,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ydOK__lwWgdp",
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 695
        },
        "outputId": "81d0f18a-cdf6-4baa-a048-827f3499c688"
      },
      "source": [
        "#@title Preview of your summary\n",
        "#@markdown You don't need to modify anything in this cell. Just press play.\n",
        "\n",
        "module2_sections = [\n",
        "  (\"Module 2 Lecture Notes\", module2_lecture_notes),\n",
        "]\n",
        "\n",
        "module2_markdown = format_module(2, \"Python\", module2_sections, module2_definitions)\n",
        "display(Markdown(module2_markdown))"
      ],
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 2: Python\n## Module 2 Lecture Notes\nAll data are treated as objects.   \nStrong typing  \nDynamic typing   \nString is immutable, use % to format, use + to concatenate  \nLists are mutable, direct assignment creates reference  \n== checks value equal, 'is' checks object equal  \nTuples: immutable  \nDictionary key must be immutable  \nFor functions returning multiple values, return type is tuple  \nNo function overloading  \nNon-default argument should always precede default arguments; otherwise, it\nreports SyntaxError  \n*args: a tuple of non-keyword arguments  \n**kwargs: a dict of the keyword args passed to the fn  \nPython uses dynamic scope, sees most current value of a variable  \nDefault parameter values are evaluated only when the def statement they belong to is\nfirst executed.  \nFunctions are first-class objects  \nMultiple inheritance is supported.  \nparentClass.__init__(self, x, y)  \nTo execute the method in the parent class in addition to new code for some\nmethod, explicitly call the parent’s version of the method:  \nparentClass.methodName(self, a, b, c)  \n__init__: The constructor for the class.  \n__len__ : Define how len( obj ) works.  \n__copy__: Define how to copy a class.  \n__cmp__ : Define how == works for class.  \n__add__ : Define how + works for class  \n__neg__ : Define how unary negation works for class  \nDuck Typing: look at methods  \nFiltered list comprehension: [elem * 2 for elem in li if elem > 4]  \niterators & generators\n\n## Module 2 Definitions\n* **Mutability** - your definition here\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "46MQLR-Gx8dZ"
      },
      "source": [
        "#Module 3 - Search Problems"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BN0b1eVYx-nd"
      },
      "source": [
        "# Russell and Norvig, AIMA 4th Edition, AIMA Chapter 3 “Problem Solving by Search” (3.1-3.4)\n",
        "chapter3_1 = \"\"\" \"\"\"\n",
        "chapter3_2 = \"\"\" \"\"\"\n",
        "chapter3_3 = \"\"\"  \"\"\"\n",
        "chapter3_4 = \"\"\"  \"\"\"\n",
        "\n",
        "\n",
        "# Lecture notes \n",
        "module3_lecture_notes = \"\"\" Define a search problem: states, initial state, actions, transition model, path cost, goal test.  \n",
        "Generalized algorithm to solve search problems: \n",
        "Enumerate in some order all possible paths from the initial state.  \n",
        "BFS: FIFO queue, complete, optimal, O(b^d) (both time and space )  \n",
        "b: maximum branching factor of search tree  \n",
        "d: depth of the least cost solution  \n",
        "m: maximum depth of the state space (∞)  \n",
        "DFS: LIFO queue, not complete, not optimal , O(b^m) time, O(b*m) space  \n",
        "Iterative deepening search: For l = 0 to ∞ do  \n",
        "depth-limited-search to level l  \n",
        "if it succeeds  \n",
        "then return solution  \n",
        "This is complete and optimal O(b^d) time O(bd) Space\n",
        "\"\"\"\n",
        "\n",
        "# Definitions of concepts from Module 3.\n",
        "# You can define any bolded term in the textbook, \n",
        "# but you most use your own words for the definitions.\n",
        "# Each definition should be at most 20 words long.\n",
        "module3_definitions = {\n",
        "\"problem-solving agent\" : \"an agent that can formulate a plan via search\",    \n",
        "}\n",
        "\n"
      ],
      "execution_count": 44,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lvtN3p7ET3Tx",
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 574
        },
        "outputId": "05a26f18-d15a-4d28-c77c-99da31a4e5ab"
      },
      "source": [
        "#@title Preview of your summary\n",
        "#@markdown You don't need to modify anything in this cell. Just press play.\n",
        "module3_sections = [\n",
        "  (\"AIMA Chapter 3.1 Problem-Solving Agents\", chapter3_1),\n",
        "  (\"AIMA Chapter 3.2 Example Problems\", chapter3_2),\n",
        "  (\"AIMA Chapter 3.3 Search Algorithms\", chapter3_3),\n",
        "  (\"AIMA Chapter 3.4 Uninformed Search Strategies\", chapter3_4),\n",
        "  (\"Module 3 Lecture Notes\", module3_lecture_notes),\n",
        "]\n",
        "\n",
        "module3_markdown = format_module(3, \"Search Problems\", module3_sections, module3_definitions)\n",
        "display(Markdown(module3_markdown))"
      ],
      "execution_count": 45,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 3: Search Problems\n## AIMA Chapter 3.1 Problem-Solving Agents\n \n\n## AIMA Chapter 3.2 Example Problems\n \n\n## AIMA Chapter 3.3 Search Algorithms\n  \n\n## AIMA Chapter 3.4 Uninformed Search Strategies\n  \n\n## Module 3 Lecture Notes\n Define a search problem: states, initial state, actions, transition model, path cost, goal test.  \nGeneralized algorithm to solve search problems: \nEnumerate in some order all possible paths from the initial state.  \nBFS: FIFO queue, complete, optimal, O(b^d) (both time and space )  \nb: maximum branching factor of search tree  \nd: depth of the least cost solution  \nm: maximum depth of the state space (∞)  \nDFS: LIFO queue, not complete, not optimal , O(b^m) time, O(b*m) space  \nIterative deepening search: For l = 0 to ∞ do  \ndepth-limited-search to level l  \nif it succeeds  \nthen return solution  \nThis is complete and optimal O(b^d) time O(bd) Space\n\n\n## Module 3 Definitions\n* **Problem-solving agent** - an agent that can formulate a plan via search\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WHREsHSSyA-h"
      },
      "source": [
        "#Module 4 - Informed Search"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tuFC8w7CyCns"
      },
      "source": [
        "# Russell and Norvig, AIMA Chapter 3 “Problem Solving by Search” (3.5-3.6)\n",
        "chapter3_5 = \"\"\"  \"\"\"\n",
        "chapter3_6 = \"\"\" \"\"\"\n",
        "\n",
        "\n",
        "# Lecture notes \n",
        "module4_lecture_notes = \"\"\" Uniform-cost search (UCS): Expand node with lowest path cost  \n",
        "frontier = priority queue ordered by g(n)  \n",
        "Tests if a node is a goal state when it is selected for expansion, not when it is\n",
        "added to the frontier.  \n",
        "Updates a node on the frontier if a better path to the same state is found.  \n",
        "So always enqueues a node before checking whether it is a goal.  \n",
        "Greedy best-first search: select node for expansion with minimal evaluation function f(n)  \n",
        "Expands the node that is estimated to be closest to goal  \n",
        "Completely ignores g(n): the cost to get to n  \n",
        "frontier = PriorityQueue()  \n",
        "frontier.put(start, 0)  \n",
        "came_from = {}    \n",
        "came_from[start] = None    \n",
        "while not frontier.empty():    \n",
        "  current = frontier.get()    \n",
        "  if current == goal:  \n",
        "    break   \n",
        "  for next in graph.neighbors(current):  \n",
        "    if next not in came_from:  \n",
        "      priority = heuristic(goal, next)  \n",
        "      frontier.put(next, priority)\n",
        "      came_from[next] = current  \n",
        "Not optimal!Not complete\n",
        "A* search: f(n)=g(n) + h(n)  \n",
        "A heuristic h(n) is admissible if it never overestimates the cost to reach the goal;   \n",
        " If h(n) is admissible, A* using Tree Search is optimal  \n",
        " The cost of an optimal solution to a relaxed problem is an admissible heuristic for the\n",
        "original problem  \n",
        "\n",
        " \"\"\"\n",
        "\n",
        "# Definitions\n",
        "module4_definitions = {\n",
        "\"concept\" : \"definition\",    \n",
        "}\n",
        "\n"
      ],
      "execution_count": 46,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vqtEs0gHs9sO",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 695
        },
        "cellView": "form",
        "outputId": "4db33e59-3ebf-419d-9c4e-bf55aafb61a8"
      },
      "source": [
        "#@title Preview of your summary\n",
        "#@markdown You don't need to modify anything in this cell. Just press play.\n",
        "module4_sections = [\n",
        "  (\"AIMA Chapter 3.5 Informed (Heuristic) Search Strategies\", chapter3_5),\n",
        "  (\"AIMA Chapter 3.6 Heuristic Functions\", chapter3_6),\n",
        "  (\"Module 4 Lecture Notes\", module4_lecture_notes),\n",
        "]\n",
        "\n",
        "module4_markdown = format_module(4, \"Informed Search\", \n",
        "                         module4_sections, \n",
        "                         module4_definitions)\n",
        "display(Markdown(module4_markdown))"
      ],
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 4: Informed Search\n## AIMA Chapter 3.5 Informed (Heuristic) Search Strategies\n  \n\n## AIMA Chapter 3.6 Heuristic Functions\n \n\n## Module 4 Lecture Notes\n Uniform-cost search (UCS): Expand node with lowest path cost  \nfrontier = priority queue ordered by g(n)  \nTests if a node is a goal state when it is selected for expansion, not when it is\nadded to the frontier.  \nUpdates a node on the frontier if a better path to the same state is found.  \nSo always enqueues a node before checking whether it is a goal.  \nGreedy best-first search: select node for expansion with minimal evaluation function f(n)  \nExpands the node that is estimated to be closest to goal  \nCompletely ignores g(n): the cost to get to n  \nfrontier = PriorityQueue()  \nfrontier.put(start, 0)  \ncame_from = {}    \ncame_from[start] = None    \nwhile not frontier.empty():    \n  current = frontier.get()    \n  if current == goal:  \n    break   \n  for next in graph.neighbors(current):  \n    if next not in came_from:  \n      priority = heuristic(goal, next)  \n      frontier.put(next, priority)\n      came_from[next] = current  \nNot optimal!Not complete\nA* search: f(n)=g(n) + h(n)  \nA heuristic h(n) is admissible if it never overestimates the cost to reach the goal;   \n If h(n) is admissible, A* using Tree Search is optimal  \n The cost of an optimal solution to a relaxed problem is an admissible heuristic for the\noriginal problem  \n\n \n\n## Module 4 Definitions\n* **Concept** - definition\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "F3FPMzG9yUls"
      },
      "source": [
        "#Module 5 - Adversarial Search and Games"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iEIOpL7vyWcc"
      },
      "source": [
        "# Russell and Norvig, AIMA chapter 5 \"Adversarial Search\"”\" (5.1-5.3, and 5.5).\n",
        "chapter5_1 = \"\"\"  \"\"\"\n",
        "chapter5_2 = \"\"\"  \"\"\"\n",
        "chapter5_3 = \"\"\"  \"\"\"\n",
        "chapter5_5 = \"\"\"  \"\"\"\n",
        "\n",
        "# Russell and Norvig, AIMA Chapter 16 “Making Simple Decisions” (16.1-16.3)\n",
        "chapter16_1 = \"\"\"  \"\"\"\n",
        "chapter16_2 = \"\"\"  \"\"\"\n",
        "chapter16_3 = \"\"\"  \"\"\"\n",
        "\n",
        "# Lecture notes \n",
        "module5_lecture_notes = \"\"\" Evaluation function: Evaluates how good a ‘board position’ is based on static \n",
        "features of the board alone.   \n",
        "Minimax :Make the best move for MAX assuming that MIN always replies with the best\n",
        "move for MIN.  \n",
        "def max-value(state):  \n",
        "if the state is a terminal state:  \n",
        "return the state’s utility  \n",
        "initialize v = -∞  \n",
        "for each successor of state:  \n",
        "v = max(v, min-value(successor))  \n",
        "return v  \n",
        "α: MAX’s current lower bound on MAX’s outcome  \n",
        "β: MIN’s current upper bound on MIN’s outcome   \n",
        "When evaluating a MAX node: a value v ≥ beta is backed-up  \n",
        "• MIN will never select that MAX node  \n",
        "When evaluating a MIN node: a value v ≤ alpha is found  \n",
        "• MAX will never select that MIN node    \n",
        "Expectimax: Values should now reflect average-case (expectimax)\n",
        "outcomes, not worst-case (minimax) outcomes  \n",
        "def exp-value(state):  \n",
        "initialize v = 0  \n",
        "for each successor of state:  \n",
        "p = probability(successor)  \n",
        "v += p * value(successor)  \n",
        "return v  \n",
        "\n",
        "\"\"\"\n",
        "\n",
        "# Definitions\n",
        "module5_definitions = {\n",
        "\"concept\" : \"definition\",    \n",
        "}\n",
        "\n"
      ],
      "execution_count": 48,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "cellView": "form",
        "id": "KUc_LgzDvWf-",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 878
        },
        "outputId": "c33e8d68-f0af-441f-f03d-7cee3c372431"
      },
      "source": [
        "#@title Preview of your summary\n",
        "#@markdown You don't need to modify anything in this cell. Just press play.\n",
        "module5_sections = [\n",
        "  (\"AIMA Chapter 5.1 Game Theory\", chapter5_1),\n",
        "  (\"AIMA Chapter 5.2 Optimal Decisions in Games\", chapter5_2),\n",
        "  (\"AIMA Chapter 5.3 Heuristic Alpha–Beta Tree Search\", chapter5_3),\n",
        "  (\"AIMA Chapter 5.5 Stochastic Games\", chapter5_5),\n",
        "  (\"AIMA Chapter 16.1 Combining Beliefs and Desires under Uncertainty\", chapter16_1),\n",
        "  (\"AIMA Chapter 16.2 The Basis of Utility Theory\", chapter16_2),\n",
        "  (\"AIMA Chapter 16.3 Utility Functions\", chapter16_3),\n",
        "  (\"Module 5 Lecture Notes\", module5_lecture_notes),\n",
        "]\n",
        "\n",
        "module5_markdown = format_module(5, \"Adversarial Search and Games\", \n",
        "                         module5_sections, \n",
        "                         module5_definitions)\n",
        "display(Markdown(module5_markdown))"
      ],
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 5: Adversarial Search and Games\n## AIMA Chapter 5.1 Game Theory\n  \n\n## AIMA Chapter 5.2 Optimal Decisions in Games\n  \n\n## AIMA Chapter 5.3 Heuristic Alpha–Beta Tree Search\n  \n\n## AIMA Chapter 5.5 Stochastic Games\n  \n\n## AIMA Chapter 16.1 Combining Beliefs and Desires under Uncertainty\n  \n\n## AIMA Chapter 16.2 The Basis of Utility Theory\n  \n\n## AIMA Chapter 16.3 Utility Functions\n  \n\n## Module 5 Lecture Notes\n Evaluation function: Evaluates how good a ‘board position’ is based on static \nfeatures of the board alone.   \nMinimax :Make the best move for MAX assuming that MIN always replies with the best\nmove for MIN.  \ndef max-value(state):  \nif the state is a terminal state:  \nreturn the state’s utility  \ninitialize v = -∞  \nfor each successor of state:  \nv = max(v, min-value(successor))  \nreturn v  \nα: MAX’s current lower bound on MAX’s outcome  \nβ: MIN’s current upper bound on MIN’s outcome   \nWhen evaluating a MAX node: a value v ≥ beta is backed-up  \n• MIN will never select that MAX node  \nWhen evaluating a MIN node: a value v ≤ alpha is found  \n• MAX will never select that MIN node    \nExpectimax: Values should now reflect average-case (expectimax)\noutcomes, not worst-case (minimax) outcomes  \ndef exp-value(state):  \ninitialize v = 0  \nfor each successor of state:  \np = probability(successor)  \nv += p * value(successor)  \nreturn v  \n\n\n\n## Module 5 Definitions\n* **Concept** - definition\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qVSQhTVSyYYO"
      },
      "source": [
        "#Module 6 - \"Constraint Satisfaction Problems\""
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Wm4mfW82yaRH"
      },
      "source": [
        "# Russell and Norvig, AIMA, Chapter 6 “Constraint Satisfaction Problems” (6.1-6.5)\n",
        "chapter6_1 = \"\"\"  \"\"\"\n",
        "chapter6_2 = \"\"\"  \"\"\"\n",
        "chapter6_3 = \"\"\" \"\"\"\n",
        "chapter6_4 = \"\"\"  \"\"\"\n",
        "chapter6_5 = \"\"\" \"\"\"\n",
        "\n",
        "\n",
        "# Lecture notes \n",
        "module6_lecture_notes = \"\"\" Find solutions by GENERAL PURPOSE search algorithms with no changes from problem to problem  \n",
        "Finite set of variables X1, X2, …, Xn  \n",
        "Nonempty domain of possible values for each variable  \n",
        "D1, D2, … Dn where Di = {v1, …, vk}  \n",
        "Finite set of constraints C1, C2, …, Cm  \n",
        "Unary, Binary, higher-order contraints.  \n",
        "A CSP can easily be expressed as a search problem  \n",
        "Initial State: the empty assignment {}.  \n",
        "Successor function: Assign value to any unassigned variable \n",
        "provided that there is not a constraint conflict.  \n",
        "Goal test: the current assignment is complete.    \n",
        "Path cost: a constant cost for every step.   \n",
        "Backtracking search is basic uninformed search for CSP  \n",
        "Modifications: Heuristics, check the most constrained variable first, then most constraining variable, least constraining value.   \n",
        "Forward checking detects unevitable failures. \n",
        "Arc consistency: satisfies binary constraints  \n",
        "• (Xi is arc-consistent w.r.t. Xj if for every value v in Di\n",
        ", there is some value w in\n",
        "Dj that satisfies the binary constraint on the arc between Xi and Xj\n",
        ")   \n",
        "X -> Y is consistent iff\n",
        "for every value x of X there is some allowed y.  \n",
        "If X loses a value, recheck neighbors of X.   \n",
        "Given d values, checking arc (i,j) takes O(d2 ) time worst case.  \n",
        "AC-3: \n",
        "inputs: csp, a binary csp with variables {X1, X2, …, Xn}  \n",
        "local variables: queue, a queue of arcs initially the arcs in csp  \n",
        "while queue is not empty do    \n",
        "(Xi, Xj) = queue.pop()  \n",
        "if REMOVE-INCONSISTENT-VALUES(Xi, Xj) then  \n",
        "for each Xk in NEIGHBORS[Xi ] – {Xj} do  \n",
        "add (Xk, Xi) to queue   \n",
        "\n",
        "function REMOVE-INCONSISTENT-VALUES(Xi\n",
        ", Xj\n",
        ") return true iff we remove a value  \n",
        "removed = false  \n",
        "for each x in DOMAIN[Xi] do  \n",
        "if no value y in DOMAIN[Xj] allows (x,y) to satisfy the constraints between Xi and Xj  \n",
        "then delete x from DOMAIN[Xi];   \n",
        " removed = true  \n",
        "return removed  \n",
        "Worst case complexity: O(n^2d^3)  \n",
        " \"\"\"\n",
        "\n",
        "# Definitions\n",
        "module6_definitions = {\n",
        "\"concept\" : \"definition\",    \n",
        "}\n"
      ],
      "execution_count": 50,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "cellView": "form",
        "id": "kKWbCsbfxL3R",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 988
        },
        "outputId": "9012e4f1-5dcd-429f-df99-ec1e4662ac2b"
      },
      "source": [
        "#@title Preview of your summary\n",
        "#@markdown You don't need to modify anything in this cell. Just press play.\n",
        "module6_sections = [\n",
        "  (\"AIMA Chapter 6.1 Defining Constraint Satisfaction Problems\", chapter6_1),\n",
        "  (\"AIMA Chapter 6.2 Constraint Propagation: Inference in CSPs\", chapter6_2),\n",
        "  (\"AIMA Chapter 6.3 Backtracking Search for CSPs\", chapter6_3),\n",
        "  (\"AIMA Chapter 6.4 Local Search for CSPs\", chapter6_4),\n",
        "  (\"AIMA Chapter 6.5 The Structure of Problems\", chapter6_5),\n",
        "  (\"Module 6 Lecture Notes\", module6_lecture_notes),\n",
        "]\n",
        "\n",
        "module6_markdown = format_module(6, \"Constraint Satisfaction Problems\", \n",
        "                         module6_sections, \n",
        "                         module6_definitions)\n",
        "display(Markdown(module6_markdown))"
      ],
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 6: Constraint Satisfaction Problems\n## AIMA Chapter 6.1 Defining Constraint Satisfaction Problems\n  \n\n## AIMA Chapter 6.2 Constraint Propagation: Inference in CSPs\n  \n\n## AIMA Chapter 6.3 Backtracking Search for CSPs\n \n\n## AIMA Chapter 6.4 Local Search for CSPs\n  \n\n## AIMA Chapter 6.5 The Structure of Problems\n \n\n## Module 6 Lecture Notes\n Find solutions by GENERAL PURPOSE search algorithms with no changes from problem to problem  \nFinite set of variables X1, X2, …, Xn  \nNonempty domain of possible values for each variable  \nD1, D2, … Dn where Di = {v1, …, vk}  \nFinite set of constraints C1, C2, …, Cm  \nUnary, Binary, higher-order contraints.  \nA CSP can easily be expressed as a search problem  \nInitial State: the empty assignment {}.  \nSuccessor function: Assign value to any unassigned variable \nprovided that there is not a constraint conflict.  \nGoal test: the current assignment is complete.    \nPath cost: a constant cost for every step.   \nBacktracking search is basic uninformed search for CSP  \nModifications: Heuristics, check the most constrained variable first, then most constraining variable, least constraining value.   \nForward checking detects unevitable failures. \nArc consistency: satisfies binary constraints  \n• (Xi is arc-consistent w.r.t. Xj if for every value v in Di\n, there is some value w in\nDj that satisfies the binary constraint on the arc between Xi and Xj\n)   \nX -> Y is consistent iff\nfor every value x of X there is some allowed y.  \nIf X loses a value, recheck neighbors of X.   \nGiven d values, checking arc (i,j) takes O(d2 ) time worst case.  \nAC-3: \ninputs: csp, a binary csp with variables {X1, X2, …, Xn}  \nlocal variables: queue, a queue of arcs initially the arcs in csp  \nwhile queue is not empty do    \n(Xi, Xj) = queue.pop()  \nif REMOVE-INCONSISTENT-VALUES(Xi, Xj) then  \nfor each Xk in NEIGHBORS[Xi ] – {Xj} do  \nadd (Xk, Xi) to queue   \n\nfunction REMOVE-INCONSISTENT-VALUES(Xi\n, Xj\n) return true iff we remove a value  \nremoved = false  \nfor each x in DOMAIN[Xi] do  \nif no value y in DOMAIN[Xj] allows (x,y) to satisfy the constraints between Xi and Xj  \nthen delete x from DOMAIN[Xi];   \n removed = true  \nreturn removed  \nWorst case complexity: O(n^2d^3)  \n \n\n## Module 6 Definitions\n* **Concept** - definition\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RYESi2kMybL8"
      },
      "source": [
        "#Module 7 - Logical Agents"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qIRJ4YYCzNHL"
      },
      "source": [
        "# Russell and Norvig, AIMA Chapter 7 “Logical Agents” (Sections 7.1-7.5)\n",
        "\n",
        "chapter7_1 = \"\"\" \"\"\"\n",
        "chapter7_2 = \"\"\"  \"\"\"\n",
        "chapter7_3 = \"\"\"  \"\"\"\n",
        "chapter7_4 = \"\"\"\"\"\"\n",
        "chapter7_5 = \"\"\"  \"\"\"\n",
        "\n",
        "# Lecture notes \n",
        "module7_lecture_notes = \"\"\"If sentence alpha is true in model m then we say  \n",
        "• m satisfies alpha, or  \n",
        "• m is a model of alpha\n",
        "We use the notation M(alpha) to mean the set of all models of alpha.  \n",
        "Possible Worlds is the process of\n",
        "enumerating all Possible Worlds that are\n",
        "compatible with the KB. M(KB) ⊆ M(alpha1 )     \n",
        "Modus Ponens, Biconditional Elimination, Elimination  \n",
        "Monotonicity says that the set of entailed sentence can only increase as information is\n",
        "assed to the KB.  \n",
        "If ℓi and m are complementary literals,\n",
        "which means that one is the negation of\n",
        "another, then we can eliminate them via\n",
        "unit resolution.  \n",
        "Conjunctive Normal Form (CNF)    \n",
        "Eliminate ⇔ by replacing  α⇔ β with (α ⟹ β) ⋀ (β ⟹α)  \n",
        "2. Eliminate ⟹ by replacing α ⟹ β with ¬α ⋁ β  \n",
        "3. Move ¬ inwards (eliminate double negation, and apply De Morgan’s rules  \n",
        "4. Now the sentence will contain nested ⋀ and ⋁ operators applied to literals. Apply\n",
        "distributivity law to distribute ⋁ over ⋀ wherever possible.   \n",
        "To show KB⊨α then we can show that KB ⋀ ¬α is unsatisfiable.  \n",
        "Here’s how:  \n",
        "• First, convert (KB ⋀ ¬α) into CNF  \n",
        "• Then apply the resolution rule to the resulting clauses.  \n",
        "• Resolve every pair that contains complementary literals to produce a new clause  \n",
        "• If that new clause isn’t already present, then add it to the set.  \n",
        "• Continue the process until one of two things happens:  \n",
        "1. There are no new clauses that can be added. In this case, the KB does not entail α  \n",
        "2. Two clauses resolve to yield the empty clause, in which case the KB does entail α   \n",
        "Horn clauses are closed under resolution. If you apply resolution to a horn clause, you\n",
        "get back a horn clause.   \n",
        "Inference with Horn clauses can be done through the forward-chaining and\n",
        "backward-chaining algorithms  \n",
        "Deciding entailment with Horn clauses can be done in linear time with respect to\n",
        "the size of the KB.  \n",
        "Backward chaining works backwards from a query. If the query is known to be true,\n",
        "then no work is needed. Otherwise, the algorithm finds implications in the KB whose\n",
        "conclusions is the query. If all the premises of one of those can be proved to be true\n",
        "(via backward chaining), then the query is true.\n",
        "\n",
        "\"\"\"\n",
        "\n",
        "# Definitions\n",
        "module7_definitions = {\n",
        "\"Definite clause\" : \"disjunction of literals where exactly one is positive\", \n",
        "\"Horn clause\" : \"disjunction of literals where at most one is positive\",\n",
        "}\n",
        "\n"
      ],
      "execution_count": 52,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "cellView": "form",
        "id": "lXNcHuKjyNXa",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 892
        },
        "outputId": "f319ceaa-fadd-40a3-8e78-6bbb413e6aac"
      },
      "source": [
        "#@title Preview of your summary\n",
        "#@markdown You don't need to modify anything in this cell. Just press play.\n",
        "module7_sections = [\n",
        "  (\"AIMA Chapter 7.1 Knowledge-Based Agents\", chapter7_1),\n",
        "  (\"AIMA Chapter 7.2 The Wumpus World\", chapter7_2),\n",
        "  (\"AIMA Chapter 7.3 Logic\", chapter7_3),\n",
        "  (\"AIMA Chapter 7.4 Propositional Logic: A Very Simple Logic\", chapter7_4),\n",
        "  (\"AIMA 7.5 Propositional Theorem Proving\", chapter7_5),\n",
        "  (\"Module 7 Lecture Notes\", module7_lecture_notes),\n",
        "]\n",
        "\n",
        "module7_markdown = format_module(7, \"Logical Agents\", \n",
        "                         module7_sections, \n",
        "                         module7_definitions)\n",
        "display(Markdown(module7_markdown))"
      ],
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 7: Logical Agents\n## AIMA Chapter 7.1 Knowledge-Based Agents\n \n\n## AIMA Chapter 7.2 The Wumpus World\n  \n\n## AIMA Chapter 7.3 Logic\n  \n\n## AIMA Chapter 7.4 Propositional Logic: A Very Simple Logic\n\n\n## AIMA 7.5 Propositional Theorem Proving\n  \n\n## Module 7 Lecture Notes\nIf sentence alpha is true in model m then we say  \n• m satisfies alpha, or  \n• m is a model of alpha\nWe use the notation M(alpha) to mean the set of all models of alpha.  \nPossible Worlds is the process of\nenumerating all Possible Worlds that are\ncompatible with the KB. M(KB) ⊆ M(alpha1 )     \nModus Ponens, Biconditional Elimination, Elimination  \nMonotonicity says that the set of entailed sentence can only increase as information is\nassed to the KB.  \nIf ℓi and m are complementary literals,\nwhich means that one is the negation of\nanother, then we can eliminate them via\nunit resolution.  \nConjunctive Normal Form (CNF)    \nEliminate ⇔ by replacing  α⇔ β with (α ⟹ β) ⋀ (β ⟹α)  \n2. Eliminate ⟹ by replacing α ⟹ β with ¬α ⋁ β  \n3. Move ¬ inwards (eliminate double negation, and apply De Morgan’s rules  \n4. Now the sentence will contain nested ⋀ and ⋁ operators applied to literals. Apply\ndistributivity law to distribute ⋁ over ⋀ wherever possible.   \nTo show KB⊨α then we can show that KB ⋀ ¬α is unsatisfiable.  \nHere’s how:  \n• First, convert (KB ⋀ ¬α) into CNF  \n• Then apply the resolution rule to the resulting clauses.  \n• Resolve every pair that contains complementary literals to produce a new clause  \n• If that new clause isn’t already present, then add it to the set.  \n• Continue the process until one of two things happens:  \n1. There are no new clauses that can be added. In this case, the KB does not entail α  \n2. Two clauses resolve to yield the empty clause, in which case the KB does entail α   \nHorn clauses are closed under resolution. If you apply resolution to a horn clause, you\nget back a horn clause.   \nInference with Horn clauses can be done through the forward-chaining and\nbackward-chaining algorithms  \nDeciding entailment with Horn clauses can be done in linear time with respect to\nthe size of the KB.  \nBackward chaining works backwards from a query. If the query is known to be true,\nthen no work is needed. Otherwise, the algorithm finds implications in the KB whose\nconclusions is the query. If all the premises of one of those can be proved to be true\n(via backward chaining), then the query is true.\n\n\n\n## Module 7 Definitions\n* **Definite clause** - disjunction of literals where exactly one is positive\n* **Horn clause** - disjunction of literals where at most one is positive\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vX7lbnJd4sT5"
      },
      "source": [
        "# Your Exam Notes\n",
        "\n",
        "Here are your full set of exam notes that you can use on your midterm.  Choose **Runtime > Run all**, then your notes will be displayed.\n",
        "\n",
        "If you are taking the exam in person, then you must print a copy of your notes for yourself.   Here's how to print:\n",
        "* Run the cell below. \n",
        "* Click its \"Mirror cell in tab\" button (this is located in the upper right corner of the cell, immediately to the right of the trash can).  This will open the cell.  \n",
        "* In Chrome, you can then print your notes using the **File > Print** option from the browser's menu.  \n",
        "* In the print options, change the layout from Landscape to Portrait. \n",
        "* Clicking the Save, will save a PDF that you can then print.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 5458
        },
        "id": "T3cm1WV542_a",
        "outputId": "62ccf694-00f2-4a9b-9be9-fee14dfa5f76"
      },
      "source": [
        "# This will generate the full set of notes that you can use for your exam.\n",
        "full_markdown = \"\\n\\n\".join([module1_markdown, \n",
        "                             module2_markdown, \n",
        "                             module3_markdown, \n",
        "                             module4_markdown, \n",
        "                             module5_markdown,\n",
        "                             module6_markdown,\n",
        "                             module7_markdown,]\n",
        "                       )\n",
        "display(Markdown(full_markdown))"
      ],
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/markdown": "# Module 1: Rational Agents and Task Environments\n## AIMA Chapter 1.1 What is AI?\n Rationality: doing the right thing. \nAbilities computers need to pass Turing test: natural language processing, knowledge representation, \nautomated reasoning, machine learning.\nValue alignment problem, the values or objectives put into the machine must be aligned with those of human.\n \n\n## AIMA Chapter 27.1 The Limits of AI\n Good Old-Fashioned AI.\n \n\n## AIMA Chapter 27.1 Can Machines Really Think?\n \n\n## AIMA 2.1 Agents and Environments\n The agent function for an artificial agent will be implemented by an agent program. Agent is meant to be a tool for \nanalyzing systems, not absolute characterization. \n\n## AIMA 2.2 Good Behavior: The Concept of Rationality\nPerformance meaure evalutes any given sequence of environmental states. It's better to design performance measures according to what actually wants to be achieved, not how one thinks agent should behave.Being rational depends on four things: (1) performance meatures that defines success (2) agent's prior knowledge of environment (3)\nactions that the agent can perform (4) Agent's percept sequence Rationality is not the same as perfection.\n \n\n## AIMA 2.3 The Nature of Environments\n Task Environments: PEAS(Performance, Environment, Actuators, Sensors)Properties of task Environments:Fully observable vs. partially observableSingle agent vs. multiagentdeterministic vs. nondeterministicepisodic vs. sequentialstatic vs. dynamicDiscrete vs. continuous\nKnown vs. Unknown Experiments are carried out for many environments from environment class\n\n## AIMA 2.4 The Structure of Agents\n Simple reflex agents select actions on basis of the current percept.Model-based reflex agents, internal state depends on percept history Goal-based agentsUtility-based agentsLearning agents: learning element, performance element, critic, problem generator\n\n## Module 1 Lecture Notes\n\n\n## Module 1 Definitions\n* **Agent** - anything that can perceive through sensors and act in an environment through actuators\n* **Percept** - contents agent's sensors are perceiving\n* **Percept sequence** - all history of what the agent ever perceived.\n* **Agent function** - maps given percept sequence to action\n* **Embodied cognition** - No need to consider the brain separately\n* **Biological naturalism** - menetal states are high level emergent features caused by low level physical processed in neurons\n* **Rational agent** - One that acts to achieve the best expected outcome\n* **Consequentialism** - evaluate agent behavior by consequences\n* **Agent architecture** - agent program run on computing device that has sensors and actuators\n\n\n# Module 2: Python\n## Module 2 Lecture Notes\nAll data are treated as objects.   \nStrong typing  \nDynamic typing   \nString is immutable, use % to format, use + to concatenate  \nLists are mutable, direct assignment creates reference  \n== checks value equal, 'is' checks object equal  \nTuples: immutable  \nDictionary key must be immutable  \nFor functions returning multiple values, return type is tuple  \nNo function overloading  \nNon-default argument should always precede default arguments; otherwise, it\nreports SyntaxError  \n*args: a tuple of non-keyword arguments  \n**kwargs: a dict of the keyword args passed to the fn  \nPython uses dynamic scope, sees most current value of a variable  \nDefault parameter values are evaluated only when the def statement they belong to is\nfirst executed.  \nFunctions are first-class objects  \nMultiple inheritance is supported.  \nparentClass.__init__(self, x, y)  \nTo execute the method in the parent class in addition to new code for some\nmethod, explicitly call the parent’s version of the method:  \nparentClass.methodName(self, a, b, c)  \n__init__: The constructor for the class.  \n__len__ : Define how len( obj ) works.  \n__copy__: Define how to copy a class.  \n__cmp__ : Define how == works for class.  \n__add__ : Define how + works for class  \n__neg__ : Define how unary negation works for class  \nDuck Typing: look at methods  \nFiltered list comprehension: [elem * 2 for elem in li if elem > 4]  \niterators & generators\n\n## Module 2 Definitions\n* **Mutability** - your definition here\n\n\n# Module 3: Search Problems\n## AIMA Chapter 3.1 Problem-Solving Agents\n \n\n## AIMA Chapter 3.2 Example Problems\n \n\n## AIMA Chapter 3.3 Search Algorithms\n  \n\n## AIMA Chapter 3.4 Uninformed Search Strategies\n  \n\n## Module 3 Lecture Notes\n Define a search problem: states, initial state, actions, transition model, path cost, goal test.  \nGeneralized algorithm to solve search problems: \nEnumerate in some order all possible paths from the initial state.  \nBFS: FIFO queue, complete, optimal, O(b^d) (both time and space )  \nb: maximum branching factor of search tree  \nd: depth of the least cost solution  \nm: maximum depth of the state space (∞)  \nDFS: LIFO queue, not complete, not optimal , O(b^m) time, O(b*m) space  \nIterative deepening search: For l = 0 to ∞ do  \ndepth-limited-search to level l  \nif it succeeds  \nthen return solution  \nThis is complete and optimal O(b^d) time O(bd) Space\n\n\n## Module 3 Definitions\n* **Problem-solving agent** - an agent that can formulate a plan via search\n\n\n# Module 4: Informed Search\n## AIMA Chapter 3.5 Informed (Heuristic) Search Strategies\n  \n\n## AIMA Chapter 3.6 Heuristic Functions\n \n\n## Module 4 Lecture Notes\n Uniform-cost search (UCS): Expand node with lowest path cost  \nfrontier = priority queue ordered by g(n)  \nTests if a node is a goal state when it is selected for expansion, not when it is\nadded to the frontier.  \nUpdates a node on the frontier if a better path to the same state is found.  \nSo always enqueues a node before checking whether it is a goal.  \nGreedy best-first search: select node for expansion with minimal evaluation function f(n)  \nExpands the node that is estimated to be closest to goal  \nCompletely ignores g(n): the cost to get to n  \nfrontier = PriorityQueue()  \nfrontier.put(start, 0)  \ncame_from = {}    \ncame_from[start] = None    \nwhile not frontier.empty():    \n  current = frontier.get()    \n  if current == goal:  \n    break   \n  for next in graph.neighbors(current):  \n    if next not in came_from:  \n      priority = heuristic(goal, next)  \n      frontier.put(next, priority)\n      came_from[next] = current  \nNot optimal!Not complete\nA* search: f(n)=g(n) + h(n)  \nA heuristic h(n) is admissible if it never overestimates the cost to reach the goal;   \n If h(n) is admissible, A* using Tree Search is optimal  \n The cost of an optimal solution to a relaxed problem is an admissible heuristic for the\noriginal problem  \n\n \n\n## Module 4 Definitions\n* **Concept** - definition\n\n\n# Module 5: Adversarial Search and Games\n## AIMA Chapter 5.1 Game Theory\n  \n\n## AIMA Chapter 5.2 Optimal Decisions in Games\n  \n\n## AIMA Chapter 5.3 Heuristic Alpha–Beta Tree Search\n  \n\n## AIMA Chapter 5.5 Stochastic Games\n  \n\n## AIMA Chapter 16.1 Combining Beliefs and Desires under Uncertainty\n  \n\n## AIMA Chapter 16.2 The Basis of Utility Theory\n  \n\n## AIMA Chapter 16.3 Utility Functions\n  \n\n## Module 5 Lecture Notes\n Evaluation function: Evaluates how good a ‘board position’ is based on static \nfeatures of the board alone.   \nMinimax :Make the best move for MAX assuming that MIN always replies with the best\nmove for MIN.  \ndef max-value(state):  \nif the state is a terminal state:  \nreturn the state’s utility  \ninitialize v = -∞  \nfor each successor of state:  \nv = max(v, min-value(successor))  \nreturn v  \nα: MAX’s current lower bound on MAX’s outcome  \nβ: MIN’s current upper bound on MIN’s outcome   \nWhen evaluating a MAX node: a value v ≥ beta is backed-up  \n• MIN will never select that MAX node  \nWhen evaluating a MIN node: a value v ≤ alpha is found  \n• MAX will never select that MIN node    \nExpectimax: Values should now reflect average-case (expectimax)\noutcomes, not worst-case (minimax) outcomes  \ndef exp-value(state):  \ninitialize v = 0  \nfor each successor of state:  \np = probability(successor)  \nv += p * value(successor)  \nreturn v  \n\n\n\n## Module 5 Definitions\n* **Concept** - definition\n\n\n# Module 6: Constraint Satisfaction Problems\n## AIMA Chapter 6.1 Defining Constraint Satisfaction Problems\n  \n\n## AIMA Chapter 6.2 Constraint Propagation: Inference in CSPs\n  \n\n## AIMA Chapter 6.3 Backtracking Search for CSPs\n \n\n## AIMA Chapter 6.4 Local Search for CSPs\n  \n\n## AIMA Chapter 6.5 The Structure of Problems\n \n\n## Module 6 Lecture Notes\n Find solutions by GENERAL PURPOSE search algorithms with no changes from problem to problem  \nFinite set of variables X1, X2, …, Xn  \nNonempty domain of possible values for each variable  \nD1, D2, … Dn where Di = {v1, …, vk}  \nFinite set of constraints C1, C2, …, Cm  \nUnary, Binary, higher-order contraints.  \nA CSP can easily be expressed as a search problem  \nInitial State: the empty assignment {}.  \nSuccessor function: Assign value to any unassigned variable \nprovided that there is not a constraint conflict.  \nGoal test: the current assignment is complete.    \nPath cost: a constant cost for every step.   \nBacktracking search is basic uninformed search for CSP  \nModifications: Heuristics, check the most constrained variable first, then most constraining variable, least constraining value.   \nForward checking detects unevitable failures. \nArc consistency: satisfies binary constraints  \n• (Xi is arc-consistent w.r.t. Xj if for every value v in Di\n, there is some value w in\nDj that satisfies the binary constraint on the arc between Xi and Xj\n)   \nX -> Y is consistent iff\nfor every value x of X there is some allowed y.  \nIf X loses a value, recheck neighbors of X.   \nGiven d values, checking arc (i,j) takes O(d2 ) time worst case.  \nAC-3: \ninputs: csp, a binary csp with variables {X1, X2, …, Xn}  \nlocal variables: queue, a queue of arcs initially the arcs in csp  \nwhile queue is not empty do    \n(Xi, Xj) = queue.pop()  \nif REMOVE-INCONSISTENT-VALUES(Xi, Xj) then  \nfor each Xk in NEIGHBORS[Xi ] – {Xj} do  \nadd (Xk, Xi) to queue   \n\nfunction REMOVE-INCONSISTENT-VALUES(Xi\n, Xj\n) return true iff we remove a value  \nremoved = false  \nfor each x in DOMAIN[Xi] do  \nif no value y in DOMAIN[Xj] allows (x,y) to satisfy the constraints between Xi and Xj  \nthen delete x from DOMAIN[Xi];   \n removed = true  \nreturn removed  \nWorst case complexity: O(n^2d^3)  \n \n\n## Module 6 Definitions\n* **Concept** - definition\n\n\n# Module 7: Logical Agents\n## AIMA Chapter 7.1 Knowledge-Based Agents\n \n\n## AIMA Chapter 7.2 The Wumpus World\n  \n\n## AIMA Chapter 7.3 Logic\n  \n\n## AIMA Chapter 7.4 Propositional Logic: A Very Simple Logic\n\n\n## AIMA 7.5 Propositional Theorem Proving\n  \n\n## Module 7 Lecture Notes\nIf sentence alpha is true in model m then we say  \n• m satisfies alpha, or  \n• m is a model of alpha\nWe use the notation M(alpha) to mean the set of all models of alpha.  \nPossible Worlds is the process of\nenumerating all Possible Worlds that are\ncompatible with the KB. M(KB) ⊆ M(alpha1 )     \nModus Ponens, Biconditional Elimination, Elimination  \nMonotonicity says that the set of entailed sentence can only increase as information is\nassed to the KB.  \nIf ℓi and m are complementary literals,\nwhich means that one is the negation of\nanother, then we can eliminate them via\nunit resolution.  \nConjunctive Normal Form (CNF)    \nEliminate ⇔ by replacing  α⇔ β with (α ⟹ β) ⋀ (β ⟹α)  \n2. Eliminate ⟹ by replacing α ⟹ β with ¬α ⋁ β  \n3. Move ¬ inwards (eliminate double negation, and apply De Morgan’s rules  \n4. Now the sentence will contain nested ⋀ and ⋁ operators applied to literals. Apply\ndistributivity law to distribute ⋁ over ⋀ wherever possible.   \nTo show KB⊨α then we can show that KB ⋀ ¬α is unsatisfiable.  \nHere’s how:  \n• First, convert (KB ⋀ ¬α) into CNF  \n• Then apply the resolution rule to the resulting clauses.  \n• Resolve every pair that contains complementary literals to produce a new clause  \n• If that new clause isn’t already present, then add it to the set.  \n• Continue the process until one of two things happens:  \n1. There are no new clauses that can be added. In this case, the KB does not entail α  \n2. Two clauses resolve to yield the empty clause, in which case the KB does entail α   \nHorn clauses are closed under resolution. If you apply resolution to a horn clause, you\nget back a horn clause.   \nInference with Horn clauses can be done through the forward-chaining and\nbackward-chaining algorithms  \nDeciding entailment with Horn clauses can be done in linear time with respect to\nthe size of the KB.  \nBackward chaining works backwards from a query. If the query is known to be true,\nthen no work is needed. Otherwise, the algorithm finds implications in the KB whose\nconclusions is the query. If all the premises of one of those can be proved to be true\n(via backward chaining), then the query is true.\n\n\n\n## Module 7 Definitions\n* **Definite clause** - disjunction of literals where exactly one is positive\n* **Horn clause** - disjunction of literals where at most one is positive\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {}
        }
      ]
    }
  ]
}