Skip to content

Move viz code + changes to search #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Updating submodule
  • Loading branch information
bakerwho committed Mar 7, 2018
commit 4f591486ee976e05fcf4f5b5f37278dfcf7f69cc
56 changes: 21 additions & 35 deletions search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"cell_type": "code",
"execution_count": 134,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
Expand Down Expand Up @@ -635,7 +636,9 @@
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"romania_map = UndirectedGraph(dict(\n",
Expand Down Expand Up @@ -680,7 +683,9 @@
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)"
Expand Down Expand Up @@ -730,7 +735,9 @@
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
Expand All @@ -754,7 +761,9 @@
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# initialise a graph\n",
Expand Down Expand Up @@ -805,36 +814,11 @@
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def show_map(node_colors):\n",
" # set the size of the plot\n",
" plt.figure(figsize=(18,13))\n",
" # draw the graph (both nodes and edges) with locations from romania_locations\n",
" nx.draw(G, pos = romania_locations, node_color = [node_colors[node] for node in G.nodes()])\n",
"\n",
" # draw labels for nodes\n",
" node_label_handles = nx.draw_networkx_labels(G, pos = node_label_pos, labels = node_labels, font_size = 14)\n",
" # add a white bounding box behind the node labels\n",
" [label.set_bbox(dict(facecolor='white', edgecolor='none')) for label in node_label_handles.values()]\n",
"\n",
" # add edge lables to the graph\n",
" nx.draw_networkx_edge_labels(G, pos = romania_locations, edge_labels=edge_labels, font_size = 14)\n",
" \n",
" # add a legend\n",
" white_circle = lines.Line2D([], [], color=\"white\", marker='o', markersize=15, markerfacecolor=\"white\")\n",
" orange_circle = lines.Line2D([], [], color=\"orange\", marker='o', markersize=15, markerfacecolor=\"orange\")\n",
" red_circle = lines.Line2D([], [], color=\"red\", marker='o', markersize=15, markerfacecolor=\"red\")\n",
" gray_circle = lines.Line2D([], [], color=\"gray\", marker='o', markersize=15, markerfacecolor=\"gray\")\n",
" green_circle = lines.Line2D([], [], color=\"green\", marker='o', markersize=15, markerfacecolor=\"green\")\n",
" plt.legend((white_circle, orange_circle, red_circle, gray_circle, green_circle),\n",
" ('Un-explored', 'Frontier', 'Currently Exploring', 'Explored', 'Final Solution'),\n",
" numpoints=1,prop={'size':16}, loc=(.8,.75))\n",
" \n",
" # show the plot. No need to use in notebooks. nx.draw will show the graph itself.\n",
" plt.show()"
]
"source": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -1056,7 +1040,9 @@
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class vacuumAgent(SimpleProblemSolvingAgentProgram):\n",
Expand Down Expand Up @@ -3915,7 +3901,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
"version": "3.6.3"
}
},
"nbformat": 4,
Expand Down
31 changes: 18 additions & 13 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def __init__(self, initial_state=None):
def __call__(self, percept):
"""[Figure 3.1] Formulate a goal and problem, then
search for a sequence of actions to solve it."""
self.state = self.update_state(self.state, percept)
self.state = self.update_state(percept)
if not self.seq:
goal = self.formulate_goal(self.state)
problem = self.formulate_problem(self.state, goal)
Expand Down Expand Up @@ -182,7 +182,7 @@ def search(self, problem):
def tree_search(problem, frontier):
"""Search through the successors of a problem to find a goal.
The argument frontier should be an empty queue.
Don't worry about repeated paths to a state. [Figure 3.7]"""
Repeats infinites in case of loops. [Figure 3.7]"""
frontier.append(Node(problem.initial))
while frontier:
node = frontier.pop()
Expand All @@ -195,6 +195,7 @@ def tree_search(problem, frontier):
def graph_search(problem, frontier):
"""Search through the successors of a problem to find a goal.
The argument frontier should be an empty queue.
Does not get trapped by loops.
If two paths reach a state, only use the first one. [Figure 3.7]"""
frontier.append(Node(problem.initial))
explored = set()
Expand Down Expand Up @@ -225,7 +226,11 @@ def depth_first_graph_search(problem):


def breadth_first_search(problem):
"""[Figure 3.11]"""
"""[Figure 3.11]
Note that this function can be implemented in a
single line as below:
return graph_search(problem, FIFOQueue())
"""
node = Node(problem.initial)
if problem.goal_test(node.state):
return node
Expand Down Expand Up @@ -730,10 +735,10 @@ def __init__(self, initial, goal, graph):
self.graph = graph

def actions(self, state):
return self.graph.dict[state].keys()
return self.graph.graph_dict[state].keys()

def output(self, state, action):
return self.graph.dict[state][action]
return self.graph.graph_dict[state][action]

def h(self, state):
"""Returns least possible cost to reach a goal for the given state."""
Expand Down Expand Up @@ -920,16 +925,16 @@ class Graph:
length of the link from A to B. 'Lengths' can actually be any object at
all, and nodes can be any hashable object."""

def __init__(self, dict=None, directed=True):
self.dict = dict or {}
def __init__(self, graph_dict=None, directed=True):
self.graph_dict = graph_dict or {}
self.directed = directed
if not directed:
self.make_undirected()

def make_undirected(self):
"""Make a digraph into an undirected graph by adding symmetric edges."""
for a in list(self.dict.keys()):
for (b, dist) in self.dict[a].items():
for a in list(self.graph_dict.keys()):
for (b, dist) in self.graph_dict[a].items():
self.connect1(b, a, dist)

def connect(self, A, B, distance=1):
Expand All @@ -941,21 +946,21 @@ def connect(self, A, B, distance=1):

def connect1(self, A, B, distance):
"""Add a link from A to B of given distance, in one direction only."""
self.dict.setdefault(A, {})[B] = distance
self.graph_dict.setdefault(A, {})[B] = distance

def get(self, a, b=None):
"""Return a link distance or a dict of {node: distance} entries.
.get(a,b) returns the distance or None;
.get(a) returns a dict of {node: distance} entries, possibly {}."""
links = self.dict.setdefault(a, {})
links = self.graph_dict.setdefault(a, {})
if b is None:
return links
else:
return links.get(b)

def nodes(self):
"""Return a list of nodes in the graph."""
return list(self.dict.keys())
return list(self.graph_dict.keys())


def UndirectedGraph(dict=None):
Expand Down Expand Up @@ -1097,7 +1102,7 @@ def path_cost(self, cost_so_far, A, action, B):
def find_min_edge(self):
"""Find minimum value of edges."""
m = infinity
for d in self.graph.dict.values():
for d in self.graph.graph_dict.values():
local_min = min(d.values())
m = min(m, local_min)

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy