def default_filename(self, mode: str = 'binary') -> str: """ Return default filename :return: """ # Get environment name in snake case, get only first letter of each word env_str_abbr = ''.join(word[0] for word in um.str_to_snake_case( self.environment.__class__.__name__).split('_')) # Extract agent name agent_str_abbr = um.str_to_snake_case( self.__class__.__name__).split('_')[-1] # Get timestamp timestamp = int(time.time()) if mode == 'binary': extension = 'bin' else: extension = '' # Prepare default filename return '{}/models/{}_{}_{}.{}'.format(agent_str_abbr, env_str_abbr, timestamp, Vector.decimal_precision, extension)
def dumps(data: dict, environment: Environment): """ Dumps full_data given into dumps directory :param environment: :param data: :return: """ timestamp = int(time.time()) # Get environment name in snake case environment = um.str_to_snake_case(environment.__class__.__name__) # Get only first letter of each word env_name_abbr = ''.join([word[0] for word in environment.split('_')]) # Specify full path file_path = Path(__file__).parent.parent.joinpath( 'dumps/w/train_data/{}_w_{}_{}.yml'.format(env_name_abbr, timestamp, Vector.decimal_precision)) # If any parents doesn't exist, make it. file_path.parent.mkdir(parents=True, exist_ok=True) with file_path.open(mode='w+', encoding='UTF-8') as f: f.write(um.structures_to_yaml(data=data))
def dumps(self): """ Dumps agent data into dumps directory :return: """ # Calc total time total_time = time.time() - self.initial_time if self.dumps_format == 'yml': # Convert states_vectors (and tuples into strings) vectors = { um.tuples_to_string(key): [vector.tolist() for vector in vectors] for key, vectors in self.states_vectors.items() } # Convert initial state initial_state = um.tuples_to_string(self.environment.initial_state) else: # Convert states_vectors vectors = { key: [vector.tolist() for vector in vectors] for key, vectors in self.states_vectors.items() } # Convert initial state initial_state = self.environment.initial_state # Prepare data to dumps data = { 'time': '{}s'.format(total_time), 'memory': { 'v_s_0': len(vectors[initial_state]), 'full': sum(len(vectors) for vectors in vectors.values()) }, 'vectors': vectors } # Extract timestamp timestamp = int(time.time()) # Get environment name in snake case env_name = um.str_to_snake_case(self.environment.__class__.__name__) # Get only first letter of each word env_name_abbr = ''.join([word[0] for word in env_name.split('_')]) # Specify full path if self.limited_precision: agent_path = 'b_lp/train_data/{}_{}_{}.yml'.format( env_name_abbr, Vector.decimal_precision, timestamp) else: agent_path = 'b/train_data/{}_{}.yml'.format( env_name_abbr, timestamp) file_path = dumps_path.joinpath(agent_path) # If any parents doesn't exist, make it. file_path.parent.mkdir(parents=True, exist_ok=True) with file_path.open(mode='w+', encoding='UTF-8') as f: if self.dumps_format == 'yml': f.write(um.structures_to_yaml(data=data)) else: f.write(json.dumps(data))