Skip to content

Commit 28a7d65

Browse files
committed
ass fs
1 parent afc4669 commit 28a7d65

File tree

8 files changed

+104
-80
lines changed

8 files changed

+104
-80
lines changed

a_star_search.ipynb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"metadata": {},
2222
"source": [
2323
"<center>\n",
24-
" <img src='pic/a_star/0.jpg'/>\n",
24+
" <img src='pic/a_star_search/0.jpg'/>\n",
2525
"</center>"
2626
]
2727
},
@@ -83,7 +83,7 @@
8383
},
8484
{
8585
"cell_type": "code",
86-
"execution_count": 3,
86+
"execution_count": null,
8787
"metadata": {},
8888
"outputs": [],
8989
"source": [
@@ -95,18 +95,21 @@
9595
" self.g = g\n",
9696
" self.h = h\n",
9797
"\n",
98+
"\n",
9899
" # Less than\n",
99100
" def __lt__(self, other):\n",
100101
" if other == None:\n",
101102
" return False\n",
102103
" return self.g + self.h < other.g + other.h\n",
103104
"\n",
105+
"\n",
104106
" # Equal\n",
105107
" def __eq__(self, other):\n",
106108
" if other == None:\n",
107109
" return False\n",
108110
" return self.name == other.name\n",
109111
"\n",
112+
"\n",
110113
" # Display\n",
111114
" def display(self):\n",
112115
" print(f'{self.name}-{self.g}-{self.h}')"
@@ -135,56 +138,72 @@
135138
},
136139
{
137140
"cell_type": "code",
138-
"execution_count": 5,
141+
"execution_count": null,
139142
"metadata": {},
140143
"outputs": [],
141144
"source": [
142145
"# Get path\n",
143146
"def get_path(O, distance):\n",
144147
" O.display()\n",
145148
" distance += O.g + O.h\n",
149+
"\n",
146150
" if O.p != None:\n",
147151
" get_path(O.p, distance)\n",
148152
" else:\n",
149153
" print(f'Distance: {distance}')\n",
154+
"\n",
150155
" return"
151156
]
152157
},
153158
{
154159
"cell_type": "code",
155-
"execution_count": 6,
160+
"execution_count": null,
156161
"metadata": {},
157162
"outputs": [],
158163
"source": [
159-
"# Best first search\n",
164+
"# A* search\n",
160165
"def ass(S=Node('A'), G=Node('B')):\n",
161166
" count = 0\n",
167+
"\n",
162168
" Open = PriorityQueue()\n",
163169
" Close = PriorityQueue()\n",
170+
"\n",
164171
" S.h = data[S.name][-1]\n",
165172
" Open.put(S)\n",
173+
"\n",
166174
" while True:\n",
167175
" count += 1\n",
176+
"\n",
168177
" # check if Open is empty\n",
169178
" if Open.empty():\n",
170179
" print('Search failed!')\n",
180+
"\n",
171181
" return\n",
182+
"\n",
172183
" O = Open.get()\n",
173184
" Close.put(O)\n",
185+
"\n",
174186
" print(f'Scan {count}: {O.name}-{O.g}-{O.h}')\n",
187+
"\n",
175188
" # check if O is destination point\n",
176189
" if O.__eq__(G):\n",
177190
" print('Search success!')\n",
191+
"\n",
178192
" get_path(O, 0)\n",
193+
"\n",
179194
" return\n",
195+
"\n",
180196
" i = 0\n",
197+
"\n",
181198
" # find all subpoints of O that are not in Open and Close\n",
182199
" while i < len(data[O.name]) - 1:\n",
183200
" name = data[O.name][i]\n",
184201
" temp = Node(name=name, g=O.g + data[O.name][i + 1], h=data[name][-1])\n",
185202
" temp.p = O\n",
203+
"\n",
186204
" if not (checkin_priority(temp, Open) and checkin_priority(temp, Close)):\n",
187205
" Open.put(temp)\n",
206+
"\n",
188207
" i += 2"
189208
]
190209
},
@@ -226,7 +245,7 @@
226245
"metadata": {},
227246
"source": [
228247
"<center>\n",
229-
" <img src='pic/a_star/1.jpg'/>\n",
248+
" <img src='pic/a_star_search/1.jpg'/>\n",
230249
"</center>"
231250
]
232251
}

breadth_first_search.ipynb

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,9 @@
2121
"cell_type": "markdown",
2222
"metadata": {},
2323
"source": [
24-
"```\n",
25-
" A\n",
26-
" ____________|____________\n",
27-
" | | |\n",
28-
" B C D\n",
29-
"____|____ ____|____ ____|____\n",
30-
"| | | | | |\n",
31-
"E F G H I J\n",
32-
" ____|____ ____|____\n",
33-
" | | | | |\n",
34-
" K L M N O\n",
35-
"```"
24+
"<center>\n",
25+
" <img src='pic/first_search/0.jpg'/>\n",
26+
"</center>"
3627
]
3728
},
3829
{
@@ -80,12 +71,14 @@
8071
" self.name = name\n",
8172
" self.p = p\n",
8273
"\n",
83-
" # Euqal\n",
74+
"\n",
75+
" # Equal\n",
8476
" def __eq__(self, other):\n",
8577
" if (other == None):\n",
8678
" return False\n",
8779
" return self.name == other.name\n",
8880
"\n",
81+
"\n",
8982
" # Display\n",
9083
" def display(self):\n",
9184
" print(self.name)"
@@ -98,12 +91,12 @@
9891
"outputs": [],
9992
"source": [
10093
"data = defaultdict(list)\n",
101-
"data['A'] = ['B', 'C', 'D']\n",
102-
"data['B'] = ['E', 'F']\n",
103-
"data['C'] = ['G', 'H']\n",
104-
"data['D'] = ['I', 'J']\n",
105-
"data['F'] = ['K', 'L', 'M']\n",
106-
"data['H'] = ['N', 'O']"
94+
"\n",
95+
"data['A'] = ['B', 'C']\n",
96+
"data['B'] = ['D', 'E']\n",
97+
"data['C'] = ['F', 'G']\n",
98+
"data['E'] = ['H', 'I']\n",
99+
"data['G'] = ['J']"
107100
]
108101
},
109102
{
@@ -137,6 +130,7 @@
137130
"# Get path\n",
138131
"def get_path(O):\n",
139132
" O.display()\n",
133+
"\n",
140134
" if O.p != None:\n",
141135
" get_path(O.p)\n",
142136
" else:\n",
@@ -152,27 +146,39 @@
152146
"# Breadth First Search\n",
153147
"def bfs(S=Node('A'), G=Node('B')):\n",
154148
" count = 0\n",
149+
"\n",
155150
" Open = []\n",
156151
" Close = []\n",
152+
"\n",
157153
" Open.append(S)\n",
154+
"\n",
158155
" while True:\n",
159156
" count += 1\n",
157+
"\n",
160158
" # check if Open is empty\n",
161159
" if len(Open) == 0:\n",
162160
" print('Search failed!')\n",
161+
"\n",
163162
" return\n",
163+
" \n",
164164
" O = Open.pop(0)\n",
165165
" Close.append(O)\n",
166+
"\n",
166167
" print(f'Scan {count}: {O.name}')\n",
168+
"\n",
167169
" # check if O is destination point\n",
168170
" if O.__eq__(G):\n",
169171
" print('Search success!')\n",
172+
"\n",
170173
" get_path(O)\n",
174+
"\n",
171175
" return\n",
176+
" \n",
172177
" # find all subpoints of O that are not in Open and Close\n",
173178
" for x in data[O.name]:\n",
174179
" temp = Node(x)\n",
175180
" temp.p = O\n",
181+
" \n",
176182
" if not (checkin_array(temp, Open) and checkin_array(temp, Close)):\n",
177183
" Open.append(temp)"
178184
]
@@ -196,28 +202,23 @@
196202
"Scan 8: H\n",
197203
"Scan 9: I\n",
198204
"Scan 10: J\n",
199-
"Scan 11: K\n",
200-
"Scan 12: L\n",
201-
"Scan 13: M\n",
202-
"Scan 14: N\n",
203-
"Scan 15: O\n",
204205
"Search success!\n",
205-
"O\n",
206-
"H\n",
206+
"J\n",
207+
"G\n",
207208
"C\n",
208209
"A\n"
209210
]
210211
}
211212
],
212213
"source": [
213214
"# Run\n",
214-
"bfs(Node('A'), Node('O'))"
215+
"bfs(Node('A'), Node('J'))"
215216
]
216217
}
217218
],
218219
"metadata": {
219220
"kernelspec": {
220-
"display_name": "Python 3",
221+
"display_name": "base",
221222
"language": "python",
222223
"name": "python3"
223224
},
@@ -231,14 +232,9 @@
231232
"name": "python",
232233
"nbconvert_exporter": "python",
233234
"pygments_lexer": "ipython3",
234-
"version": "3.11.3"
235+
"version": "3.11.7"
235236
},
236-
"orig_nbformat": 4,
237-
"vscode": {
238-
"interpreter": {
239-
"hash": "bb31923fe1462919371239128cb0b2695295dfc560fc452d1a3a2e251a4b53eb"
240-
}
241-
}
237+
"orig_nbformat": 4
242238
},
243239
"nbformat": 4,
244240
"nbformat_minor": 2

0 commit comments

Comments
 (0)