def gameover_screen(playerWon): gameover = True while gameover: for event in pygame.event.get(): if event.type == pygame.QUIT: gameover = False pygame.quit() interface.Game("resume") if event.type == pygame.KEYDOWN: if event.key == K_r: gameover = False if event.key == K_q or event.key == K_ESCAPE: gameover = False pygame.quit() interface.Game("resume") if playerWon: screen.blit(font2.render("You won!", True, GREEN), [330, 150]) else: screen.blit(font2.render("You lost!", True, RED), [330, 150]) screen.blit(font1.render("PRESS 'R' TO PLAY AGAIN", True, BLACK), [330, 290]) screen.blit(font1.render("PRESS 'Q' TO QUIT", True, BLACK), [375, 320]) pygame.display.update() screen.blit(washingtonHall_BG, (0, 0)) game_clock.tick(FPS) if gameover == False: play_game(playerID, opponentID)
def _evaluate(self, weight_candidate): agent = ESAgent(self.actions, self.discount, self.featureExtractor, weights=weight_candidate).getAgent() agents = deepcopy(self.strategies) # add current agent to strategies agents.append(agent) game = interface.Game(grid_size, len(agents), candy_ratio=1., max_iter=max_iter) game.start(agents) totalDiscount = 1 totalReward = 0 while not game.isEnd() and agent.isAlive(game): # Compute the actions for each player following its strategy actions = game.agentActions() newState = game.succ(game.current_state, actions) reward = agent.lastReward(game) totalReward += totalDiscount * reward totalDiscount *= self.discount # fitness = totalReward fitness = game.current_state.currentScore(agent_id) # fitness = max(0, totalReward) # we need positive values return fitness
def train(self, strategies, grid_size, num_trials=100, max_iter=1000, verbose=False): print "RL training" totalRewards = [] # The rewards we get on each trial rl_id = len(strategies) for trial in xrange(num_trials): progressBar(trial, num_trials) game = interface.Game(grid_size, len(strategies) + 1, candy_ratio=1., max_iter=max_iter) state = game.startState() totalDiscount = 1 totalReward = 0 points = state.snakes[rl_id].points history = [] while not game.isEnd(state) and rl_id in state.snakes: # Compute the actions for each player following its strategy actions = { i: strategies[i](i, state) for i in state.snakes.keys() if i != rl_id } action, optimal_action = self.getAction(state) actions[rl_id] = action newState = game.succ(state, actions) if rl_id in newState.snakes: reward = newState.snakes[rl_id].points - points if len(newState.snakes) == 1: # it won reward += 10. points = newState.snakes[rl_id].points self.incorporateFeedback(state, action, reward, newState, history) else: # it died reward = -10. self.incorporateFeedback(state, action, reward, newState, history) # add decsion to history, or reset if non-greedy choice if optimal_action: history.append(self.featureExtractor(state, action)) else: history = [] totalReward += totalDiscount * reward totalDiscount *= self.discount state = newState if verbose: print "Trial %d (totalReward = %s)" % (trial, totalReward) totalRewards.append(totalReward) progressBar(num_trials, num_trials) print "Average reward:", sum(totalRewards) / num_trials return totalRewards
def train(self, opponents, grid_size, num_trials=100, max_iter=1000, verbose=False): print("RL training") totalRewards = [] # The rewards we get on each trial # rl_id = len(opponents) rl_agent = self.getAgent() agents = deepcopy(opponents) # add current agent to strategies agents.append(rl_agent) for trial in range(num_trials): # game = interface.Game(grid_size, len(strategies) + 1, candy_ratio = 1., max_iter = max_iter) # state = game.startState() game = interface.Game(grid_size, len(agents), candy_ratio=1., max_iter=max_iter) game.start(agents) totalDiscount = 1 totalReward = 0 rewards = [] while not game.isEnd() and rl_agent.isAlive(game): # Compute the actions for each player following its strategy # actions = {i: strategies[i](i, state) for i in state.snakes.keys() if i != rl_id} # action = self.getAction(state) # actions[rl_id] = action actions = game.agentActions() newState = game.succ(game.current_state, actions) reward = rl_agent.lastReward(game) rewards.append(reward) totalReward += totalDiscount * reward totalDiscount *= self.discount if self.rl_type == "qlearning": self.incorporateFeedback(game.previous_state, actions[rl_agent.getPlayerId()], reward, game.current_state) if self.rl_type == "policy_gradients": self.addRolloutFeedback(rewards, trial) progressBar(trial, num_trials, info="Last reward: {}".format(totalReward)) if verbose: print("Trial %d (totalReward = %s)" % (trial, totalReward)) totalRewards.append(totalReward) progressBar(num_trials, num_trials) print("Average reward:", sum(totalRewards) / num_trials) return totalRewards
def train(self, grid_size, num_trials=100, max_iter=1000, verbose=False): print "RL training" totalRewards = [] # The rewards we get on each trial for trial in xrange(num_trials): progressBar(trial, num_trials) game = interface.Game(grid_size, n_snakes= self.n_opponents + 1, candy_ratio = 1., max_iter = max_iter) state = game.startState() totalDiscount = 1 totalReward = 0 points = state.snakes[self.TDagent_id].points while not game.isEnd(state) and self.TDagent_id in state.snakes: # Compute the actions for each player following its strategy actions = {i: self.getAction(state,i)} newState = game.succ(state, actions) if self.TDagent_id in newState.snakes: if len(newState.snakes) == 1: # it won reward = 2.0 * newState.snakes[self.TDagent_id].points else: reward = newState.snakes[self.TDagent_id].points - points points = newState.snakes[self.TDagent_id].points self.incorporateFeedback(state, action, reward, newState) else: # it died reward = - points self.incorporateFeedback(state, action, reward, newState) totalReward += totalDiscount * reward totalDiscount *= self.discount state = newState if verbose: print "Trial %d (totalReward = %s)" % (trial, totalReward) totalRewards.append(totalReward) progressBar(num_trials, num_trials) print "Average reward:", sum(totalRewards)/num_trials return totalRewards ############################################################ ############################################################
def play_game(playerID, opponentID): def gameover_screen(playerWon): gameover = True while gameover: for event in pygame.event.get(): if event.type == pygame.QUIT: gameover = False pygame.quit() interface.Game("resume") if event.type == pygame.KEYDOWN: if event.key == K_r: gameover = False if event.key == K_q or event.key == K_ESCAPE: gameover = False pygame.quit() interface.Game("resume") if playerWon: screen.blit(font2.render("You won!", True, GREEN), [330, 150]) else: screen.blit(font2.render("You lost!", True, RED), [330, 150]) screen.blit(font1.render("PRESS 'R' TO PLAY AGAIN", True, BLACK), [330, 290]) screen.blit(font1.render("PRESS 'Q' TO QUIT", True, BLACK), [375, 320]) pygame.display.update() screen.blit(washingtonHall_BG, (0, 0)) game_clock.tick(FPS) if gameover == False: play_game(playerID, opponentID) ############################################################# pygame.init() game_clock = pygame.time.Clock() FPS = 25 screen = pygame.display.set_mode((WIDTH, HEIGHT)) screen.fill((255, 255, 255)) washingtonHall_BG = pygame.image.load('resources/wh.png') font1 = pygame.font.SysFont('comicsansms', 25) font2 = pygame.font.SysFont('comicsansms', 80) pygame.display.set_caption("Brigade Brawlers") display = pygame.display.set_mode((WIDTH, HEIGHT)) player = Fighter(50, 400, playerID - 1) opponent = Fighter(800, 400, opponentID - 1) screen.fill((255, 255, 255)) screen.blit(washingtonHall_BG, (0, 0)) game_clock = pygame.time.Clock() FPS = 25 pygame.key.set_repeat(10, 10) score = 0 score_font = pygame.font.SysFont(None, 20) punching_PlayerImages = [ player.standing_image, player.standing_image, player.punching_image, player.punching_image ] punching_OpponentImages = [ opponent.standing_image, opponent.standing_image, opponent.punching_image, opponent.punching_image ] seconds = -1 playerFacingRight = True opponentFacingRight = False strategy = random.randrange(0, 3) oppRetreat = False activeGame = True while activeGame: playerMove = 0 controls = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == QUIT: activeGame = False pygame.quit() interface.Game("resume") elif event.type == KEYDOWN: if event.key == K_ESCAPE: activeGame = False pygame.quit() interface.Game("resume") if controls[K_LEFT]: playerFacingRight = False player.move_left(25, display) playerMove = 1 if controls[K_RIGHT]: playerFacingRight = True player.move_right(25, display) playerMove = 2 if controls[K_SPACE]: playerMove = 3 if abs(player.fighter.x - opponent.fighter.x) <= 60: opponent.health -= 10 else: playerMove = 0 screen.blit(washingtonHall_BG, (0, 0)) if playerMove == 0: if playerFacingRight: display.blit(player.standing_image, player.fighter) else: display.blit( pygame.transform.flip(player.standing_image, True, False), player.fighter) if playerMove == 1: player.running_images.append(player.running_images.pop(0)) flipped_img = pygame.transform.flip(player.running_images[0], True, False) display.blit(flipped_img, player.fighter) if playerMove == 2: player.running_images.append(player.running_images.pop(0)) display.blit(player.running_images[0], player.fighter) if playerMove == 3: punching_PlayerImages.append(punching_PlayerImages.pop(0)) puncing_img = punching_PlayerImages[0] if playerFacingRight: display.blit(puncing_img, player.fighter) else: display.blit(pygame.transform.flip(puncing_img, True, False), player.fighter) dist = opponent.fighter.centerx - player.fighter.centerx oppOnRight = True oppInRange = False if (dist < 0): oppOnRight = False if (abs(dist) <= 60): oppInRange = True oppMove = 0 if strategy == 0: if opponent.health > 350: if oppInRange: oppMove = 3 else: oppMove = 0 elif opponent.health > 250: if oppInRange and opponent.health < 300: oppMove = 3 elif opponent.fighter.centerx < 100: opponentFacingRight = True oppMove = 0 else: oppMove = 1 elif opponent.health > 60: if oppInRange and opponent.health > 150: oppMove = 3 elif opponent.fighter.centerx > 700: opponentFacingRight = False oppMove = 0 else: oppMove = 2 else: oppMove = 3 elif strategy == 1: if oppRetreat: if opponent.fighter.centerx > 100: oppMove = 1 elif oppInRange: oppMove = 3 else: opponentFacingRight = True oppMove = 0 elif opponent.health > 80: if oppInRange: oppMove = 3 elif oppOnRight: oppMove = 1 else: oppMove = 2 else: if oppOnRight: if opponent.fighter.centerx < 900: oppMove = 2 elif abs(dist < 130): oppRetreat = True else: oppRetreat = True else: if opponent.health > 300 and opponent.health < 370: oppMove = 3 elif opponent.health <= 300: if opponent.health < 70: if opponent.fighter.centerx > 930: if oppInRange: oppMove = 3 else: opponentFacingRight = False oppMove = 0 else: oppMove = 2 elif opponent.fighter.centerx > 50: oppMove = 1 elif oppInRange: oppMove = 3 else: opponentFacingRight = True oppMove = 0 elif opponent.fighter.centerx > 900 and oppInRange == False: opponentFacingRight = False oppMove = 0 elif oppOnRight and oppRetreat == False: if oppInRange: oppRetreat = True oppMove = 2 elif opponent.fighter.centerx > 320: oppMove = 1 else: oppMove = 0 else: oppMove = 2 if oppMove == 0: if opponentFacingRight: display.blit(opponent.standing_image, opponent.fighter) else: display.blit( pygame.transform.flip(opponent.standing_image, True, False), opponent.fighter) if oppMove == 1: opponentFacingRight = False opponent.move_left(25, display) opponent.running_images.append(opponent.running_images.pop(0)) flipped_img = pygame.transform.flip(opponent.running_images[0], True, False) display.blit(flipped_img, opponent.fighter) if oppMove == 2: opponentFacingRight = True opponent.move_right(25, display) opponent.running_images.append(opponent.running_images.pop(0)) flipped_img = opponent.running_images[0] display.blit(flipped_img, opponent.fighter) if oppMove == 3: if abs(player.fighter.x - opponent.fighter.x) <= 60: player.health -= 10 punching_OpponentImages.append(punching_OpponentImages.pop(0)) puncing_img = punching_OpponentImages[0] if opponentFacingRight: display.blit(puncing_img, opponent.fighter) else: display.blit(pygame.transform.flip(puncing_img, True, False), opponent.fighter) if player.health <= 0 or opponent.health <= 0: gameover_screen(player.health > opponent.health) health_bars(player.health, opponent.health, display) pygame.display.update() screen.blit(washingtonHall_BG, (0, 0)) game_clock.tick(FPS)