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()
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()
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()
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
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()
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()
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()
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()