def simulate(origin, destination, time, algorithm, sampling_rate): route_result = {'distance': 0, 'success': False, 'current_location': origin, 'time': time, 'points': []} while not route_result['success']: time1 = time_fn() new_result = route_vehicle(route_result['current_location'], destination, route_result['time'], algorithm, sampling_rate) time2 = time_fn() running_time = time2 - time1 if new_result is None: route_result['distance'] = 'NA' route_result['success'] = 'NA' route_result['current_location'] = None route_result['time'] = None route_result['points'] = None route_result['walking_distance'] = 'NA' elif new_result['success']: route_result['distance'] += new_result['distance'] route_result['walking_distance'] = new_result['walking_distance'] route_result['success'] = new_result['success'] route_result['current_location'] = new_result['current_location'] route_result['time'] = new_result['time'] route_result['points'].extend(new_result['points']) route_result['points'].append(new_result['current_location']) route_result['running_time'] = running_time return route_result
def heuristic(self, board: list, is_self_player): # if the board presents ending state for self_player # Utility function: # (is_final, self_player_succ, rival_player_succ) = self.is_board_final(board, is_sel_player) is_final = self.is_board_final(board, is_self_player) rival_player_succ, rival_moves = self.get_board_successors( board, not is_self_player) if is_final and is_self_player: # is_self_player == True -> meaning, final board is due to self_player have no moves # in this case only tie or a lose is posiible. if len(rival_player_succ) == 0: return 0 else: return -1000 elif is_final and not is_self_player: # is_self_player == False -> meaning, final board is due to rival_player have no moves # in this case only tie or a win is posiible. # a win is possible if self_player successors have moves ( i.e they have legal successors. second_level_succ_num = 0 self_player_succ = rival_player_succ if len(self_player_succ) > 0: second_level_succ_num = sum([ len( self.get_board_successors(succ_board, not is_self_player)[0]) for succ_board in self_player_succ ]) if second_level_succ_num > 0: return 1000 return 0 # Heuristic Function: else: # weight = 2 heuristic_start_time = time_fn() loc_in_board = self.get_loc_in_board(board, is_self_player) simple_score = self.simple_state_score(board, loc_in_board) enemy_loc_in_board = self.get_loc_in_board(board, not is_self_player) simple_rival_score_improved = 0 manhatan_dist = abs(loc_in_board[0] - enemy_loc_in_board[0]) + abs( loc_in_board[1] - enemy_loc_in_board[1]) assert manhatan_dist != 0, "players in the same cell" manhatan_score = 1 / manhatan_dist dfs_score = 0 if manhatan_score < self.dfs_depth: simple_rival_score = self.simple_state_score( board, enemy_loc_in_board) simple_rival_score_improved = 10 if simple_rival_score == -1 else simple_rival_score dfs_cells = self.dfs(board, loc_in_board, self.dfs_depth) dfs_rival_cells = self.dfs(board, enemy_loc_in_board, self.dfs_depth) assert len(dfs_cells) != 0, "logic error" dfs_score = len(dfs_cells) - len(dfs_rival_cells) heuristics = simple_score + simple_rival_score_improved + dfs_score + manhatan_score curr_heuristic_time = time_fn() - heuristic_start_time self.heuristic_time = max(curr_heuristic_time, self.heuristic_time) return heuristics if is_self_player else -heuristics
def heuristic(self, board: list, is_self_player): # if the board presents ending state for self_player # Utility function: # (is_final, self_player_succ, rival_player_succ) = self.is_board_final(board, is_sel_player) is_final = self.is_board_final(board, is_self_player) rival_player_succ, rival_moves = self.get_board_successors( board, not is_self_player) if is_final and is_self_player: # is_self_player == True -> meaning, final board is due to self_player have no moves # in this case only tie or a lose is posiible. if len(rival_player_succ) == 0: return 0 else: return -1000 elif is_final and not is_self_player: # is_self_player == False -> meaning, final board is due to rival_player have no moves # in this case only tie or a win is posiible. # a win is possible if self_player successors have moves ( i.e they have legal successors. second_level_succ_num = 0 self_player_succ = rival_player_succ if len(self_player_succ) > 0: second_level_succ_num = sum([ len( self.get_board_successors(succ_board, not is_self_player)[0]) for succ_board in self_player_succ ]) if second_level_succ_num > 0: return 1000 return 0 # Heuristic Function: else: # weight = 2 heuristic_start_time = time_fn() loc_in_board = self.get_loc_in_board(board, is_self_player) simple_score = self.simple_state_score(board, loc_in_board) enemy_loc_in_board = self.get_loc_in_board(board, not is_self_player) simple_rival_score = self.simple_state_score( board, enemy_loc_in_board) simple_rival_score_improved = 10 if simple_rival_score == -1 else simple_rival_score heuristics = simple_score + simple_rival_score_improved curr_heuristic_time = time_fn() - heuristic_start_time self.heuristic_time = max(curr_heuristic_time, self.heuristic_time) return heuristics if is_self_player else -heuristics
def make_move(self, time=float( 'inf')): # time parameter is not used, we assume we have enough time. # time_predictions = [] ID_start_time = time_fn() self.last_iter_moves_values = {} self.iter_num = 1 assert self.count_ones(self.board) == 1 prev_loc = self.loc is_self_player = True depth = 1 best_move, minimax_value, best_new_loc = None, float('-inf'), None best_move, minimax_value = self.alpha_beta(self.board.copy(), is_self_player, depth) last_iteration_time = time_fn() - ID_start_time next_iter_time_limit = self.get_iter_time_prediction( last_iteration_time) time_until_now = time_fn() - ID_start_time while time_until_now + next_iter_time_limit < time and depth < self.board.shape[ 0] * self.board.shape[1]: iteration_start_time = time_fn() depth += 1 self.iter_num += 1 self.prev_iter_leaves_developed = self.curr_iter_leaves_developed self.curr_iter_leaves_developed = 0 best_move, minimax_value = self.alpha_beta(self.board.copy(), is_self_player, depth) if minimax_value == 1000 or minimax_value == -1000: break last_iteration_time = time_fn() - iteration_start_time next_iter_time_limit = self.get_iter_time_prediction( last_iteration_time) time_until_now = time_fn() - ID_start_time if best_move is None: exit() best_new_loc = (prev_loc[0] + best_move[0], prev_loc[1] + best_move[1]) self.board[best_new_loc] = 1 self.board[prev_loc] = -1 assert self.count_ones(self.board) == 1 self.loc = best_new_loc print('returning move', best_move) time_until_now = time_fn() - ID_start_time return best_move
from wtforms import StringField, TextAreaField, HiddenField from wtforms.validators import DataRequired, Length from typing import List, Union # ~~ load the configuration config_path = os.path.abspath(os.getenv('PASTA_CONFIG_PATH', './config.toml')) with open(config_path, 'r') as fp: config = EasyDict(toml.load(fp)) logging_path = os.getenv('PASTA_LOGGING_CONFIG', config.logging_path) fileConfig(logging_path) logger = getLogger('pasta') app_log = getLogger('pasta.app').info DATA_DIR = os.getenv('PASTA_DATA_PATH', config.data_folder) ENCODING = config.encoding STARTED = datetime.utcnow(), time_fn() logger.info('started_at: %s, %0.1f', *STARTED) logger.info('config: %s', config_path) logger.info('logging: %s', logging_path) logger.info('data: %s', DATA_DIR) logger.info('encoding: %s', ENCODING) # ~~~~~~ ENCRYPTION key_path = os.path.join(DATA_DIR, '.keys') # delete stale keys if forced if config.get('force_keygen', False): if os.path.exists(key_path): os.unlink(key_path)