Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec662d6

Browse files
committed
finished ILS
1 parent abba7c2 commit ec662d6

File tree

1 file changed

+18
-60
lines changed

1 file changed

+18
-60
lines changed

iterative_lengthening_search.ipynb

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 51,
5+
"execution_count": 127,
66
"id": "ef35fc56-180a-4d1a-9099-bed021f6e0ee",
77
"metadata": {},
88
"outputs": [],
@@ -24,33 +24,28 @@
2424
" s = child.state\n",
2525
" # if the path_cost is larger than cost_limit, we need to ignore this child.\n",
2626
" if child.path_cost > cost_limit:\n",
27-
" print(child.state, child.path_cost) \n",
2827
" if lowest_path_cost == None or child.path_cost < lowest_path_cost:\n",
2928
" 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",
3131
" elif s not in reached or child.path_cost < reached[s].path_cost:\n",
3232
" reached[s] = child\n",
3333
" frontier.add(child)\n",
3434
" return result\n",
3535
"\n",
3636
"def iterative_lengthening_search(problem):\n",
37-
" print(\"first iteration\")\n",
3837
" 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",
4843
" return n"
4944
]
5045
},
5146
{
5247
"cell_type": "code",
53-
"execution_count": 52,
48+
"execution_count": 134,
5449
"id": "99015fa1-471a-4149-bfe1-4bd7f12671fd",
5550
"metadata": {},
5651
"outputs": [],
@@ -76,72 +71,35 @@
7671
},
7772
{
7873
"cell_type": "code",
79-
"execution_count": 53,
74+
"execution_count": 135,
8075
"id": "9e580734-b4f8-4d48-82d2-adcac10f8714",
8176
"metadata": {},
8277
"outputs": [
8378
{
8479
"name": "stdout",
8580
"output_type": "stream",
8681
"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"
10085
]
10186
},
10287
{
10388
"data": {
10489
"text/plain": [
105-
"['A', 'S', 'R', 'P', 'B']"
90+
"['E', 'H', 'U', 'B', 'P', 'R', 'S', 'A', 'T']"
10691
]
10792
},
108-
"execution_count": 53,
93+
"execution_count": 135,
10994
"metadata": {},
11095
"output_type": "execute_result"
11196
}
11297
],
11398
"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))"
115102
]
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": []
145103
}
146104
],
147105
"metadata": {

0 commit comments

Comments
 (0)