Ejemplo n.º 1
0
def check_control(human_control=True):
    from PIL import Image
    from pathlib import Path
    from os.path import isdir
    home = str(Path.home())
    full_path = home + '/Desktop/Images/'
    print(full_path)
    count = 0

    game = MilkFactory(render=True,
                       speed=10,
                       human_control=human_control,
                       number_of_milk_robots=2,
                       number_of_fix_robots=1,
                       error_freq=0.01,
                       number_of_milks=2,
                       milk_speed=3,
                       number_of_exits=2,
                       human_control_robot=0,
                       debug=True,
                       action_combined_mode=False,
                       show_status=True)
    game.reset()
    num_of_agents = game.get_num_of_agents()

    while True:
        if not human_control:
            num_of_actions = game.get_num_of_actions()
            actions = []
            for i in range(num_of_agents):
                random_action = np.random.randint(0, num_of_actions)
                actions.append(random_action)

            print(actions)
            reward = game.step(actions)
            print(reward)
        else:
            game.render()

        next_state = Utils.process_state(game.get_state())
        next_map = Utils.process_state(game.get_map())

        # save state and map
        count = count + 1
        if isdir(full_path):
            img = Image.fromarray(next_state, 'L')
            img.save(full_path + str(count) + '_state.png')
            img = Image.fromarray(next_map, 'L')
            img.save(full_path + str(count) + '_map.png')

        is_terminal = game.is_terminal()

        if is_terminal:
            print("Total Score", game.total_score)
            break
Ejemplo n.º 2
0
    def __draw_score(self):
        total_score = self.font.render(
            'Score:' + str(self.total_score) + ',' + str(self.total_score2),
            False, Utils.get_color(Utils.WHITE))
        self.screen.blit(
            total_score,
            (10, self.screen_size + total_score.get_height() / 1.3))

        life = self.font.render('Energy:', False, Utils.get_color(Utils.WHITE))
        self.screen.blit(life,
                         (self.screen_size / 2 - life.get_width() / 2,
                          self.screen_size + total_score.get_height() / 1.3))
Ejemplo n.º 3
0
    def reset(self):
        global global_steps
        global_steps = 0

        self.end_of_game = False
        self.frames_count = 0
        self.started_time = Utils.get_current_time()

        for sprite in self.sprites:
            sprite.kill()

        for player in self.players:
            player.kill()

        for apple in self.apples:
            apple.kill()

        for door in self.doors:
            door.kill()

        for tree in self.trees:
            tree.kill()

        for food in self.food:
            food.kill()

        for land in self.lands:
            land.kill()

        self.stage_map.load_map(self.current_stage)

        # Create door
        self.__generate_particles()

        # Create food
        self.__generate_food()

        # Create players
        self.__generate_players()

        if self.is_debug:
            interval = Utils.get_current_time() - self.started_time
            print("#################  RESET GAME  ##################")
            print("Episode terminated after:", interval, "(s)")
            print("Total score:", self.total_score)
            print("Total eat:", self.total_score2)
            print("#################################################")

        self.total_score = 0
        self.total_score2 = 0
        self.life = 100

        self.__render()
Ejemplo n.º 4
0
    def reset(self):
        global global_steps
        global_steps = 0

        self.end_of_game = False
        self.frames_count = 0
        self.started_time = Utils.get_current_time()

        for sprite in self.conveyor_sprites:
            sprite.kill()
        self.conveyor_sprites.empty()

        for sprite in self.milk_sprites:
            sprite.kill()
        self.milk_sprites.empty()

        for sprite in self.target_sprites:
            sprite.kill()
        self.target_sprites.empty()

        for sprite in self.robot_sprites:
            sprite.kill()
        self.robot_sprites.empty()

        for sprite in self.sprites:
            sprite.kill()
        self.sprites.empty()

        self.robots.clear()

        self.stage_map.load_map(self.current_stage, self.conveyor_sprites,
                                self.target_sprites)

        # Create milks
        self.__generate_milks()

        # Create players
        self.__generate_robots()

        if self.is_debug:
            interval = Utils.get_current_time() - self.started_time
            print("#################  RESET GAME  ##################")
            print("Episode terminated after:", interval, "(s)")
            print("Total score:", self.total_score)
            print("#################################################")

        self.total_score = [0 for _ in range(self.num_of_objs)]

        self.__render()
Ejemplo n.º 5
0
    def __draw_score(self):

        total_score = self.font.render(str(self.total_score) + " " + str(self.total_score_2) + " " +
                                       str(self.total_score_3),
                                       False, Utils.get_color(Utils.BLACK))
        self.screen.blit(total_score, (self.screen_size/2 - total_score.get_width()/2,
                                       self.screen_size - total_score.get_height()*2))
Ejemplo n.º 6
0
def check_map():
    from PIL import Image
    from pathlib import Path
    home = str(Path.home())
    game = MountainCar(render=False, graphical_state=True)
    num_of_actions = game.get_num_of_actions()
    game.reset()
    full_path = home + '/Desktop/Images/'
    count = 0

    while True:
        random_action = np.random.randint(0, num_of_actions)
        reward = game.step(random_action)
        print(reward)
        next_state = Utils.process_state(game.get_state())
        is_terminal = game.is_terminal()

        # save state and map
        count = count + 1
        img = Image.fromarray(next_state, 'L')
        img.save(full_path + str(count) +'.png')

        if is_terminal:
            print("Total Score", game.total_score)
            game.reset()
            break
Ejemplo n.º 7
0
    def reset(self):

        for sprite in self.sprites:
            sprite.kill()

        self.pos = self.default_init_pos
        self.vel = self.default_init_vel
        if self.rand_starts:
            rand_start_pos = self.default_init_pos + 0.25 * (np.random.rand() - 0.5)
            self.pos = rand_start_pos
            rand_start_vel = self.default_init_vel + 0.25 * (np.random.rand() - 0.5)
            self.vel = rand_start_vel

        self.__generate_car()

        self.__generate_goal()

        if self.is_debug:
            interval = Utils.get_current_time() - self.started_time
            print("#################  RESET GAME  ##################")
            print("Episode terminated after:", interval, "(s)")
            print("Total score:", self.total_score)
            print("#################################################")

        self.rewards_2.clear()
        self.rewards_3.clear()
        self.total_score = 0
        self.total_score_2 = 0
        self.total_score_3 = 0
        self.__render()
Ejemplo n.º 8
0
def check_map():
    from PIL import Image
    from pathlib import Path
    home = str(Path.home())
    game = FoodCollector(render=True,
                         speed=60,
                         max_frames=10000,
                         frame_skip=1,
                         seed=None,
                         num_of_apples=1,
                         human_control=False,
                         debug=True)
    num_of_actions = game.get_num_of_actions()
    game.reset()
    full_path = home + '/Desktop/Images/'
    count = 0

    while True:
        random_action = np.random.randint(0, num_of_actions)
        reward = game.step(random_action)
        print(reward)
        next_state = Utils.process_state(game.get_state())
        is_terminal = game.is_terminal()

        # save state and map
        count = count + 1
        img = Image.fromarray(next_state, 'L')
        img.save(full_path + str(count) + '.png')

        if is_terminal:
            print("Total Score", game.total_score)
            game.reset()
Ejemplo n.º 9
0
 def __draw_score(self):
     total_score = self.font.render(
         str(self.total_score) + " " + str(self.total_score_2), False,
         Utils.get_color(Utils.WHITE))
     self.screen.blit(total_score,
                      (self.width / 2 - total_score.get_width() / 2,
                       self.height - total_score.get_height() * 2))
Ejemplo n.º 10
0
 def __draw_life(self):
     if self.total_score < 50:
         self.life_fq = 10
     elif self.total_score < 100:
         self.life_fq = 9
     elif self.total_score < 150:
         self.life_fq = 8
     elif self.total_score < 200:
         self.life_fq = 7
     elif self.total_score < 250:
         self.life_fq = 6
     elif self.total_score < 300:
         self.life_fq = 5
     elif self.total_score < 350:
         self.life_fq = 4
     elif self.total_score < 400:
         self.life_fq = 3
     elif self.total_score < 450:
         self.life_fq = 2
     else:
         self.life_fq = 1
     if self.frames_count % self.life_fq == 0:
         self.life = self.life - 2 * GlobalConstants.LIVES_FACTOR_SPEED
     if self.life < 0:
         self.life = 0
         self.end_of_game = True
     max_width = self.screen_size / 2.6
     current_width = max_width * self.life / 100
     pygame.draw.rect(self.screen, Utils.get_color(Utils.WHITE), [
         self.screen_size / 2 + self.tile_size / 1.5, self.screen_size +
         self.tile_size / 8, current_width, self.tile_size / 4
     ])
Ejemplo n.º 11
0
 def __calculate_fps(self):
     self.frames_count = self.frames_count + 1
     if self.max_frames > 0:
         if self.frames_count > self.max_frames:
             self.end_of_game = True
     current_time = Utils.get_current_time()
     if current_time > self.started_time:
         self.frame_speed = self.frames_count / (current_time - self.started_time)
     else:
         self.frame_speed = 0
Ejemplo n.º 12
0
    def __update_map(self):
        self.map.fill(Utils.get_color(Utils.BLACK))

        index = 0
        for robot in self.robots:
            if type(robot) is MilkRobotSprite:
                if robot.error:
                    self.map_sprites[index][1].draw(self.map)
                elif robot.picked:
                    self.map_sprites[index][0].draw(self.map)
                index += 1
Ejemplo n.º 13
0
    def __draw_score(self):
        score_str = 'Score: '
        for i in range(self.num_of_objs):
            score_str += str(self.total_score[i])
            if i < self.num_of_objs - 1:
                score_str += ','

        total_score = self.font.render(score_str, False,
                                       Utils.get_color(Utils.WHITE))
        self.screen.blit(
            total_score,
            (10, self.screen_size + total_score.get_height() / 1.3))
Ejemplo n.º 14
0
    def __render(self):

        # Handle user event
        if self.rd:
            self.__handle_event()

        # Draw background first
        self.screen.fill(Utils.get_color(Utils.BLACK))

        # Update sprites
        self.sprites.update()

        # Redraw all sprites
        self.sprites.draw(self.screen)

        # Redraw conveyors
        self.conveyor_sprites.draw(self.screen)

        # Redraw milks
        self.milk_sprites.draw(self.screen)

        # Redraw target
        self.target_sprites.draw(self.screen)

        # Redraw robots
        self.robot_sprites.draw(self.screen)

        # Update map
        self.__update_map()

        # Show status
        self.__show_status()

        # Draw score
        self.__draw_score()

        # Check terminal state
        self.__check_goal()

        # Show to the screen what we're have drawn so far
        if self.rd:
            pygame.display.flip()

        # Maintain 20 fps
        pygame.time.Clock().tick(self.speed)

        # Calculate fps
        self.__calculate_fps()

        # Debug
        self.__print_info()
Ejemplo n.º 15
0
    def __render(self):

        # Handle user event
        if self.rd:
            self.__handle_event()

        # Draw background first
        self.screen.fill(Utils.get_color(Utils.BLACK))

        # Draw land
        self.lands.draw(self.screen)

        # Update sprites
        self.sprites.update()

        # Redraw all sprites
        self.sprites.draw(self.screen)

        # Update player
        self.players.update()

        # Draw player
        self.players.draw(self.screen)

        # Draw lives
        self.__draw_life()

        # Draw score
        self.__draw_score()

        # Check terminal state
        self.__check_goal()

        # Show to the screen what we're have drawn so far
        if self.rd:
            pygame.display.flip()

        # Maintain 20 fps
        pygame.time.Clock().tick(self.speed)

        # Calculate fps
        self.__calculate_fps()

        # Debug
        self.__print_info()
Ejemplo n.º 16
0
    def __init__(self,
                 render=False,
                 speed=600,
                 max_frames=1000,
                 frame_skip=1,
                 number_of_milk_robots=3,
                 number_of_fix_robots=2,
                 number_of_milks=1,
                 seed=None,
                 human_control=True,
                 error_freq=0.01,
                 human_control_robot=0,
                 milk_speed=3,
                 debug=False,
                 action_combined_mode=False,
                 show_status=False,
                 number_of_exits=1):

        # Prepare internal data
        self.num_of_objs = number_of_fix_robots + number_of_milk_robots

        self.screen_size = GlobalConstants.SCREEN_SIZE
        self.tile_size = GlobalConstants.TILE_SIZE
        self.max_frames = max_frames
        self.rd = render
        self.screen = None
        self.speed = speed

        self.sprites = pygame.sprite.Group()
        self.conveyor_sprites = pygame.sprite.Group()
        self.milk_sprites = pygame.sprite.Group()
        self.robot_sprites = pygame.sprite.Group()
        self.target_sprites = pygame.sprite.Group()

        self.robots = []

        self.number_of_milk_robots = number_of_milk_robots
        self.number_of_fix_robots = number_of_fix_robots
        self.number_of_milks = number_of_milks

        self.num_of_actions = GlobalConstants.NUM_OF_ACTIONS
        self.num_of_tiles = int(self.screen_size / self.tile_size)

        self.end_of_game = False
        self.is_debug = debug

        self.frames_count = 0
        self.total_score = [0 for _ in range(self.num_of_objs)]

        self.milk_speed = milk_speed
        self.show_status = show_status

        self.font_size = GlobalConstants.FONT_SIZE
        self.human_control = human_control
        self.human_control_robot = human_control_robot
        self.num_of_exits = number_of_exits

        self.log_freq = 60
        if self.log_freq == 0:
            self.log_freq = 60

        self.error_freq = error_freq

        self.current_path = os.path.dirname(os.path.abspath(__file__))

        self.current_stage = number_of_exits - 1

        self.current_buffer = np.array(
            [[[0, 0, 0] for _ in range(self.screen_size + int(self.tile_size))]
             for _ in range(self.screen_size)])

        self.map_buffer = np.array([[[0, 0, 0]
                                     for _ in range(self.screen_size)]
                                    for _ in range(self.screen_size)])

        self.map = None
        self.map_sprites = [[] for _ in range(self.number_of_milk_robots)]
        self.map_sprites_status = [[]
                                   for _ in range(self.number_of_milk_robots)]

        self.pareto_solutions = None
        self.frame_speed = 0
        self.frame_skip = frame_skip
        self.started_time = Utils.get_current_time()
        self.steps = 0

        self.rewards = [cl.deque(maxlen=100) for _ in range(self.num_of_objs)]

        self.action_combined_mode = action_combined_mode

        if self.human_control:
            if not self.rd:
                raise ValueError(
                    "Invalid parameter ! Human control must be in rendering mode"
                )

        # Seed is used to generate a stochastic environment
        if seed is None or seed < 0 or seed >= 9999:
            self.seed = np.random.randint(0, 9999)
            self.random_seed = True
        else:
            self.random_seed = False
            self.seed = seed
            np.random.seed(seed)

        # Initialize
        self.__init_pygame_engine()

        # Load map
        self.stage_map.load_map(self.current_stage, self.conveyor_sprites,
                                self.target_sprites)

        # Create milks
        self.__generate_milks()

        # Create robots
        self.__generate_robots()

        # Render communication map
        self.__init_map()

        # Render the first frame
        self.__render()
Ejemplo n.º 17
0
    def __init__(self,
                 render=False,
                 speed=60,
                 max_frames=100000,
                 frame_skip=5,
                 seed=None,
                 num_of_apples=1,
                 human_control=True,
                 debug=False):

        # Prepare internal data
        self.screen_size = GlobalConstants.SCREEN_SIZE
        self.tile_size = GlobalConstants.TILE_SIZE
        self.max_frames = max_frames
        self.rd = render
        self.screen = None
        self.speed = speed
        self.num_of_apples = num_of_apples
        self.sprites = pygame.sprite.Group()
        self.apples = pygame.sprite.Group()
        self.players = pygame.sprite.Group()
        self.doors = pygame.sprite.Group()
        self.trees = pygame.sprite.Group()
        self.food = pygame.sprite.Group()
        self.lands = pygame.sprite.Group()
        self.num_of_actions = GlobalConstants.NUM_OF_ACTIONS
        self.num_of_tiles = int(self.screen_size / self.tile_size)
        self.end_of_game = False
        self.is_debug = debug
        self.frames_count = 0
        self.total_score = 0
        self.total_score2 = 0
        self.font_size = GlobalConstants.FONT_SIZE
        self.human_control = human_control
        self.log_freq = 60
        if self.log_freq == 0:
            self.log_freq = 60
        self.current_stage = 0
        self.current_path = os.path.dirname(os.path.abspath(__file__))
        self.player_speed = GlobalConstants.PLAYER_SPEED
        self.current_buffer = np.array(
            [[[0, 0, 0]
              for _ in range(self.screen_size + int(self.tile_size / 2))]
             for _ in range(self.screen_size)])
        self.pareto_solutions = None
        self.frame_speed = 0
        self.frame_skip = frame_skip
        self.started_time = Utils.get_current_time()
        self.next_rewards = cl.deque(maxlen=100)
        self.num_of_objs = 1
        self.steps = 0
        self.life = 100
        self.life_fq = 10
        self.rewards = cl.deque(maxlen=100)
        self.rewards_eat = cl.deque(maxlen=100)

        if self.human_control:
            if not self.rd:
                raise ValueError(
                    "Invalid parameter ! Human control must be in rendering mode"
                )

        # Seed is used to generate a stochastic environment
        if seed is None or seed < 0 or seed >= 9999:
            self.seed = np.random.randint(0, 9999)
            self.random_seed = True
        else:
            self.random_seed = False
            self.seed = seed
            np.random.seed(seed)

        # Initialize
        self.__init_pygame_engine()

        # Load map
        self.stage_map.load_map(self.current_stage)

        # Create apples and food
        self.__generate_food()

        # Create door
        self.__generate_particles()

        # Create players
        self.__generate_players()

        # Render the first frame
        self.__render()
Ejemplo n.º 18
0
    def __add_resources(self):
        image = pygame.image.load(self.current_path +
                                  ResourceManager.HARD_WALL)
        if self.render:
            image = pygame.transform.scale(
                image, (self.tile_size, self.tile_size)).convert_alpha()
        else:
            image = pygame.transform.scale(image,
                                           (self.tile_size, self.tile_size))

        self.resources[ResourceManager.HARD_WALL] = image

        image = pygame.image.load(self.current_path +
                                  ResourceManager.SOFT_WALL)
        if self.render:
            image = pygame.transform.scale(
                image, (self.tile_size, self.tile_size)).convert_alpha()
        else:
            image = pygame.transform.scale(image,
                                           (self.tile_size, self.tile_size))
        self.resources[ResourceManager.SOFT_WALL] = image

        image = pygame.image.load(self.current_path + ResourceManager.SEA_WALL)
        if self.render:
            image = pygame.transform.scale(
                image, (self.tile_size, self.tile_size)).convert_alpha()
        else:
            image = pygame.transform.scale(image,
                                           (self.tile_size, self.tile_size))
        self.resources[ResourceManager.SEA_WALL] = image

        image = pygame.image.load(self.current_path + ResourceManager.BASE)
        if self.render:
            image = pygame.transform.scale(
                image, (self.tile_size, self.tile_size)).convert_alpha()
        else:
            image = pygame.transform.scale(image,
                                           (self.tile_size, self.tile_size))
        self.resources[ResourceManager.BASE] = image

        image_up = pygame.image.load(self.current_path +
                                     ResourceManager.PLAYER1_UP)
        image_left = pygame.transform.rotate(image_up, 90)
        image_right = pygame.transform.rotate(image_up, -90)
        image_down = pygame.transform.rotate(image_up, 180)
        if self.render:
            image_up = pygame.transform.scale(
                image_up,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_left = pygame.transform.scale(
                image_left,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_right = pygame.transform.scale(
                image_right,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_down = pygame.transform.scale(
                image_down,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
        else:
            image_up = pygame.transform.scale(
                image_up, (self.tile_size - 1, self.tile_size - 1))
            image_left = pygame.transform.scale(
                image_left, (self.tile_size - 1, self.tile_size - 1))
            image_right = pygame.transform.scale(
                image_right, (self.tile_size - 1, self.tile_size - 1))
            image_down = pygame.transform.scale(
                image_down, (self.tile_size - 1, self.tile_size - 1))
        self.resources[ResourceManager.PLAYER1_UP] = image_up
        self.resources[ResourceManager.PLAYER1_LEFT] = image_left
        self.resources[ResourceManager.PLAYER1_RIGHT] = image_right
        self.resources[ResourceManager.PLAYER1_DOWN] = image_down

        image_up = pygame.image.load(self.current_path +
                                     ResourceManager.PLAYER2_UP)
        image_left = pygame.transform.rotate(image_up, 90)
        image_right = pygame.transform.rotate(image_up, -90)
        image_down = pygame.transform.rotate(image_up, 180)
        if self.render:
            image_up = pygame.transform.scale(
                image_up,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_left = pygame.transform.scale(
                image_left,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_right = pygame.transform.scale(
                image_right,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_down = pygame.transform.scale(
                image_down,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
        else:
            image_up = pygame.transform.scale(
                image_up, (self.tile_size - 1, self.tile_size - 1))
            image_left = pygame.transform.scale(
                image_left, (self.tile_size - 1, self.tile_size - 1))
            image_right = pygame.transform.scale(
                image_right, (self.tile_size - 1, self.tile_size - 1))
            image_down = pygame.transform.scale(
                image_down, (self.tile_size - 1, self.tile_size - 1))
        self.resources[ResourceManager.PLAYER2_UP] = image_up
        self.resources[ResourceManager.PLAYER2_LEFT] = image_left
        self.resources[ResourceManager.PLAYER2_RIGHT] = image_right
        self.resources[ResourceManager.PLAYER2_DOWN] = image_down

        image_up = pygame.image.load(self.current_path +
                                     ResourceManager.ENEMY_UP)
        image_left = pygame.transform.rotate(image_up, 90)
        image_right = pygame.transform.rotate(image_up, -90)
        image_down = pygame.transform.rotate(image_up, 180)
        if self.render:
            image_up = pygame.transform.scale(
                image_up,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_left = pygame.transform.scale(
                image_left,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_right = pygame.transform.scale(
                image_right,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
            image_down = pygame.transform.scale(
                image_down,
                (self.tile_size - 1, self.tile_size - 1)).convert_alpha()
        else:
            image_up = pygame.transform.scale(
                image_up, (self.tile_size - 1, self.tile_size - 1))
            image_left = pygame.transform.scale(
                image_left, (self.tile_size - 1, self.tile_size - 1))
            image_right = pygame.transform.scale(
                image_right, (self.tile_size - 1, self.tile_size - 1))
            image_down = pygame.transform.scale(
                image_down, (self.tile_size - 1, self.tile_size - 1))
        self.resources[ResourceManager.ENEMY_UP] = image_up
        self.resources[ResourceManager.ENEMY_LEFT] = image_left
        self.resources[ResourceManager.ENEMY_RIGHT] = image_right
        self.resources[ResourceManager.ENEMY_DOWN] = image_down

        image = pygame.Surface([self.bullet_size, self.bullet_size])
        image.fill(Utils.get_color(Utils.WHITE))
        self.resources[ResourceManager.BULLET] = image

        image_1 = pygame.image.load(self.current_path +
                                    ResourceManager.EXPLOSION_1)
        image_2 = pygame.image.load(self.current_path +
                                    ResourceManager.EXPLOSION_2)
        image_3 = pygame.image.load(self.current_path +
                                    ResourceManager.EXPLOSION_3)
        if self.render:
            image_1 = pygame.transform.scale(
                image_1, (self.tile_size, self.tile_size)).convert_alpha()
            image_2 = pygame.transform.scale(
                image_2, (self.tile_size, self.tile_size)).convert_alpha()
            image_3 = pygame.transform.scale(
                image_3, (self.tile_size, self.tile_size)).convert_alpha()
        else:
            image_1 = pygame.transform.scale(image_1,
                                             (self.tile_size, self.tile_size))
            image_2 = pygame.transform.scale(image_2,
                                             (self.tile_size, self.tile_size))
            image_3 = pygame.transform.scale(image_3,
                                             (self.tile_size, self.tile_size))

        self.resources[ResourceManager.EXPLOSION_1] = image_1
        self.resources[ResourceManager.EXPLOSION_2] = image_2
        self.resources[ResourceManager.EXPLOSION_3] = image_3
Ejemplo n.º 19
0
    def get_state(self):
        self.map.fill(Utils.get_color(Utils.BLACK))
        if self.multi_target:
            if self.strategy == 0:
                for enemy in self.enemies:
                    if enemy.pos_y > 5 or enemy.pos_x == 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                    else:
                        enemy.is_in_map = False
            elif self.strategy == 1:
                for enemy in self.enemies:
                    if (enemy.pos_y >= 6
                            and enemy.pos_y <= 8) or enemy.pos_x == 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                    else:
                        enemy.is_in_map = False
            elif self.strategy == 2:
                for enemy in self.enemies:
                    if enemy.pos_y >= 9:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                    else:
                        enemy.is_in_map = False
            elif self.strategy == 3:
                for enemy in self.enemies:
                    if enemy.pos_x == 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                    else:
                        enemy.is_in_map = False
                for enemy in self.enemies:
                    if enemy.is_in_map and enemy.pos_y >= 6:
                        if enemy.pos_y <= 8 and enemy.pos_x > 6:
                            pygame.draw.rect(self.map,
                                             Utils.get_color(Utils.WHITE),
                                             (enemy.rect.x, enemy.rect.y,
                                              self.tile_size, self.tile_size))
                            return self.map
                        else:
                            enemy.is_in_map = False
                for enemy in self.enemies:
                    if enemy.pos_y >= 6 and enemy.pos_y <= 8 and enemy.pos_x > 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                        return self.map
            elif self.strategy == 4:  # MSCS
                for enemy in self.enemies:
                    if enemy.pos_x == 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                    else:
                        enemy.is_in_map = False
                for enemy in self.enemies:
                    if enemy.is_in_map and enemy.pos_y >= 6:
                        if enemy.pos_y <= 11:
                            pygame.draw.rect(self.map,
                                             Utils.get_color(Utils.WHITE),
                                             (enemy.rect.x, enemy.rect.y,
                                              self.tile_size, self.tile_size))
                            return self.map
                        else:
                            enemy.is_in_map = False
                for enemy in self.enemies:
                    if enemy.pos_y >= 6 and enemy.pos_y <= 11:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                        return self.map
        else:
            if self.strategy == 0:  # Full defend
                for enemy in self.enemies:
                    if enemy.is_in_map:
                        if enemy.pos_y > 5 or enemy.pos_x == 6:
                            pygame.draw.rect(self.map,
                                             Utils.get_color(Utils.WHITE),
                                             (enemy.rect.x, enemy.rect.y,
                                              self.tile_size, self.tile_size))
                            return self.map
                        else:
                            enemy.is_in_map = False
                for enemy in self.enemies:
                    if enemy.pos_y > 5 or enemy.pos_x == 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                        return self.map
            elif self.strategy == 1:  # Defend top
                for enemy in self.enemies:
                    if enemy.is_in_map:
                        if (enemy.pos_y >= 6
                                and enemy.pos_y <= 8) or enemy.pos_x == 6:
                            pygame.draw.rect(self.map,
                                             Utils.get_color(Utils.WHITE),
                                             (enemy.rect.x, enemy.rect.y,
                                              self.tile_size, self.tile_size))
                            return self.map
                        else:
                            enemy.is_in_map = False

                for enemy in self.enemies:
                    if (enemy.pos_y >= 6
                            and enemy.pos_y <= 8) or enemy.pos_x == 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                        return self.map
            elif self.strategy == 2:  # Defend bottom
                for enemy in self.enemies:
                    if enemy.is_in_map:
                        if enemy.pos_y >= 9 and enemy.pos_x < 6:
                            pygame.draw.rect(self.map,
                                             Utils.get_color(Utils.WHITE),
                                             (enemy.rect.x, enemy.rect.y,
                                              self.tile_size, self.tile_size))
                            return self.map
                        else:
                            enemy.is_in_map = False

                for enemy in self.enemies:
                    if enemy.pos_y >= 9 and enemy.pos_x < 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                        return self.map
            elif self.strategy == 3:  # Defend top left
                for enemy in self.enemies:
                    if enemy.is_in_map:
                        if (enemy.pos_y >= 6 and enemy.pos_y <= 8
                                and enemy.pos_x <= 6) or enemy.pos_x == 6:
                            pygame.draw.rect(self.map,
                                             Utils.get_color(Utils.WHITE),
                                             (enemy.rect.x, enemy.rect.y,
                                              self.tile_size, self.tile_size))
                            return self.map
                        else:
                            enemy.is_in_map = False

                for enemy in self.enemies:
                    if (enemy.pos_y >= 6 and enemy.pos_y <= 8
                            and enemy.pos_x <= 6) or enemy.pos_x == 6:
                        pygame.draw.rect(self.map,
                                         Utils.get_color(Utils.WHITE),
                                         (enemy.rect.x, enemy.rect.y,
                                          self.tile_size, self.tile_size))
                        enemy.is_in_map = True
                        return self.map
        return self.map
Ejemplo n.º 20
0
 def __draw_score(self):
     total_score = self.font.render('Score:' + str(self.total_score), False,
                                    Utils.get_color(Utils.WHITE))
     self.screen.blit(
         total_score,
         (10, self.screen_height + total_score.get_height() / 1.3))
Ejemplo n.º 21
0
    def __init__(self,
                 render=False,
                 speed=60,
                 max_frames=100000,
                 graphical_state=True,
                 seed=None,
                 num_of_obstacles=2,
                 number_of_rows=4,
                 number_of_columns=4,
                 debug=False,
                 stage=0,
                 agent_start_x=0,
                 agent_start_y=0):

        self.num_of_rows = number_of_rows
        self.num_of_columns = number_of_columns
        self.screen_width = GlobalConstants.TILE_SIZE * number_of_columns
        self.screen_height = GlobalConstants.TILE_SIZE * number_of_rows
        self.tile_size = GlobalConstants.TILE_SIZE
        self.max_frames = max_frames
        self.rd = render
        self.screen = None
        self.speed = speed
        self.num_of_obstacles = num_of_obstacles

        self.sprites = pygame.sprite.Group()
        self.obstacles = pygame.sprite.Group()
        self.minuses = pygame.sprite.Group()
        self.players = pygame.sprite.Group()
        self.keys = pygame.sprite.Group()
        self.lands = pygame.sprite.Group()

        self.graphical_state = graphical_state

        self.num_of_actions = GlobalConstants.NUM_OF_ACTIONS
        self.end_of_game = False
        self.is_debug = debug
        self.frames_count = 0
        self.total_score = 0
        self.font_size = GlobalConstants.FONT_SIZE
        self.log_freq = 60
        if self.log_freq == 0:
            self.log_freq = 60
        self.current_stage = stage
        self.current_path = os.path.dirname(os.path.abspath(__file__))
        self.player_speed = GlobalConstants.PLAYER_SPEED
        self.current_buffer = np.array(
            [[[0, 0, 0]
              for _ in range(self.screen_height + int(self.tile_size / 2))]
             for _ in range(self.screen_width)])
        self.frame_speed = 0
        self.frame_skip = 1
        self.started_time = Utils.get_current_time()
        self.num_of_objs = 1
        self.steps = 0
        self.rewards = cl.deque(maxlen=100)
        self.agent_start_x = agent_start_x
        self.agent_start_y = agent_start_y

        # Seed is used to generate a stochastic environment
        if seed is None or seed < 0 or seed >= 9999:
            self.seed = np.random.randint(0, 9999)
            self.random_seed = True
        else:
            self.random_seed = False
            self.seed = seed
            np.random.seed(seed)

        # Initialize
        self.__init_pygame_engine()

        # Load map
        self.stage_map.load_map(self.current_stage)

        # Create players
        self.__generate_players()

        # Render the first frame
        self.__render()
Ejemplo n.º 22
0
    def __init__(self, min_pos=-1.2, max_pos=0.6, min_vel=-0.07, max_vel=0.07, goal_pos=0.5, acceleration=0.001,
                 gravity_factor=-0.0025, hill_peak_freq=3.0, default_init_pos=-0.5, default_init_vel=0.0,
                 reward_per_step=-1, reward_at_goal=0, random_starts=False, transition_noise=0., seed=None,
                 render=True, speed=60, is_debug=False, frame_skip=1, friction=0, graphical_state=False,
                 discrete_states=6):
        self.vel = default_init_vel
        self.pos = default_init_pos
        self.min_pos = min_pos
        self.max_pos = max_pos
        self.min_vel = min_vel
        self.max_vel = max_vel
        self.goal_pos = goal_pos
        self.acc = acceleration
        self.gra = gravity_factor
        self.hill_peak_freq = hill_peak_freq
        self.default_init_pos = default_init_pos
        self.default_init_vel = default_init_vel
        self.reward_per_step = reward_per_step
        self.reward_at_goal = reward_at_goal
        self.rand_starts = random_starts
        self.trans_noise = transition_noise
        self.last_action = 0
        self.rd = render
        self.speed = speed

        self.num_of_actions = 3
        self.num_of_objs = 3
        self.screen_size = 500
        self.current_path = os.path.dirname(os.path.abspath(__file__))
        self.car_width = 60
        self.car_height = 30
        self.sprites = pygame.sprite.Group()
        self.frames_count = 0
        self.max_frames = 10000
        self.is_debug = is_debug
        self.frame_speed = 0
        self.log_freq = 500
        self.started_time = Utils.get_current_time()
        self.frame_skip = frame_skip
        self.current_buffer = np.array([[[0, 0, 0] for _ in range(self.screen_size)] for _ in range(self.screen_size)])
        self.mountain = []
        self.surface = pygame.Surface((self.screen_size, self.screen_size))
        self.friction = friction
        self.rewards_2 = cl.deque(maxlen=100)
        self.rewards_3 = cl.deque(maxlen=100)
        self.total_score = 0
        self.total_score_2 = 0
        self.total_score_3 = 0
        self.graphical_state = graphical_state
        self.max_states_per_dim = discrete_states

        if seed is None or seed < 0 or seed >= 9999:
            if seed is not None and (seed < 0 or seed >= 9999):
                print("Invalid seed ! Default seed = randint(0, 9999")
            self.seed = np.random.randint(0, 9999)
            self.random_seed = True
        else:
            self.random_seed = False
            self.seed = seed
            np.random.seed(seed)

        # Initialize
        self.__init_pygame_engine()

        # Create mountain
        self.__draw_sine_wave()

        # Create player
        self.__generate_car()

        # Create goal
        self.__generate_goal()

        # Render the first frame
        self.__render()