master
/ 02.01 基于搜索的问题求解(学生版).ipynb

02.01 基于搜索的问题求解(学生版).ipynb @7439c2a

4b0cb67
 
 
 
 
 
32f9dec
4b0cb67
 
 
 
 
 
 
 
 
18d0870
 
 
 
 
 
 
4b0cb67
 
 
 
32f9dec
4b0cb67
 
 
 
 
 
47bf824
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd58965
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
47bf824
 
4b0cb67
47bf824
4b0cb67
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
18d0870
4b0cb67
 
 
18d0870
4b0cb67
 
18d0870
 
4b0cb67
 
 
 
 
 
47bf824
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32f9dec
4b0cb67
 
 
 
 
 
 
 
18d0870
4b0cb67
18d0870
 
 
 
 
4b0cb67
 
 
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
5ecac81
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ecac81
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
32f9dec
4b0cb67
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
18d0870
 
 
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
18d0870
 
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18d0870
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47bf824
 
 
4b0cb67
18d0870
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
32f9dec
4b0cb67
 
 
 
 
 
32f9dec
4b0cb67
 
 
 
 
 
32f9dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b0cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.1 基于搜索的问题求解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现实世界中许多问题都可以通过搜索的方法来求解,例如设计最佳出行路线或是制订合理的课程表。当给定一个待求解问题后,搜索算法会按照事先设定的逻辑来自动寻找符合求解问题的答案,因此一般可将搜索算法称为问题求解智能体。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://imgbed.momodel.cn//20200110155424.png\" width=500>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1.1 搜索算法基本概念"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们把书中的公交换乘的问题,转为无向图中的的路径寻找问题。无向图指的是边没有方向的图。\n",
    "\n",
    "首先,我们画出如下的无向图。该无向图中有 A,B,C,D,E,F,G 七个节点,其中 A 是起点, G 是目标点。\n",
    "\n",
    "点与点之间的连线称为边,边可以有权重,可以代表点与点之间的距离或者从一个点转移到另一个点需要花费的代价。\n",
    "\n",
    "下面我们先创建一个图。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先导入必要的包\n",
    "from search import SearchGraph\n",
    "import collections\n",
    "import matplotlib.pyplot as plt\n",
    "import collections\n",
    "from IPython import display\n",
    "import networkx as nx\n",
    "import numpy as np\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义节点列表\n",
    "node_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G']\n",
    "\n",
    "# 定义边及权重列表\n",
    "weighted_edges_list = [('A', 'B', 8), ('A', 'C', 20),\n",
    "                       ('B', 'F', 40), ('B', 'E', 30),\n",
    "                       ('B', 'D', 20), ('C', 'D', 10), \n",
    "                       ('D', 'G', 10), ('D', 'E', 10),\n",
    "                       ('E', 'F', 30), ('F', 'G', 30)]\n",
    "# 定义绘图中各个节点的坐标\n",
    "nodes_pos = {\"A\": (1, 1), \"B\": (3, 3), \"C\": (5, 0), \"D\": (9, 2),\n",
    "             \"E\": (7, 4), \"F\": (6,6),\"G\": (11,5)}\n",
    "\n",
    "# 绘制无向图\n",
    "g = SearchGraph(node_list, weighted_edges_list, 'A', 'G', max_depth=3, nodes_pos = nodes_pos )\n",
    "g.show_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "观察上图,可以看到从起点 A 到目标点 G 距离最短的路径是 A -> B -> D  -> G,其距离是 38,我们可以设计一个计算机程序,按照既定的规则,从起点 A 出发,不断尝试从一个节点移动到下一个节点,直到抵达目标点 G。\n",
    "\n",
    "在详细描述搜索算法之前,先了解下面四个重要的概念。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ **状态**。  在上面的例子中状态是什么?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ **测试目标**。 在上面的例子中测试目标是什么?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ **动作**。在上面的例子中动作是什么?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ **路径**。在上面的例子中路径是什么?\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更改下面的代码,设置一条路径并查看。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g.show_graph(this_path=\"ABDG\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1.2 搜索算法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "搜索算法就是不断从某一状态转移到下一状态,直至到达终止状态为止。\n",
    "\n",
    "**搜索树**是什么?\n",
    "\n",
    "如何构造搜索树 ?\n",
    "\n",
    "思考一下,路径搜索能出现回路吗?\n",
    "\n",
    "在搜索树中能够扩展的节点需满足什么条件?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看搜索树\n",
    "g.show_search_tree()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1.3 深度优先搜索和广度优先搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://imgbed.momodel.cn//20200110151426.png\" width=600>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**深度优先搜索**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g.animation_search_tree('dfs')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**广度优先搜索**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g.animation_search_tree('bfs')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于一个搜索问题,只要存在答案(即从初始节点到终止节点存在满足条件的一条路径),那么深度优先搜索和广度优先搜索都能找到一个答案吗?找到的答案一定是路径最短的吗?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1.4 启发式搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "能否在搜索过程中利用问题的定义以外**辅助信息**?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**辅助信息:各个节点到目标节点G的直线距离**\n",
    "\n",
    "|站点|A|B|C|D|E|F|G|\n",
    "|--|--|--|--|--|--|--|--|\n",
    "|距离|30|20|19|10|5|25|0|\n",
    "\n",
    "如果每下一站都选最近的,那么称之为“贪婪”。\n",
    "\n",
    "利用下面的代码查看贪婪最佳优先算法搜索过程:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 为搜索算法提供辅助信息\n",
    "g.help_info = {'A': 30, 'B': 20, 'C': 19, 'D':10, 'E':5, 'F':25, 'G': 0}\n",
    "# 动态演示贪婪搜索\n",
    "g.animation_search_tree('greedy')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "“贪婪”机制下找到的最佳路径是什么呢?它是最短路径吗?为什么会产生这样的搜索结果?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如何克服贪婪算法的不足?\n",
    " A\\*算法\n",
    "\n",
    " A\\*算法搜索过程:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 为搜索算法提供辅助信息\n",
    "g.help_info = {'A': 30, 'B': 20, 'C': 19, 'D':10, 'E':5, 'F':25, 'G': 0}\n",
    "# 动态演示 A* 算法\n",
    "g.animation_search_tree('a_star')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以调整辅助信息的比重\n",
    "# 当只考虑额外信息时,即 origin_info_weight 设置为 0 的时候,A* 算法退化为贪婪算法。\n",
    "g.animation_search_tree('a_star',help_info_weight=1, origin_info_weight=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在实际中,A\\* 算法的性能表现一定很优秀吗?如果启发函数设计的不好怎么办?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  思考与练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下图是一张线路示意图。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_list = [\"0\",\"1\",\"2\",\"3\",\"4\"]\n",
    "weighted_edges_list = [(\"0\",\"1\",10), (\"0\",\"2\",10),\n",
    "                       (\"1\", \"3\", 10), (\"2\", \"3\", 5), \n",
    "                       (\"2\", \"4\", 20), (\"3\", \"4\", 14),\n",
    "                       (\"3\", \"2\", 5)]\n",
    "nodes_pos = {\"0\":(1,7),\"1\":(5,1),\"2\":(5,13),\"3\":(9,7),\"4\":(11,13)}\n",
    "h_graph = SearchGraph(node_list, weighted_edges_list, \"0\", \"4\", 5, nodes_pos)\n",
    "h_graph.show_graph()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果使用深度优先搜索求状态 0 到状态 4 的一条路径,我们可以用下表来模拟搜索过程。注意:在下表中,结点的深度定义为它对应路径中状态转移的次数,如果多个未访问结点的深度相同,那么在这个例子里算法优先选择状态编号大的节点。\n",
    "\n",
    "|步骤|当前状态|当前未访问节点集合(用上划线标出了下一个扩展的节点)|\n",
    "|:--:|:--:|:--|\n",
    "|1|0|深度1:${0 -> 1,\\overline{0 -> 2}}$|\n",
    "|2|2|深度1:${0 -> 1}$ 深度2:$\\underline{(1)}$|\n",
    "|3|$\\underline{(2)}$|找到路径:0 -> 2 -> 4|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题 1**:请仔细观察上表中各项内容的含义,根据深度优先搜素的思路,横线(1)和(2)处应该填写什么。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**答案 1**:(在此处填写你的答案。)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题 2**:找到的路径0->2->4是代价最小的吗?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**答案 2**:(在此处填写你的答案。)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}