class GameEnvironmentTests(unittest.TestCase): def setUp(self): self.lupin = WerewolfAI("Lupin") self.trelawney = SeerAI("Trelawney") self.bellatrix = SeerAI("Bellatrix") self.test_village = GameEnvironment() self.test_crew = (self.lupin, self.trelawney, self.bellatrix) def test_game_states(self): # Create a GameEnvironment for crew in self.test_crew: self.test_village.register_villager(crew) self.test_village.village_open = False # Test that we can't open it again with self.assertRaises(VillageClosedError): self.test_village.village_open = True # Test that we can't add any other character self.assertRaises(VillageClosedError, self.test_village.register_villager, SeerAI("Cassandra")) def test_deep_duplicates(self): """ Registering deep duplicates (same name and role but different objects, i.e., "deep copies") shall not pass. Throw a RegistrationError. """ self.test_village.register_villager(self.lupin) self.lupin_clone = WerewolfAI("Lupin") self.assertRaises(RegistrationError, self.test_village.register_villager, self.lupin_clone)
def __init__(self, eval_keyword: str, args): self.eval_keyword = eval_keyword GameEnvironment.__init__(self, args=args, agent_type='evaluation') self.eval_name = args.eval_name # load params and evaluators self.control_param, self.control_evaluator = \ load_param_and_evaluator(eval_keyword=eval_keyword, args=args, model_type='control') self.stop_param, self.stop_evaluator = \ load_param_and_evaluator(eval_keyword=eval_keyword, args=args, model_type='stop') self.high_level_param, self.high_level_evaluator = \ load_param_and_evaluator(eval_keyword=eval_keyword, args=args, model_type='high') # set image type self.image_type = self.high_level_param.image_type if 'd' in self.image_type: from model import DeepLabModel, prepare_deeplab_model self.deeplab_model: DeepLabModel = prepare_deeplab_model() self.final_images = [] self.eval_dataset, self.eval_sentences = load_evaluation_dataset( self.high_level_param) self.eval_transforms = list( map(lambda x: x[0].state.transform, self.eval_dataset)) self.high_level_sentences = self.eval_sentences logger.info('fetched {} sentences from {}'.format( len(self.high_level_sentences), self.high_level_param.eval_keyword.lower())) self.softmax = torch.nn.Softmax(dim=1) EvaluationDirectory.__init__(self, *self.eval_info) self.high_level_data_dict = dict()
def setUp(self): self.lupin = WerewolfAI("Lupin") self.trelawney = SeerAI("Trelawney") self.bellatrix = SeerAI("Bellatrix") self.test_village = GameEnvironment() self.test_crew = (self.lupin, self.trelawney, self.bellatrix)
SAVE_EXPERIENCE = False WHITE = (255, 255, 255) BLACK = (0, 0, 0) pygame.init() DISPLAY_SHAPE = (480, 480) FPS = 60 clock = pygame.time.Clock() gameDisplay = pygame.display.set_mode(DISPLAY_SHAPE) pygame.display.set_caption('Bouncing Balls') pygame.key.set_repeat(1, 1) env = GameEnvironment(DISPLAY_SHAPE, 1.0 / float(FPS)) def action_vector(a): res = np.zeros(9) res[int(a)] = 1.0 return res # Define Experience Replay if SAVE_EXPERIENCE: er = ExperienceReplay.load(EXP_REPLAY_FILE) if er == None: er = ExperienceReplay(BUFFER_SIZE)
DISPLAY_SHAPE = (480, 480) FPS = 60 MODEL_FILE = './saved/bouncing-balls.ckpt' LEARNING_RATE = 0.0001 TAU = 0.01 with tf.Session() as sess: pygame.init() clock = pygame.time.Clock() gameDisplay = pygame.display.set_mode(DISPLAY_SHAPE) pygame.display.set_caption('Bouncing Balls') env = GameEnvironment(DISPLAY_SHAPE,1.0/float(FPS)) hero_state_dim = 2 balls_state_shape = (10,5) action_dim = 9 qvalue_network = QValueNetwork(sess, hero_state_dim, balls_state_shape, action_dim, LEARNING_RATE, TAU) saver = tf.train.Saver(max_to_keep=1) saver.restore(sess, MODEL_FILE) while True: s = env.reset() score, done = 0.0, False
def __init__(self, args, model_type: str): GameEnvironment.__init__(self, args=args, agent_type='evaluation') self.eval_param, self.evaluator = load_param_and_evaluator( args=args, model_type=model_type) self.eval_transforms = self.world.get_map().get_spawn_points() EvaluationDirectory.__init__(self, *self.eval_info)
class Game: """ Class to represent a single game. instance variables: int level = The difficulty level of the game. This is obtained from the food in the environment int score = The score the user has scored. environment = The game environment object in which this game is player """ def __init__(self): self.score = 0 self.environment = GameEnvironment() self.level = self.environment.food.level def set_level(self, level): """ Sets the initial difficulty level of the game. Changes the food levels in the environment accordingly and changes the fps of the environment. :param level: The initial level of the game. :return: None >>> game_object = Game() >>> game_object.set_level(3) >>> print(game_object.level) 3 >>> game_object.set_level(4) >>> print(game_object.level) 4 """ self.level = level self.environment.food.level = self.level self.environment.fps += 5 def run_game(self): """ Calls the events method on this game's environment and then calls end_game_display when the game ends :return: None """ self.environment.events() if self.environment.status == 0: pygame.quit() self.end_game_display() def end_game_display(self): """ This method creates and displays a pygame window with the appropriate message displayed """ pygame.init() end_screen = True while end_screen: red_font = (255, 0, 0) white_font = (255, 255, 255) screen = pygame.display surface = screen.set_mode((800, 600)) pygame.display.set_caption('Sorry! You Lost.') # Creates text for end screen, when snake dies large_text = pygame.font.Font('freesansbold.ttf', 90) text_surf = large_text.render('Sorry! You Died.', True, red_font) text_rect = text_surf.get_rect() text_rect.center = (400, 300) surface.blit(text_surf, text_rect) # Creates two rectangles, which will act as buttons pygame.draw.rect(surface, red_font, (150, 450, 100, 50)) pygame.draw.rect(surface, red_font, (550, 450, 100, 50)) # Creates the text for the restart button small_text = pygame.font.Font('freesansbold.ttf', 25) button_restart_text = small_text.render('Restart', True, white_font) button_restart_rect = text_surf.get_rect() button_restart_rect.center = (504, 510) surface.blit(button_restart_text, button_restart_rect) # Creates text for te quit button button_quit_text = small_text.render('Quit', True, white_font) button_quit_rect = text_surf.get_rect() button_quit_rect.center = (920, 510) surface.blit(button_quit_text, button_quit_rect) screen.flip() mouse_pos = pygame.mouse.get_pos() # If the mouse positions are in the area of the restart or quit buttons, carry out the appropriate action if 250 > mouse_pos[0] > 150 and 500 > mouse_pos[1] > 450: pygame.quit() end_screen = False new_game = Game() new_game.run_game() if 550 + 100 > mouse_pos[0] > 550 and 450 + 50 > mouse_pos[1] > 450: end_screen = False pygame.quit()
def __init__(self): self.score = 0 self.environment = GameEnvironment() self.level = self.environment.food.level
sess.run( tf.global_variables_initializer() ) # Initialize target network weights qvalue_network.update_target_network() # Define Tensorboard file writer if SAVE_TENSORBOARD: writer = tf.summary.FileWriter('./saved/tensorboard/') writer.add_graph( sess.graph ) # Define Experience Replay er = ExperienceReplay.load(EXP_REPLAY_FILE) if er == None: er = ExperienceReplay(BUFFER_SIZE) env = GameEnvironment(DISPLAY_SHAPE,1.0/float(FPS)) fig, ax_list = plt.subplots(5,1) for i in range(MAX_EPISODES): s = env.reset() ep_reward = 0 terminal = False num_steps = 0 l = 1.0 while not terminal: # Epsilon Greedy