|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 51, |
| 6 | + "id": "ef35fc56-180a-4d1a-9099-bed021f6e0ee", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "from search_2 import *\n", |
| 11 | + "\n", |
| 12 | + "def best_first_limited_cost_search(problem, f, cost_limit):\n", |
| 13 | + " node = Node(problem.initial)\n", |
| 14 | + " frontier = PriorityQueue([node], key=f)\n", |
| 15 | + " reached = {problem.initial : node}\n", |
| 16 | + " result = failure\n", |
| 17 | + " lowest_path_cost = None;\n", |
| 18 | + " while frontier:\n", |
| 19 | + " node = frontier.pop()\n", |
| 20 | + " if problem.is_goal(node.state):\n", |
| 21 | + " # we found a solution\n", |
| 22 | + " return node\n", |
| 23 | + " for child in expand(problem, node):\n", |
| 24 | + " s = child.state\n", |
| 25 | + " # if the path_cost is larger than cost_limit, we need to ignore this child.\n", |
| 26 | + " if child.path_cost > cost_limit:\n", |
| 27 | + " print(child.state, child.path_cost) \n", |
| 28 | + " if lowest_path_cost == None or child.path_cost < lowest_path_cost:\n", |
| 29 | + " lowest_path_cost = child.path_cost\n", |
| 30 | + " result = Node('cost_limit_reached', path_cost = lowest_path_cost)\n", |
| 31 | + " elif s not in reached or child.path_cost < reached[s].path_cost:\n", |
| 32 | + " reached[s] = child\n", |
| 33 | + " frontier.add(child)\n", |
| 34 | + " return result\n", |
| 35 | + "\n", |
| 36 | + "def iterative_lengthening_search(problem):\n", |
| 37 | + " print(\"first iteration\")\n", |
| 38 | + " n = best_first_limited_cost_search(problem, g, 0)\n", |
| 39 | + " print('\\t', n.path_cost)\n", |
| 40 | + " \n", |
| 41 | + " print(\"second iteration\")\n", |
| 42 | + " n = best_first_limited_cost_search(problem, g, n.path_cost)\n", |
| 43 | + " print('\\t', n.path_cost)\n", |
| 44 | + " \n", |
| 45 | + " print(\"third iteration\")\n", |
| 46 | + " n = best_first_limited_cost_search(problem, g, 1000)\n", |
| 47 | + " print('\\t', n.path_cost)\n", |
| 48 | + " return n" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 52, |
| 54 | + "id": "99015fa1-471a-4149-bfe1-4bd7f12671fd", |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "romania = Map(\n", |
| 59 | + " {('O', 'Z'): 71, ('O', 'S'): 151, ('A', 'Z'): 75, ('A', 'S'): 140, ('A', 'T'): 118, \n", |
| 60 | + " ('L', 'T'): 111, ('L', 'M'): 70, ('D', 'M'): 75, ('C', 'D'): 120, ('C', 'R'): 146, \n", |
| 61 | + " ('C', 'P'): 138, ('R', 'S'): 80, ('F', 'S'): 99, ('B', 'F'): 211, ('B', 'P'): 101, \n", |
| 62 | + " ('B', 'G'): 90, ('B', 'U'): 85, ('H', 'U'): 98, ('E', 'H'): 86, ('U', 'V'): 142, \n", |
| 63 | + " ('I', 'V'): 92, ('I', 'N'): 87, ('P', 'R'): 97},\n", |
| 64 | + " {'A': ( 76, 497), 'B': (400, 327), 'C': (246, 285), 'D': (160, 296), 'E': (558, 294), \n", |
| 65 | + " 'F': (285, 460), 'G': (368, 257), 'H': (548, 355), 'I': (488, 535), 'L': (162, 379),\n", |
| 66 | + " 'M': (160, 343), 'N': (407, 561), 'O': (117, 580), 'P': (311, 372), 'R': (227, 412),\n", |
| 67 | + " 'S': (187, 463), 'T': ( 83, 414), 'U': (471, 363), 'V': (535, 473), 'Z': (92, 539)})\n", |
| 68 | + "\n", |
| 69 | + "\n", |
| 70 | + "r0 = RouteProblem('A', 'A', map=romania)\n", |
| 71 | + "r1 = RouteProblem('A', 'B', map=romania)\n", |
| 72 | + "r2 = RouteProblem('N', 'L', map=romania)\n", |
| 73 | + "r3 = RouteProblem('E', 'T', map=romania)\n", |
| 74 | + "r4 = RouteProblem('O', 'M', map=romania)" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 53, |
| 80 | + "id": "9e580734-b4f8-4d48-82d2-adcac10f8714", |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "name": "stdout", |
| 85 | + "output_type": "stream", |
| 86 | + "text": [ |
| 87 | + "first iteration\n", |
| 88 | + "Z 75\n", |
| 89 | + "S 140\n", |
| 90 | + "T 118\n", |
| 91 | + "\t 75\n", |
| 92 | + "second iteration\n", |
| 93 | + "S 140\n", |
| 94 | + "T 118\n", |
| 95 | + "O 146\n", |
| 96 | + "A 150\n", |
| 97 | + "\t 118\n", |
| 98 | + "third iteration\n", |
| 99 | + "\t 418\n" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "data": { |
| 104 | + "text/plain": [ |
| 105 | + "['A', 'S', 'R', 'P', 'B']" |
| 106 | + ] |
| 107 | + }, |
| 108 | + "execution_count": 53, |
| 109 | + "metadata": {}, |
| 110 | + "output_type": "execute_result" |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [ |
| 114 | + "path_states(iterative_lengthening_search(r1))" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 54, |
| 120 | + "id": "8b501fbc-64e9-48c4-81d1-03af28b8c8c6", |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [ |
| 123 | + { |
| 124 | + "name": "stdout", |
| 125 | + "output_type": "stream", |
| 126 | + "text": [ |
| 127 | + "['A', 'S', 'R', 'P', 'B']\n", |
| 128 | + "418\n" |
| 129 | + ] |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "n = uniform_cost_search(r1)\n", |
| 134 | + "print(path_states(n))\n", |
| 135 | + "print(n.path_cost)" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": null, |
| 141 | + "id": "09f683d4-d27e-4b9b-b279-bb7c46673870", |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [] |
| 145 | + } |
| 146 | + ], |
| 147 | + "metadata": { |
| 148 | + "kernelspec": { |
| 149 | + "display_name": "Python 3 (ipykernel)", |
| 150 | + "language": "python", |
| 151 | + "name": "python3" |
| 152 | + }, |
| 153 | + "language_info": { |
| 154 | + "codemirror_mode": { |
| 155 | + "name": "ipython", |
| 156 | + "version": 3 |
| 157 | + }, |
| 158 | + "file_extension": ".py", |
| 159 | + "mimetype": "text/x-python", |
| 160 | + "name": "python", |
| 161 | + "nbconvert_exporter": "python", |
| 162 | + "pygments_lexer": "ipython3", |
| 163 | + "version": "3.11.2" |
| 164 | + } |
| 165 | + }, |
| 166 | + "nbformat": 4, |
| 167 | + "nbformat_minor": 5 |
| 168 | +} |
0 commit comments