def knight(algorithm_type, starting_x, starting_y, goal_x, goal_y): # Create a board of size 8 b = board(8) # Set starting position and goal position p_start = position(starting_x, starting_y) p_goal = position(goal_x, goal_y) # Check if the positions are available if not p_start.is_available_pos(b) or not p_goal.is_available_pos(b): print("position out of board range") return -1 # Create agent a = agent(p_start, p_goal) # Classify the type of agent if algorithm_type == 0: a = bfs(p_start, p_goal) elif algorithm_type == 1: a = dfs(p_start, p_goal) elif algorithm_type == 2: a = ids(p_start, p_goal) elif algorithm_type == 3: a = astar(p_start, p_goal) elif algorithm_type == 4: a = idastar(p_start, p_goal) else: usage() return -1 # Search path path_list = a.search(b) # Print path for path in path_list: print(path, end="") print() # Return the number of expanded nodes return a.expanded_node_count
def board_size_test(): print("======== Board size comparison ========") p_start = position(0, 0) p_goal = position(2, 2) agents = [ bfs(p_start, p_goal), dfs(p_start, p_goal), ids(p_start, p_goal), astar(p_start, p_goal), idastar(p_start, p_goal) ] for a in agents: print(a) print("Board size\tSearch time (ms)\tSteps\t\tExpanded nodes") bm = [-1, -1, -1, -1] for b_size in range(3, 17): b = board(b_size) start_time = time.time() path = a.search(b) search_time = (time.time() - start_time) * 100 steps = len(path) - 1 bm[0] = b_size if bm[0] == -1 else bm[0] bm[1] = search_time if bm[1] == -1 else bm[1] bm[2] = steps if bm[2] == -1 and steps != 0 else bm[2] bm[3] = a.expanded_node_count != 0 if bm[ 3] == -1 and a.expanded_node_count else bm[3] print("{} ({:.3f})\t{:.6f} ({:.3f})\t{} ({:.3f})\t{} ({:.3f})". format(b_size, b_size / bm[0], search_time, search_time / bm[1], steps, steps / bm[2], a.expanded_node_count, a.expanded_node_count / bm[3])) print()
i = 0 while i < 5: command = input() if command == 'usi': print('id name suisui') print('usiok') if command == 'isready': print('readyok') if re.match('position', command): sfen = command if re.match('go', command): #初期局面にする Bboard.shoki() Wboard.shoki() #startposを読み込み、現局面を表示する board.position(sfen) board.synth() board.kyokumen() print(f'{board.turn = }') #以下合法手生成 if board.turn == 1: Bmoves.move1() print(f'{Bmoves.depth1=}') moves = Bmoves.depth1 #合法手がなければ投了 if Bmoves.depth1 == []: print('bestmove resign') #合法手があればランダムで選択 else: Beval.eval(moves) if Wkikimoves1.depth1 == []:
def algorithm_type_test(): print("======== Algorithm type comparison ========") b = board(8) pairs = [(position(2, 2), position(4, 4)), (position(4, 4), position(2, 2)), (position(0, 0), position(7, 7)), (position(7, 7), position(0, 0)), (position(0, 0), position(0, 1)), (position(0, 1), position(0, 0)), (position(0, 0), position(9, 10))] for pair in pairs: p_start = pair[0] p_goal = pair[1] agents = [ bfs(p_start, p_goal), dfs(p_start, p_goal), ids(p_start, p_goal), astar(p_start, p_goal), idastar(p_start, p_goal) ] bm = [-1, -1, -1] print("From", p_start, "to", p_goal) print("Algorithm\tSearch time (ms)\tSteps\t\tExpanded nodes") for a in agents: start_time = time.time() path = a.search(b) search_time = (time.time() - start_time) * 100 steps = len(path) - 1 bm[0] = search_time if bm[0] == -1 else bm[0] bm[1] = steps if bm[1] == -1 and steps != 0 else bm[1] bm[2] = a.expanded_node_count if bm[ 2] == -1 and a.expanded_node_count else bm[2] print("{}\t\t{:.6f} ({:.3f})\t{} ({:.3f})\t{} ({:.3f})".format( a, search_time, search_time / bm[0], steps, steps / bm[1], a.expanded_node_count, a.expanded_node_count / bm[2])) print()
cur_node.add_child(child) # Set frontier pair = (estimated_cost(child.depth, child, self.goal), child) bisect.insort(frontier, pair) if len(explorered_set) >= math.pow(b.size, 2): return [] return [] if __name__ == '__main__': # Examples b = board(8) p_start = position(0, 0) p_goal = position(2, 2) a = bfs(p_start, p_goal) path = a.search(b) b.print_pathway(path) print(a.expanded_node_count) a = dfs(p_start, p_goal) path = a.search(b) b.print_pathway(path) print(a.expanded_node_count) a = ids(p_start, p_goal) path = a.search(b) b.print_pathway(path)