1798 lines
58 KiB
Plaintext
1798 lines
58 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Artificial Intelligence UE\n",
|
|
"## Exercises 1 - Uninformed Search\n",
|
|
"\n",
|
|
"In this series of exercises you can implement a few different **uninformed** search algorithms: \n",
|
|
"- Breadth First Search\n",
|
|
"- Uniform Cost Search\n",
|
|
"- Depth First Search\n",
|
|
"- Depth Limited Depth First Search\n",
|
|
"- Iterative Deepening Search\n",
|
|
"\n",
|
|
"The algorithms have been explained in the lecture (VO) and we gave you some additional information in the exercise (UE). Please refer to the lecture slides (VO) for the pseudo algorithms and the exercise slides (UE) for additional hints. \n",
|
|
"Before implementing the algorithms make sure to check out the introductory notebook \"introducing_pig.ipynb\" and read the following instructions carefully.\n",
|
|
"\n",
|
|
"<div class=\"alert alert-warning\">\n",
|
|
"\n",
|
|
"<p><strong>Practical hints:</strong></p>\n",
|
|
"<ul>\n",
|
|
"<li>Replace the placeholders <code># YOUR CODE HERE</code>, <code>raise NotImplementedError()</code> with your code.</li>\n",
|
|
"<li>Do not rename any of the already existing variables (this might lead to some tests failing / not working).</li>\n",
|
|
"<li><code>solve()</code> should return the found solution node or <code>None</code> if no solution is found. You do not need to store the path, the function <code>node.get_action_sequence()</code> can be used to retrieve it later via backtracking.</li>\n",
|
|
"<li>Use a <code>set()</code> to store already visited nodes (when needed).</li>\n",
|
|
"<li>Use the imported data structures <code>Queue</code>, <code>Stack</code>, and <code>PriorityQueue</code> as the fringe / frontier (choose the right datatype depending on the algorithm)</li>\n",
|
|
"</ul>\n",
|
|
"</div>\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "e8c3c2892151630ce3b9132174833fd7",
|
|
"grade": false,
|
|
"grade_id": "cell-c5c2d34ef5fe1e7d",
|
|
"locked": true,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pig_lite.problem.base import Problem\n",
|
|
"from pig_lite.datastructures.queue import Queue\n",
|
|
"from pig_lite.datastructures.stack import Stack\n",
|
|
"from pig_lite.datastructures.priority_queue import PriorityQueue\n",
|
|
"from pig_lite.instance_generation.problem_factory import ProblemFactory"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<div class=\"alert alert-warning\">\n",
|
|
"<strong>Hint:</strong> Here we load and visualize a problem instance that is used for some basic tests, please do not change it. You can use the printed output after each algorithm (i.e., position of the end node, action sequence) to compare whether your algorithm behaves as expected).\n",
|
|
"</div>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "b9b312c546887a10a22564e421213312",
|
|
"grade": false,
|
|
"grade_id": "cell-6f2cca86b47722a9",
|
|
"locked": true,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"factory = ProblemFactory()\n",
|
|
"maze = factory.create_problem_from_json(json_path='boards/tiny0.json')\n",
|
|
"maze.visualize()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<div class=\"alert alert-warning\">\n",
|
|
"Now it's your turn to implement 5 different uninformed search algorithms - all spots that need your attention are marked with <code># YOUR CODE HERE</code>!\n",
|
|
"</div>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Implementing BFS"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "716c2661a31500af4a81eb11f7c180ca",
|
|
"grade": false,
|
|
"grade_id": "cell-d2df9b0e3d90cf00",
|
|
"locked": false,
|
|
"schema_version": 3,
|
|
"solution": true,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class BFS(object):\n",
|
|
"\n",
|
|
" def __init__(self) -> None:\n",
|
|
" # YOUR CODE HERE: initialize self.visited and self.fringe here or in the solve function with the correct datatypes\n",
|
|
" self.visited = None\n",
|
|
" self.fringe = None\n",
|
|
"\n",
|
|
" def solve(self, problem: Problem): \n",
|
|
" # YOUR CODE HERE\n",
|
|
" raise NotImplementedError()\n",
|
|
" return None\n",
|
|
" \n",
|
|
"bfs_search = BFS()\n",
|
|
"maze.reset() # resets maze for hidden tests\n",
|
|
"bfs_solution = bfs_search.solve(maze)\n",
|
|
"\n",
|
|
"if bfs_solution is not None:\n",
|
|
" bfs_solution.pretty_print()\n",
|
|
" maze.visualize(sequences=[('bfs', bfs_solution.get_action_sequence())])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Basic Checks"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "bf5d1779e9b62e8a9be1d3cd10c6a820",
|
|
"grade": true,
|
|
"grade_id": "cell-ca9a92a1cfa1444d",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(bfs_solution is not None), \"your algorithm did not return a solution\"\n",
|
|
"assert(bfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"\n",
|
|
"assert(bfs_solution.cost == 20), \"the solution found by your algorithm did not return the expected cost\"\n",
|
|
"assert(bfs_solution.depth == 8), \"the solution found by your algorithm does not have the expected length\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check visited set"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "170ce4b6b16402c27f4505b590329d0c",
|
|
"grade": true,
|
|
"grade_id": "cell-ced878c4bd3f8b22",
|
|
"locked": true,
|
|
"points": 0.5,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(bfs_search.visited is not None), \"it seems you did not correctly initialize the visited set\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check fringe"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "432e7c6ad5496a0cf9d4adb22873838d",
|
|
"grade": true,
|
|
"grade_id": "cell-97963b0eac5a8e46",
|
|
"locked": true,
|
|
"points": 0.6,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(bfs_search.fringe is not None), \"it seems you did not correctly initialize the fringe\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check expaned nodes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "9416b34fff06cfd2dd7611e489a7d53b",
|
|
"grade": true,
|
|
"grade_id": "cell-25fc2adbfe6e02be",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_expanded_nodes = maze.get_number_of_expanded_nodes()\n",
|
|
"assert(bfs_expanded_nodes > 0), \"it seems your algorithm did not expand any nodes\"\n",
|
|
"assert(bfs_expanded_nodes == 18), \"it seems your algorithm did not expand the correct number of nodes\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check different mazes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"tiny0 = factory.create_problem_from_json(json_path='boards/tiny0.json')\n",
|
|
"bfs_solution = bfs_search.solve(tiny0)\n",
|
|
"assert(bfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == 'c283a9803562a0053fc1ea0c30d421e0b4a7a9f599c699d74477cbeeffec23bc'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"tiny1 = factory.create_problem_from_json(json_path='boards/tiny1.json')\n",
|
|
"bfs_solution = bfs_search.solve(tiny1)\n",
|
|
"assert(bfs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == '66ec8af4739b256cec553c3d2c2cbacbc1fd4c853859c633e44996eed1f6021c'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"tiny2 = factory.create_problem_from_json(json_path='boards/tiny2.json')\n",
|
|
"bfs_solution = bfs_search.solve(tiny2)\n",
|
|
"assert(bfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == '5bda40f5b72290920507aa1d23329fb5a3445346372a30bd35cc85f743c439ac'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"tiny3 = factory.create_problem_from_json(json_path='boards/tiny3.json')\n",
|
|
"bfs_solution = bfs_search.solve(tiny3)\n",
|
|
"assert(bfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == 'e1c95ac72da74163d0c237b69f99dc766c1bbe5e4270fd2934b721d5eab9de31'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"tiny4 = factory.create_problem_from_json(json_path='boards/tiny4.json')\n",
|
|
"bfs_solution = bfs_search.solve(tiny4)\n",
|
|
"assert(bfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == '0123d362bf2df8f84e7c41197827be005159724c07774ef32d9f15373a440091'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"cyclic_map = factory.create_problem_from_json(json_path='boards/cyclic_map.json')\n",
|
|
"bfs_solution = bfs_search.solve(cyclic_map)\n",
|
|
"assert(bfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == 'e1c95ac72da74163d0c237b69f99dc766c1bbe5e4270fd2934b721d5eab9de31'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"narrow_path = factory.create_problem_from_json(json_path='boards/narrow_path.json')\n",
|
|
"bfs_solution = bfs_search.solve(narrow_path)\n",
|
|
"assert(bfs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == '2ee4599132a71cf20e4feae6aff58b9ff77ea9486fb0e12225ddd8896ccb9843'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"no_possible_path = factory.create_problem_from_json(json_path='boards/no_possible_path.json')\n",
|
|
"bfs_solution = bfs_search.solve(no_possible_path)\n",
|
|
"assert(bfs_solution is None), \"your algorithm magically found a solution in an impossible maze\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"start_is_goal = factory.create_problem_from_json(json_path='boards/start_is_goal.json')\n",
|
|
"bfs_solution = bfs_search.solve(start_is_goal)\n",
|
|
"assert(bfs_solution.state == (0, 0)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(bfs_solution.get_action_sequence_hash() == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this test is supposed to test whether the visited set is used somewhat correctly (otherwise it will timeout)\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"large = factory.create_problem_from_json(json_path='boards/large.json')\n",
|
|
"bfs_solution = bfs_search.solve(large)\n",
|
|
"assert(bfs_solution)\n",
|
|
"assert(bfs_solution.state == (48, 48)), \"your algorithm did not return the expected solution\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Implementing UCS\n",
|
|
"\n",
|
|
"- implement Uniform Cost Search (UCS), a variant of Dijkstra's Graph Search"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "57efee82377a65469cc75a8c797111ab",
|
|
"grade": false,
|
|
"grade_id": "cell-0bc80e814a5b5029",
|
|
"locked": false,
|
|
"schema_version": 3,
|
|
"solution": true,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class UCS(object):\n",
|
|
"\n",
|
|
" def __init__(self) -> None:\n",
|
|
" # YOUR CODE HERE: initialize self.visited and self.fringe here or in the solve function with the correct datatypes\n",
|
|
" self.visited = None\n",
|
|
" self.fringe = None\n",
|
|
"\n",
|
|
" def solve(self, problem: Problem):\n",
|
|
" # YOUR CODE HERE\n",
|
|
" raise NotImplementedError()\n",
|
|
" return None\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"maze.reset() # resets maze for hidden tests\n",
|
|
"ucs_solution = ucs_search.solve(maze)\n",
|
|
"\n",
|
|
"if ucs_solution is not None:\n",
|
|
" ucs_solution.pretty_print()\n",
|
|
" maze.visualize(sequences=[('ucs', ucs_solution.get_action_sequence())])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Basic checks"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "e5f06248b3e49c5e4347509304ce684c",
|
|
"grade": true,
|
|
"grade_id": "cell-aac452910d38e14e",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(ucs_solution is not None), \"your algorithm did not return a solution\"\n",
|
|
"assert(ucs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"\n",
|
|
"assert(ucs_solution.depth == 8), \"the solution found by your algorithm does not have the expected length\"\n",
|
|
"\n",
|
|
"ucs_expanded_nodes = maze.get_number_of_expanded_nodes()\n",
|
|
"assert(ucs_expanded_nodes == 12), \"it seems your algorithm did not expand the correct number of nodes\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check visited set"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "1eb3167d55f3292f93f4985fcfc514f2",
|
|
"grade": true,
|
|
"grade_id": "cell-f7d28a2a7702c9bd",
|
|
"locked": true,
|
|
"points": 0.5,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(ucs_search.visited is not None), \"it seems you did not correctly initialize the visited set\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check fringe"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "8122ec4f35430855efc6c1e0acb75beb",
|
|
"grade": true,
|
|
"grade_id": "cell-7502444279f2a79a",
|
|
"locked": true,
|
|
"points": 0.8,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(ucs_search.fringe is not None), \"it seems you did not correctly initialize the fringe\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Checking cost"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "ddafb0736b258f1b71dadfaf6824b67c",
|
|
"grade": true,
|
|
"grade_id": "cell-d395b6d5f9d370b1",
|
|
"locked": true,
|
|
"points": 1,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"bfs_search = BFS()\n",
|
|
"maze.reset()\n",
|
|
"bfs_solution = bfs_search.solve(maze)\n",
|
|
"\n",
|
|
"assert(ucs_solution.cost < bfs_solution.cost), \"the solution cost for UCS should be lower than the one for BFS\"\n",
|
|
"assert(ucs_solution.cost == 12), \"the solution found by your algorithm did not return the expected cost\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check different mazes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"tiny0 = factory.create_problem_from_json(json_path='boards/tiny0.json')\n",
|
|
"ucs_solution = ucs_search.solve(tiny0)\n",
|
|
"assert(ucs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == '6bdb8d2c34f7d512c6c555c27ec66385b0fd13c5401db6a83cdc68000541dbb5'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"tiny1 = factory.create_problem_from_json(json_path='boards/tiny1.json')\n",
|
|
"ucs_solution = ucs_search.solve(tiny1)\n",
|
|
"assert(ucs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == '473818e05eb92df10661a91a41e2fefd9d3d0b49466df773091a94791b0ac2a5'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"tiny2 = factory.create_problem_from_json(json_path='boards/tiny2.json')\n",
|
|
"ucs_solution = ucs_search.solve(tiny2)\n",
|
|
"assert(ucs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == '5bda40f5b72290920507aa1d23329fb5a3445346372a30bd35cc85f743c439ac'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"tiny3 = factory.create_problem_from_json(json_path='boards/tiny3.json')\n",
|
|
"ucs_solution = ucs_search.solve(tiny3)\n",
|
|
"assert(ucs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == 'e1c95ac72da74163d0c237b69f99dc766c1bbe5e4270fd2934b721d5eab9de31'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"tiny4 = factory.create_problem_from_json(json_path='boards/tiny4.json')\n",
|
|
"ucs_solution = ucs_search.solve(tiny4)\n",
|
|
"assert(ucs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == '0123d362bf2df8f84e7c41197827be005159724c07774ef32d9f15373a440091'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"cyclic_map = factory.create_problem_from_json(json_path='boards/cyclic_map.json')\n",
|
|
"ucs_solution = ucs_search.solve(cyclic_map)\n",
|
|
"assert(ucs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == 'e1c95ac72da74163d0c237b69f99dc766c1bbe5e4270fd2934b721d5eab9de31'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"narrow_path = factory.create_problem_from_json(json_path='boards/narrow_path.json')\n",
|
|
"ucs_solution = ucs_search.solve(narrow_path)\n",
|
|
"assert(ucs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == '2ee4599132a71cf20e4feae6aff58b9ff77ea9486fb0e12225ddd8896ccb9843'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"no_possible_path = factory.create_problem_from_json(json_path='boards/no_possible_path.json')\n",
|
|
"ucs_solution = ucs_search.solve(no_possible_path)\n",
|
|
"assert(ucs_solution is None), \"your algorithm did not handle unreachable goal state correctly\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"start_is_goal = factory.create_problem_from_json(json_path='boards/start_is_goal.json')\n",
|
|
"ucs_solution = ucs_search.solve(start_is_goal)\n",
|
|
"assert(ucs_solution.state == (0, 0)), \"your algorithm did not return the expected solution when starting from the goal state\"\n",
|
|
"assert(ucs_solution.get_action_sequence_hash() == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this test is supposed to test whether the visited set is used somewhat correctly (otherwise it will timeout)\n",
|
|
"\n",
|
|
"ucs_search = UCS()\n",
|
|
"large = factory.create_problem_from_json(json_path='boards/large.json')\n",
|
|
"ucs_solution = ucs_search.solve(large)\n",
|
|
"assert(ucs_solution)\n",
|
|
"assert(ucs_solution.state == (48, 48)), \"your algorithm did not return the expected solution\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Implementing DFS"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "6c1e9ac7733fbca45f4b842d89e4416f",
|
|
"grade": false,
|
|
"grade_id": "cell-e94fcb80013152e4",
|
|
"locked": false,
|
|
"schema_version": 3,
|
|
"solution": true,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class DFS(object):\n",
|
|
"\n",
|
|
" def __init__(self) -> None:\n",
|
|
" # YOUR CODE HERE: initialize self.visited and self.fringe here or in the solve function with the correct datatypes\n",
|
|
" self.visited = None\n",
|
|
" self.fringe = None\n",
|
|
"\n",
|
|
" def solve(self, problem: Problem):\n",
|
|
" # YOUR CODE HERE\n",
|
|
" raise NotImplementedError()\n",
|
|
" return None\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"maze.reset() # resets maze for hidden tests\n",
|
|
"dfs_solution = dfs_search.solve(maze)\n",
|
|
"\n",
|
|
"if dfs_solution is not None:\n",
|
|
" dfs_solution.pretty_print()\n",
|
|
" maze.visualize(sequences=[('dfs', dfs_solution.get_action_sequence())])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Basic checks"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "13ec840f2f77f176e68155b1f34fcd9d",
|
|
"grade": true,
|
|
"grade_id": "cell-7729bdb99cbcb357",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(dfs_solution is not None), \"your algorithm did not return a solution\"\n",
|
|
"assert(dfs_solution is not None or isinstance(dfs_solution, type(maze.get_start_node()))), \"your solution is not of the right type\"\n",
|
|
"assert(dfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"\n",
|
|
"assert(dfs_solution.cost == 24), \"the solution found by your algorithm did not return the expected cost\"\n",
|
|
"assert(dfs_solution.depth == 12), \"the solution found by your algorithm does not have the expected length\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check visited set"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "8c6f1ddd46e0cc7c3830bb347c612fc5",
|
|
"grade": true,
|
|
"grade_id": "cell-ea2de578de0929dd",
|
|
"locked": true,
|
|
"points": 0.5,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(dfs_search.visited is not None), \"it seems you did not correctly initialize the visited set\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check fringe"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "31e1861c96a7c40bdca43bd26f943874",
|
|
"grade": true,
|
|
"grade_id": "cell-45ac331b0d77b101",
|
|
"locked": true,
|
|
"points": 0.8,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(dfs_search.fringe is not None), \"it seems you did not correctly initialize the fringe\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check different mazes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "00f5c7aa425bc6f808edea2478337c48",
|
|
"grade": true,
|
|
"grade_id": "cell-fbef9ebfc64d0579",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"tiny0 = factory.create_problem_from_json(json_path='boards/tiny0.json')\n",
|
|
"dfs_solution = dfs_search.solve(tiny0)\n",
|
|
"assert(dfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == 'e1fa32922bd28adb1dbb8a08b56547137b98d1105229a033162ae0edb274f418'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "fb8984e9d558951d18e22f60972f98ce",
|
|
"grade": true,
|
|
"grade_id": "cell-b7dffcb6abd93552",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"tiny1 = factory.create_problem_from_json(json_path='boards/tiny1.json')\n",
|
|
"dfs_solution = dfs_search.solve(tiny1)\n",
|
|
"assert(dfs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == '50ad40a2921e4009cab8f9bdb1c7a00bf36f69fbe7a20b83aeb93227334ee601'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "cc94a80ee24db1db3fb7bcf2f5fc910b",
|
|
"grade": true,
|
|
"grade_id": "cell-d10f2fec91098425",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"tiny2 = factory.create_problem_from_json(json_path='boards/tiny2.json')\n",
|
|
"dfs_solution = dfs_search.solve(tiny2)\n",
|
|
"assert(dfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == '5bda40f5b72290920507aa1d23329fb5a3445346372a30bd35cc85f743c439ac'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "4cc6902eda0bdb8335c102f497a4ace0",
|
|
"grade": true,
|
|
"grade_id": "cell-1b277be9230355d0",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"tiny3 = factory.create_problem_from_json(json_path='boards/tiny3.json')\n",
|
|
"dfs_solution = dfs_search.solve(tiny3)\n",
|
|
"assert(dfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == '5bda40f5b72290920507aa1d23329fb5a3445346372a30bd35cc85f743c439ac'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "09d1faa9749bb1e2cf83b33c1fb528f2",
|
|
"grade": true,
|
|
"grade_id": "cell-c4f7dbe4d3a2ca3d",
|
|
"locked": true,
|
|
"points": 0.2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"tiny4 = factory.create_problem_from_json(json_path='boards/tiny4.json')\n",
|
|
"dfs_solution = dfs_search.solve(tiny4)\n",
|
|
"assert(dfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == '0123d362bf2df8f84e7c41197827be005159724c07774ef32d9f15373a440091'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"cyclic_map = factory.create_problem_from_json(json_path='boards/cyclic_map.json')\n",
|
|
"dfs_solution = dfs_search.solve(cyclic_map)\n",
|
|
"assert(dfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == '61ade70d1b7727f7e21ea8d65cbc2afc5239a5cc2a0c056593d39b43edcc82df'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"narrow_path = factory.create_problem_from_json(json_path='boards/narrow_path.json')\n",
|
|
"dfs_solution = dfs_search.solve(narrow_path)\n",
|
|
"assert(dfs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == '2ee4599132a71cf20e4feae6aff58b9ff77ea9486fb0e12225ddd8896ccb9843'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"no_possible_path = factory.create_problem_from_json(json_path='boards/no_possible_path.json')\n",
|
|
"dfs_solution = dfs_search.solve(no_possible_path)\n",
|
|
"assert(dfs_solution is None), \"your algorithm magically found a solution in an impossible maze\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"start_is_goal = factory.create_problem_from_json(json_path='boards/start_is_goal.json')\n",
|
|
"dfs_solution = dfs_search.solve(start_is_goal)\n",
|
|
"assert(dfs_solution.state == (0, 0)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dfs_solution.get_action_sequence_hash() == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "e9c1cb9ad2c23a47501c54fddb3d4a28",
|
|
"grade": true,
|
|
"grade_id": "cell-f24c21dd867ccc44",
|
|
"locked": true,
|
|
"points": 0.5,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this test is supposed to test whether the visited set is used somewhat correctly (otherwise it will timeout)\n",
|
|
"\n",
|
|
"dfs_search = DFS()\n",
|
|
"large = factory.create_problem_from_json(json_path='boards/large.json')\n",
|
|
"dfs_solution = dfs_search.solve(large)\n",
|
|
"assert(dfs_solution)\n",
|
|
"assert(dfs_solution.state == (48, 48)), \"your algorithm did not return the expected solution\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Implementing DLDFS\n",
|
|
"\n",
|
|
"<strong>Hints:</strong>\n",
|
|
"<ul>\n",
|
|
"<li>This algorithm should be implemented recursively, which means you will not require an explicit fringe. Think about why this is the case!</li>\n",
|
|
"<li>To be comparable to our solution you also do not need to use a visited set for this algorithm.</li>\n",
|
|
"</ul>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "da284236de56495b7e580ad070a76dc8",
|
|
"grade": false,
|
|
"grade_id": "cell-3e0fae5d39e252d7",
|
|
"locked": false,
|
|
"schema_version": 3,
|
|
"solution": true,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class DLDFS_Recursive(object):\n",
|
|
" def __init__(self, max_depth):\n",
|
|
" self.max_depth = max_depth\n",
|
|
"\n",
|
|
" def solve_aux(self, problem, current):\n",
|
|
" # YOUR CODE HERE\n",
|
|
" raise NotImplementedError()\n",
|
|
" return None\n",
|
|
"\n",
|
|
" def solve(self, problem: Problem):\n",
|
|
" return self.solve_aux(problem, problem.get_start_node())\n",
|
|
"\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(12)\n",
|
|
"maze.reset() # resets maze for hidden tests\n",
|
|
"dldfs_solution = dldfs_search.solve(maze)\n",
|
|
"\n",
|
|
"if dldfs_solution is not None:\n",
|
|
" dldfs_solution.pretty_print()\n",
|
|
" maze.visualize(sequences=[('dldfs', dldfs_solution.get_action_sequence())])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Basic Checks"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "8f0da3ceb956f1584b6b0fde776cc8c3",
|
|
"grade": true,
|
|
"grade_id": "cell-07d2f9c64ab2c152",
|
|
"locked": true,
|
|
"points": 2,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(dldfs_solution is not None), \"your algorithm did not return a solution\"\n",
|
|
"assert(dldfs_solution is not None or isinstance(dldfs_solution, type(maze.get_start_node()))), \"your solution is not of the right type\"\n",
|
|
"assert(dldfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"\n",
|
|
"assert(dldfs_solution.cost == 36), \"the solution found by your algorithm did not return the expected cost\"\n",
|
|
"assert(dldfs_solution.depth == 12), \"the solution found by your algorithm does not have the expected length\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check different mazes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# we need more depth for some of the problem instances\n",
|
|
"max_dldfs_depth = 25"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "807edb2d77a6dd6e592db96a14c2df80",
|
|
"grade": true,
|
|
"grade_id": "cell-5fafe913c0c969c2",
|
|
"locked": true,
|
|
"points": 0.4,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"tiny0 = factory.create_problem_from_json(json_path='boards/tiny0.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(tiny0)\n",
|
|
"assert(dldfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == '5eb9f93f575001cbddca893c699d418e47eff2b53f0c9a1a2072c3b9ad643dc7'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "046f9d9fc7c3aa47f7d1864795046cf6",
|
|
"grade": true,
|
|
"grade_id": "cell-462d4b407fabc045",
|
|
"locked": true,
|
|
"points": 0.4,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"tiny1 = factory.create_problem_from_json(json_path='boards/tiny1.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(tiny1)\n",
|
|
"assert(dldfs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == '7f10f7179c1d448ed33e8b8d0bb5372266cea15bd9ec0ffbdd11118438754d99'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "d9ca3e75bfa55423bc3d947434ba26f9",
|
|
"grade": true,
|
|
"grade_id": "cell-30634f5520d90c17",
|
|
"locked": true,
|
|
"points": 0.4,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"tiny2 = factory.create_problem_from_json(json_path='boards/tiny2.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(tiny2)\n",
|
|
"assert(dldfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == 'ebb80a630c129a7234ed6c9bcde740930a01622f4adde8b304db362ec5a24725'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "db1f664f956e3158981b58e5a41ae86c",
|
|
"grade": true,
|
|
"grade_id": "cell-4d12d065035af707",
|
|
"locked": true,
|
|
"points": 0.4,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"tiny3 = factory.create_problem_from_json(json_path='boards/tiny3.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(tiny3)\n",
|
|
"assert(dldfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == '8ec892c800ce37ea54e9ba4dab28681de6956e481d2ae0e032b1e11c59e4a718'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "28b14dd36a9a8cfb1552d94655a67b79",
|
|
"grade": true,
|
|
"grade_id": "cell-9320c5aa64bda863",
|
|
"locked": true,
|
|
"points": 0.4,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"tiny4 = factory.create_problem_from_json(json_path='boards/tiny4.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(tiny4)\n",
|
|
"assert(dldfs_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == 'e097ad8539f0d2f7abe3160c1a47c24e1b19bdb9b0c40e0dcd7e9ecb3307c7bb'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=2)\n",
|
|
"cyclic_map = factory.create_problem_from_json(json_path='boards/cyclic_map.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(cyclic_map)\n",
|
|
"assert(dldfs_solution is None), \"your algorithm should not find a solution at max_depth=2\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"cyclic_map = factory.create_problem_from_json(json_path='boards/cyclic_map.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(cyclic_map)\n",
|
|
"assert(dldfs_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == '7ea076a692e54d03a54b160dbceb28175b083f29615149dda6a21a8c6347b28a'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"narrow_path = factory.create_problem_from_json(json_path='boards/narrow_path.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(narrow_path)\n",
|
|
"assert(dldfs_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == 'ffb856b0937bb3fb160f44c61f57d391e0edfa9c6671bbfa773fcdba7c317b13'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"no_possible_path = factory.create_problem_from_json(json_path='boards/no_possible_path.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(no_possible_path)\n",
|
|
"assert(dldfs_solution is None), \"your algorithm magically found a solution in an impossible maze\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"dldfs_search = DLDFS_Recursive(max_depth=max_dldfs_depth)\n",
|
|
"start_is_goal = factory.create_problem_from_json(json_path='boards/start_is_goal.json')\n",
|
|
"dldfs_solution = dldfs_search.solve(start_is_goal)\n",
|
|
"assert(dldfs_solution.state == (0, 0)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(dldfs_solution.get_action_sequence_hash() == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Implementing IDS\n",
|
|
"\n",
|
|
"<strong>Hint:</strong>\n",
|
|
"<ul>\n",
|
|
"<li>Make use of the DLDFS algorithm that you implemented above</li>\n",
|
|
"</ul>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "8f21b2a9464561f48f79410a2f8a1467",
|
|
"grade": false,
|
|
"grade_id": "cell-b3614f9983cf1dce",
|
|
"locked": false,
|
|
"schema_version": 3,
|
|
"solution": true,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class IDS(object):\n",
|
|
" def solve(self, problem: Problem):\n",
|
|
" node = None\n",
|
|
" depth = 0\n",
|
|
"\n",
|
|
" # YOUR CODE HERE\n",
|
|
" raise NotImplementedError()\n",
|
|
" return node\n",
|
|
" \n",
|
|
"ids_search = IDS()\n",
|
|
"maze.reset() # resets maze for hidden tests\n",
|
|
"ids_solution = ids_search.solve(maze)\n",
|
|
"\n",
|
|
"if ids_solution is not None:\n",
|
|
" ids_solution.pretty_print()\n",
|
|
" maze.visualize(sequences=[('ids', ids_solution.get_action_sequence())])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "05b85a83571ad65322ac4af47727e218",
|
|
"grade": true,
|
|
"grade_id": "cell-36d9e04df571467f",
|
|
"locked": true,
|
|
"points": 0.5,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"assert(ids_solution is not None), \"your algorithm did not return a solution\"\n",
|
|
"assert(ids_solution is not None or isinstance(ids_solution, type(maze.get_start_node()))), \"your solution is not of the right type\"\n",
|
|
"assert(ids_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"\n",
|
|
"assert(ids_solution.cost == 20), \"the solution found by your algorithm did not return the expected cost\"\n",
|
|
"assert(ids_solution.depth == 8), \"the solution found by your algorithm does not have the expected length\"\n",
|
|
"\n",
|
|
"ids_expanded_nodes = maze.get_number_of_expanded_nodes()\n",
|
|
"bfs_search = BFS()\n",
|
|
"maze.reset() # resets maze for hidden tests\n",
|
|
"bfs_solution = bfs_search.solve(maze)\n",
|
|
"bfs_expanded_nodes = maze.get_number_of_expanded_nodes()\n",
|
|
"assert(ids_expanded_nodes > bfs_expanded_nodes), \"the solution found by your algorithm is expected to have more expanded nodes than your solution for BFS.\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check different mazes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "d2957d5766c737d2afc1045b5ccaf34f",
|
|
"grade": true,
|
|
"grade_id": "cell-951813681d37fac0",
|
|
"locked": true,
|
|
"points": 0.1,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"tiny0 = factory.create_problem_from_json(json_path='boards/tiny0.json')\n",
|
|
"ids_solution = ids_search.solve(tiny0)\n",
|
|
"assert(ids_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == 'c283a9803562a0053fc1ea0c30d421e0b4a7a9f599c699d74477cbeeffec23bc'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "458ff494791981e923559334759eb463",
|
|
"grade": true,
|
|
"grade_id": "cell-90bd1d9cace41b93",
|
|
"locked": true,
|
|
"points": 0.1,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"tiny1 = factory.create_problem_from_json(json_path='boards/tiny1.json')\n",
|
|
"ids_solution = ids_search.solve(tiny1)\n",
|
|
"assert(ids_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == '66ec8af4739b256cec553c3d2c2cbacbc1fd4c853859c633e44996eed1f6021c'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "269cd4b03aa9fa4ccd4b09cf9982420b",
|
|
"grade": true,
|
|
"grade_id": "cell-f38380f9ab502704",
|
|
"locked": true,
|
|
"points": 0.1,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"tiny2 = factory.create_problem_from_json(json_path='boards/tiny2.json')\n",
|
|
"ids_solution = ids_search.solve(tiny2)\n",
|
|
"assert(ids_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == '5bda40f5b72290920507aa1d23329fb5a3445346372a30bd35cc85f743c439ac'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "5962b78731fc9cc9fad3645abc3d69ab",
|
|
"grade": true,
|
|
"grade_id": "cell-58d667cc9d701e52",
|
|
"locked": true,
|
|
"points": 0.1,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"tiny3 = factory.create_problem_from_json(json_path='boards/tiny3.json')\n",
|
|
"ids_solution = ids_search.solve(tiny3)\n",
|
|
"assert(ids_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == 'e1c95ac72da74163d0c237b69f99dc766c1bbe5e4270fd2934b721d5eab9de31'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"deletable": false,
|
|
"editable": false,
|
|
"nbgrader": {
|
|
"cell_type": "code",
|
|
"checksum": "c4d100fafaf5c914e3329ac98c1ecec0",
|
|
"grade": true,
|
|
"grade_id": "cell-768a694fd1d7c870",
|
|
"locked": true,
|
|
"points": 0.1,
|
|
"schema_version": 3,
|
|
"solution": false,
|
|
"task": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"tiny4 = factory.create_problem_from_json(json_path='boards/tiny4.json')\n",
|
|
"ids_solution = ids_search.solve(tiny4)\n",
|
|
"assert(ids_solution.state == (4, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == '0123d362bf2df8f84e7c41197827be005159724c07774ef32d9f15373a440091'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"cyclic_map = factory.create_problem_from_json(json_path='boards/cyclic_map.json')\n",
|
|
"ids_solution = ids_search.solve(cyclic_map)\n",
|
|
"assert(ids_solution.state == (2, 2)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == 'e1c95ac72da74163d0c237b69f99dc766c1bbe5e4270fd2934b721d5eab9de31'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"narrow_path = factory.create_problem_from_json(json_path='boards/narrow_path.json')\n",
|
|
"ids_solution = ids_search.solve(narrow_path)\n",
|
|
"assert(ids_solution.state == (2, 4)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == '2ee4599132a71cf20e4feae6aff58b9ff77ea9486fb0e12225ddd8896ccb9843'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# this is a testing cell, do not edit or delete\n",
|
|
"\n",
|
|
"ids_search = IDS()\n",
|
|
"start_is_goal = factory.create_problem_from_json(json_path='boards/start_is_goal.json')\n",
|
|
"ids_solution = ids_search.solve(start_is_goal)\n",
|
|
"assert(ids_solution.state == (0, 0)), \"your algorithm did not return the expected solution\"\n",
|
|
"assert(ids_solution.get_action_sequence_hash() == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'), \"your algorithm did not return the expected solution path\""
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.12.11"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|