def min_value(board, alpha, beta, depth): board.change_player() if depth <= 0: #Check for depth return evaluate(board.board, 0) v = np.inf actions = board.available_moves() for a in actions: #For each legal move, make it succ_board = copy.deepcopy(board) succ_state, result, done, _ = succ_board.step(a) if not result == 0 and done: #If some result was reached, return the utility of this board if result == 1: #If win, invert it, since we are "playing as the opponent" result = -1 return evaluate(succ_state, result) else: #If no result was reached, go one level deeper v = min(v, max_value(succ_board, alpha, beta, depth - 1)) if v <= alpha: return v beta = min(beta, v) return v
def ai_place(self, turn, board, depth, arr): if (depth > 0): for col in range(board.col): #place piece in col deepBoard = copy.deepcopy(board) if (not deepBoard.place(turn, col)): return -1 #check if board is win if (deepBoard.win): print("here") arr = [-100 for x in range(board.col) if x != col] return 100 print(deepBoard, turn, depth, arr) arr[col] += self.ai_place((turn + 1) % 2, deepBoard, depth - turn, arr) return 0
def alpha_beta_decision(): print("Thinking...") if (print_debug): print("Using depth " + str(depth)) start_depth = depth val = -np.inf move = -10 values = [None] * 7 debug = [] actions = env.available_moves() for a in actions: #For each legal move, make it succ_board = copy.deepcopy(env) succ_state, result, _, _ = succ_board.step(a) if result == 1: #If win in one move is possible, play that move print("I have made the move " + str(a + 1)) if (print_debug): print("which is a winning move") return a new_val = min_value(succ_board, -np.inf, np.inf, start_depth) #Beginning of recursion debug.append((a, new_val, succ_state)) #Saving for debugging if new_val >= val: #Find the best move val = new_val move = a values[a] = new_val #Saving to print, for debugging print("I have made the move " + str(move + 1)) if (print_debug): #Debug printout print("which has the value: " + str(val)) print("Values: " + str(values)) return move
def max_value(board, alpha, beta, depth): board.change_player() if depth <= 0: #Check if depth is reached return evaluate(board.board, 0) v = -np.inf actions = board.available_moves() for a in actions: #For each legal move, make it succ_board = copy.deepcopy(board) succ_state, result, done, _ = succ_board.step(a) #TODO: This loop will break even if a loss or a draw is found, but there might be a win further to #the right. FIX! if not result == 0 and done: #If some result was reached, return the utility of this board return evaluate(succ_state, result) else: #If no result was reached, go one level deeper v = max(v, min_value(succ_board, alpha, beta, depth - 1)) #Recursive call if v >= beta: #If the maximum value is larger than beta, return it return v alpha = max(alpha, v) #Update alpha return v
def _deserialize_best_gs_params(serialized_gs_params, gs_deserialized_init_params): """ _deserialize_best_gs_params: Converts the string names of the serialized methods back into executable functions for use during refit operations once the initial grid search has been completed. :param serialized_gs_params: The serialized grid search parameters which contain string representations of methods. :return: """ deserialized_params = {} serialized_initializer = serialized_gs_params['initializer'] if 'VarianceScaling' in serialized_initializer: if 'truncated_normal' in serialized_initializer: deserialized_params['initializer'] = gs_deserialized_init_params[ 'initializer'][0] elif 'uniform' in serialized_initializer: deserialized_params['initializer'] = gs_deserialized_init_params[ 'initializer'][1] else: tf.logging.error( msg= 'CRITICAL ERROR: Failed to deserialize grid search \'initializer\' parameter. Any future refit operations WILL FAIL.' ) elif 'TruncatedNormal' in serialized_initializer: deserialized_params['initializer'] = gs_deserialized_init_params[ 'initializer'][2] else: tf.logging.error( msg= 'CRITICAL ERROR: Failed to deserialize grid search \'initializer\' parameter. Any future refit operations WILL FAIL.' ) serialized_activation = serialized_gs_params['activation'] if 'leaky_relu' in serialized_activation: deserialized_params['activation'] = gs_deserialized_init_params[ 'activation'][0] elif 'function elu' in serialized_activation: deserialized_params['activation'] = gs_deserialized_init_params[ 'activation'][1] else: tf.logging.error( msg= 'CRITICAL ERROR: Failed to deserialize grid search \'activation\' parameter. Any future refit operations WILL FAIL.' ) serialized_optimizer = serialized_gs_params['optimizer'] if 'MomentumOptimizer' in serialized_optimizer: deserialized_params['optimizer'] = gs_deserialized_init_params[ 'optimizer'][0] elif 'AdamOptimizer' in serialized_optimizer: deserialized_params['optimizer'] = gs_deserialized_init_params[ 'optimizer'][1] else: tf.logging.error( msg= 'CRITICAL ERROR: Failed to deserialize grid search \'optimizer\' parameter. Any future refit operations WILL FAIL.' ) serialized_train_batch_size = serialized_gs_params['train_batch_size'] deserialized_train_batch_size = False for train_batch_size in gs_deserialized_init_params['train_batch_size']: if train_batch_size == serialized_train_batch_size: deserialized_params['train_batch_size'] = train_batch_size deserialized_train_batch_size = True break if not deserialized_train_batch_size: tf.logging.error( msg= 'CRITICAL ERROR: Failed to deserialize grid search \'train_batch_size\' parameter. Any future refit operations WILL FAIL.' ) # Copy over any other keys from the serialized updated dictionary to the deserialized one: for inherited_key in serialized_gs_params.keys(): if inherited_key not in deserialized_params: deserialized_params[inherited_key] = deepcopy( serialized_gs_params[inherited_key]) return deserialized_params