def pre_process1(board_values_2d): board = Board(size=4, board_values=board_values_2d) possible_moves = board.get_possible_moves() num_empty_tiles, max_tile_value, tile_sum = board.get_tile_stats() x_vector = [] # flat board state for row in board_values_2d: x_vector += row # normalized "linear" tile values x_vector = map(lambda y: float(y) / max_tile_value, x_vector) # possible moves for i in range(4): x_vector.append(1 if i in possible_moves else 0) return x_vector
def run(self): board = Board(size=4) board.place_new_value_randomly() states = [ [0], # move up after having moved left [0], # move up after having moved right [1], # move right [3], # move left [1, 3], # move right after having moved down [2] # move down ] state = 0 num_consecutive_ignores = 0 for x in xrange(1000): # self.gfx.draw(board.board_values) moved = False for direction in states[state]: if board.can_move(direction): board_values_copy = deepcopy(board.board_values) self.board_states.append(board_values_copy) self.moves.append(direction) board.move(direction) moved = True break if moved: state = self.get_next_state(state) board.place_new_value_randomly() num_consecutive_ignores = 0 else: # print 'ignored move' num_consecutive_ignores += 1 state = self.get_next_state(state, force_down=num_consecutive_ignores >= 3) if len(board.get_possible_moves()) == 0: # print # print 'game over' break # print x + 1, 'moves' # print len(self.board_states), len(self.moves) filename = "run_" + str(uuid4()) + ".pickle" pickle.dump([self.board_states, self.moves], open('runs/' + filename, "wb"))
def play_game_randomly(self, max_num_moves=2000): board = Board(size=4) board.place_new_value_randomly() for x in xrange(max_num_moves): directions = range(4) random.shuffle(directions) moved = False for direction in directions: if board.can_move(direction): board.move(direction) moved = True break if not moved: break board.place_new_value_randomly() num_empty_tiles, max_tile_value, tile_sum = board.get_tile_stats() print(max_tile_value) self.max_tile_value = max_tile_value
def run(self): self.board_states = [] board = Board(size=4) board.place_new_value_randomly() for i in xrange(2000): if self.args.print_results: print('iteration', i) print(board) self.board_states.append(deepcopy(board.board_values)) directions = self.choose_direction(board.board_values) has_moved = False for direction in directions: if board.can_move(direction): board.move(direction) has_moved = True break if not has_moved: break board.place_new_value_randomly() num_empty_tiles, max_tile_value, tile_sum = board.get_tile_stats() print(max_tile_value) self.max_tile_value = max_tile_value
def pre_process2(board_values_2d): board = Board(size=4, board_values=board_values_2d) possible_moves = board.get_possible_moves() num_empty_tiles, max_tile_value, tile_sum = board.get_tile_stats() max_tile_value_log2 = float(math.log(max_tile_value, 2)) x_vector = [] # board state for row in board_values_2d: x_vector += row # normalized log_2 representation x_vector = map(lambda y: 0 if y == 0 else math.log(y, 2) / max_tile_value_log2, x_vector) # possible moves for i in range(4): x_vector.append(1 if i in possible_moves else 0) # number of empty tiles x_vector.append(math.tanh(num_empty_tiles / 8)) # tile sum x_vector.append(math.tanh(tile_sum / 2048)) # indicate mergeable tiles for i in range(board.size): for j in range(board.size): current_tile = board_values_2d[i][j] has_matching_neighbour = \ (i - 1 >= 0 and board_values_2d[i - 1][j] == current_tile) \ or (i + 1 <= 3 and board_values_2d[i + 1][j] == current_tile) \ or (j - 1 >= 0 and board_values_2d[i][j - 1] == current_tile) \ or (j + 1 <= 3 and board_values_2d[i][j + 1] == current_tile) x_vector.append(1 if has_matching_neighbour else 0) # is max tile in corner is_max_tile_in_corner = False for i in range(board.size): for j in range(board.size): current_tile = board_values_2d[i][j] if current_tile == max_tile_value: if i in (0, 3) and j in (0, 3): is_max_tile_in_corner = True x_vector.append(1 if is_max_tile_in_corner else 0) # average tile position (x) avg_position_x = 0 for row in board.board_values: avg_position_x += PrepareData.get_avg_position(row) avg_position_x /= board.size x_vector.append(max(avg_position_x, 0)) # how far east from the center x_vector.append(abs(min(avg_position_x, 0))) # how far west from the center # average tile position (y) avg_position_y = 0 for i in range(board.size): column = board.get_column(i) avg_position_y += PrepareData.get_avg_position(column) avg_position_y /= board.size x_vector.append(max(avg_position_y, 0)) # how far south from the center x_vector.append(abs(min(avg_position_y, 0))) # how far north from the center return x_vector
def pre_process2(board_values_2d): board = Board(size=4, board_values=board_values_2d) possible_moves = board.get_possible_moves() num_empty_tiles, max_tile_value, tile_sum = board.get_tile_stats() max_tile_value_log2 = float(math.log(max_tile_value, 2)) x_vector = [] # board state for row in board_values_2d: x_vector += row # normalized log_2 representation x_vector = map( lambda y: 0 if y == 0 else math.log(y, 2) / max_tile_value_log2, x_vector) # possible moves for i in range(4): x_vector.append(1 if i in possible_moves else 0) # number of empty tiles x_vector.append(math.tanh(num_empty_tiles / 8)) # tile sum x_vector.append(math.tanh(tile_sum / 2048)) # indicate mergeable tiles for i in range(board.size): for j in range(board.size): current_tile = board_values_2d[i][j] has_matching_neighbour = \ (i - 1 >= 0 and board_values_2d[i - 1][j] == current_tile) \ or (i + 1 <= 3 and board_values_2d[i + 1][j] == current_tile) \ or (j - 1 >= 0 and board_values_2d[i][j - 1] == current_tile) \ or (j + 1 <= 3 and board_values_2d[i][j + 1] == current_tile) x_vector.append(1 if has_matching_neighbour else 0) # is max tile in corner is_max_tile_in_corner = False for i in range(board.size): for j in range(board.size): current_tile = board_values_2d[i][j] if current_tile == max_tile_value: if i in (0, 3) and j in (0, 3): is_max_tile_in_corner = True x_vector.append(1 if is_max_tile_in_corner else 0) # average tile position (x) avg_position_x = 0 for row in board.board_values: avg_position_x += PrepareData.get_avg_position(row) avg_position_x /= board.size x_vector.append(max(avg_position_x, 0)) # how far east from the center x_vector.append(abs(min(avg_position_x, 0))) # how far west from the center # average tile position (y) avg_position_y = 0 for i in range(board.size): column = board.get_column(i) avg_position_y += PrepareData.get_avg_position(column) avg_position_y /= board.size x_vector.append(max(avg_position_y, 0)) # how far south from the center x_vector.append(abs(min(avg_position_y, 0))) # how far north from the center return x_vector