def simulated_annealing(input_file): current_state = parse_file(input_file) state_list = [current_state] current_heuristic_val = eval(current_state[2],current_state[3],current_state[0],current_state[1]) max_state = current_state max_heuristic_val = current_heuristic_val T = 100000 while (T > 0.1): new_state = find_next_state(current_state) new_state_heuristic_val = eval(new_state[2],new_state[3],new_state[0],new_state[1]) d = delta(current_heuristic_val, new_state_heuristic_val) if d > 0: current_state = new_state current_heuristic_val = new_state_heuristic_val state_list.append(current_state) else: if (acceptance_probabilty(T,d)): current_state = new_state current_heuristic_val = new_state_heuristic_val state_list.append(current_state) if (compare_heuristic_greater(current_heuristic_val,max_heuristic_val)): max_heuristic_val = current_heuristic_val max_state = current_state T = update_temperature(T) print_board(max_state) print(max_heuristic_val)
def hillclimb(input_file): ''' Menggunakan algoritma Hill-Climbing untuk mencari kombinasi optimal jika diberikan bidaknya. Parameter: input_file (str) Mencetak board yang sudah diselesaikan. ''' # membuat current state berupa list apa saja bidak current_state = parse_file(input_file) # membuat list yang menyimpan current_state board dan nilai heuristik keadaan board max_heuristic_val = eval(current_state[2], current_state[3], current_state[0], current_state[1]) running = True while running: new_state = find_next_state(current_state) new_state_heuristic_val = eval(new_state[2], new_state[3], new_state[0], new_state[1]) if compare_heuristic_greater(new_state_heuristic_val, max_heuristic_val): current_state = new_state max_heuristic_val = new_state_heuristic_val else: running = False print_board(current_state) print(max_heuristic_val)
def genetic_algorithm(input_file, init_pop=4096, epoc_length=1000): state = parse_file(input_file) genetic_result = genetic(state[0], state[2], init_pop, epoc_length) new_state = (state[0], genetic_result[0], state[2], genetic_result[1]) print_board(new_state) print(eval(new_state[2], new_state[3], new_state[0], new_state[1]))
def print(self): print_board(self.tiles)
print(total_value) """ """ randomboard = mctsnode.generate_random_board(30) print("RANDOM BOARD") print_board(part2_to_part1(randomboard)) randomuppers = randomboard.thrown_uppers randomlowers = randomboard.thrown_lowers randomturn = randomboard.turn print(randomuppers) print(randomlowers) """ """ u = {'s': [(-4, -2), (2, 4)], 'p': [], 'r': []} l = {'s': [], 'p': [], 'r': [(0, 3)]} testboard = Board(u, l, 0, 0, 37, None) print_board(part2_to_part1(testboard)) print(testboard.generate_turns()[0]) """ thrown_uppers = {'s': [], 'p': [(4,-1), (3,-3), (0,-3)], 'r': [(4,-3)]} thrown_lowers = {'s': [(0,-1), (-3,4), (-4,3)], 'p': [(-2,2)], 'r': [(-4,0), (-1,4)]} testboard = Board(thrown_uppers, thrown_lowers, 1, 1, 0, None) print_board(part2_to_part1(testboard)) mctsnode = MCTSNode(testboard, "UPPER") print(len(mctsnode.simultaneous_moves))
""" Quick game I designed to allow you to play tic tac toe against your computer. The project goal was to use a minimax function and alpha-beta pruning to teach the computer how to play both optimally and quickly.""" import util as util import time X = "X" O = "O" EMPTY = " " # Welcome human print("Welcome to Tic Tac Toe: Human Vs Computer.") print("Let's see if you can beat the machine.") time.sleep(1) # Initialize board and give directions board = util.initial_state() util.print_board(board) print("Directions:") print( "Use the grid to give the X (on top) and Y (on side) coordinate of the position you would like to play in." ) print( "For example the space in the upper left-hand corner would be X: 0, Y: 0") time.sleep(3) # Make sure human puts in a valid choice while True: human = input("Will you be player X or O?: ").upper() if human == "X" or human == "O": break print("Please type X or O.") # Asign which player is who based off above