def get_unexpanded_move(self): if USING_OPPONENT and self.act_id != self.tree[0].act_id: dice = random.random() if dice <= 0.7: moves = [m for m, _ in self.moves.items()] return NaivePlayer(self.act_id).SelectMove(moves, self.state) unexp_dict = {} for m, (c, p) in self.moves.items(): if c is None: unexp_dict[m] = p # unexp_prob = sum(unexp_dict.values()) # assert len(unexp_dict) > 0 # # print(unexp_dict.values()) # for m, p in unexp_dict.items(): # unexp_dict[m] = p / unexp_prob # # print(sum(unexp_dict.values())) # unexp_m_list = [(k, v) for k, v in unexp_dict.items()] # p = np.array([v for k, v in unexp_m_list]) # # print(p) # index = np.random.choice([i for i in range(len(p))], p=p.ravel()) # m, _ = unexp_m_list[index] m = max(unexp_dict, key=unexp_dict.get) return m
def track(self, game_state, root_node, player_order, move): dict = {} for m, (c, p) in root_node.moves.items(): Q = get_max_difference(c.value, self.id) if c is not None else -1000 r = self.agent.get_place_reward(game_state, m, self.id, player_order)[0] dict[m] = Q, r print( '{:2}{}: {:5} {:5}, {:5} r:{:5}'.format(m[1], str(m[2]), round(c.value[0], 2), round(c.value[1], 2), round(Q, 2), round(r,2))) print() print(BoardToString(game_state)) print(PlayerToString(self.id, game_state.players[self.id])) max_tuple = max(dict.items(), key=lambda x:x[1][1]) max_move, max_r = max_tuple[0], max_tuple[1][1] naive_player_id = self.id + 1 if self.id + 1 < len(player_order) else 0 print(BoardToString(game_state)) print(PlayerToString(naive_player_id, game_state.players[naive_player_id])) print('Naive may choose:') moves = game_state.players[naive_player_id].GetAvailableMoves(game_state) naive_move = NaivePlayer(naive_player_id).SelectMove(moves, game_state) print() print('{:2}{}'.format(naive_move[1], str(naive_move[2]))) if naive_move[1] == move[1] and move[1] != -1 and dict[move][1] != max_r and max_move[1] != move[1]: increase_actions('interfer')
def SelectMove(self, moves, game_state): player_order = [] for i in range(self.id + 1, len(game_state.players)): player_order.append(i) for i in range(0, self.id + 1): player_order.append(i) i_moves = game_state.players[self.id].GetAvailableMoves(game_state) move = NaivePlayer(self.id).SelectMove(i_moves, game_state) #print(move) return move
# Written by Michelle Blom, 2019 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. # from model import GameRunner, Player from iplayer import InteractivePlayer from naive_player import NaivePlayer players = [InteractivePlayer(0), NaivePlayer(1), NaivePlayer(2)] gr = GameRunner(players, 54298758156784) scores = gr.Run(True) print("Player 0 score is {}".format(scores[0])) print("Player 1 score is {}".format(scores[1])) print("Player 2 score is {}".format(scores[2]))
# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. # from model import GameRunner, Player from iplayer import InteractivePlayer from naive_player import NaivePlayer from simple_player import SimplePlayer from utils import * players = [NaivePlayer(0), SimplePlayer(1), NaivePlayer(2), NaivePlayer(3)] win = 0 for i in range(1384754846764, 1384754846864): gr = GameRunner(players, i) activity = gr.Run(False) if activity[1][0] == max([activity[0][0]], [activity[1][0]], [activity[2][0]], [activity[3][0]]): win += 1 print(win) '''print("Player 0 score is {}".format(activity[0][0])) print("Player 1 score is {}".format(activity[1][0])) print("Player 2 score is {}".format(activity[2][0])) print("Player 3 score is {}".format(activity[3][0])) ''' #print("Player 0 round-by-round activity")
from manager import Manager from naive_player import NaivePlayer from human_player import HumanPlayer from random_player import RandomPlayer from price_player import PricePlayer import utils # set up game manager gm = Manager() # the following is taken from test_initial_tests/initial_state_1.xlsx initialStateNum = "1" initialStatePath = "test_initial_states/initial_state_" + initialStateNum + ".xlsx" # construct player agents playerAName = "Atlantis" playerA = NaivePlayer(playerAName) playerAState = utils.getInitialState(initialStatePath, playerAName) playerAState["cash"] = 10000 playerBName = "Brobdingnag" playerB = RandomPlayer(playerBName) playerBState = utils.getInitialState(initialStatePath, playerBName) playerBState["cash"] = 15000 playerCName = "Carpania" playerC = HumanPlayer(playerCName) playerCState = utils.getInitialState(initialStatePath, playerCName) playerCState["cash"] = 20000 playerDName = "Dinotopia" playerD = PricePlayer(playerDName) playerDState = utils.getInitialState(initialStatePath, playerDName)
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. # from model import GameRunner, Player from iplayer import InteractivePlayer from naive_player import NaivePlayer from utils import * players = [ InteractivePlayer(0), NaivePlayer(1), NaivePlayer(2), NaivePlayer(3) ] gr = GameRunner(players, 1384754856864) activity = gr.Run(True) print("Player 0 score is {}".format(activity[0][0])) print("Player 1 score is {}".format(activity[1][0])) print("Player 2 score is {}".format(activity[2][0])) print("Player 3 score is {}".format(activity[3][0])) #print("Player 0 round-by-round activity") #player_trace = activity[0][1]
win = [0] * 4 mark_sum = [0] * 4 result = [] #players_name=['MI_Player', 'MI_PlayerSimuSave'] players_name = ['MI_PlayerNew', 'BFS_Player'] #players_name=['BFS_Player', 'NaivePlayer'] #players_name=['BFS_Player', 'MI_Player'] #players_name=['MI_PlayerNew', 'NaivePlayer'] #players_name=['RandomPlayer', 'NaivePlayer'] #players_name=['MI_PlayerNew', 'MI_Player'] #players_name=['BFS_Player', 'MI_PlayerNew'] #players_name=['MI_Player', 'InteractivePlayer'] players = [eval(players_name[0])(0), eval(players_name[1])(1)] players_reverse = [eval(players_name[1])(0), eval(players_name[0])(1)] players.extend([NaivePlayer(i) for i in range(len(players), PLAYERS_NUM)]) players_reverse.extend( [NaivePlayer(i) for i in range(len(players), PLAYERS_NUM)]) SEED = [] for i in range(ROUND // 2 + 1): SEED.append(random.randrange(99999)) print('seeds:', SEED) start = time.time() for i in range(ROUND): print('NEW GAME') print('seed:', SEED[i // 2]) ps = players if i % 2 == 0 else players_reverse gr = GameRunner(ps, SEED[i // 2]) activity = gr.Run(True)