{
  "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": "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",
      "execution_count": 26,
      "metadata": {
        "id": "2RECnJArJew4"
      },
      "outputs": [],
      "source": [
        "name = \"\"\n",
        "permission = True\n",
        "anonymous = True"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GZZXyrV-xuAp"
      },
      "source": [
        "# Module 1 - Intro to AI"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {
        "id": "Y2NfE1CKxzS-"
      },
      "outputs": [],
      "source": [
        "# Russell and Norvig, AIMA 4th Edition, Chapter 1 “Introduction”, Section 1.1 “What is AI”\n",
        "\n",
        "chapter1_1 = \"\"\" Machine Learning is a subfield of AI. Turing tests, cognitive science, and rational thought offer different ways of \\\n",
        "defining artificial intelligence. Turing tests involve external testing, determining if some AI seems human or not from the outside. \\\n",
        "While correct inferences are one way to achieve rationality, there are other ways of being a rational agent. \\\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 = \"\"\" Weak AI = act as if intelligent. Strong AI = actually consciously thinking. \\\n",
        "Biological brains are good at actions, but bad at logic, so logic systems fail to capture everything \\\n",
        "about intelligence. Embodied cognition: consider the brain as a part of a system (sensors, vision are central, not peripheral). \\\n",
        "Computers exceed humans in some respects but fall behind in others. Godel's incompleteness theorems suggest that \\ \n",
        "logic systems cannot be consistent. \"\"\"\n",
        "chapter27_2 = \"\"\" Alan Turing introduced a behavioral test instead of the question Can machines \\\n",
        "think? Systems performance was more important than the ability to imitate humans, he thought. \"\"\"\n",
        "\n",
        "# Russell and Norvig, AIMA 4th Edition, Chapter 2 “Intelligent Agents” (whole chapter)\n",
        "chapter2_1 = \"\"\" Agent = perceives and acts in environment. \\\n",
        "Agent Function = specifies action taken by agent in response to percept sequence. \\\n",
        "Performance measure = evaulates behavior of environemnt. \"\"\"\n",
        "chapter2_2 = \"\"\" A task environment specification includes the performance measure, \\\n",
        "the external environment, the actuators, and the sensors. In designing an agent, \\\n",
        "the first step must always be to specify the task environment as fully as possible.\n",
        "Task environments vary along several significant dimensions.  \\\n",
        "They can be fully or partially observable, single-agent or multiagent, deterministic \\\n",
        "or nondeterministic, episodic or sequential, static or dynamic, discrete or continuous, \\\n",
        "and known or unknown. \"\"\"\n",
        "chapter2_3 = \"\"\" In cases where the performance measure is unknown or hard to specify correctly, there is a significant risk of the agent optimizing the wrong objective. In such cases the agent design should reflect uncertainty about the true objective \"\"\"\n",
        "chapter2_4 = \"\"\" The agent program implements the agent function. There exists a variety of basic agent program designs reflecting the kind of information made explicit and used in the decision process. The designs vary in efficiency, compactness, and flexibility. The appropriate design of the agent program depends on the nature of the environment\n",
        "Simple reflex agents respond directly to percepts, whereas model-based reflex agents maintain internal state to track aspects of the world that are not evident in the current percept. Goal-based agents act to achieve their goals, and utility-based agents try to maximize their own expected “happiness. \"\"\"\n",
        "\n",
        "# Lecture notes \n",
        "module1_lecture_notes = \"\"\" Philosophers have had different takes on what it means to be \\\n",
        "intelligent.  \"\"\"\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 and act in an environment\",\n",
        "\"percept\" : \"your definitions here\",\n",
        "\"agent function\" : \"specifies the action taken by the agent in response to any percept sequence\",\n",
        "\"rational agent\": \"acts so as to maximize the expected value of the performance measure, given the percept sequence it has seen so far\",\n",
        "\"task environment\": \"includes the performance measure, the external environment, the actuators, and the sensors\",\n",
        "\"agent program\": \"implements agent function\"\n",
        "}\n",
        "\n"
      ]
    },
    {
      "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",
      "execution_count": 28,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gYYeNcdM_maE",
        "outputId": "10ce52d9-d4f9-4137-99ff-9578dcdade24"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "## WARNING: Your definition of 'rational agent' is too long!\n",
              "# Module 1: Rational Agents and Task Environments\n",
              "## AIMA Chapter 1.1 What is AI?\n",
              " Machine Learning is a subfield of AI. Turing tests, cognitive science, and rational thought offer different ways of defining artificial intelligence. Turing tests involve external testing, determining if some AI seems human or not from the outside. While correct inferences are one way to achieve rationality, there are other ways of being a rational agent. \n",
              "\n",
              "## AIMA Chapter 27.1 The Limits of AI\n",
              " Weak AI = act as if intelligent. Strong AI = actually consciously thinking. Biological brains are good at actions, but bad at logic, so logic systems fail to capture everything about intelligence. Embodied cognition: consider the brain as a part of a system (sensors, vision are central, not peripheral). Computers exceed humans in some respects but fall behind in others. Godel's incompleteness theorems suggest that \\ \n",
              "logic systems cannot be consistent. \n",
              "\n",
              "## AIMA Chapter 27.1 Can Machines Really Think?\n",
              " Alan Turing introduced a behavioral test instead of the question Can machines think? Systems performance was more important than the ability to imitate humans, he thought. \n",
              "\n",
              "## AIMA 2.1 Agents and Environments\n",
              " Agent = perceives and acts in environment. Agent Function = specifies action taken by agent in response to percept sequence. Performance measure = evaulates behavior of environemnt. \n",
              "\n",
              "## AIMA 2.2 Good Behavior: The Concept of Rationality\n",
              " A task environment specification includes the performance measure, the external environment, the actuators, and the sensors. In designing an agent, the first step must always be to specify the task environment as fully as possible.\n",
              "Task environments vary along several significant dimensions.  They can be fully or partially observable, single-agent or multiagent, deterministic or nondeterministic, episodic or sequential, static or dynamic, discrete or continuous, and known or unknown. \n",
              "\n",
              "## AIMA 2.3 The Nature of Environments\n",
              " In cases where the performance measure is unknown or hard to specify correctly, there is a significant risk of the agent optimizing the wrong objective. In such cases the agent design should reflect uncertainty about the true objective \n",
              "\n",
              "## AIMA 2.4 The Structure of Agents\n",
              " The agent program implements the agent function. There exists a variety of basic agent program designs reflecting the kind of information made explicit and used in the decision process. The designs vary in efficiency, compactness, and flexibility. The appropriate design of the agent program depends on the nature of the environment\n",
              "Simple reflex agents respond directly to percepts, whereas model-based reflex agents maintain internal state to track aspects of the world that are not evident in the current percept. Goal-based agents act to achieve their goals, and utility-based agents try to maximize their own expected “happiness. \n",
              "\n",
              "## Module 1 Lecture Notes\n",
              " Philosophers have had different takes on what it means to be intelligent.  \n",
              "\n",
              "## Module 1 Definitions\n",
              "* **Agent** - anything that can perceive and act in an environment\n",
              "* **Percept** - your definitions here\n",
              "* **Agent function** - specifies the action taken by the agent in response to any percept sequence\n",
              "* **Rational agent** - acts so as to maximize the expected value of the performance measure, given the percept sequence it has seen so far\n",
              "* **Task environment** - includes the performance measure, the external environment, the actuators, and the sensors\n",
              "* **Agent program** - implements agent function\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9xRbdqDox2zD"
      },
      "source": [
        "#Module 2 - Python"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "metadata": {
        "id": "DpBvu7EMx7sy"
      },
      "outputs": [],
      "source": [
        "# No readings for this module.  Instead you can summarize the lecture instead.\n",
        "\n",
        "# Lecture notes \n",
        "module2_lecture_notes = \"\"\"  \"\"\"\n",
        "\n",
        "# Each definition should be at most 20 words long.\n",
        "module2_definitions = {\n",
        "\"mutability\" : \"your definition here\",\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ydOK__lwWgdp",
        "outputId": "6c9789f4-242b-4b15-98f8-42d0e16986a9"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "# Module 2: Python\n",
              "## Module 2 Lecture Notes\n",
              "  \n",
              "\n",
              "## Module 2 Definitions\n",
              "* **Mutability** - your definition here\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "46MQLR-Gx8dZ"
      },
      "source": [
        "#Module 3 - Search Problems"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 31,
      "metadata": {
        "id": "BN0b1eVYx-nd"
      },
      "outputs": [],
      "source": [
        "# Russell and Norvig, AIMA 4th Edition, AIMA Chapter 3 “Problem Solving by Search” (3.1-3.4)\n",
        "chapter3_1 = \"\"\"  Search algorithms are judged on the basis of completeness, cost optimality, \\\n",
        "    time complexity and space complexity.\"\"\"\n",
        "chapter3_2 = \"\"\"  \"\"\"\n",
        "chapter3_3 = \"\"\"  \"\"\"\n",
        "chapter3_4 = \"\"\" The performance of heuristic search algorithms depends on the quality of the heuristic function. One can sometimes construct good heuristics by relaxing the problem definition, by storing precomputed solution costs for subproblems in a pattern database, by defining landmarks, or by learning from experience with the problem class \"\"\"\n",
        "\n",
        "\n",
        "# Lecture notes \n",
        "module3_lecture_notes = \"\"\"  \n",
        "Search Strategies:\n",
        "strategy = order of tree expansion. implemented by different (FIFO, LIFO, priority) queue structures\n",
        "Dimensions for evaluation: completeness (always find a soln), optimality (finds a least cost soln first), time complexity (# nodes generated), space complexity (# nodes simultaneously in memory)\n",
        "Time/Space Complexity Variables:\n",
        "- b = max branching factor of search tree\n",
        "- d = depth of shallowest node\n",
        "- m = max length of any path in the space (potentially inf)\n",
        "\n",
        "If h2(n) >= h1(n) for all n (both admissible), then h2 dominates h1. h2 is optimistic, but more accurate than h1. h2 is better for search.\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",
        "\"problem\": \"initial state, set of actions, transition model describing the results of those actions \\\n",
        "    set of goal states, action cost function\",  \n",
        "\"solution\": \"A path through the state space (a sequence of actions) from the initial state to a goal state is a solution\",\n",
        "\"atomic\": \"states and actions do no have any internal structure\",\n",
        "\"uninformed search\": \"only have access to the problem defintion\",\n",
        "\"best-first search\": \"selects nodes for expansion using an evaluation function\",\n",
        "\"breadth-first search\": \"expands the shallowest nodes first. complete, optimal for unit action \\\n",
        "    costs, has exponential space complexity\",\n",
        "\"uniform cost search\": \"expands the node with the lowest path cost g(n) and is optimal for general action \\\n",
        "    costs\",\n",
        "\"depth first search\": \"expands the deepest unexpanded node first. neither complete nor nor optimal, \\\n",
        "    but has linear space complexity\",\n",
        "\"iterative deepening search\": \"dfs w/ increasing depth limits until a goal is found\",\n",
        "\"bidirectional search\": \"expands two frontiers, one around the initial state and on around the goal, \\\n",
        "    stopping when the two frontiers meet.\",\n",
        "\"informed search\": \"have access to a heuristic function h(n) that estimates the cost of a solution from n\",\n",
        "\"greedy best-first search\": \"expands nodes with minimal h(n). not optimal but often efficient\",\n",
        "\"a* search\": \"expands nodes with minimal f(n) = g(n) + h(n). A* is complete and optimal, if h(n) is admissible. large space complexity\",\n",
        "\"iterative deepening a* (ida)\": \"iterative deepening for a*, solving space complexity issue\",\n",
        "\"rbfs (recursive best first search) and sma (simplified memory-bounded A*)\": \"robust, optimal search algos that use limited amounts of memory \\\n",
        "    given enough time, they can solve problems where A* runs out of memory\",\n",
        "\"beam search\": \"puts a limit on the size of the frontier. makes it incomplete, suboptimal, but often finds reasonably good search solutions and runs faster than complete searches\",\n",
        "\"weighted a*\": \"focuses the search towards a goal, expanding fewer nodes but sacrificing optimality\"\n",
        "}\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lvtN3p7ET3Tx",
        "outputId": "f8c6dc37-627d-494b-d493-ce53672224a4"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "## WARNING: Your definition of 'problem' is too long!\n",
              "WARNING: Your definition of 'solution' is too long!\n",
              "WARNING: Your definition of 'bidirectional search' is too long!\n",
              "WARNING: Your definition of 'a* search' is too long!\n",
              "WARNING: Your definition of 'rbfs (recursive best first search) and sma (simplified memory-bounded A*)' is too long!\n",
              "WARNING: Your definition of 'beam search' is too long!\n",
              "# Module 3: Search Problems\n",
              "## AIMA Chapter 3.1 Problem-Solving Agents\n",
              "  Search algorithms are judged on the basis of completeness, cost optimality,     time complexity and space complexity.\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",
              " The performance of heuristic search algorithms depends on the quality of the heuristic function. One can sometimes construct good heuristics by relaxing the problem definition, by storing precomputed solution costs for subproblems in a pattern database, by defining landmarks, or by learning from experience with the problem class \n",
              "\n",
              "## Module 3 Lecture Notes\n",
              "  \n",
              "Search Strategies:\n",
              "strategy = order of tree expansion. implemented by different (FIFO, LIFO, priority) queue structures\n",
              "Dimensions for evaluation: completeness (always find a soln), optimality (finds a least cost soln first), time complexity (# nodes generated), space complexity (# nodes simultaneously in memory)\n",
              "Time/Space Complexity Variables:\n",
              "- b = max branching factor of search tree\n",
              "- d = depth of shallowest node\n",
              "- m = max length of any path in the space (potentially inf)\n",
              "\n",
              "If h2(n) >= h1(n) for all n (both admissible), then h2 dominates h1. h2 is optimistic, but more accurate than h1. h2 is better for search.\n",
              "\n",
              "\n",
              "## Module 3 Definitions\n",
              "* **Problem-solving agent** - an agent that can formulate a plan via search\n",
              "* **Problem** - initial state, set of actions, transition model describing the results of those actions     set of goal states, action cost function\n",
              "* **Solution** - A path through the state space (a sequence of actions) from the initial state to a goal state is a solution\n",
              "* **Atomic** - states and actions do no have any internal structure\n",
              "* **Uninformed search** - only have access to the problem defintion\n",
              "* **Best-first search** - selects nodes for expansion using an evaluation function\n",
              "* **Breadth-first search** - expands the shallowest nodes first. complete, optimal for unit action     costs, has exponential space complexity\n",
              "* **Uniform cost search** - expands the node with the lowest path cost g(n) and is optimal for general action     costs\n",
              "* **Depth first search** - expands the deepest unexpanded node first. neither complete nor nor optimal,     but has linear space complexity\n",
              "* **Iterative deepening search** - dfs w/ increasing depth limits until a goal is found\n",
              "* **Bidirectional search** - expands two frontiers, one around the initial state and on around the goal,     stopping when the two frontiers meet.\n",
              "* **Informed search** - have access to a heuristic function h(n) that estimates the cost of a solution from n\n",
              "* **Greedy best-first search** - expands nodes with minimal h(n). not optimal but often efficient\n",
              "* **A* search** - expands nodes with minimal f(n) = g(n) + h(n). A* is complete and optimal, if h(n) is admissible. large space complexity\n",
              "* **Iterative deepening a* (ida)** - iterative deepening for a*, solving space complexity issue\n",
              "* **Rbfs (recursive best first search) and sma (simplified memory-bounded a*)** - robust, optimal search algos that use limited amounts of memory     given enough time, they can solve problems where A* runs out of memory\n",
              "* **Beam search** - puts a limit on the size of the frontier. makes it incomplete, suboptimal, but often finds reasonably good search solutions and runs faster than complete searches\n",
              "* **Weighted a*** - focuses the search towards a goal, expanding fewer nodes but sacrificing optimality\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WHREsHSSyA-h"
      },
      "source": [
        "#Module 4 - Informed Search"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 33,
      "metadata": {
        "id": "tuFC8w7CyCns"
      },
      "outputs": [],
      "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 = \"\"\"  \"\"\"\n",
        "\n",
        "# Definitions\n",
        "module4_definitions = {\n",
        "\"state space\" : \"set of all states reachable from the initial state by any sequence of actions\",  \n",
        "\"satisficing\": \"uses inadmissible heuristic to prune more\",\n",
        "\"pattern database\": \"stores solution costs \"\n",
        "}\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": 34,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vqtEs0gHs9sO",
        "outputId": "6a33bad1-c16f-44e7-b70a-5755faa0ad38"
      },
      "outputs": [
        {
          "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",
              "  \n",
              "\n",
              "## Module 4 Definitions\n",
              "* **State space** - set of all states reachable from the initial state by any sequence of actions\n",
              "* **Satisficing** - uses inadmissible heuristic to prune more\n",
              "* **Pattern database** - stores solution costs \n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "F3FPMzG9yUls"
      },
      "source": [
        "#Module 5 - Adversarial Search and Games"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 35,
      "metadata": {
        "id": "iEIOpL7vyWcc"
      },
      "outputs": [],
      "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 = \"\"\" \n",
        "Minimax rule: Make the best move for MAX assuming that MIN always replies with the best move for MIN\n",
        "For a MAX/MIN node, the backed-up value is the maximum/minimum of the values of its children\n",
        "\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",
        "\n",
        "def min-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 = min(v, max-value(successor))\n",
        "   return v\n",
        "\n",
        "ALPHA-BETA PRUNING\n",
        "O(b^m) and space complexity O(bm) for gen actions all at once and O(m) for actions one at a time. optimal O(b^(m/2)).\n",
        "α: MAX’s current lower bound on MAX’s outcome\n",
        "β: MIN’s current upper bound on MIN’s outcome\n",
        "α: MAX’s best already explored option on path to root\n",
        "β: MIN’s best already explored option on path to root\n",
        "MAX will never allow a move that could lead to a worse score (for MAX) than α.\n",
        "MIN will never allow a move that could lead to a better score (for MAX) than β.\n",
        "\n",
        "Therefore, stop evaluating a branch whenever:\n",
        "When evaluating a MAX node: a value 𝑣 ≥ 𝛽 is backed-up\n",
        "    MIN will never select that MAX node\n",
        "When evaluating a MIN node: a value 𝑣 ≤ 𝛼 is found\n",
        "    MAX will never select that MIN node\n",
        "\n",
        "Expectimax search: compute the average score under optimal\n",
        "Expectimax takes O(b^m n^m) time, n is number of \"d i c e r o l l s\". Can get bounds on averages if utility is bounded.\n",
        "\n",
        "def value(state):\n",
        "   if the state is a terminal state: return the state’s utility \n",
        "   if the next agent is MAX: return max-value(state)\n",
        "   if the next agent is EXP: return exp-value(state)\n",
        "\n",
        "Expectiminimax: Environment is an extra “random agent” player that moves after each min/max agent. Each node computes the appropriate combination of its children.\n",
        "\n",
        " \"\"\"\n",
        "\n",
        "# Definitions\n",
        "module5_definitions = {\n",
        "\"\" : \"definition\",    \n",
        "}\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 36,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "KUc_LgzDvWf-",
        "outputId": "39ec8635-89e2-4f2f-99f8-00aecd47f6ca"
      },
      "outputs": [
        {
          "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",
              " \n",
              "Minimax rule: Make the best move for MAX assuming that MIN always replies with the best move for MIN\n",
              "For a MAX/MIN node, the backed-up value is the maximum/minimum of the values of its children\n",
              "\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",
              "\n",
              "def min-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 = min(v, max-value(successor))\n",
              "   return v\n",
              "\n",
              "ALPHA-BETA PRUNING\n",
              "O(b^m) and space complexity O(bm) for gen actions all at once and O(m) for actions one at a time. optimal O(b^(m/2)).\n",
              "α: MAX’s current lower bound on MAX’s outcome\n",
              "β: MIN’s current upper bound on MIN’s outcome\n",
              "α: MAX’s best already explored option on path to root\n",
              "β: MIN’s best already explored option on path to root\n",
              "MAX will never allow a move that could lead to a worse score (for MAX) than α.\n",
              "MIN will never allow a move that could lead to a better score (for MAX) than β.\n",
              "\n",
              "Therefore, stop evaluating a branch whenever:\n",
              "When evaluating a MAX node: a value 𝑣 ≥ 𝛽 is backed-up\n",
              "    MIN will never select that MAX node\n",
              "When evaluating a MIN node: a value 𝑣 ≤ 𝛼 is found\n",
              "    MAX will never select that MIN node\n",
              "\n",
              "Expectimax search: compute the average score under optimal\n",
              "Expectimax takes O(b^m n^m) time, n is number of \"d i c e r o l l s\". Can get bounds on averages if utility is bounded.\n",
              "\n",
              "def value(state):\n",
              "   if the state is a terminal state: return the state’s utility \n",
              "   if the next agent is MAX: return max-value(state)\n",
              "   if the next agent is EXP: return exp-value(state)\n",
              "\n",
              "Expectiminimax: Environment is an extra “random agent” player that moves after each min/max agent. Each node computes the appropriate combination of its children.\n",
              "\n",
              " \n",
              "\n",
              "## Module 5 Definitions\n",
              "* **** - definition\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qVSQhTVSyYYO"
      },
      "source": [
        "#Module 6 - \"Constraint Satisfaction Problems\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 37,
      "metadata": {
        "id": "Wm4mfW82yaRH"
      },
      "outputs": [],
      "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 = \"\"\" \n",
        "Constraint Satisfaction Problem.\n",
        "\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} Finite set of constraints C1, C2, ..., Cm\n",
        "• Each constraint Ci limits the values that variables can take, e.g., X1 ≠ X2 A state is defined as an assignment of values to some or all variables.\n",
        "\n",
        "function AC-3(csp) (return the CSP, possibly with reduced domains inputs: csp, a binary csp with variables {X1, X2, ..., Xn})\n",
        "local variables: queue, a queue of arcs initially the arcs in csp while queue is not empty do\n",
        "\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, Xj) (return true iff we remove a value 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 then\n",
        "        delete x from DOMAIN[Xi]; removed ¬ true \n",
        "    return removed\n",
        "\n",
        "o All nodes can be connected to every other node,\n",
        "so each of n nodes must be compared against n-1 other nodes, so total # of arcs is 2*n*(n-1), i.e. O(n2)\n",
        "o If there are d values, checking arc (i,j) takes O(d2) time\n",
        "o Each arc (i,j) can only be inserted into the queue d times o Worst case complexity: O(n2d3\n",
        " \"\"\"\n",
        "\n",
        "# Definitions\n",
        "module6_definitions = {\n",
        "\"Node consistency\" : \"satisfies unary constraints\",  \n",
        "\"Arc consistency\": \"satisfies binary constraints\"  \n",
        "}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 38,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kKWbCsbfxL3R",
        "outputId": "5f3d2500-dfb2-42e4-9373-fb3bec356333"
      },
      "outputs": [
        {
          "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",
              " \n",
              "Constraint Satisfaction Problem.\n",
              "\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} Finite set of constraints C1, C2, ..., Cm\n",
              "• Each constraint Ci limits the values that variables can take, e.g., X1 ≠ X2 A state is defined as an assignment of values to some or all variables.\n",
              "\n",
              "function AC-3(csp) (return the CSP, possibly with reduced domains inputs: csp, a binary csp with variables {X1, X2, ..., Xn})\n",
              "local variables: queue, a queue of arcs initially the arcs in csp while queue is not empty do\n",
              "\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, Xj) (return true iff we remove a value 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 then\n",
              "        delete x from DOMAIN[Xi]; removed ¬ true \n",
              "    return removed\n",
              "\n",
              "o All nodes can be connected to every other node,\n",
              "so each of n nodes must be compared against n-1 other nodes, so total # of arcs is 2*n*(n-1), i.e. O(n2)\n",
              "o If there are d values, checking arc (i,j) takes O(d2) time\n",
              "o Each arc (i,j) can only be inserted into the queue d times o Worst case complexity: O(n2d3\n",
              " \n",
              "\n",
              "## Module 6 Definitions\n",
              "* **Node consistency** - satisfies unary constraints\n",
              "* **Arc consistency** - satisfies binary constraints\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RYESi2kMybL8"
      },
      "source": [
        "#Module 7 - Logical Agents"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 39,
      "metadata": {
        "id": "qIRJ4YYCzNHL"
      },
      "outputs": [],
      "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 = \"\"\" Knowledge base is a set of facts known about all games. \\\n",
        "A model is built on the knowledge base and applied to some specific instance of a game. \\\n",
        "\n",
        "((A AND B) OR Y) = ((A AND B) OR (A AND Y))\n",
        "((A OR B) AND Y) = ( )\n",
        "Search for a proof: use search algorithms to find a sequence of steps.\n",
        "Monotonicity implies soundness. Single inference rule (resolution) which yields a complete \\\n",
        "    inference algorithm.\n",
        "Resolution: if l_i and m are complementary literals, we can eliminate one of them via unit \\\n",
        "    resolution. Resolution rule applies only to clauses (disjunctions of literals).\n",
        "Every sentence is equivalent to a conjunction of clauses.\n",
        "\n",
        "Resolution Algorithm: to show KB |= a, we can show that KB ^ -a is unsatisfiable.\n",
        "- turn KB ^ -a into CNF\n",
        "- apply resolution algorithm on every pair of clauses\n",
        "- if at any point a pair yields empty clause when resolved, return true\n",
        "- if after applying resolution, there are no new clauses to add, then return false\n",
        "\n",
        "Forward Chaining: data-driven reasoning. An agent can start with known data, adding premises as new percepts come in, and \\\n",
        "    then incrementally apply the forward chaining algo to derive new conclusions. Doesn't have specific \\\n",
        "    query in mind to do so.\n",
        "\n",
        "Backward Chaining: works backward from a query. If the query is known to be true, then no work is needed. \\\n",
        "    Otherwise, the algo finds implications in the KB whose conclusions is the query. If all the premises of one \\\n",
        "    those can be proved to be true (via backward chaining) then the query is true.\n",
        "\"\"\"\n",
        "\n",
        "# Definitions\n",
        "module7_definitions = {\n",
        "\"knowledge based agent\" : \"use a process of reasoning over an internal representation of knowledge to decide what action to take.\",\n",
        "\"atomic state representation\": \"representation of states do not contain information to reason about state/plan ahead\",\n",
        "\"knowledge base KB\": \"a set of sentences written in a knowledge representation language\",\n",
        "\"axiom\": \"sentence that is given as true\",\n",
        "\"inference\": \"process of deriving new sentences from old sentences\",\n",
        "\"model checking\": \"the process of enumerating all possible models that are compatible with the KB.\",    \n",
        "\"model\": \"abstractions that have a fixed set of truth values (true/false) for each sentence\",\n",
        "\"sound\": \"the inference algorithm should only derive entailed sentences\",\n",
        "\"complete\": \"an inference algo is complete if it can derive all sentences that are entailed\",\n",
        "\"monotonicity\": \"if K |= a then KB AND b |= a. We can safely add to the KB without invalidating \\\n",
        "    anything else that we inferred\",\n",
        "\"CNF\": \"(a OR b) AND (c OR d) AND ...\"\n",
        "}\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 40,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lXNcHuKjyNXa",
        "outputId": "2fc31cf5-b9c0-41b0-9a0e-5f7744a61e68"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "## WARNING: Your definition of 'monotonicity' is too long!\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\n",
              " Knowledge base is a set of facts known about all games. A model is built on the knowledge base and applied to some specific instance of a game. \n",
              "((A AND B) OR Y) = ((A AND B) OR (A AND Y))\n",
              "((A OR B) AND Y) = ( )\n",
              "Search for a proof: use search algorithms to find a sequence of steps.\n",
              "Monotonicity implies soundness. Single inference rule (resolution) which yields a complete     inference algorithm.\n",
              "Resolution: if l_i and m are complementary literals, we can eliminate one of them via unit     resolution. Resolution rule applies only to clauses (disjunctions of literals).\n",
              "Every sentence is equivalent to a conjunction of clauses.\n",
              "\n",
              "Resolution Algorithm: to show KB |= a, we can show that KB ^ -a is unsatisfiable.\n",
              "- turn KB ^ -a into CNF\n",
              "- apply resolution algorithm on every pair of clauses\n",
              "- if at any point a pair yields empty clause when resolved, return true\n",
              "- if after applying resolution, there are no new clauses to add, then return false\n",
              "\n",
              "Forward Chaining: data-driven reasoning. An agent can start with known data, adding premises as new percepts come in, and     then incrementally apply the forward chaining algo to derive new conclusions. Doesn't have specific     query in mind to do so.\n",
              "\n",
              "Backward Chaining: works backward from a query. If the query is known to be true, then no work is needed.     Otherwise, the algo finds implications in the KB whose conclusions is the query. If all the premises of one     those can be proved to be true (via backward chaining) then the query is true.\n",
              "\n",
              "\n",
              "## Module 7 Definitions\n",
              "* **Knowledge based agent** - use a process of reasoning over an internal representation of knowledge to decide what action to take.\n",
              "* **Atomic state representation** - representation of states do not contain information to reason about state/plan ahead\n",
              "* **Knowledge base kb** - a set of sentences written in a knowledge representation language\n",
              "* **Axiom** - sentence that is given as true\n",
              "* **Inference** - process of deriving new sentences from old sentences\n",
              "* **Model checking** - the process of enumerating all possible models that are compatible with the KB.\n",
              "* **Model** - abstractions that have a fixed set of truth values (true/false) for each sentence\n",
              "* **Sound** - the inference algorithm should only derive entailed sentences\n",
              "* **Complete** - an inference algo is complete if it can derive all sentences that are entailed\n",
              "* **Monotonicity** - if K |= a then KB AND b |= a. We can safely add to the KB without invalidating     anything else that we inferred\n",
              "* **Cnf** - (a OR b) AND (c OR d) AND ...\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    },
    {
      "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",
      "execution_count": 41,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "T3cm1WV542_a",
        "outputId": "1e35f8dc-8712-4aad-fced-e8d79768a61b"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "## WARNING: Your definition of 'rational agent' is too long!\n",
              "# Module 1: Rational Agents and Task Environments\n",
              "## AIMA Chapter 1.1 What is AI?\n",
              " Machine Learning is a subfield of AI. Turing tests, cognitive science, and rational thought offer different ways of defining artificial intelligence. Turing tests involve external testing, determining if some AI seems human or not from the outside. While correct inferences are one way to achieve rationality, there are other ways of being a rational agent. \n",
              "\n",
              "## AIMA Chapter 27.1 The Limits of AI\n",
              " Weak AI = act as if intelligent. Strong AI = actually consciously thinking. Biological brains are good at actions, but bad at logic, so logic systems fail to capture everything about intelligence. Embodied cognition: consider the brain as a part of a system (sensors, vision are central, not peripheral). Computers exceed humans in some respects but fall behind in others. Godel's incompleteness theorems suggest that \\ \n",
              "logic systems cannot be consistent. \n",
              "\n",
              "## AIMA Chapter 27.1 Can Machines Really Think?\n",
              " Alan Turing introduced a behavioral test instead of the question Can machines think? Systems performance was more important than the ability to imitate humans, he thought. \n",
              "\n",
              "## AIMA 2.1 Agents and Environments\n",
              " Agent = perceives and acts in environment. Agent Function = specifies action taken by agent in response to percept sequence. Performance measure = evaulates behavior of environemnt. \n",
              "\n",
              "## AIMA 2.2 Good Behavior: The Concept of Rationality\n",
              " A task environment specification includes the performance measure, the external environment, the actuators, and the sensors. In designing an agent, the first step must always be to specify the task environment as fully as possible.\n",
              "Task environments vary along several significant dimensions.  They can be fully or partially observable, single-agent or multiagent, deterministic or nondeterministic, episodic or sequential, static or dynamic, discrete or continuous, and known or unknown. \n",
              "\n",
              "## AIMA 2.3 The Nature of Environments\n",
              " In cases where the performance measure is unknown or hard to specify correctly, there is a significant risk of the agent optimizing the wrong objective. In such cases the agent design should reflect uncertainty about the true objective \n",
              "\n",
              "## AIMA 2.4 The Structure of Agents\n",
              " The agent program implements the agent function. There exists a variety of basic agent program designs reflecting the kind of information made explicit and used in the decision process. The designs vary in efficiency, compactness, and flexibility. The appropriate design of the agent program depends on the nature of the environment\n",
              "Simple reflex agents respond directly to percepts, whereas model-based reflex agents maintain internal state to track aspects of the world that are not evident in the current percept. Goal-based agents act to achieve their goals, and utility-based agents try to maximize their own expected “happiness. \n",
              "\n",
              "## Module 1 Lecture Notes\n",
              " Philosophers have had different takes on what it means to be intelligent.  \n",
              "\n",
              "## Module 1 Definitions\n",
              "* **Agent** - anything that can perceive and act in an environment\n",
              "* **Percept** - your definitions here\n",
              "* **Agent function** - specifies the action taken by the agent in response to any percept sequence\n",
              "* **Rational agent** - acts so as to maximize the expected value of the performance measure, given the percept sequence it has seen so far\n",
              "* **Task environment** - includes the performance measure, the external environment, the actuators, and the sensors\n",
              "* **Agent program** - implements agent function\n",
              "\n",
              "\n",
              "# Module 2: Python\n",
              "## Module 2 Lecture Notes\n",
              "  \n",
              "\n",
              "## Module 2 Definitions\n",
              "* **Mutability** - your definition here\n",
              "\n",
              "\n",
              "## WARNING: Your definition of 'problem' is too long!\n",
              "WARNING: Your definition of 'solution' is too long!\n",
              "WARNING: Your definition of 'bidirectional search' is too long!\n",
              "WARNING: Your definition of 'a* search' is too long!\n",
              "WARNING: Your definition of 'rbfs (recursive best first search) and sma (simplified memory-bounded A*)' is too long!\n",
              "WARNING: Your definition of 'beam search' is too long!\n",
              "# Module 3: Search Problems\n",
              "## AIMA Chapter 3.1 Problem-Solving Agents\n",
              "  Search algorithms are judged on the basis of completeness, cost optimality,     time complexity and space complexity.\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",
              " The performance of heuristic search algorithms depends on the quality of the heuristic function. One can sometimes construct good heuristics by relaxing the problem definition, by storing precomputed solution costs for subproblems in a pattern database, by defining landmarks, or by learning from experience with the problem class \n",
              "\n",
              "## Module 3 Lecture Notes\n",
              "  \n",
              "Search Strategies:\n",
              "strategy = order of tree expansion. implemented by different (FIFO, LIFO, priority) queue structures\n",
              "Dimensions for evaluation: completeness (always find a soln), optimality (finds a least cost soln first), time complexity (# nodes generated), space complexity (# nodes simultaneously in memory)\n",
              "Time/Space Complexity Variables:\n",
              "- b = max branching factor of search tree\n",
              "- d = depth of shallowest node\n",
              "- m = max length of any path in the space (potentially inf)\n",
              "\n",
              "If h2(n) >= h1(n) for all n (both admissible), then h2 dominates h1. h2 is optimistic, but more accurate than h1. h2 is better for search.\n",
              "\n",
              "\n",
              "## Module 3 Definitions\n",
              "* **Problem-solving agent** - an agent that can formulate a plan via search\n",
              "* **Problem** - initial state, set of actions, transition model describing the results of those actions     set of goal states, action cost function\n",
              "* **Solution** - A path through the state space (a sequence of actions) from the initial state to a goal state is a solution\n",
              "* **Atomic** - states and actions do no have any internal structure\n",
              "* **Uninformed search** - only have access to the problem defintion\n",
              "* **Best-first search** - selects nodes for expansion using an evaluation function\n",
              "* **Breadth-first search** - expands the shallowest nodes first. complete, optimal for unit action     costs, has exponential space complexity\n",
              "* **Uniform cost search** - expands the node with the lowest path cost g(n) and is optimal for general action     costs\n",
              "* **Depth first search** - expands the deepest unexpanded node first. neither complete nor nor optimal,     but has linear space complexity\n",
              "* **Iterative deepening search** - dfs w/ increasing depth limits until a goal is found\n",
              "* **Bidirectional search** - expands two frontiers, one around the initial state and on around the goal,     stopping when the two frontiers meet.\n",
              "* **Informed search** - have access to a heuristic function h(n) that estimates the cost of a solution from n\n",
              "* **Greedy best-first search** - expands nodes with minimal h(n). not optimal but often efficient\n",
              "* **A* search** - expands nodes with minimal f(n) = g(n) + h(n). A* is complete and optimal, if h(n) is admissible. large space complexity\n",
              "* **Iterative deepening a* (ida)** - iterative deepening for a*, solving space complexity issue\n",
              "* **Rbfs (recursive best first search) and sma (simplified memory-bounded a*)** - robust, optimal search algos that use limited amounts of memory     given enough time, they can solve problems where A* runs out of memory\n",
              "* **Beam search** - puts a limit on the size of the frontier. makes it incomplete, suboptimal, but often finds reasonably good search solutions and runs faster than complete searches\n",
              "* **Weighted a*** - focuses the search towards a goal, expanding fewer nodes but sacrificing optimality\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",
              "  \n",
              "\n",
              "## Module 4 Definitions\n",
              "* **State space** - set of all states reachable from the initial state by any sequence of actions\n",
              "* **Satisficing** - uses inadmissible heuristic to prune more\n",
              "* **Pattern database** - stores solution costs \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",
              " \n",
              "Minimax rule: Make the best move for MAX assuming that MIN always replies with the best move for MIN\n",
              "For a MAX/MIN node, the backed-up value is the maximum/minimum of the values of its children\n",
              "\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",
              "\n",
              "def min-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 = min(v, max-value(successor))\n",
              "   return v\n",
              "\n",
              "ALPHA-BETA PRUNING\n",
              "O(b^m) and space complexity O(bm) for gen actions all at once and O(m) for actions one at a time. optimal O(b^(m/2)).\n",
              "α: MAX’s current lower bound on MAX’s outcome\n",
              "β: MIN’s current upper bound on MIN’s outcome\n",
              "α: MAX’s best already explored option on path to root\n",
              "β: MIN’s best already explored option on path to root\n",
              "MAX will never allow a move that could lead to a worse score (for MAX) than α.\n",
              "MIN will never allow a move that could lead to a better score (for MAX) than β.\n",
              "\n",
              "Therefore, stop evaluating a branch whenever:\n",
              "When evaluating a MAX node: a value 𝑣 ≥ 𝛽 is backed-up\n",
              "    MIN will never select that MAX node\n",
              "When evaluating a MIN node: a value 𝑣 ≤ 𝛼 is found\n",
              "    MAX will never select that MIN node\n",
              "\n",
              "Expectimax search: compute the average score under optimal\n",
              "Expectimax takes O(b^m n^m) time, n is number of \"d i c e r o l l s\". Can get bounds on averages if utility is bounded.\n",
              "\n",
              "def value(state):\n",
              "   if the state is a terminal state: return the state’s utility \n",
              "   if the next agent is MAX: return max-value(state)\n",
              "   if the next agent is EXP: return exp-value(state)\n",
              "\n",
              "Expectiminimax: Environment is an extra “random agent” player that moves after each min/max agent. Each node computes the appropriate combination of its children.\n",
              "\n",
              " \n",
              "\n",
              "## Module 5 Definitions\n",
              "* **** - 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",
              " \n",
              "Constraint Satisfaction Problem.\n",
              "\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} Finite set of constraints C1, C2, ..., Cm\n",
              "• Each constraint Ci limits the values that variables can take, e.g., X1 ≠ X2 A state is defined as an assignment of values to some or all variables.\n",
              "\n",
              "function AC-3(csp) (return the CSP, possibly with reduced domains inputs: csp, a binary csp with variables {X1, X2, ..., Xn})\n",
              "local variables: queue, a queue of arcs initially the arcs in csp while queue is not empty do\n",
              "\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, Xj) (return true iff we remove a value 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 then\n",
              "        delete x from DOMAIN[Xi]; removed ¬ true \n",
              "    return removed\n",
              "\n",
              "o All nodes can be connected to every other node,\n",
              "so each of n nodes must be compared against n-1 other nodes, so total # of arcs is 2*n*(n-1), i.e. O(n2)\n",
              "o If there are d values, checking arc (i,j) takes O(d2) time\n",
              "o Each arc (i,j) can only be inserted into the queue d times o Worst case complexity: O(n2d3\n",
              " \n",
              "\n",
              "## Module 6 Definitions\n",
              "* **Node consistency** - satisfies unary constraints\n",
              "* **Arc consistency** - satisfies binary constraints\n",
              "\n",
              "\n",
              "## WARNING: Your definition of 'monotonicity' is too long!\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\n",
              " Knowledge base is a set of facts known about all games. A model is built on the knowledge base and applied to some specific instance of a game. \n",
              "((A AND B) OR Y) = ((A AND B) OR (A AND Y))\n",
              "((A OR B) AND Y) = ( )\n",
              "Search for a proof: use search algorithms to find a sequence of steps.\n",
              "Monotonicity implies soundness. Single inference rule (resolution) which yields a complete     inference algorithm.\n",
              "Resolution: if l_i and m are complementary literals, we can eliminate one of them via unit     resolution. Resolution rule applies only to clauses (disjunctions of literals).\n",
              "Every sentence is equivalent to a conjunction of clauses.\n",
              "\n",
              "Resolution Algorithm: to show KB |= a, we can show that KB ^ -a is unsatisfiable.\n",
              "- turn KB ^ -a into CNF\n",
              "- apply resolution algorithm on every pair of clauses\n",
              "- if at any point a pair yields empty clause when resolved, return true\n",
              "- if after applying resolution, there are no new clauses to add, then return false\n",
              "\n",
              "Forward Chaining: data-driven reasoning. An agent can start with known data, adding premises as new percepts come in, and     then incrementally apply the forward chaining algo to derive new conclusions. Doesn't have specific     query in mind to do so.\n",
              "\n",
              "Backward Chaining: works backward from a query. If the query is known to be true, then no work is needed.     Otherwise, the algo finds implications in the KB whose conclusions is the query. If all the premises of one     those can be proved to be true (via backward chaining) then the query is true.\n",
              "\n",
              "\n",
              "## Module 7 Definitions\n",
              "* **Knowledge based agent** - use a process of reasoning over an internal representation of knowledge to decide what action to take.\n",
              "* **Atomic state representation** - representation of states do not contain information to reason about state/plan ahead\n",
              "* **Knowledge base kb** - a set of sentences written in a knowledge representation language\n",
              "* **Axiom** - sentence that is given as true\n",
              "* **Inference** - process of deriving new sentences from old sentences\n",
              "* **Model checking** - the process of enumerating all possible models that are compatible with the KB.\n",
              "* **Model** - abstractions that have a fixed set of truth values (true/false) for each sentence\n",
              "* **Sound** - the inference algorithm should only derive entailed sentences\n",
              "* **Complete** - an inference algo is complete if it can derive all sentences that are entailed\n",
              "* **Monotonicity** - if K |= a then KB AND b |= a. We can safely add to the KB without invalidating     anything else that we inferred\n",
              "* **Cnf** - (a OR b) AND (c OR d) AND ...\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "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))"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "FINAL Template_for CIS521_midterm notes.ipynb",
      "provenance": []
    },
    "interpreter": {
      "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
    },
    "kernelspec": {
      "display_name": "Python 3.10.0 64-bit",
      "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.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
