def more_pour_problem(capacities, goal, start=None): # your code here if not start: start = (0,) * len(capacities) return shortest_path_search(start, more_successors(capacities), lambda state: goal in state)
''' Created on Sep 15, 2013 @author: pglauner ''' from algorithms.routing.shortest_path import shortest_path_search from algorithms.routing.lowest_cost import lowest_cost_search from algorithms.routing.a_star.simple import a_star as a_star_simple from algorithms.routing.a_star.queue_based import a_star as a_star_queue from algorithms.routing.test_graphs.test_graphs import tricky_graph from algorithms.routing.test_graphs.phrase_graphs import simple_phrase from algorithms.routing.test_graphs.graph_utils import action_cost, is_goal, successors if __name__ == '__main__': print shortest_path_search(0, successors(tricky_graph), is_goal(100)) print lowest_cost_search(0, successors(tricky_graph), is_goal(100), action_cost) print a_star_simple(tricky_graph, 0, 100) print a_star_queue(tricky_graph, 0, 100) print print shortest_path_search(0, successors(simple_phrase), is_goal(1)) print lowest_cost_search(0, successors(simple_phrase), is_goal(1), action_cost) print a_star_simple(simple_phrase, 0, 1) print a_star_queue(simple_phrase, 0, 1)
for z in (x, y): if z == "M": m1 -= 1 m2 += 1 action += "M" if z == "C": c1 -= 1 c2 += 1 action += "C" if all(x >= 0 for x in (m1, c1, b1, m2, c2, b2)): if B1: res[(m1, c1, b1, m2, c2, b2)] = action + "->" elif B2: res[(m2, c2, b1, m1, c1, b2)] = "<-" + action return res def is_goal(start): def g(state): return state == (0, 0, 0) + start return g if __name__ == "__main__": start = m1, c1, b1, m2, c2, b2 = 3, 3, 1, 0, 0, 0 print shortest_path_search(start, successors, is_goal(start[0:3])) print lowest_cost_search(start, successors, is_goal(start[0:3]), lambda action: 1)
if z == 'M': m1 -= 1 m2 += 1 action += 'M' if z == 'C': c1 -= 1 c2 += 1 action += 'C' if all(x >= 0 for x in (m1, c1, b1, m2, c2, b2)): if B1: res[(m1, c1, b1, m2, c2, b2)] = action + '->' elif B2: res[(m2, c2, b1, m1, c1, b2)] = '<-' + action return res def is_goal(start): def g(state): return state == (0, 0, 0) + start return g if __name__ == '__main__': start = m1, c1, b1, m2, c2, b2 = 3, 3, 1, 0, 0, 0 print shortest_path_search(start, successors, is_goal(start[0:3])) print lowest_cost_search(start, successors, is_goal(start[0:3]), lambda action: 1)
res = {} empty_y, empty_x = get_y_x(state.index('E'), N) row_idx1 = empty_y * N row_idx2 = row_idx1 + N move_row_or_col(state, res, row_idx1, row_idx2, 'Row') col_idx1 = empty_x col_idx2 = N * (N - 1) + empty_x % N + 1 move_row_or_col(state, res, col_idx1, col_idx2, 'Col', N) return res return successors_n def is_goal(N): def is_goal_n(state): return tuple(range(N*N-1) + ['E']) == state return is_goal_n if __name__ == '__main__': for start in tile_games: N = tile_lens[len(start)] print start shortest_path = shortest_path_search(start, successors(N), is_goal(N)) print shortest_path print len(shortest_path) / 2, 'moves'
Created on Sep 8, 2013 @author: pglauner ''' from algorithms.routing.shortest_path import shortest_path_search from algorithms.routing.lowest_cost import lowest_cost_search def successors(X, Y): def sc(state): x, y = state assert x <= X and y <= Y return {((0, y+x) if y+x <= Y else (x-(Y-y), (Y))): 'x->y', ((x+y, 0) if x+y <= X else (X, (y-(X-x)))): 'x<-y', (X, y): 'fill x', (x, Y): 'fill y', (0, y): 'empty x', (x, 0): 'empty y'} return sc if __name__ == '__main__': res = shortest_path_search((0, 0), successors(418, 986), lambda state: state == (6, 0)) print res print '%s transitions' % (len(res) / 2) print res = lowest_cost_search((0, 0), successors(418, 986), lambda state: state == (6, 0), lambda action: 1) print res print '%s transitions' % (len(res) / 2)