def find_winning_move_helper(board, player): valid_moves = utils.get_valid_moves(board) _board = copy.deepcopy(board) for move in valid_moves: if utils.get_winner(make_move(_board, move, player), player): return move return None
def group_outcomes_and_roles(self): buyer_wins = [] seller_wins = [] ties = 0 total_chats = 0 for ex in self.dataset: roles = { 0: ex["scenario"]["kbs"][0]["personal"]["Role"], 1: ex["scenario"]["kbs"][1]["personal"]["Role"] } winner = utils.get_winner(ex) if winner is None: continue total_chats += 1 if winner == -1: buyer_wins.append(ex) seller_wins.append(ex) ties += 1 elif roles[winner] == utils.BUYER: buyer_wins.append(ex) elif roles[winner] == utils.SELLER: seller_wins.append(ex) print "# of ties: {:d}".format(ties) print "Total chats with outcomes: {:d}".format(total_chats) return buyer_wins, seller_wins
def plot_margin_histograms(self): for (lbl, group) in zip(['buyer_wins', 'seller_wins'], [self.buyer_wins, self.seller_wins]): margins = [] for ex in group: winner = utils.get_winner(ex) if winner is None: continue margin = utils.get_margin(ex) if 0 <= margin <= MAX_MARGIN: margins.append(margin) b = np.linspace(0, MAX_MARGIN, num=int(MAX_MARGIN / 0.2) + 2) print b hist, bins = np.histogram(margins, bins=b) width = np.diff(bins) center = (bins[:-1] + bins[1:]) / 2 fig, ax = plt.subplots(figsize=(8, 3)) ax.bar(center, hist, align='center', width=width) ax.set_xticks(bins) save_path = os.path.join( self.stats_path, '{:s}_wins_margins_histogram.png'.format(lbl)) plt.savefig(save_path)
def finds_winning_and_losing_moves_ai(board, player): # Checking if we can win supposed_winning_move = finds_winning_moves_ai(board, player) temp_board = utils.make_move(board, supposed_winning_move, player) if utils.get_winner(temp_board) is player: return supposed_winning_move # Checking if we can loose opposite_player = utils.get_opposite_player(player) supposed_opposite_winning_move = finds_winning_moves_ai( board, opposite_player) temp_board = utils.make_move(board, supposed_opposite_winning_move, opposite_player) if utils.get_winner(temp_board) is opposite_player: return supposed_opposite_winning_move # block this move so opponent cannot play this. return random_ai(board, player)
def minimax(game_state, turn, maximizer_turn, round_num=0, top=False): node_key = hash_game_state(game_state) if node_key in MINIMAX_TREE: return MINIMAX_TREE[node_key] possible_moves = get_possible_moves(game_state) if not possible_moves or get_winner(game_state)[0] is not None: return _score(game_state, maximizer_turn), None, round_num if turn == maximizer_turn: best = -2 # Lower than lowest possible score. best_move = None best_move_depth = 10 # Larger than deepest possible tree. for i, move in enumerate(possible_moves): move_score, _, move_depth = minimax( _simulate_move(game_state, move, turn), toggle_turn(turn), maximizer_turn, round_num + 1 ) # break ties by preferring faster wins is_faster = move_depth < best_move_depth if move_score > best or (move_score == best and is_faster): best_move = move best = move_score best_move_depth = move_depth # Print a progress bar at the top level if top: progress = 100. * (i + 1.) / len(possible_moves) sys.stdout.write( 'Computer is thinking... Progress: %.02f%%\r' % progress) sys.stdout.flush() ret = (best, best_move, best_move_depth) MINIMAX_TREE[node_key] = ret return ret else: best = 2 # Higher than highest possible score best_move = None best_move_depth = 10 # Larger than deepest possible tree. for move in possible_moves: move_score, _, move_depth = minimax( _simulate_move(game_state, move, turn), toggle_turn(turn), maximizer_turn, round_num + 1 ) # break ties by preferring faster wins is_faster = move_depth < best_move_depth if move_score < best or (move_score == best and is_faster): best_move = move best = move_score best_move_depth = move_depth ret = (best, best_move, best_move_depth) MINIMAX_TREE[node_key] = ret return ret
def plot_speech_acts_old(self): labels = ['buyer_wins', 'seller_wins'] for (group, lbl) in zip([self.buyer_wins, self.seller_wins], labels): plt.figure(figsize=(10, 6)) speech_act_counts = dict( (act, defaultdict(list)) for act in SpeechActs.ACTS) for chat in group: winner = utils.get_winner(chat) margin = utils.get_margin(chat) if margin > MAX_MARGIN or margin < 0.: continue if winner is None: continue margin = round_partial( margin ) # round the margin to the nearest 0.1 to reduce noise if winner == -1 or winner == 0: speech_acts = self.get_speech_acts(chat, agent=0) # print "Chat {:s}\tWinner: {:d}".format(chat['uuid'], winner) # print speech_acts for act in SpeechActs.ACTS: frac = float(speech_acts.count(act)) / float( len(speech_acts)) speech_act_counts[act][margin].append(frac) if winner == -1 or winner == 1: speech_acts = self.get_speech_acts(chat, agent=1) # print "Chat {:s}\tWinner: {:d}".format(chat['uuid'], winner) # print speech_acts for act in SpeechActs.ACTS: frac = float(speech_acts.count(act)) / float( len(speech_acts)) speech_act_counts[act][margin].append(frac) for act in SpeechActs.ACTS: counts = speech_act_counts[act] margins = [] fracs = [] errors = [] bin_totals = 0. for m in sorted(counts.keys()): if len(counts[m]) > THRESHOLD: bin_totals += len(counts[m]) margins.append(m) fracs.append(np.mean(counts[m])) errors.append(stats.sem(counts[m])) print bin_totals / float(len(margins)) plt.errorbar(margins, fracs, yerr=errors, label=act, fmt='--o') plt.xlabel('Margin of victory') plt.ylabel('Fraction of speech act occurences') plt.title('Speech act frequency vs. margin of victory') plt.legend() save_path = os.path.join(self.stats_path, '{:s}_speech_acts.png'.format(lbl)) plt.savefig(save_path)
def rollout(board, turn): curr_board = utils.copy_board(board) while True: next_states = utils.get_next_board_states(curr_board, turn) if len(next_states) == 0: break curr_board = random.choice(next_states) turn = utils.next_turn(turn) return utils.get_winner(curr_board)
def play_game(player_x_func, player_o_func): game_state = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] turn = 'X' redraw(game_state) winner, _ = get_winner(game_state) while not winner: print_status(turn) if turn == 'X': move = player_x_func(game_state, turn) else: move = player_o_func(game_state, turn) apply_move(game_state, move, turn) redraw(game_state) turn = toggle_turn(turn) winner, _ = get_winner(game_state) if winner in ['X', 'O']: print winner, 'is victorious!' else: print "It's a tie!" print '' return winner
def finds_winning_moves_ai(board, player): winning_move = None for i in range(0, 3): for j in range(0, 3): move = (i, j) if (utils.is_a_valid_move(board, move)): temp_board = utils.make_move(board, move, player) if utils.get_winner(temp_board) is player: # This is a winning move. winning_move = move break return winning_move if winning_move is not None else random_ai( board, player)
def plot_price_trends(self, top_n=10): labels = ['buyer_wins', 'seller_wins'] for (group, lbl) in zip([self.buyer_wins, self.seller_wins], labels): plt.figure(figsize=(10, 6)) trends = [] for chat in group: winner = utils.get_winner(chat) margin = utils.get_margin(chat) if margin > 1.0 or margin < 0.: continue if winner is None: continue # print "Winner: Agent {:d}\tWin margin: {:.2f}".format(winner, margin) if winner == -1 or winner == 0: trend = self.get_price_trend(self.price_tracker, chat, agent=0) if len(trend) > 1: trends.append((margin, chat, trend)) if winner == -1 or winner == 1: trend = self.get_price_trend(self.price_tracker, chat, agent=1) if len(trend) > 1: trends.append((margin, chat, trend)) # print "" sorted_trends = sorted(trends, key=lambda x: x[0], reverse=True) for (idx, (margin, chat, trend)) in enumerate(sorted_trends[:top_n]): print '{:s}: Chat {:s}\tMargin: {:.2f}'.format( lbl, chat['uuid'], margin) print 'Trend: ', trend print chat['scenario']['kbs'] print "" plt.plot(trend, label='Margin={:.2f}'.format(margin)) plt.legend() plt.xlabel('N-th price mentioned in chat') plt.ylabel('Value of mentioned price') out_path = os.path.join(self.stats_path, '{:s}_trend.png'.format(lbl)) plt.savefig(out_path)
def play_game(playerX, playerO): board = utils.new_board() moves = 0 while True: utils.render(board) player = 'X' if moves % 2 == 0 else 'O' move = bots.get_ai(playerX if player == 'X' else playerO)(board, player) board = utils.make_move(board, move, player) #time.sleep(0.2) os.system('clear') if utils.get_winner(board, player): print('Player', player, 'wins!') utils.render(board) return 1 if player == 'X' else 2 elif utils.check_for_tie(board): print('It\'s a tie!') utils.render(board) return 0 moves += 1
def plot_speech_acts_by_role(self): labels = utils.ROLES for lbl in labels: plt.figure(figsize=(10, 6)) speech_act_counts = dict( (act, defaultdict(list)) for act in SpeechActs.ACTS) for chat in self.dataset: if utils.get_winner(chat) is None: # skip chats with no outcomes continue speech_acts = self.get_speech_acts(chat, role=lbl) agent = 1 if chat['scenario']['kbs'][1]['personal'][ 'Role'] == lbl else 0 margin = utils.get_margin(chat, agent=agent) if margin > MAX_MARGIN: continue margin = round_partial(margin) for act in SpeechActs.ACTS: frac = float(speech_acts.count(act)) / float( len(speech_acts)) speech_act_counts[act][margin].append(frac) for act in SpeechActs.ACTS: counts = speech_act_counts[act] margins = [] fracs = [] errors = [] for m in sorted(counts.keys()): if len(counts[m]) > THRESHOLD: margins.append(m) fracs.append(np.mean(counts[m])) errors.append(stats.sem(counts[m])) plt.errorbar(margins, fracs, yerr=errors, label=act, fmt='--o') plt.xlabel('Margin of victory') plt.ylabel('Fraction of speech act occurences') plt.title('Speech act frequency vs. margin of victory') plt.legend() save_path = os.path.join(self.stats_path, '{:s}_speech_acts.png'.format(lbl)) plt.savefig(save_path)
def plot_length_histograms(self): lengths = [] for ex in self.dataset: winner = utils.get_winner(ex) if winner is None: continue turns = utils.get_turns_per_agent(ex) total_turns = turns[0] + turns[1] lengths.append(total_turns) hist, bins = np.histogram(lengths) width = np.diff(bins) center = (bins[:-1] + bins[1:]) / 2 fig, ax = plt.subplots(figsize=(8, 3)) ax.bar(center, hist, align='center', width=width) ax.set_xticks(bins) save_path = os.path.join(self.stats_path, 'turns_histogram.png') plt.savefig(save_path)
def Game(player_types): print_game_info() # Starting Game print("Initializing Board") board = new_board() players = ['O', 'X'] chance = 0 while True: player = players[chance % 2] player_type = player_types[chance % 2] render(board) move_coords = get_player_move(player_type, player, board) board = make_move(board, move_coords, player) winner = get_winner(board) if winner is not None: render(board) print("\nWohoo {} has Won!!!\nRun Again\n".format(winner)) break if is_board_full(board): render(board) print("\nOhho! This is a draw. Begin Again\n") break chance += 1
'Play a move. (type e.g. "0, 0" for a move in the left upper corner)\n' ) try: move = [int(m) for m in move.split(',')] except ValueError: print('\nMove coordinates in improper format. Try again.\n') continue if board[move[0], move[1]] != 0: print('\nThis field is taken.\n') continue board[move[0], move[1]] = human_player print(f'\nState of the board:\n{board}\n') if get_winner(board) == human_player: print('You won!') break if check_if_board_is_full(board, size): print('Draw!') break break while True: computer_move = negamax_alpha_beta_pruned(board, computer_player, -np.inf, np.inf)['move'] row = computer_move[0] col = computer_move[1] board[row, col] = computer_player
def _score(game_state, our_turn): winner, _ = get_winner(game_state) assert winner if winner == our_turn: return 1 if winner == toggle_turn(our_turn): return -1 if winner == 'Tie': return -0.5