From 7c72db7f18dca8812d429d8012a95a4d702e0c6e Mon Sep 17 00:00:00 2001 From: tirthasheshpatel Date: Fri, 20 Dec 2019 02:19:07 +0530 Subject: [PATCH 1/3] DOC: Add docstring to __hash__ method in Node --- search.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/search.py b/search.py index 0104eb341..17af40ed2 100644 --- a/search.py +++ b/search.py @@ -123,6 +123,10 @@ def __eq__(self, other): return isinstance(other, Node) and self.state == other.state def __hash__(self): + # We use the hash value of the state + # stored in the node instead of the node + # object itself to quickly search a node + # with the same state in a Hash Table return hash(self.state) From 7d07802d7c1e07a9bd975043ec209e91dd25a3f9 Mon Sep 17 00:00:00 2001 From: tirthasheshpatel Date: Wed, 25 Dec 2019 02:56:29 +0530 Subject: [PATCH 2/3] MAINT: Add documenation and descriptive variable names --- search.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/search.py b/search.py index 17af40ed2..756a1af3e 100644 --- a/search.py +++ b/search.py @@ -340,42 +340,44 @@ def extend(U, open_dir, open_other, g_dir, g_other, closed_dir): open_dir.remove(n) closed_dir.append(n) - for c in problem.actions(n): - if c in open_dir or c in closed_dir: - if g_dir[c] <= problem.path_cost(g_dir[n], n, None, c): + for child in problem.actions(n): + if child in open_dir or child in closed_dir: + if g_dir[child] <= problem.path_cost(g_dir[n], n, None, child): continue - open_dir.remove(c) + open_dir.remove(child) - g_dir[c] = problem.path_cost(g_dir[n], n, None, c) - open_dir.append(c) + g_dir[child] = problem.path_cost(g_dir[n], n, None, child) + open_dir.append(child) - if c in open_other: - U = min(U, g_dir[c] + g_other[c]) + if child in open_other: + U = min(U, g_dir[child] + g_other[child]) return U, open_dir, closed_dir, g_dir def find_min(open_dir, g): """Finds minimum priority, g and f values in open_dir""" - m, m_f = np.inf, np.inf + # pr_min_f isn't forward pr_min instead it's the f-value + # of node with priority pr_min. + pr_min, pr_min_f = np.inf, np.inf for n in open_dir: f = g[n] + problem.h(n) pr = max(f, 2 * g[n]) - m = min(m, pr) - m_f = min(m_f, f) + pr_min = min(pr_min, pr) + pr_min_f = min(pr_min_f, f) - return m, m_f, min(g.values()) + return pr_min, pr_min_f, min(g.values()) def find_key(pr_min, open_dir, g): """Finds key in open_dir with value equal to pr_min and minimum g value.""" - m = np.inf + gmin = np.inf state = -1 for n in open_dir: pr = max(g[n] + problem.h(n), 2 * g[n]) if pr == pr_min: - if g[n] < m: - m = g[n] + if g[n] < gmin: + gmin = g[n] state = n return state From babb2032d1efb22ce7f39e13d60d5d2347406b9f Mon Sep 17 00:00:00 2001 From: Tirth Patel Date: Wed, 1 Jan 2020 18:13:11 +0530 Subject: [PATCH 3/3] FIXUP: Revert to previos names --- search.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/search.py b/search.py index 756a1af3e..689671769 100644 --- a/search.py +++ b/search.py @@ -340,18 +340,18 @@ def extend(U, open_dir, open_other, g_dir, g_other, closed_dir): open_dir.remove(n) closed_dir.append(n) - for child in problem.actions(n): - if child in open_dir or child in closed_dir: - if g_dir[child] <= problem.path_cost(g_dir[n], n, None, child): + for c in problem.actions(n): + if c in open_dir or c in closed_dir: + if g_dir[c] <= problem.path_cost(g_dir[n], n, None, c): continue - open_dir.remove(child) + open_dir.remove(c) - g_dir[child] = problem.path_cost(g_dir[n], n, None, child) - open_dir.append(child) + g_dir[c] = problem.path_cost(g_dir[n], n, None, c) + open_dir.append(c) - if child in open_other: - U = min(U, g_dir[child] + g_other[child]) + if c in open_other: + U = min(U, g_dir[c] + g_other[c]) return U, open_dir, closed_dir, g_dir @@ -371,13 +371,13 @@ def find_min(open_dir, g): def find_key(pr_min, open_dir, g): """Finds key in open_dir with value equal to pr_min and minimum g value.""" - gmin = np.inf + m = np.inf state = -1 for n in open_dir: pr = max(g[n] + problem.h(n), 2 * g[n]) if pr == pr_min: - if g[n] < gmin: - gmin = g[n] + if g[n] < m: + m = g[n] state = n return state