def heuristic(self, game): F = Flow(game.board) """ Gives a cost to the board state by measuring how far between growth points this also makes the cost max value when one path can't be moved :type game: Flow :return: double """ cost = len(game.paths) * 2 for color in game.paths: path = game.paths[color] if path.is_complete(): cost -= 2 continue gp1, gp2 = path.get_grow_points() # print abs((gp1[0] - gp2[0]) + (gp1[1] - gp2[1])), gp1, gp2 cost += abs((gp1[0] - gp2[0]) + (gp1[1] - gp2[1])) adjecent_points = utils.get_adjacent_points(gp1) if not F.is_valid(adjecent_points[0][0], adjecent_points[0][1]): if not F.is_valid(adjecent_points[1][0], adjecent_points[1][1]): if not F.is_valid(adjecent_points[2][0], adjecent_points[2][1]): if not F.is_valid(adjecent_points[3][0], adjecent_points[3][1]): cost = sys.maxint return cost # print game.board[1][1] # for i, value in enumerate(game.board): # for j in enumerate(game.board[i]): # if j[1] == '0': # cost += 1 return cost
def heuristic(self, game): F = Flow(game.board) """ Gives a cost to the board state by measuring how far between growth points this also makes the cost max value when one path can't be moved :type game: Flow :return: double """ cost = len(game.paths) * 2 for color in game.paths: path = game.paths[color] if path.is_complete(): cost -= 2 continue gp1, gp2 = path.get_grow_points() # print abs((gp1[0] - gp2[0]) + (gp1[1] - gp2[1])), gp1, gp2 cost += abs((gp1[0] - gp2[0]) + (gp1[1] - gp2[1])) adjecent_points = utils.get_adjacent_points(gp1) if not F.is_valid(adjecent_points[0][0], adjecent_points[0][1]): if not F.is_valid(adjecent_points[1][0], adjecent_points[1][1]): if not F.is_valid(adjecent_points[2][0], adjecent_points[2][1]): if not F.is_valid(adjecent_points[3][0], adjecent_points[3][1]): cost = sys.maxint return cost # print game.board[1][1] # for i, value in enumerate(game.board): # for j in enumerate(game.board[i]): # if j[1] == '0': # cost += 1 return cost
def generate_possible_moves_rr(self, game): """ Takes in a game and updates the queue. Games are added as if each grow point got a turn to pick a space. Colors will alternate. :type game: Flow """ gp1_games = [] gp2_games = [] for color in game.paths: # TODO clean up expression path = game.paths[color] # if path.is_complete(): # continue # Get grow points and points adjacent to them gp1, gp2 = path.get_grow_points() adj2gp1 = utils.get_adjacent_points(gp1) adj2gp2 = utils.get_adjacent_points(gp2) # Handle adding adjacent points to grow points separately - in order to maintain RR order. for possible in adj2gp1: if path.can_be_added_to_path(possible, 1): copy_game = deepcopy(game) """:type: Flow""" copy_game.paths[color].add_to_path(possible, 1) gp1_games.append(copy_game) for possible in adj2gp2: if path.can_be_added_to_path(possible, 1): copy_game = deepcopy(game) """:type: Flow""" copy_game.paths[color].add_to_path(possible, 2) gp2_games.append(copy_game) # Add games to queue self.queue += gp1_games + gp2_games
def generate_possible_moves_rr(self, game): """ Takes in a game and updates the queue. Games are added as if each grow point got a turn to pick a space. Colors will alternate. :type game: Flow """ gp1_games = [] gp2_games = [] for color in game.paths: # TODO clean up expression path = game.paths[color] # if path.is_complete(): # continue # Get grow points and points adjacent to them gp1, gp2 = path.get_grow_points() adj2gp1 = utils.get_adjacent_points(gp1) adj2gp2 = utils.get_adjacent_points(gp2) # Handle adding adjacent points to grow points separately - in order to maintain RR order. for possible in adj2gp1: if path.can_be_added_to_path(possible, 1): copy_game = deepcopy(game) """:type: Flow""" copy_game.paths[color].add_to_path(possible, 1) gp1_games.append(copy_game) for possible in adj2gp2: if path.can_be_added_to_path(possible, 1): copy_game = deepcopy(game) """:type: Flow""" copy_game.paths[color].add_to_path(possible, 2) gp2_games.append(copy_game) # Add games to queue self.queue += gp1_games + gp2_games
def generate_new_paths(self, path): """ :type path: Path :return: None """ if path.is_complete(): self.paths_queue.append(path) # print "Adding path, num paths", len(self.paths_queue), path.flow_game gp1, gp2 = path.get_grow_points() adj_points = utils.get_adjacent_points(gp1) for point in adj_points: if path.can_be_added_to_path(point): copy_path = copy.deepcopy(path) """:type:Path""" copy_path.add_to_path(point) self.generate_new_paths(copy_path)
def generate_new_paths(self, path): """ :type path: Path :return: None """ if path.is_complete(): self.paths_queue.append(path) # print "Adding path, num paths", len(self.paths_queue), path.flow_game gp1, gp2 = path.get_grow_points() adj_points = utils.get_adjacent_points(gp1) for point in adj_points: if path.can_be_added_to_path(point): copy_path = copy.deepcopy(path) """:type:Path""" copy_path.add_to_path(point) self.generate_new_paths(copy_path)
def finish_path(self, path): """ :type path: Path :type add_to_queue: bool :return: """ if path.is_complete(): return True, path gp1, gp2 = path.get_grow_points() adj_points = utils.get_adjacent_points(gp1) for point in adj_points: if path.can_be_added_to_path(point): copy_path = copy.deepcopy(path) """:type:Path""" copy_path.add_to_path(point) rv = self.finish_path(copy_path) if rv[0]: return rv return False, None
def generate_possible_moves_single_gp(self, game): """ Takes in a game and upates the queue. New game states are added is if only 1 growth point for each color is allowed to make a move. :type game: Flow """ for color in game.paths: path = game.paths[color] if path.is_complete(): continue gp1, gp2 = path.get_grow_points() adj2gp1 = utils.get_adjacent_points(gp1) for possible in adj2gp1: # print "Attempting" # print game # print possible, path.can_be_added_to_path(possible, 1) if path.can_be_added_to_path(possible, 1): copy_game = deepcopy(game) """:type: Flow""" copy_game.paths[color].add_to_path(possible, 1) self.queue.append(copy_game)
def generate_possible_moves_single_gp(self, game): """ Takes in a game and upates the queue. New game states are added is if only 1 growth point for each color is allowed to make a move. :type game: Flow """ for color in game.paths: path = game.paths[color] if path.is_complete(): continue gp1, gp2 = path.get_grow_points() adj2gp1 = utils.get_adjacent_points(gp1) for possible in adj2gp1: # print "Attempting" # print game # print possible, path.can_be_added_to_path(possible, 1) if path.can_be_added_to_path(possible, 1): copy_game = deepcopy(game) """:type: Flow""" copy_game.paths[color].add_to_path(possible, 1) self.queue.append(copy_game)
def finish_path(self, path): """ :type path: Path :type add_to_queue: bool :return: """ if path.is_complete(): return True, path gp1, gp2 = path.get_grow_points() adj_points = utils.get_adjacent_points(gp1) for point in adj_points: if path.can_be_added_to_path(point): copy_path = copy.deepcopy(path) """:type:Path""" copy_path.add_to_path(point) rv = self.finish_path(copy_path) if rv[0]: return rv return False, None