def travel(adj_mat, src=0): # Start time start = clock() optimal_tour = [] n = len(adj_mat) if not n: raise ValueError("Invalid adj Matrix") u = Node() PQ = PriorityQueue() optimal_length = 0 v = Node(level=0, path=[0]) min_length = float('inf') # infinity v.bound = bound(adj_mat, v) PQ.put(v) while not PQ.empty(): v = PQ.get() if v.bound < min_length: u.level = v.level + 1 for i in filter(lambda x: x not in v.path, range(1, n)): u.path = v.path[:] u.path.append(i) if u.level == n - 2: l = set(range(1, n)) - set(u.path) u.path.append(list(l)[0]) # putting the first vertex at last u.path.append(0) _len = length(adj_mat, u) if _len < min_length: min_length = _len optimal_length = _len optimal_tour = u.path[:] else: u.bound = bound(adj_mat, u) if u.bound < min_length: PQ.put(u) # make a new node at each iteration! python it is!! u = Node(level=u.level) # shifting to proper source(start of path) optimal_tour_src = optimal_tour if src is not 1: optimal_tour_src = optimal_tour[:-1] y = optimal_tour_src.index(src) optimal_tour_src = optimal_tour_src[y:] + optimal_tour_src[:y] optimal_tour_src.append(optimal_tour_src[0]) BnB_time = clock() - start BnB_output = [ "Branch and Bound", optimal_length, optimal_tour_src, BnB_time ] #return optimal_tour_src, optimal_length return BnB_output
def a_star(problem): frontier = [] heapq.heappush(frontier, Node(problem.init_state, None, 0)) explored_set = [] while True: if len(frontier) <= 0: return 'FAIL' current_node = heapq.heappop(frontier) if problem.goal_test(current_node.state): solution = get_solution(current_node) return solution explored_set.append(current_node.state) for action, destination in problem.get_possible_actions( current_node.state): child = expand(parent=current_node, destination=destination, action=action, goal_state=problem.goal_state, algorithm='A_STAR') if child.state not in [ node.state for node in frontier ] and child.state not in [state for state in explored_set]: heapq.heappush(frontier, child) for node in frontier: if child.state == node.state: if child.path_cost < node.path_cost: frontier.remove(node) heapq.heappush(frontier, child) # not sure
def bfs(problem): current_node = Node(problem.init_state, None, 0) if problem.goal_test(current_node.state): solution = get_solution(current_node) return solution frontier = Queue() frontier.enqueue(current_node) explored_set = [] while True: if frontier.is_empty(): return 'FAIL' current_node = frontier.dequeue() # print(current_node.state) explored_set.append(current_node.state) for action, destination in problem.get_possible_actions( current_node.state): child = expand(parent=current_node, destination=destination, action=action, goal_state=problem.goal_state, algorithm='BFS') if child.state not in [ node.state for node in frontier.queue ] and child.state not in [state for state in explored_set]: # print(child.state) if problem.goal_test(child.state): solution = get_solution(child) return solution frontier.enqueue(child)
def expand(parent, destination, action, goal_state, algorithm): if algorithm == 'UCS' or algorithm == 'BFS': child = Node( state=destination, parent=parent, path_cost=parent.path_cost + step_cost(algorithm, action, cal_abs_elevation_diff(parent.state, destination))) else: child = Node( state=destination, parent=parent, path_cost=parent.path_cost + step_cost(algorithm, action, cal_abs_elevation_diff(parent.state, destination)), estimated_cost=cal_h(destination, goal_state)) return child
def greenTravel(adj_mat, src): optimal_tour = [] n = len(adj_mat) u = Node() PQ = PriorityQueue() optimal_length = 0 v = Node(level=0, path=[0]) min_length = float('inf') v.bound = bound(adj_mat, v) PQ.put(v) while not PQ.empty(): v = PQ.get() if v.bound < min_length: u.level = v.level + 1 for i in filter(lambda x: x not in v.path, range(1, n)): u.path = v.path[:] u.path.append(i) if u.level == n - 2: l = set(range(1, n)) - set(u.path) u.path.append(list(l)[0]) u.path.append(0) _len = length(adj_mat, u) if _len < min_length: min_length = _len optimal_length = _len optimal_tour = u.path[:] else: u.bound = bound(adj_mat, u) if u.bound < min_length: PQ.put(u) u = Node(level=u.level) optimal_tour_src = optimal_tour if src is not 1: optimal_tour_src = optimal_tour[:-1] y = optimal_tour_src.index(src) optimal_tour_src = optimal_tour_src[y:] + optimal_tour_src[:y] optimal_tour_src.append(optimal_tour_src[0]) return optimal_tour_src, optimal_length
def travel(adj_mat, src=0): optimal_tour = [] n = len(adj_mat) if not n: raise ValueError("Invalid adj Matrix") u = Node() PQ = PriorityQueue() optimal_length = 0 v = Node(level=0, path=[0]) min_length = float('inf') # infinity v.bound = bound(adj_mat, v) PQ.put(v) while not PQ.empty(): v = PQ.get() if v.bound < min_length: u.level = v.level + 1 for i in filter(lambda x: x not in v.path, range(1, n)): u.path = v.path[:] u.path.append(i) if u.level == n - 2: l = set(range(1, n)) - set(u.path) u.path.append(list(l)[0]) # putting the first vertex at last u.path.append(0) _len = length(adj_mat, u) if _len < min_length: min_length = _len optimal_length = _len optimal_tour = u.path[:] else: u.bound = bound(adj_mat, u) if u.bound < min_length: PQ.put(u) # make a new node at each iteration! python it is!! u = Node(level=u.level) # shifting to proper source(start of path) optimal_tour_src = optimal_tour if src is not 1: optimal_tour_src = optimal_tour[:-1] y = optimal_tour_src.index(src) optimal_tour_src = optimal_tour_src[y:] + optimal_tour_src[:y] optimal_tour_src.append(optimal_tour_src[0]) return optimal_tour_src, optimal_length