|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
5 |
| - "execution_count": 51, |
| 5 | + "execution_count": 127, |
6 | 6 | "id": "ef35fc56-180a-4d1a-9099-bed021f6e0ee",
|
7 | 7 | "metadata": {},
|
8 | 8 | "outputs": [],
|
|
24 | 24 | " s = child.state\n",
|
25 | 25 | " # if the path_cost is larger than cost_limit, we need to ignore this child.\n",
|
26 | 26 | " if child.path_cost > cost_limit:\n",
|
27 |
| - " print(child.state, child.path_cost) \n", |
28 | 27 | " if lowest_path_cost == None or child.path_cost < lowest_path_cost:\n",
|
29 | 28 | " lowest_path_cost = child.path_cost\n",
|
30 |
| - " result = Node('cost_limit_reached', path_cost = lowest_path_cost)\n", |
| 29 | + " result = Node('cost_limit_reached',\n", |
| 30 | + " path_cost = lowest_path_cost)\n", |
31 | 31 | " elif s not in reached or child.path_cost < reached[s].path_cost:\n",
|
32 | 32 | " reached[s] = child\n",
|
33 | 33 | " frontier.add(child)\n",
|
34 | 34 | " return result\n",
|
35 | 35 | "\n",
|
36 | 36 | "def iterative_lengthening_search(problem):\n",
|
37 |
| - " print(\"first iteration\")\n", |
38 | 37 | " 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", |
| 38 | + " iteration = 0\n", |
| 39 | + " while(n.state == 'cost_limit_reached'):\n", |
| 40 | + " n = best_first_limited_cost_search(problem, g, n.path_cost)\n", |
| 41 | + " iteration = iteration + 1\n", |
| 42 | + " print(iteration)\n", |
48 | 43 | " return n"
|
49 | 44 | ]
|
50 | 45 | },
|
51 | 46 | {
|
52 | 47 | "cell_type": "code",
|
53 |
| - "execution_count": 52, |
| 48 | + "execution_count": 134, |
54 | 49 | "id": "99015fa1-471a-4149-bfe1-4bd7f12671fd",
|
55 | 50 | "metadata": {},
|
56 | 51 | "outputs": [],
|
|
76 | 71 | },
|
77 | 72 | {
|
78 | 73 | "cell_type": "code",
|
79 |
| - "execution_count": 53, |
| 74 | + "execution_count": 135, |
80 | 75 | "id": "9e580734-b4f8-4d48-82d2-adcac10f8714",
|
81 | 76 | "metadata": {},
|
82 | 77 | "outputs": [
|
83 | 78 | {
|
84 | 79 | "name": "stdout",
|
85 | 80 | "output_type": "stream",
|
86 | 81 | "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" |
| 82 | + "23\n", |
| 83 | + "35\n", |
| 84 | + "37\n" |
100 | 85 | ]
|
101 | 86 | },
|
102 | 87 | {
|
103 | 88 | "data": {
|
104 | 89 | "text/plain": [
|
105 |
| - "['A', 'S', 'R', 'P', 'B']" |
| 90 | + "['E', 'H', 'U', 'B', 'P', 'R', 'S', 'A', 'T']" |
106 | 91 | ]
|
107 | 92 | },
|
108 |
| - "execution_count": 53, |
| 93 | + "execution_count": 135, |
109 | 94 | "metadata": {},
|
110 | 95 | "output_type": "execute_result"
|
111 | 96 | }
|
112 | 97 | ],
|
113 | 98 | "source": [
|
114 |
| - "path_states(iterative_lengthening_search(r1))" |
| 99 | + "path_states(iterative_lengthening_search(r1))\n", |
| 100 | + "path_states(iterative_lengthening_search(r2))\n", |
| 101 | + "path_states(iterative_lengthening_search(r3))" |
115 | 102 | ]
|
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 | 103 | }
|
146 | 104 | ],
|
147 | 105 | "metadata": {
|
|
0 commit comments