def uniform_cost_search(problem): """ Uniform cost search reference: page 84 Peter Norvig AI book """ color = defaultdict(lambda: WHITE) distance = defaultdict(lambda: float('inf')) parent = defaultdict(lambda: None) color[problem.initial] = GRAY distance[problem.initial] = 0 #parent[problem.initial] = None pq = CustomPriorityQueue() initial_state = (0,problem.initial) pq.add(initial_state) while pq.empty() == False: (pathcost,u) = pq.pop() children_states = problem.children(u) if problem.goal_test(u): return (pathcost,reconstruct_path(parent,problem.goal)) for (child,cost) in children_states: if color[child] == WHITE: color[child] = GRAY distance[child] = pathcost + cost pq.add((distance[child],child)) parent[child] = u elif color[child] == GRAY and distance[child] > pathcost + cost: distance[child] = pathcost + cost parent[child] = u pq.replace(child,(distance[child],child)) color[u] = BLACK
def uniform_cost_search(problem): """ Uniform cost search reference: page 84 Peter Norvig AI book """ color = defaultdict(lambda: WHITE) distance = defaultdict(lambda: float('inf')) parent = defaultdict(lambda: None) color[problem.initial] = GRAY distance[problem.initial] = 0 #parent[problem.initial] = None pq = CustomPriorityQueue() initial_state = (0, problem.initial) pq.add(initial_state) while pq.empty() == False: (pathcost, u) = pq.pop() children_states = problem.children(u) if problem.goal_test(u): return (pathcost, reconstruct_path(parent, problem.goal)) for (child, cost) in children_states: if color[child] == WHITE: color[child] = GRAY distance[child] = pathcost + cost pq.add((distance[child], child)) parent[child] = u elif color[child] == GRAY and distance[child] > pathcost + cost: distance[child] = pathcost + cost parent[child] = u pq.replace(child, (distance[child], child)) color[u] = BLACK
def a_star(problem): """ A* search reference: http://en.wikipedia.org/wiki/A*_search_algorithm """ closedset = Set() openset = Set() parent = defaultdict(lambda: None) g = {} f = {} openset.add(problem.initial) g[problem.initial] = 0 f[problem.initial] = g[problem.initial] + problem.h( problem.initial, problem.goal) pq = CustomPriorityQueue() pq.add((f[problem.initial], problem.initial)) while pq.empty() == False: (current_f, current) = pq.pop() if problem.goal_test(current): return (g[current], reconstruct_path(parent, problem.goal)) openset.remove(current) closedset.add(current) children = problem.children(current) for (child, cost) in children: tentative_g_score = g[current] + cost if child in closedset and tentative_g_score >= g[child]: continue if child not in closedset or tentative_g_score < g[ child]: # don't need to initialize for each i in g.V: g[i]=inf. The reason is # if a node x is not initilized that means there was visited. # That means it the node x is not in openset. Hence the node # x is also not in closedset parent[child] = current g[child] = tentative_g_score f[child] = g[child] + problem.h(child, problem.goal) #print "edge:",current,"-->",child,"real:",cost, "approx:",problem.h(current,child) if child not in openset: openset.add(child) pq.add((f[child], child)) elif child in openset: # Replace the priority queue f[child] value. Wiki algo didn't use a priority queue. # They choose the current node by selecting the node in openset having the lowest f[] value # Therefore they are getting the updated f values from f[]. In our case, the lowest f[] value # is taken from the priority queue. Therefore, we need to keep the most uptodate f values in # the priority queue. The replace operation is optimized according to python documentation. pq.replace(child, (f[child], child)) #print "replace" return None
def a_star(problem): """ A* search reference: http://en.wikipedia.org/wiki/A*_search_algorithm """ closedset = Set() openset = Set() parent = defaultdict(lambda: None) g = {} f = {} openset.add(problem.initial) g[problem.initial] = 0 f[problem.initial] = g[problem.initial] + problem.h(problem.initial,problem.goal) pq = CustomPriorityQueue() pq.add((f[problem.initial],problem.initial)) while pq.empty() == False: (current_f, current) = pq.pop() if problem.goal_test(current): return (g[current],reconstruct_path(parent,problem.goal)) openset.remove(current) closedset.add(current) children = problem.children(current) for (child,cost) in children: tentative_g_score = g[current] + cost if child in closedset and tentative_g_score >= g[child]: continue if child not in closedset or tentative_g_score < g[child]: # don't need to initialize for each i in g.V: g[i]=inf. The reason is # if a node x is not initilized that means there was visited. # That means it the node x is not in openset. Hence the node # x is also not in closedset parent[child] = current g[child] = tentative_g_score f[child] = g[child] + problem.h(child,problem.goal) #print "edge:",current,"-->",child,"real:",cost, "approx:",problem.h(current,child) if child not in openset: openset.add(child) pq.add((f[child],child)) elif child in openset: # Replace the priority queue f[child] value. Wiki algo didn't use a priority queue. # They choose the current node by selecting the node in openset having the lowest f[] value # Therefore they are getting the updated f values from f[]. In our case, the lowest f[] value # is taken from the priority queue. Therefore, we need to keep the most uptodate f values in # the priority queue. The replace operation is optimized according to python documentation. pq.replace(child,(f[child],child)) #print "replace" return None
def a_star_beam_search(problem, beam_size): """ A* search reference: http://en.wikipedia.org/wiki/A*_search_algorithm """ closedset = Set() openset = Set() parent = defaultdict(lambda: None) g = {} f = {} openset.add(problem.initial) g[problem.initial] = 0 f[problem.initial] = g[problem.initial] + problem.h( problem.initial, problem.goal) pq = CustomPriorityQueue() pq.add((f[problem.initial], problem.initial)) while pq.empty() == False: (current_f, current) = pq.pop() if problem.goal_test(current): #todo path return (g[current], reconstruct_path(parent, problem.goal)) openset.remove(current) closedset.add(current) children = problem.children(current) children_count = 0 for (child, cost) in children: tentative_g_score = g[current] + cost if child in closedset and tentative_g_score >= g[child]: continue if (child not in closedset or tentative_g_score < g[child] ) and children_count < beam_size: parent[child] = current g[child] = tentative_g_score f[child] = g[child] + problem.h(child, problem.goal) children_count += 1 if child not in openset: openset.add(child) pq.add((f[child], child)) elif child in openset: pq.replace(child, (f[child], child)) return None
def a_star_beam_search(problem,beam_size): """ A* search reference: http://en.wikipedia.org/wiki/A*_search_algorithm """ closedset = Set() openset = Set() parent = defaultdict(lambda: None) g = {} f = {} openset.add(problem.initial) g[problem.initial] = 0 f[problem.initial] = g[problem.initial] + problem.h(problem.initial,problem.goal) pq = CustomPriorityQueue() pq.add((f[problem.initial],problem.initial)) while pq.empty() == False: (current_f, current) = pq.pop() if problem.goal_test(current): #todo path return (g[current],reconstruct_path(parent,problem.goal)) openset.remove(current) closedset.add(current) children = problem.children(current) children_count = 0 for (child,cost) in children: tentative_g_score = g[current] + cost if child in closedset and tentative_g_score >= g[child]: continue if (child not in closedset or tentative_g_score < g[child]) and children_count < beam_size: parent[child] = current g[child] = tentative_g_score f[child] = g[child] + problem.h(child,problem.goal) children_count += 1 if child not in openset: openset.add(child) pq.add((f[child],child)) elif child in openset: pq.replace(child,(f[child],child)) return None