def ILDS(SearchingProblem): print("---Iterative LDS Search---") before = datetime.now() search = iterative_limited_depth_first(SearchingProblem) #search = iterative_limited_depth_first(SearchingProblem,viewer=my_viewer) after = datetime.now() print("Path -->",search.path()) print("Time -->", (after - before).total_seconds()) print("Path cost-->",search.cost) print("-" * 40)
def main(): start_board = Board.board_from_file(sys.argv[1]) duplication_checks = sys.argv[2] method = sys.argv[3] heuristic = '' check_duplicates = 'graph' in duplication_checks if 'star' in method: heuristic = sys.argv[4] if heuristic == 'max_moves': start_board.heuristic = heuristics.max_moves elif heuristic == 'min_moves': start_board.heuristic = heuristics.min_moves elif heuristic == 'max_movable_pegs': start_board.heuristic = heuristics.max_movable_pegs elif heuristic == 'man': start_board.heuristic = heuristics.manhattan_distance else: print('You did not pick a viable heuristic. Exiting...') return start = time.time() if method == 'dfs': result = depth_first(start_board, check_duplicates) elif method == 'bfs': result = breadth_first(start_board, check_duplicates) elif method == 'astar': result = astar(start_board, check_duplicates) elif method == 'ildf': result = iterative_limited_depth_first(start_board, check_duplicates) else: print('You must choose a valid search method. Exiting...') return end = time.time() print("-" * 30) print('Search:', sys.argv[2], 'on', sys.argv[3], sys.argv[4] if len(sys.argv) > 4 else '') print('Input File:', sys.argv[1]) if result: path = result.path() for step in path: print(step[0], '-->', Board.board_from_state(step[1])) print('Duration: {0:.4f} seconds'.format(end - start)) print('Nodes Visited:', len(path)) else: print("No solution found!") print("-" * 30)
def resolver(metodo_busqueda): visor = BaseViewer() if metodo_busqueda == 'breadth_first': result = breadth_first(entrega1tradicional(INITIAL), graph_search=True, viewer=visor) if metodo_busqueda == 'depth_first': result = depth_first(entrega1tradicional(INITIAL), graph_search=True, viewer=visor) if metodo_busqueda == 'limited_depth_first': result = iterative_limited_depth_first(entrega1tradicional(INITIAL),10, viewer=visor) if metodo_busqueda == 'greedy': result = greedy(entrega1tradicional(INITIAL), graph_search=True, viewer=visor) if metodo_busqueda == 'astar': result = astar(entrega1tradicional(INITIAL), graph_search=True, viewer=visor) return result
# rows[row_e][col_e], rows[row_n][col_n] = rows[row_n][col_n], rows[row_e][col_e] # rows.append(graph[state][action]) return action def is_goal(self, state): '''Returns true if a state is the goal state.''' return str(state) == str(GOAL) def cost(self, state1, action, state2): '''Uniform cost ''' return int(graph[state1][action]) start = timer() result = iterative_limited_depth_first(EigthPuzzleProblem(INITIAL), graph_search=True) end = timer() # Time print "Time: " + str(end - start) # cost of solution print result.cost # Solution for action, state in result.path(): print 'Move number', action print state
#search 함수에 있는 파라미터를 조작함으로써 과제의 원하는 값을 얻어낼 수 있음. #예를 들면, 브리드 퍼스트에선 피포리스트를 사용하고, 프라블럼도 인자로 넘기고, 그래프 서치, 뷰 등등(뒤에 2개는 자세히 설명 x) #depth first같은 경우에는 라스트인 퍼스트아웃 리스트를 사용, #여기다가 ida* 구현해. 이름적고 return 값적고, 핸들링 하는 부분 어디다 추가하는진 모르겠지만 그것만 잘 추가하면 될것이야~(웬지 맨 밑에 search일것 같은데...) #memory라는 set이 있음. set이 뭐냐! 탐색했던 노드들을 메모리에 집어 넣는 것이다. 노드들을 탐색할때마다 메모리에다 에드를 시킴. 그렇다면, 메모리 렝스를 출력하면, 내가 탐색했던 노드의 길이수를 알 수 있겠지! #끝나기 전에 메모리 렝스를 출력하면 확장노드가 7개인것을 얻어낼 수 있음. 어디에다가 넣는진 모르겠지만, str(len(memory))를 잘 넣어봐. #또한, 생성노드는 fringe라는 리스트를 이용하면 구할 수 있음. fringe는 탐색할 노드들을 집어넣음. 종료직전에는 아직 탐색하지 않은 노드들만 남아있음. #ida스타는 a스타하고 iterative limited ~하고 합치면 된다. #f limit는 직접구현해야할거야~~ 평가함수값을 계산하는것도 다 나와있어 잘 찾아봐. #프로젝트 파일 전체하고 한글파일. #result = astar(problem, graph_search=True) #result = depth_first(problem, graph_search=True) #result = breadth_first(problem, graph_search=True) result = iterative_limited_depth_first(problem, graph_search=True) #result = idastar(problem, graph_search=True) # Extract the path path = [x[1] for x in result.path()] print("해길이", path) print(len(path)) # Print the result print() for y in range(len(MAP)): for x in range(len(MAP[y])): if (x, y) == problem.initial: print('o', end='') elif (x, y) == problem.goal: print('x', end='') elif (x, y) in path:
def test_iterative_limited_depth_first(self): result = iterative_limited_depth_first(self.problem) self.assertEquals(result.state, GOAL)